check is svg when serialization

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 544ef0b3a3
commit e9786e7596
3 changed files with 9 additions and 100 deletions

View File

@@ -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)) {

View File

@@ -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.

View File

@@ -27,6 +27,7 @@ export type elementNode = {
tagName: string;
attributes: attributes;
childNodes: serializedNodeWithId[];
isSVG?: true;
};
export type textNode = {