@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Serializer.js +48 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@untrue/serializer",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "XML and HTML serializer for Untrue.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/Serializer.js CHANGED
@@ -25,32 +25,62 @@ class Serializer {
25
25
 
26
26
  let string = "";
27
27
 
28
- const type = node.getType();
29
- const attributes = node.getAttributes();
30
- const children = node.getChildren();
28
+ if (node.isComponent()) {
29
+ const Component = node.getType();
30
+ const props = node.getProps();
31
31
 
32
- const keys = Object.keys(attributes);
32
+ const component = new Component(props);
33
33
 
34
- const attr =
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
- const selfClose =
42
- content === this.content.html && this.htmlSelfClosingTags.includes(type);
36
+ node.setChildren(children);
43
37
 
44
- if (selfClose) {
45
- string += `<${type}${attr !== null ? ` ${attr}` : ""}/>`;
46
- } else {
47
- string += `<${type}${attr !== null ? ` ${attr}` : ""}>`;
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
- for (const child of children) {
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
- string += `</${type}>`;
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;