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 } // 检查一个字符串是否是富文本字符