MCPcopy
hub / github.com/antvis/Infographic / processElement

Function processElement

src/jsx/renderer.ts:17–65  ·  view source on GitHub ↗

* pre-process the element tree, expanding all components and layouts

(element: JSXNode, context: RenderContext)

Source from the content-addressed store, hash-verified

15 * pre-process the element tree, expanding all components and layouts
16 */
17function processElement(element: JSXNode, context: RenderContext): JSXNode {
18 if (element == null || typeof element === 'boolean') return null;
19 if (typeof element === 'string' || typeof element === 'number') {
20 return element;
21 }
22 if (Array.isArray(element)) {
23 return element
24 .map((child) => processElement(child, context))
25 .filter(Boolean);
26 }
27
28 // layout component
29 if (isLayoutComponent(element)) {
30 const layoutResult = performLayout(element, context);
31 return processElement(layoutResult, context);
32 }
33
34 // expand function components
35 if (typeof element.type === 'function') {
36 const rendered = element.type(element.props);
37 return processElement(rendered, context);
38 }
39
40 // process children
41 const children = getRenderableChildrenOf(element)
42 .map((child) => processElement(child, context))
43 .filter(Boolean) as RenderableNode[];
44
45 // process Fragment
46 if (element.type === Fragment) {
47 return children;
48 }
49
50 // process Defs, collect defs content
51 if (element.type === DefsSymbol) {
52 children.forEach((child) => {
53 if (typeof child === 'object' && child.props.id) {
54 context.defs.set(child.props.id, child);
55 }
56 });
57 return null;
58 }
59
60 if (children.length) {
61 return cloneElement(element, { children });
62 }
63
64 return element;
65}
66
67/**
68 * render a single element to string

Callers 1

renderSVGFunction · 0.85

Calls 5

isLayoutComponentFunction · 0.90
performLayoutFunction · 0.90
getRenderableChildrenOfFunction · 0.90
cloneElementFunction · 0.90
forEachMethod · 0.80

Tested by

no test coverage detected