From 3405fb7e8a8f770238351b30b7a1942feb3a591a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Fri, 26 Jan 2024 11:09:17 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E6=95=B0=E5=AD=A6=E5=85=AC=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=EF=BC=8C=E5=88=87=E6=8D=A2=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E6=97=B6=E6=96=87=E6=9C=AC=E6=A0=B7=E5=BC=8F=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=94=B9=E5=8F=98=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/utils/index.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 25458a54..c6786844 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -489,9 +489,27 @@ export const removeHtmlStyle = html => { } // 给html标签中指定的标签添加内联样式 +let addHtmlStyleEl = null export const addHtmlStyle = (html, tag, style) => { - const reg = new RegExp(`(<${tag}[^>]*)(>[^<>]*)`, 'g') - return html.replaceAll(reg, `$1 style="${style}"$2`) + if (!addHtmlStyleEl) { + addHtmlStyleEl = document.createElement('div') + } + addHtmlStyleEl.innerHTML = html + let walk = root => { + let childNodes = root.childNodes + childNodes.forEach(node => { + if (node.nodeType === 1) { + // 元素节点 + if (node.tagName.toLowerCase() === tag) { + node.style.cssText = style + } else { + walk(node) + } + } + }) + } + walk(addHtmlStyleEl) + return addHtmlStyleEl.innerHTML } // 检查一个字符串是否是富文本字符