Skip to content

Commit

Permalink
fix: svg style parse
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed Nov 7, 2024
1 parent 34c5be8 commit 684000f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 94 deletions.
16 changes: 11 additions & 5 deletions src/svg-dom/parseCSSStylesheet.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
export function parseCSSStylesheet(node: SVGStyleElement, stylesheets: Record<string, any>): 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<string, any> = {}
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 },
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/svg-dom/parseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export function parseNode(
node: SVGElement,
style: Record<string, any>,
paths: Path2D[] = [],
stylesheets: Record<string, any> = {},
): Path2D[] {
if (node.nodeType !== 1)
return paths

let isDefsNode = false
let path: Path2D | null = null
let _style = { ...style }
const stylesheets: Record<string, any> = {}

switch (node.nodeName) {
case 'svg':
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 7 additions & 35 deletions test/fixtures/006.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 14 additions & 51 deletions test/fixtures/007.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions test/fixtures/ref.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 684000f

Please sign in to comment.