From e9786e75960e0902cd6569c306dcd389f8e4e105 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] check is svg when serialization --- src/rebuild.ts | 103 ++---------------------------------------------- src/snapshot.ts | 5 +++ src/types.ts | 1 + 3 files changed, 9 insertions(+), 100 deletions(-) diff --git a/src/rebuild.ts b/src/rebuild.ts index e4e79b28..9a045000 100644 --- a/src/rebuild.ts +++ b/src/rebuild.ts @@ -7,103 +7,6 @@ import { INode, } from './types'; -// TODO: need a more accurate list -const svgTags = [ - 'altGlyph', - 'altGlyphDef', - 'altGlyphItem', - 'animate', - 'animateColor', - 'animateMotion', - 'animateTransform', - 'animation', - 'circle', - 'clipPath', - 'color-profile', - 'cursor', - 'defs', - 'desc', - 'discard', - 'ellipse', - 'feBlend', - 'feColorMatrix', - 'feComponentTransfer', - 'feComposite', - 'feConvolveMatrix', - 'feDiffuseLighting', - 'feDisplacementMap', - 'feDistantLight', - 'feDropShadow', - 'feFlood', - 'feFuncA', - 'feFuncB', - 'feFuncG', - 'feFuncR', - 'feGaussianBlur', - 'feImage', - 'feMerge', - 'feMergeNode', - 'feMorphology', - 'feOffset', - 'fePointLight', - 'feSpecularLighting', - 'feSpotLight', - 'feTile', - 'feTurbulence', - 'filter', - 'font', - 'font-face', - 'font-face-format', - 'font-face-name', - 'font-face-src', - 'font-face-uri', - 'foreignObject', - 'g', - 'glyph', - 'glyphRef', - 'handler', - 'hatch', - 'hatchpath', - 'hkern', - 'image', - 'line', - 'linearGradient', - 'listener', - 'marker', - 'mask', - 'mesh', - 'meshgradient', - 'meshpatch', - 'meshrow', - 'metadata', - 'missing-glyph', - 'mpath', - 'path', - 'pattern', - 'polygon', - 'polyline', - 'prefetch', - 'radialGradient', - 'rect', - 'set', - 'solidColor', - 'solidcolor', - 'stop', - 'svg', - 'switch', - 'symbol', - 'tbreak', - 'text', - 'textArea', - 'textPath', - 'tref', - 'tspan', - 'unknown', - 'use', - 'view', - 'vkern', -]; - const tagMap: tagMap = { script: 'noscript', }; @@ -144,10 +47,10 @@ function buildNode(n: serializedNodeWithId, doc: Document): Node | null { case NodeType.Element: const tagName = getTagName(n); let node: Element; - if (svgTags.indexOf(tagName) < 0) { - node = doc.createElement(tagName); - } else { + if (n.isSVG) { node = doc.createElementNS('http://www.w3.org/2000/svg', tagName); + } else { + node = doc.createElement(tagName); } for (const name in n.attributes) { if (n.attributes.hasOwnProperty(name)) { diff --git a/src/snapshot.ts b/src/snapshot.ts index f1376ae7..a0623055 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -77,6 +77,10 @@ function absoluteToDoc(doc: Document, attributeValue: string): string { return a.href; } +function isSVGElement(el: Element): boolean { + return el.tagName === 'svg' || el instanceof SVGElement; +} + function serializeNode(n: Node, doc: Document): serializedNode | false { switch (n.nodeType) { case n.DOCUMENT_NODE: @@ -142,6 +146,7 @@ function serializeNode(n: Node, doc: Document): serializedNode | false { tagName, attributes, childNodes: [], + isSVG: isSVGElement(n as Element) || undefined, }; case n.TEXT_NODE: // The parent node may not be a html element which has a tagName attribute. diff --git a/src/types.ts b/src/types.ts index 62d6af8b..21fa2de3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -27,6 +27,7 @@ export type elementNode = { tagName: string; attributes: attributes; childNodes: serializedNodeWithId[]; + isSVG?: true; }; export type textNode = {