Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion browser/components/MarkdownPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export default class MarkdownPreview extends React.Component {
if (content === '\`\`\`') {
inCodeTag = !inCodeTag
} else if (inCodeTag) {
content = escapeHtmlCharacters(content)
content = escapeHtmlCharacters(content, {skipCodeBlock: true})
}
result += content
}
Expand Down
4 changes: 2 additions & 2 deletions browser/lib/htmlTextHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
export function decodeEntities (text) {
var entities = [
['apos', '\''],
['amp', '&'],
['lt', '<'],
['gt', '>'],
['#63', '\\?'],
['#36', '\\$']
['#36', '\\$'],
['amp', '&']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you changed this file? Is the order of the entities matter?

]

for (var i = 0, max = entities.length; i < max; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion browser/lib/markdown-it-sanitize-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function sanitizePlugin (md, options) {
// escapeHtmlCharacters has better performance
state.tokens[tokenIdx].content = escapeHtmlCharacters(
state.tokens[tokenIdx].content,
{ skipSingleQuote: true }
{skipSingleQuote: true, skipCodeBlock: false}
)
}
if (state.tokens[tokenIdx].type === 'inline') {
Expand Down
4 changes: 2 additions & 2 deletions browser/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function lastFindInArray (array, callback) {

export function escapeHtmlCharacters (
html,
opt = { detectCodeBlock: false, skipSingleQuote: false }
opt = { detectCodeBlock: false, skipSingleQuote: false, skipCodeBlock: true }
) {
const matchHtmlRegExp = /["'&<>]/g
const matchCodeBlockRegExp = /```/g
Expand Down Expand Up @@ -98,7 +98,7 @@ export function escapeHtmlCharacters (
break
}
}
if (!escapedStr) {
if (!opt.skipCodeBlock || !escapedStr) {
// this & char is not a part of an escaped string
html = replaceAt(html, current.index, '&amp;')
}
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/escapeHtmlCharacters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ test("escapeHtmlCharacters should NOT escape & character if it's a part of an es
t.is(actual, expected)
})

test("escapeHtmlCharacters shoud escape & character if it's a part of an escaped character", t => {
const input = 'Do escape &amp; or &quot; but do escape &'
const expected = 'Do escape &amp;amp; or &amp;quot; but do escape &amp;'
const actual = escapeHtmlCharacters(input, {skipCodeBlock: false})
t.is(actual, expected)
})

test('escapeHtmlCharacters should skip char if in code block', t => {
const input = `
\`\`\`
Expand Down