@untrue/serializer 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Serializer.js +48 -18
package/package.json
CHANGED
package/src/Serializer.js
CHANGED
|
@@ -25,32 +25,62 @@ class Serializer {
|
|
|
25
25
|
|
|
26
26
|
let string = "";
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
if (node.isComponent()) {
|
|
29
|
+
const Component = node.getType();
|
|
30
|
+
const props = node.getProps();
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
const component = new Component(props);
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
keys.length > 0
|
|
36
|
-
? keys
|
|
37
|
-
.map((key) => `${key}="${this.escape(attributes[key])}"`)
|
|
38
|
-
.join(" ")
|
|
39
|
-
: null;
|
|
34
|
+
const children = component.render();
|
|
40
35
|
|
|
41
|
-
|
|
42
|
-
content === this.content.html && this.htmlSelfClosingTags.includes(type);
|
|
36
|
+
node.setChildren(children);
|
|
43
37
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
for (const child of node.getChildren()) {
|
|
39
|
+
string += this.serialize(child, content);
|
|
40
|
+
}
|
|
41
|
+
} else if (node.isFunction()) {
|
|
42
|
+
const Function = node.getType();
|
|
43
|
+
const props = node.getProps();
|
|
44
|
+
|
|
45
|
+
const children = Function(props);
|
|
48
46
|
|
|
49
|
-
|
|
47
|
+
node.setChildren(children);
|
|
48
|
+
|
|
49
|
+
for (const child of node.getChildren()) {
|
|
50
50
|
string += this.serialize(child, content);
|
|
51
51
|
}
|
|
52
|
+
} else if (node.isElement()) {
|
|
53
|
+
const type = node.getType();
|
|
54
|
+
const attributes = node.getAttributes();
|
|
55
|
+
|
|
56
|
+
const keys = Object.keys(attributes);
|
|
57
|
+
|
|
58
|
+
const attr =
|
|
59
|
+
keys.length > 0
|
|
60
|
+
? keys
|
|
61
|
+
.map((key) => `${key}="${this.escape(attributes[key])}"`)
|
|
62
|
+
.join(" ")
|
|
63
|
+
: null;
|
|
52
64
|
|
|
53
|
-
|
|
65
|
+
const selfClose =
|
|
66
|
+
content === this.content.html &&
|
|
67
|
+
this.htmlSelfClosingTags.includes(type);
|
|
68
|
+
|
|
69
|
+
if (selfClose) {
|
|
70
|
+
string += `<${type}${attr !== null ? ` ${attr}` : ""}/>`;
|
|
71
|
+
} else {
|
|
72
|
+
string += `<${type}${attr !== null ? ` ${attr}` : ""}>`;
|
|
73
|
+
|
|
74
|
+
for (const child of node.getChildren()) {
|
|
75
|
+
string += this.serialize(child, content);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
string += `</${type}>`;
|
|
79
|
+
}
|
|
80
|
+
} else if (node.isNull()) {
|
|
81
|
+
for (const child of node.getChildren()) {
|
|
82
|
+
string += this.serialize(child, content);
|
|
83
|
+
}
|
|
54
84
|
}
|
|
55
85
|
|
|
56
86
|
return string;
|