diff --git a/package.json b/package.json index b9016e1..5393de6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lawsafrica/indigo-akn", - "version": "5.3.0", + "version": "5.3.1", "description": "Akoma Ntoso support libraries for the Indigo platform.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/grammars.js b/src/grammars.js index b4e9a0b..824d65a 100644 --- a/src/grammars.js +++ b/src/grammars.js @@ -194,7 +194,14 @@ export class GrammarModel { const cb = pasteEvent.clipboardData; // if it's coming from the vscode editor, then don't mess with it if (!cb.types.includes('vscode-editor-data') && cb.types.includes('text/html')) { - const doc = new DOMParser().parseFromString(cb.getData('text/html'), 'text/html'); + let doc; + try { + doc = new DOMParser().parseFromString(cb.getData('text/html'), 'text/html'); + } catch (e) { + console.log(e); + return; + } + this.onPasteHtml(editor, doc); } } @@ -206,13 +213,19 @@ export class GrammarModel { * if necessary, and replace the pasted text with our text. */ onPasteHtml (editor, doc) { - const xml = htmlToAkn(doc.body); let lines; - if (xml.childElementCount === 0) { - // no children, just text - lines = [xml.textContent]; - } else { - lines = [...xml.children].map(root => this.xmlToText(root)); + + try { + const xml = htmlToAkn(doc.body); + if (xml.childElementCount === 0) { + // no children, just text + lines = [xml.textContent]; + } else { + lines = [...xml.children].map(root => this.xmlToText(root)); + } + } catch (e) { + console.log(e); + return; } editor.trigger(this.language_id, 'undo'); diff --git a/tests/grammars.js b/tests/grammars.js new file mode 100644 index 0000000..4e937d5 --- /dev/null +++ b/tests/grammars.js @@ -0,0 +1,14 @@ + import { GrammarModel } from '../src/grammars'; + +describe('GrammarModel', () => { + describe('#onPaste()', () => { + it('should handle bad html', () => { + const grammar = new GrammarModel(); + const clipboardData = { + types: ['text/html'], + getData: () => '<--! escape
bad html', + }; + grammar.onPaste(null, { clipboardData }); + }); + }); +});