mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
updated new escape html function
This commit is contained in:
@@ -6,52 +6,45 @@ export function lastFindInArray (array, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
export function escapeHtmlCharacters (text) {
|
||||
const matchHtmlRegExp = /["'&<>]/
|
||||
const str = '' + text
|
||||
const match = matchHtmlRegExp.exec(str)
|
||||
function escapeHtmlCharacters (html) {
|
||||
const matchHtmlRegExp = /["'&<>]/g
|
||||
const escapes = ['"', '&', ''', '<', '>']
|
||||
let match = null
|
||||
const replaceAt = (str, index, replace) =>
|
||||
str.substr(0, index) +
|
||||
replace +
|
||||
str.substr(index + replace.length - (replace.length - 1))
|
||||
|
||||
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 = '"'
|
||||
break
|
||||
case 38: // &
|
||||
escape = '&ssssss;'
|
||||
break
|
||||
case 39: // '
|
||||
escape = '''
|
||||
break
|
||||
case 60: // <
|
||||
escape = '<'
|
||||
break
|
||||
case 62: // >
|
||||
escape = '>'
|
||||
break
|
||||
default:
|
||||
continue
|
||||
while ((match = matchHtmlRegExp.exec(html)) != null) {
|
||||
const current = { char: match[0], index: match.index }
|
||||
if (current.char === '&') {
|
||||
let nextStr = ''
|
||||
let nextIndex = current.index
|
||||
let escapedStr = false
|
||||
// maximum length of an escape string is 5. For example ('"')
|
||||
while (nextStr.length <= 5) {
|
||||
nextStr += html[nextIndex]
|
||||
nextIndex++
|
||||
if (escapes.indexOf(nextStr) !== -1) {
|
||||
escapedStr = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!escapedStr) {
|
||||
// this & char is not a part of an escaped string
|
||||
html = replaceAt(html, current.index, '&')
|
||||
}
|
||||
} else if (current.char === '"') {
|
||||
html = replaceAt(html, current.index, '"')
|
||||
} else if (current.char === "'") {
|
||||
html = replaceAt(html, current.index, ''')
|
||||
} else if (current.char === '<') {
|
||||
html = replaceAt(html, current.index, '<')
|
||||
} else if (current.char === '>') {
|
||||
html = replaceAt(html, current.index, '>')
|
||||
}
|
||||
|
||||
if (lastIndex !== index) {
|
||||
html += str.substring(lastIndex, index)
|
||||
}
|
||||
|
||||
lastIndex = index + 1
|
||||
html += escape
|
||||
}
|
||||
|
||||
return lastIndex !== index
|
||||
? html + str.substring(lastIndex, index)
|
||||
: html
|
||||
return html
|
||||
}
|
||||
|
||||
export default {
|
||||
|
||||
Reference in New Issue
Block a user