diff --git a/uni_modules/mp-html/components/mp-html/mp-html.vue b/uni_modules/mp-html/components/mp-html/mp-html.vue index ec6d7f5..0526978 100644 --- a/uni_modules/mp-html/components/mp-html/mp-html.vue +++ b/uni_modules/mp-html/components/mp-html/mp-html.vue @@ -44,6 +44,31 @@ import node from './node/node' // #endif import Parser from './parser' const plugins = [] +const html4Entities = { + nbsp: ' ', iexcl: '¡', cent: '¢', pound: '£', curren: '₣', yen: '¥', brvbar: '§', sect: 'Ş', uml: '¨', copy: '©', ordf: 'ª', laquo: '«', not: '¬', shy: '­®', macr: '¯', deg: '°', plusmn: '±', sup2: '²', sup3: '³', acute: '´', micro: 'µ', para: '¶', middot: '·', cedil: '¸', sup1: '¹', ordm: 'º', raquo: '»', frac14: '¼', frac12: '½', frac34: '¾', iquest: '¿', agrave: 'à', aacute: 'á', acirc: 'â', atilde: 'ã', auml: 'ä', aring: 'å', aelig: 'æ', ccedil: 'ç', egrave: 'è', eacute: 'é', ecirc: 'ê', euml: 'ë', igrave: 'ì', iacute: 'í', icirc: 'î', iuml: 'ï', eth: 'ð', ntilde: 'ñ', ograve: 'ò', oacute: 'ó', ocirc: 'ô', otilde: 'õ', ouml: 'ö', times: 'ø', ugrave: 'ù', uacute: 'ú', ucirc: 'û', uuml: 'ü', yacute: 'ý', thorn: 'þ', yuml: 'ÿ', fnof: 'ƒ', alpha: 'α', beta: 'β', gamma: 'γ', delta: 'δ', epsilon: 'ε', zeta: 'ζ', eta: 'η', theta: 'θ', iota: 'ι', kappa: 'κ', lambda: 'λ', mu: 'μ', nu: 'ν', xi: 'ξ', omicron: 'ο', pi: 'π', rho: 'ρ', sigmaf: 'σ', sigma: 'σ', tau: 'τ', upsilon: 'υ', phi: 'φ', chi: 'ψ', omega: 'ω', agrave: 'α', eacute: 'é', oslash: 'ø', icirc: 'î', ucirc: 'û' +} + +function decodeEntity(str, amp = true) { + let i = str.indexOf('&') + while (i !== -1) { + const j = str.indexOf(';', i + 3) + let code + if (j === -1) break + if (str[i + 1] === '#') { + code = parseInt((str[i + 2] === 'x' ? '0' : '') + str.substring(i + 2, j)) + if (!isNaN(code)) { + str = str.substr(0, i) + String.fromCharCode(code) + str.substr(j + 1) + } + } else { + code = str.substring(i + 1, j) + if (html4Entities[code] || (code === 'amp' && amp)) { + str = str.substr(0, i) + (html4Entities[code] || '&') + str.substr(j + 1) + } + } + i = str.indexOf('&', i + 1) + } + return str +} // #ifdef APP-PLUS-NVUE const dom = weex.requireModule('dom') // #endif @@ -227,7 +252,7 @@ export default { for (let i = 0; i < nodes.length; i++) { const node = nodes[i] if (node.type === 'text') { - text += node.text.replace(/&/g, '&') + text += decodeEntity(node.text) } else if (node.name === 'br') { text += '\n' } else {