* pre-process the element tree, expanding all components and layouts
(element: JSXNode, context: RenderContext)
| 15 | * pre-process the element tree, expanding all components and layouts |
| 16 | */ |
| 17 | function 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 |
no test coverage detected