Skip to content

Commit

Permalink
Merge pull request #1753 from nlopin/encode_content_in_html_export
Browse files Browse the repository at this point in the history
Escape html characters before convert to HTML
  • Loading branch information
Rokt33r authored Mar 31, 2018
2 parents 9590559 + 90f21f4 commit f5915f3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion browser/components/MarkdownPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import htmlTextHelper from 'browser/lib/htmlTextHelper'
import copy from 'copy-to-clipboard'
import mdurl from 'mdurl'
import exportNote from 'browser/main/lib/dataApi/exportNote'
import {escapeHtmlCharacters} from 'browser/lib/utils'

const { remote } = require('electron')
const { app } = remote
Expand Down Expand Up @@ -208,7 +209,7 @@ export default class MarkdownPreview extends React.Component {
const {fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme} = this.getStyleParams()

const inlineStyles = buildStyle(fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme, lineNumber)
const body = this.markdown.render(noteContent)
const body = this.markdown.render(escapeHtmlCharacters(noteContent))
const files = [this.GetCodeThemeLink(codeBlockTheme), ...CSS_FILES]

files.forEach((file) => {
Expand Down
51 changes: 50 additions & 1 deletion browser/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,55 @@ export function lastFindInArray (array, callback) {
}
}

export function escapeHtmlCharacters (text) {
const matchHtmlRegExp = /["'&<>]/
const str = '' + text
const match = matchHtmlRegExp.exec(str)

if (!match) {
return str
}

let escape
let html = ''
let index = 0
let lastIndex = 0

for (index = match.index; index < str.length; index++) {
switch (str.charCodeAt(index)) {
case 34: // "
escape = '&quot;'
break
case 38: // &
escape = '&amp;'
break
case 39: // '
escape = '&#39;'
break
case 60: // <
escape = '&lt;'
break
case 62: // >
escape = '&gt;'
break
default:
continue
}

if (lastIndex !== index) {
html += str.substring(lastIndex, index)
}

lastIndex = index + 1
html += escape
}

return lastIndex !== index
? html + str.substring(lastIndex, index)
: html
}

export default {
lastFindInArray
lastFindInArray,
escapeHtmlCharacters
}

0 comments on commit f5915f3

Please sign in to comment.