check is svg when serialization
This commit is contained in:
103
src/rebuild.ts
103
src/rebuild.ts
@@ -7,103 +7,6 @@ import {
|
|||||||
INode,
|
INode,
|
||||||
} from './types';
|
} 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 = {
|
const tagMap: tagMap = {
|
||||||
script: 'noscript',
|
script: 'noscript',
|
||||||
};
|
};
|
||||||
@@ -144,10 +47,10 @@ function buildNode(n: serializedNodeWithId, doc: Document): Node | null {
|
|||||||
case NodeType.Element:
|
case NodeType.Element:
|
||||||
const tagName = getTagName(n);
|
const tagName = getTagName(n);
|
||||||
let node: Element;
|
let node: Element;
|
||||||
if (svgTags.indexOf(tagName) < 0) {
|
if (n.isSVG) {
|
||||||
node = doc.createElement(tagName);
|
|
||||||
} else {
|
|
||||||
node = doc.createElementNS('http://www.w3.org/2000/svg', tagName);
|
node = doc.createElementNS('http://www.w3.org/2000/svg', tagName);
|
||||||
|
} else {
|
||||||
|
node = doc.createElement(tagName);
|
||||||
}
|
}
|
||||||
for (const name in n.attributes) {
|
for (const name in n.attributes) {
|
||||||
if (n.attributes.hasOwnProperty(name)) {
|
if (n.attributes.hasOwnProperty(name)) {
|
||||||
|
|||||||
@@ -77,6 +77,10 @@ function absoluteToDoc(doc: Document, attributeValue: string): string {
|
|||||||
return a.href;
|
return a.href;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isSVGElement(el: Element): boolean {
|
||||||
|
return el.tagName === 'svg' || el instanceof SVGElement;
|
||||||
|
}
|
||||||
|
|
||||||
function serializeNode(n: Node, doc: Document): serializedNode | false {
|
function serializeNode(n: Node, doc: Document): serializedNode | false {
|
||||||
switch (n.nodeType) {
|
switch (n.nodeType) {
|
||||||
case n.DOCUMENT_NODE:
|
case n.DOCUMENT_NODE:
|
||||||
@@ -142,6 +146,7 @@ function serializeNode(n: Node, doc: Document): serializedNode | false {
|
|||||||
tagName,
|
tagName,
|
||||||
attributes,
|
attributes,
|
||||||
childNodes: [],
|
childNodes: [],
|
||||||
|
isSVG: isSVGElement(n as Element) || undefined,
|
||||||
};
|
};
|
||||||
case n.TEXT_NODE:
|
case n.TEXT_NODE:
|
||||||
// The parent node may not be a html element which has a tagName attribute.
|
// The parent node may not be a html element which has a tagName attribute.
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ export type elementNode = {
|
|||||||
tagName: string;
|
tagName: string;
|
||||||
attributes: attributes;
|
attributes: attributes;
|
||||||
childNodes: serializedNodeWithId[];
|
childNodes: serializedNodeWithId[];
|
||||||
|
isSVG?: true;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type textNode = {
|
export type textNode = {
|
||||||
|
|||||||
Reference in New Issue
Block a user