import { makeStr } from './helper/makeStr'
import htmlEntities from './helper/htmlEntities'
// 详情见 https://github.com/epeli/underscore.string/blob/master/unescapeHTML.js
function unescape(entity, entityCode) {
let match
if (entityCode in htmlEntities) {
return htmlEntities[entityCode]
} else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
return String.fromCharCode(parseInt(match[1], 16))
} else if (match = entityCode.match(/^#(\d+)$/)) {
return String.fromCharCode(~~match[1])
}
return entity
}
/**
* 将HTML字符串解码,这个函数支持 cent, yen, euro, pound, lt, gt, copy, reg, quote, amp, apos, nbsp。
* @module str/unescapeHTML
* @param {String} str 需要解码的HTML字符串
* @return {String} 已解码的HTML字符串
* @example
* // '<div>Blah & "blah" & \'blah\'</div>'
* str.unescapeHTML('<div>Blah & "blah" & 'blah'</div>')
*
* str.unescapeHTML('&lt;') // '<'
* str.unescapeHTML(''') // '\''
* str.unescapeHTML(''') // '\''
* str.unescapeHTML(''') // '\''
* str.unescapeHTML('J') // 'J'
* str.unescapeHTML('J') // 'J'
* str.unescapeHTML('J') // 'J'
* str.unescapeHTML('&_#39;') // '&_#39;'
* str.unescapeHTML('&#38;') // '&'
* str.unescapeHTML('&amp;') // '&'
* str.unescapeHTML(' ') // ' '
*
* // 'what is the ¥ to £ to € conversion process?'
* str.unescapeHTML('what is the ¥ to £ to € conversion process?')
*
* // '© 1992. License available for 50 ¢'
* str.unescapeHTML('© 1992. License available for 50 ¢')
*/
export let unescapeHTML = str => {
str = makeStr(str)
if (str === '') {
return ''
}
return str.replace(/&([^;]+);/g, unescape)
}