From a2dd7eb6f12833036082c2e2670d1fb94ebc62ea Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Mon, 25 May 2026 11:42:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(handler.js):=20=E5=AE=8C=E5=96=84HTML?= =?UTF-8?q?=E5=AE=9E=E4=BD=93=E8=A7=A3=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复文本节点仅替换&的问题,新增完整HTML4实体解码支持,正确解析各类字符实体和数字实体 --- .../static/app-plus/mp-html/js/handler.js | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/uni_modules/mp-html/static/app-plus/mp-html/js/handler.js b/uni_modules/mp-html/static/app-plus/mp-html/js/handler.js index 1b3ee25..71dd21a 100644 --- a/uni_modules/mp-html/static/app-plus/mp-html/js/handler.js +++ b/uni_modules/mp-html/static/app-plus/mp-html/js/handler.js @@ -15,6 +15,32 @@ document.addEventListener('UniAppJSBridgeReady', () => { let options let medias = [] +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: 'ω' +} + +function decodeEntity(str, amp) { + amp = amp === undefined ? true : amp + 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.substring(0, i) + String.fromCharCode(code) + str.substring(j + 1) + } + } else { + code = str.substring(i + 1, j) + if (html4Entities[code] || (code === 'amp' && amp)) { + str = str.substring(0, i) + (html4Entities[code] || '&') + str.substring(j + 1) + } + } + i = str.indexOf('&', i + 1) + } + return str +} /** * @description 获取标签的所有属性 @@ -186,7 +212,7 @@ function createDom (nodes, parent, namespace) { namespace = undefined } } else { - ele = document.createTextNode(node.text.replace(/&/g, '&')) + ele = document.createTextNode(decodeEntity(node.text)) } parent.appendChild(ele) }