diff --git a/packages/uni-components/src/components/rich-text/index.tsx b/packages/uni-components/src/components/rich-text/index.tsx index bb72bef97b412d7e4d73bbb6592ee9839a8ccdcd..eebf7c051b3e3f4481ce7caa6df06c6f013390a6 100644 --- a/packages/uni-components/src/components/rich-text/index.tsx +++ b/packages/uni-components/src/components/rich-text/index.tsx @@ -1,4 +1,4 @@ -import { onMounted, ref, watch } from 'vue' +import { onMounted, ref, watch, getCurrentInstance } from 'vue' import { defineBuiltInComponent } from '@dcloudio/uni-components' import parseHtml from './html-parser' import parseNodes from './nodes-parser' @@ -18,14 +18,19 @@ export default /*#__PURE__*/ defineBuiltInComponent({ MODE: 3, }, props, - setup(props) { + setup(props, ...args) { + const vm = getCurrentInstance() const rootRef = ref(null) function _renderNodes(nodes: string | unknown[]) { if (typeof nodes === 'string') { nodes = parseHtml(nodes) } - const nodeList = parseNodes(nodes, document.createDocumentFragment()) + const nodeList = parseNodes( + nodes, + document.createDocumentFragment(), + (vm?.root.type as any).__scopeId || '' + ) rootRef.value!.firstElementChild!.innerHTML = '' rootRef.value!.firstElementChild!.appendChild(nodeList) } 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 bfb63933e255e7810298d2903b5c2c4ea1e75b28..1aa71a1ce2ac6a96afce38e9acb0190221897f7c 100644 --- a/packages/uni-components/src/components/rich-text/nodes-parser.js +++ b/packages/uni-components/src/components/rich-text/nodes-parser.js @@ -96,7 +96,7 @@ function decodeEntities(htmlString) { ) } -export default function parseNodes(nodes, parentNode) { +export default function parseNodes(nodes, parentNode, scopeId) { nodes.forEach(function (node) { if (!isPlainObject(node)) { return @@ -124,6 +124,7 @@ export default function parseNodes(nodes, parentNode) { Array.isArray(value) && (value = value.join(' ')) case 'style': elem.setAttribute(name, value) + scopeId && elem.setAttribute(scopeId, '') break default: if (tagAttrs.indexOf(name) !== -1) {