Skip to content

Commit

Permalink
perf: svg cloning optimized using deep clone
Browse files Browse the repository at this point in the history
  • Loading branch information
jagermesh committed Apr 17, 2024
1 parent 359baf3 commit b672638
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/clone-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,24 @@ async function cloneSingleNode<T extends HTMLElement>(
return cloneIFrameElement(node)
}

return node.cloneNode(false) as T
return node.cloneNode(isSVGElement(node)) as T
}

const isSlotElement = (node: HTMLElement): node is HTMLSlotElement =>
node.tagName != null && node.tagName.toUpperCase() === 'SLOT'

const isSVGElement = (node: HTMLElement): node is HTMLSlotElement =>
node.tagName != null && node.tagName.toUpperCase() === 'SVG'

async function cloneChildren<T extends HTMLElement>(
nativeNode: T,
clonedNode: T,
options: Options,
): Promise<T> {
if (isSVGElement(clonedNode)) {

Check warning on line 78 in src/clone-node.ts

View check run for this annotation

Codecov / codecov/patch

src/clone-node.ts#L78

Added line #L78 was not covered by tests
return clonedNode
}

let children: T[] = []

if (isSlotElement(nativeNode) && nativeNode.assignedNodes) {
Expand Down Expand Up @@ -133,11 +140,11 @@ function cloneCSSStyle<T extends HTMLElement>(nativeNode: T, clonedNode: T) {
) {
value = 'block'
}

if (name === 'd' && clonedNode.getAttribute('d')) {
value = `path(${clonedNode.getAttribute('d')})`
}

targetStyle.setProperty(
name,
value,
Expand Down

0 comments on commit b672638

Please sign in to comment.