Skip to content

Commit

Permalink
Merge pull request #66 from laws-africa/handle-errors
Browse files Browse the repository at this point in the history
Handle parse errors
  • Loading branch information
longhotsummer authored Mar 5, 2024
2 parents a0b7656 + 8d1b27a commit 0110718
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 20 additions & 7 deletions src/grammars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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');
Expand Down
14 changes: 14 additions & 0 deletions tests/grammars.js
Original file line number Diff line number Diff line change
@@ -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 <p><p>bad html',
};
grammar.onPaste(null, { clipboardData });
});
});
});

0 comments on commit 0110718

Please sign in to comment.