Skip to content

Commit

Permalink
better handling of xml paste issues
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Mar 4, 2024
1 parent a0b7656 commit 1b0e666
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
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 1b0e666

Please sign in to comment.