Skip to content

Commit

Permalink
Move code escaping to html()
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Feb 11, 2022
1 parent cf9d835 commit c32b67a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
5 changes: 2 additions & 3 deletions formats/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ class CodeBlockContainer extends Container {
}

code(index, length) {
const text = this.children
return this.children
.map(child => (child.length() <= 1 ? '' : child.domNode.innerText))
.join('\n')
.slice(index, index + length);
return escapeText(text);
}

html(index, length) {
// `\n`s are needed in order to support empty lines at the beginning and the end.
// https://html.spec.whatwg.org/multipage/syntax.html#element-restrictions
return `<pre>\n${this.code(index, length)}\n</pre>`;
return `<pre>\n${escapeText(this.code(index, length))}\n</pre>`;
}
}

Expand Down
19 changes: 8 additions & 11 deletions modules/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ class SyntaxCodeBlockContainer extends CodeBlockContainer {
? SyntaxCodeBlock.formats(codeBlock.domNode)
: 'plain';

return `<pre data-language="${language}">\n${this.code(
index,
length,
return `<pre data-language="${language}">\n${escapeText(
this.code(index, length),
)}\n</pre>`;
}

Expand Down Expand Up @@ -243,14 +242,12 @@ class Syntax extends Module {
highlightBlot(text, language = 'plain') {
language = this.languages[language] ? language : 'plain';
if (language === 'plain') {
return escapeText(text)
.split('\n')
.reduce((delta, line, i) => {
if (i !== 0) {
delta.insert('\n', { [CodeBlock.blotName]: language });
}
return delta.insert(line);
}, new Delta());
return text.split('\n').reduce((delta, line, i) => {
if (i !== 0) {
delta.insert('\n', { [CodeBlock.blotName]: language });
}
return delta.insert(line);
}, new Delta());
}
const container = this.quill.root.ownerDocument.createElement('div');
container.classList.add(CodeBlock.className);
Expand Down

0 comments on commit c32b67a

Please sign in to comment.