From 684000f27f40dae1b6abe476aa9fcb68e9ea67c7 Mon Sep 17 00:00:00 2001 From: wengxiangmin <157215725@qq.com> Date: Thu, 7 Nov 2024 15:03:22 +0800 Subject: [PATCH] fix: svg style parse --- src/svg-dom/parseCSSStylesheet.ts | 16 ++++--- src/svg-dom/parseNode.ts | 6 +-- test/fixtures/006.svg | 42 ++++--------------- test/fixtures/007.svg | 65 +++++++--------------------- test/fixtures/ref.svg | 70 +++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 94 deletions(-) create mode 100644 test/fixtures/ref.svg diff --git a/src/svg-dom/parseCSSStylesheet.ts b/src/svg-dom/parseCSSStylesheet.ts index aa2309c..ab7651f 100644 --- a/src/svg-dom/parseCSSStylesheet.ts +++ b/src/svg-dom/parseCSSStylesheet.ts @@ -1,22 +1,28 @@ export function parseCSSStylesheet(node: SVGStyleElement, stylesheets: Record): void { if (!node.sheet || !node.sheet.cssRules || !node.sheet.cssRules.length) return + for (let i = 0; i < node.sheet.cssRules.length; i++) { const stylesheet = node.sheet.cssRules[i] as CSSStyleRule + if (stylesheet.type !== 1) continue + const selectorList = stylesheet.selectorText .split(/,/g) .filter(Boolean) .map(i => i.trim()) + + const definitions: Record = {} + for (let len = stylesheet.style.length, i = 0; i < len; i++) { + const name = stylesheet.style.item(i) + definitions[name] = stylesheet.style.getPropertyValue(name) + } + for (let j = 0; j < selectorList.length; j++) { - // Remove empty rules - const definitions = Object.fromEntries( - Object.entries(stylesheet.style).filter(([, v]) => v !== ''), - ) stylesheets[selectorList[j]] = Object.assign( stylesheets[selectorList[j]] || {}, - definitions, + { ...definitions }, ) } } diff --git a/src/svg-dom/parseNode.ts b/src/svg-dom/parseNode.ts index a9d215f..0cb4218 100644 --- a/src/svg-dom/parseNode.ts +++ b/src/svg-dom/parseNode.ts @@ -15,6 +15,7 @@ export function parseNode( node: SVGElement, style: Record, paths: Path2D[] = [], + stylesheets: Record = {}, ): Path2D[] { if (node.nodeType !== 1) return paths @@ -22,7 +23,6 @@ export function parseNode( let isDefsNode = false let path: Path2D | null = null let _style = { ...style } - const stylesheets: Record = {} switch (node.nodeName) { case 'svg': @@ -72,7 +72,7 @@ export function parseNode( const usedNodeId = href.substring(1) const usedNode = (node.viewportElement as any)?.getElementById(usedNodeId) if (usedNode) { - parseNode(usedNode, _style, paths) + parseNode(usedNode, _style, paths, stylesheets) } else { console.warn(`'use node' references non-existent node id: ${usedNodeId}`) @@ -104,7 +104,7 @@ export function parseNode( const node = childNodes[i] if (isDefsNode && node.nodeName !== 'style' && node.nodeName !== 'defs') continue - parseNode(node as SVGElement, style, paths) + parseNode(node as SVGElement, style, paths, stylesheets) } if (transform) { diff --git a/test/fixtures/006.svg b/test/fixtures/006.svg index fd3b04f..eaa41f6 100644 --- a/test/fixtures/006.svg +++ b/test/fixtures/006.svg @@ -1,38 +1,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + viewBox="0 0 72 72" style="enable-background:new 0 0 72 72;" xml:space="preserve"> + + diff --git a/test/fixtures/007.svg b/test/fixtures/007.svg index 9bc2e81..8d893af 100644 --- a/test/fixtures/007.svg +++ b/test/fixtures/007.svg @@ -1,54 +1,17 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + viewBox="0 0 72 72" style="enable-background:new 0 0 72 72;" xml:space="preserve"> + + diff --git a/test/fixtures/ref.svg b/test/fixtures/ref.svg new file mode 100644 index 0000000..fd97f20 --- /dev/null +++ b/test/fixtures/ref.svg @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +