|
1 | 1 | import * as React from 'react' |
2 | | -import { Attributes, Element } from './ast' |
| 2 | +import { ASTElement, Attributes, NodeType } from './ast' |
3 | 3 |
|
4 | 4 | const reactAttributesMap: Attributes = { |
5 | 5 | acceptcharset: 'acceptCharset', |
@@ -56,43 +56,47 @@ function transformAttributes(attributes?: Attributes): Attributes { |
56 | 56 |
|
57 | 57 | const transformedAttributes: Attributes = {} |
58 | 58 | Object.keys(attributes).forEach((key) => { |
59 | | - if (!attributes[key].startsWith('on') && reactAttributesMap[key]) { |
60 | | - transformedAttributes[reactAttributesMap[key]] = attributes[key] |
| 59 | + if (!key.startsWith('on')) { |
| 60 | + if (reactAttributesMap[key]) { |
| 61 | + transformedAttributes[reactAttributesMap[key]] = attributes[key] |
| 62 | + } else { |
| 63 | + transformedAttributes[key] = attributes[key] |
| 64 | + } |
61 | 65 | } |
62 | 66 | }) |
63 | 67 | return transformedAttributes |
64 | 68 | } |
65 | 69 |
|
66 | | -function transformChildren(children?: Element[]) { |
| 70 | +function transformChildren(children?: ASTElement[]) { |
67 | 71 | return children || [] |
68 | 72 | } |
69 | 73 |
|
70 | | -export function transform(element: Element) { |
| 74 | +export function transform(element: ASTElement) { |
71 | 75 | return { |
72 | 76 | ...element, |
73 | | - attribs: transformAttributes(element.attribs), |
| 77 | + attributes: transformAttributes(element.attributes), |
74 | 78 | children: transformChildren(element.children), |
75 | 79 | } |
76 | 80 | } |
77 | 81 |
|
78 | | -export function renderElement(element: Element): React.ReactNode { |
79 | | - if (element.type === 'text') { |
80 | | - return element.data |
81 | | - } else if (element.type === 'tag') { |
82 | | - const children = element.data ? [ |
83 | | - element.data, |
| 82 | +export function renderElement(element: ASTElement): React.ReactNode { |
| 83 | + if (element.type === NodeType.TEXT_NODE) { |
| 84 | + return element.value |
| 85 | + } else if (element.type === NodeType.ELEMENT_NODE) { |
| 86 | + const children = element.value ? [ |
| 87 | + element.value, |
84 | 88 | ...renderElements(element.children), |
85 | 89 | ] : renderElements(element.children) |
86 | | - return React.createElement(element.name, element.attribs, children) |
| 90 | + return React.createElement(element.name, element.attributes, children) |
87 | 91 | } |
88 | 92 |
|
89 | 93 | return null |
90 | 94 | } |
91 | 95 |
|
92 | | -export function renderElements(ast: Element[]): React.ReactNode [] { |
| 96 | +export function renderElements(ast: ASTElement[]): React.ReactNode[] { |
93 | 97 | return ast.reduce((elements, element) => { |
94 | | - // Only keep text and tags. Remove everything else like comments, scritps,... |
95 | | - if (element.type === 'tag' || element.type === 'text') { |
| 98 | + // Only keep text and tags. Remove everything else |
| 99 | + if (element.type === NodeType.ELEMENT_NODE || element.type === NodeType.TEXT_NODE) { |
96 | 100 | elements.push(renderElement(transform(element))) |
97 | 101 | } |
98 | 102 | return elements |
|
0 commit comments