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) {
|
function escapeHtmlCharacters (html) {
|
||||||
const matchHtmlRegExp = /["'&<>]/
|
const matchHtmlRegExp = /["'&<>]/g
|
||||||
const str = '' + text
|
const escapes = ['"', '&', ''', '<', '>']
|
||||||
const match = matchHtmlRegExp.exec(str)
|
let match = null
|
||||||
|
const replaceAt = (str, index, replace) =>
|
||||||
|
str.substr(0, index) +
|
||||||
|
replace +
|
||||||
|
str.substr(index + replace.length - (replace.length - 1))
|
||||||
|
|
||||||
if (!match) {
|
while ((match = matchHtmlRegExp.exec(html)) != null) {
|
||||||
return str
|
const current = { char: match[0], index: match.index }
|
||||||
}
|
if (current.char === '&') {
|
||||||
|
let nextStr = ''
|
||||||
let escape
|
let nextIndex = current.index
|
||||||
let html = ''
|
let escapedStr = false
|
||||||
let index = 0
|
// maximum length of an escape string is 5. For example ('"')
|
||||||
let lastIndex = 0
|
while (nextStr.length <= 5) {
|
||||||
|
nextStr += html[nextIndex]
|
||||||
for (index = match.index; index < str.length; index++) {
|
nextIndex++
|
||||||
switch (str.charCodeAt(index)) {
|
if (escapes.indexOf(nextStr) !== -1) {
|
||||||
case 34: // "
|
escapedStr = true
|
||||||
escape = '"'
|
break
|
||||||
break
|
}
|
||||||
case 38: // &
|
}
|
||||||
escape = '&ssssss;'
|
if (!escapedStr) {
|
||||||
break
|
// this & char is not a part of an escaped string
|
||||||
case 39: // '
|
html = replaceAt(html, current.index, '&')
|
||||||
escape = '''
|
}
|
||||||
break
|
} else if (current.char === '"') {
|
||||||
case 60: // <
|
html = replaceAt(html, current.index, '"')
|
||||||
escape = '<'
|
} else if (current.char === "'") {
|
||||||
break
|
html = replaceAt(html, current.index, ''')
|
||||||
case 62: // >
|
} else if (current.char === '<') {
|
||||||
escape = '>'
|
html = replaceAt(html, current.index, '<')
|
||||||
break
|
} else if (current.char === '>') {
|
||||||
default:
|
html = replaceAt(html, current.index, '>')
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastIndex !== index) {
|
|
||||||
html += str.substring(lastIndex, index)
|
|
||||||
}
|
|
||||||
|
|
||||||
lastIndex = index + 1
|
|
||||||
html += escape
|
|
||||||
}
|
}
|
||||||
|
return html
|
||||||
return lastIndex !== index
|
|
||||||
? html + str.substring(lastIndex, index)
|
|
||||||
: html
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
Reference in New Issue
Block a user