/**
* @module transfer
* @description html编码(转义)
* @param { String } text 需要转义的字符串
* @param { Boolean } useDom 转换方法选择
* @return { String }
*/
function htmlEncode(text, useDom) {
if (text.length === 0) return ''
let output = ''
if (useDom) {
let el = document.createElement('div')
el.textContent !== undefined ? (el.textContent = text) : (el.innerText = text)
output = el.innerHTML
el = null
} else {
output = text.replace(/&/g, '&')
output = output.replace(/</g, '<')
output = output.replace(/>/g, '>')
output = output.replace(/\s/g, ' ')
output = output.replace(/\\'/g, ''')
output = output.replace(/\\"/g, '"')
}
return output
}
export default htmlEncode