Skip to content

Commit

Permalink
output escaped html in getHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Aug 10, 2018
1 parent 83e416d commit 6577825
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
16 changes: 15 additions & 1 deletion blots/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@ import { TextBlot } from 'parchment';

class Text extends TextBlot {}

export default Text;
function escapeText(text) {
return text.replace(/[&<>"']/g, s => {
// https://lodash.com/docs#escape
const entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
};
return entityMap[s];
});
}

export { Text as default, escapeText };
4 changes: 2 additions & 2 deletions core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LeafBlot } from 'parchment';
import CursorBlot from '../blots/cursor';
import Block, { bubbleFormats } from '../blots/block';
import Break from '../blots/break';
import TextBlot from '../blots/text';
import TextBlot, { escapeText } from '../blots/text';

const ASCII = /^[ -~]*$/;

Expand Down Expand Up @@ -255,7 +255,7 @@ function convertHTML(blot, index, length, isRoot = false) {
if (typeof blot.html === 'function') {
return blot.html(index, length);
} else if (blot instanceof TextBlot) {
return blot.value().slice(index, index + length);
return escapeText(blot.value().slice(index, index + length));
} else if (blot.children) {
// TODO fix API
if (blot.statics.blotName === 'list-container') {
Expand Down
15 changes: 2 additions & 13 deletions modules/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Module from '../core/module';
import { blockDelta } from '../blots/block';
import BreakBlot from '../blots/break';
import CursorBlot from '../blots/cursor';
import TextBlot from '../blots/text';
import TextBlot, { escapeText } from '../blots/text';
import CodeBlock, { CodeBlockContainer } from '../formats/code';
import { traverse } from '../modules/clipboard';

Expand Down Expand Up @@ -238,18 +238,7 @@ class Syntax extends Module {

highlightBlot(text, language = 'plain') {
if (language === 'plain') {
return text
.replace(/[&<>"']/g, s => {
// https://lodash.com/docs#escape
const entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
};
return entityMap[s];
})
return escapeText(text)
.split('\n')
.reduce((delta, line, i) => {
if (i !== 0) {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,11 @@ describe('Editor', function() {
const editor = this.initialize(Editor, '<p><a>a</a></p>');
expect(editor.getHTML(0, 1)).toEqual('<a>a</a>');
});

it('escape html', function() {
const editor = this.initialize(Editor, '<p><br></p>');
editor.insertText(0, '<b>Test</b>');
expect(editor.getHTML(0, 11)).toEqual('&lt;b&gt;Test&lt;/b&gt;');
});
});
});

0 comments on commit 6577825

Please sign in to comment.