Render SVG nodes correctly

This commit is contained in:
MoyuScript 2017-08-13 17:48:37 +08:00
parent 615deb7ecd
commit c2c91a50b8
4 changed files with 54 additions and 6 deletions

View File

@ -1,2 +1,9 @@
declare var __DEV__: boolean; declare var __DEV__: boolean;
declare var __VERSION__: string; declare var __VERSION__: string;
declare class SVGSVGElement extends Element {
className: string;
style: CSSStyleDeclaration;
getPresentationAttribute(name: string): any;
}

View File

@ -46,7 +46,7 @@ export class Bounds {
} }
} }
export const parseBounds = (node: HTMLElement): Bounds => { export const parseBounds = (node: HTMLElement | SVGSVGElement): Bounds => {
return Bounds.fromClientRect(node.getBoundingClientRect()); return Bounds.fromClientRect(node.getBoundingClientRect());
}; };

View File

@ -85,7 +85,7 @@ export default class NodeContainer {
index: number; index: number;
constructor( constructor(
node: HTMLElement, node: HTMLElement | SVGSVGElement,
parent: ?NodeContainer, parent: ?NodeContainer,
imageLoader: ImageLoader, imageLoader: ImageLoader,
index: number index: number
@ -157,7 +157,11 @@ export default class NodeContainer {
if (__DEV__) { if (__DEV__) {
this.name = `${node.tagName.toLowerCase()}${node.id this.name = `${node.tagName.toLowerCase()}${node.id
? `#${node.id}` ? `#${node.id}`
: ''}${node.className.split(' ').map(s => (s.length ? `.${s}` : '')).join('')}`; : ''}${node.className
.toString()
.split(' ')
.map(s => (s.length ? `.${s}` : ''))
.join('')}`;
} }
} }
getClipPaths(): Array<Path> { getClipPaths(): Array<Path> {
@ -215,7 +219,16 @@ export default class NodeContainer {
} }
} }
const getImage = (node: HTMLElement, imageLoader: ImageLoader): ?string => { const getImage = (node: HTMLElement | SVGSVGElement, imageLoader: ImageLoader): ?string => {
if (
node instanceof node.ownerDocument.defaultView.SVGSVGElement ||
node instanceof SVGSVGElement
) {
const s = new XMLSerializer();
return imageLoader.loadImage(
`data:image/svg+xml,${encodeURIComponent(s.serializeToString(node))}`
);
}
switch (node.tagName) { switch (node.tagName) {
case 'IMG': case 'IMG':
// $FlowFixMe // $FlowFixMe

View File

@ -102,6 +102,28 @@ const parseNodeTree = (
} }
} }
} }
} else if (
childNode instanceof defaultView.SVGSVGElement ||
childNode instanceof SVGSVGElement
) {
const container = new NodeContainer(childNode, parent, imageLoader, index++);
const treatAsRealStackingContext = createsRealStackingContext(container, childNode);
if (treatAsRealStackingContext || createsStackingContext(container)) {
// for treatAsRealStackingContext:false, any positioned descendants and descendants
// which actually create a new stacking context should be considered part of the parent stacking context
const parentStack =
treatAsRealStackingContext || container.isPositioned()
? stack.getRealParentStackingContext()
: stack;
const childStack = new StackingContext(
container,
parentStack,
treatAsRealStackingContext
);
parentStack.contexts.push(childStack);
} else {
stack.children.push(container);
}
} }
} }
}; };
@ -144,7 +166,10 @@ const inlinePseudoElement = (node: HTMLElement, pseudoElt: ':before' | ':after')
} }
}; };
const createsRealStackingContext = (container: NodeContainer, node: HTMLElement): boolean => { const createsRealStackingContext = (
container: NodeContainer,
node: HTMLElement | SVGSVGElement
): boolean => {
return ( return (
container.isRootElement() || container.isRootElement() ||
container.isPositionedWithZIndex() || container.isPositionedWithZIndex() ||
@ -158,7 +183,10 @@ const createsStackingContext = (container: NodeContainer): boolean => {
return container.isPositioned() || container.isFloating(); return container.isPositioned() || container.isFloating();
}; };
const isBodyWithTransparentRoot = (container: NodeContainer, node: HTMLElement): boolean => { const isBodyWithTransparentRoot = (
container: NodeContainer,
node: HTMLElement | SVGSVGElement
): boolean => {
return ( return (
node.nodeName === 'BODY' && node.nodeName === 'BODY' &&
container.parent instanceof NodeContainer && container.parent instanceof NodeContainer &&