From 936382fadf803dc5f8f3cd3f278d0014988d0a0c Mon Sep 17 00:00:00 2001 From: mehaotian <490272692@qq.com> Date: Sun, 26 Sep 2021 11:43:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(h5):=20=E4=BF=AE=E5=A4=8D=20rich-text=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=97=A0=E6=B3=95=E6=B8=B2=E6=9F=93=20sectio?= =?UTF-8?q?n=20=E7=AD=89=E6=A0=87=E7=AD=BE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/rich-text/nodes-parser.js | 288 ++++++++++-------- 1 file changed, 155 insertions(+), 133 deletions(-) diff --git a/packages/uni-components/src/components/rich-text/nodes-parser.js b/packages/uni-components/src/components/rich-text/nodes-parser.js index eca1d100a..bfb63933e 100644 --- a/packages/uni-components/src/components/rich-text/nodes-parser.js +++ b/packages/uni-components/src/components/rich-text/nodes-parser.js @@ -1,133 +1,155 @@ -import { hasOwn, isPlainObject } from '@vue/shared' - -const TAGS = { - a: '', - abbr: '', - b: '', - blockquote: '', - br: '', - code: '', - col: ['span', 'width'], - colgroup: ['span', 'width'], - dd: '', - del: '', - div: '', - dl: '', - dt: '', - em: '', - fieldset: '', - h1: '', - h2: '', - h3: '', - h4: '', - h5: '', - h6: '', - hr: '', - i: '', - img: ['alt', 'src', 'height', 'width'], - ins: '', - label: '', - legend: '', - li: '', - ol: ['start', 'type'], - p: '', - q: '', - span: '', - strong: '', - sub: '', - sup: '', - table: ['width'], - tbody: '', - td: ['colspan', 'rowspan', 'height', 'width'], - tfoot: '', - th: ['colspan', 'rowspan', 'height', 'width'], - thead: '', - tr: '', - ul: '' -} -const CHARS = { - amp: '&', - gt: '>', - lt: '<', - nbsp: ' ', - quot: '"', - apos: "'" -} - -function decodeEntities(htmlString) { - return htmlString.replace(/&(([a-zA-Z]+)|(#x{0,1}[\da-zA-Z]+));/gi, function( - match, - stage - ) { - if (hasOwn(CHARS, stage) && CHARS[stage]) { - return CHARS[stage] - } - if (/^#[0-9]{1,4}$/.test(stage)) { - return String.fromCharCode(stage.slice(1)) - } - if (/^#x[0-9a-f]{1,4}$/i.test(stage)) { - return String.fromCharCode('0' + stage.slice(1)) - } - const wrap = document.createElement('div') - wrap.innerHTML = match - return wrap.innerText || wrap.textContent - }) -} - -export default function parseNodes(nodes, parentNode) { - nodes.forEach(function(node) { - if (!isPlainObject(node)) { - return - } - if (!hasOwn(node, 'type') || node.type === 'node') { - if (!(typeof node.name === 'string' && node.name)) { - return - } - const tagName = node.name.toLowerCase() - if (!hasOwn(TAGS, tagName)) { - return - } - const elem = document.createElement(tagName) - if (!elem) { - return - } - const attrs = node.attrs - if (isPlainObject(attrs)) { - const tagAttrs = TAGS[tagName] || [] - Object.keys(attrs).forEach(function(name) { - let value = attrs[name] - switch (name) { - case 'class': - /* eslint-disable no-fallthrough */ - Array.isArray(value) && (value = value.join(' ')) - case 'style': - elem.setAttribute(name, value) - break - default: - if (tagAttrs.indexOf(name) !== -1) { - elem.setAttribute(name, value) - } - } - }) - } - - const children = node.children - if (Array.isArray(children) && children.length) { - parseNodes(node.children, elem) - } - - parentNode.appendChild(elem) - } else { - if ( - node.type === 'text' && - typeof node.text === 'string' && - node.text !== '' - ) { - parentNode.appendChild( - document.createTextNode(decodeEntities(node.text)) - ) - } - } - }) - return parentNode -} +import { hasOwn, isPlainObject } from '@vue/shared' + +const TAGS = { + a: '', + abbr: '', + address: '', + article: '', + aside: '', + b: '', + bdi: '', + bdo: ['dir'], + big: '', + blockquote: '', + br: '', + caption: '', + center: '', + cite: '', + code: '', + col: ['span', 'width'], + colgroup: ['span', 'width'], + dd: '', + del: '', + div: '', + dl: '', + dt: '', + em: '', + fieldset: '', + font: '', + footer: '', + h1: '', + h2: '', + h3: '', + h4: '', + h5: '', + h6: '', + header: '', + hr: '', + i: '', + img: ['alt', 'src', 'height', 'width'], + ins: '', + label: '', + legend: '', + li: '', + mark: '', + nav: '', + ol: ['start', 'type'], + p: '', + pre: '', + q: '', + rt: '', + ruby: '', + s: '', + section: '', + small: '', + span: '', + strong: '', + sub: '', + sup: '', + table: ['width'], + tbody: '', + td: ['colspan', 'height', 'rowspan', 'width'], + tfoot: '', + th: ['colspan', 'height', 'rowspan', 'width'], + thead: '', + tr: ['colspan', 'height', 'rowspan', 'width'], + tt: '', + u: '', + ul: '', +} +const CHARS = { + amp: '&', + gt: '>', + lt: '<', + nbsp: ' ', + quot: '"', + apos: "'", +} + +function decodeEntities(htmlString) { + return htmlString.replace( + /&(([a-zA-Z]+)|(#x{0,1}[\da-zA-Z]+));/gi, + function (match, stage) { + if (hasOwn(CHARS, stage) && CHARS[stage]) { + return CHARS[stage] + } + if (/^#[0-9]{1,4}$/.test(stage)) { + return String.fromCharCode(stage.slice(1)) + } + if (/^#x[0-9a-f]{1,4}$/i.test(stage)) { + return String.fromCharCode('0' + stage.slice(1)) + } + const wrap = document.createElement('div') + wrap.innerHTML = match + return wrap.innerText || wrap.textContent + } + ) +} + +export default function parseNodes(nodes, parentNode) { + nodes.forEach(function (node) { + if (!isPlainObject(node)) { + return + } + if (!hasOwn(node, 'type') || node.type === 'node') { + if (!(typeof node.name === 'string' && node.name)) { + return + } + const tagName = node.name.toLowerCase() + if (!hasOwn(TAGS, tagName)) { + return + } + const elem = document.createElement(tagName) + if (!elem) { + return + } + const attrs = node.attrs + if (isPlainObject(attrs)) { + const tagAttrs = TAGS[tagName] || [] + Object.keys(attrs).forEach(function (name) { + let value = attrs[name] + switch (name) { + case 'class': + /* eslint-disable no-fallthrough */ + Array.isArray(value) && (value = value.join(' ')) + case 'style': + elem.setAttribute(name, value) + break + default: + if (tagAttrs.indexOf(name) !== -1) { + elem.setAttribute(name, value) + } + } + }) + } + + const children = node.children + if (Array.isArray(children) && children.length) { + parseNodes(node.children, elem) + } + + parentNode.appendChild(elem) + } else { + if ( + node.type === 'text' && + typeof node.text === 'string' && + node.text !== '' + ) { + parentNode.appendChild( + document.createTextNode(decodeEntities(node.text)) + ) + } + } + }) + return parentNode +} -- GitLab