@untrue/serializer 1.0.0 → 2.0.0

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/LICENSE CHANGED
File without changes
@@ -0,0 +1,9 @@
1
+ import { Slot } from "untrue";
2
+ export type ContentFormat = "xml" | "html";
3
+ export declare class Serializer {
4
+ static serialize(slot: Slot, format?: ContentFormat): string;
5
+ private static serializeSlot;
6
+ private static serializeChildren;
7
+ private static escape;
8
+ private static htmlSelfClosingTags;
9
+ }
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Serializer = void 0;
4
+ const untrue_1 = require("untrue");
5
+ class Serializer {
6
+ static serialize(slot, format = "html") {
7
+ if (!["xml", "html"].includes(format)) {
8
+ throw new Error('Valid formats are: "xml", "html".');
9
+ }
10
+ if (!(slot instanceof untrue_1.Slot)) {
11
+ throw new Error("Invalid slot.");
12
+ }
13
+ return this.serializeSlot(slot, format);
14
+ }
15
+ static serializeSlot(slot, format) {
16
+ if (slot === null || slot === undefined || slot === false) {
17
+ return "";
18
+ }
19
+ if (!(slot instanceof untrue_1.Slot)) {
20
+ return this.escape(`${slot}`);
21
+ }
22
+ if (slot.isClass()) {
23
+ const ComponentClass = slot.getContentType();
24
+ const props = slot.getProps();
25
+ const component = new ComponentClass(props);
26
+ const children = component.render();
27
+ slot.setChildren(children);
28
+ return this.serializeChildren(slot, format);
29
+ }
30
+ else if (slot.isFunction()) {
31
+ const ComponentFunction = slot.getContentType();
32
+ const props = slot.getProps();
33
+ const children = ComponentFunction(props);
34
+ slot.setChildren(children);
35
+ return this.serializeChildren(slot, format);
36
+ }
37
+ else if (slot.isElement()) {
38
+ let result = "";
39
+ const tagName = slot.getContentType();
40
+ const attributes = slot.getAttributes() ?? {};
41
+ const keys = Object.keys(attributes);
42
+ const attr = keys.length !== 0
43
+ ? keys
44
+ .map((key) => `${key}="${this.escape(attributes[key])}"`)
45
+ .join(" ")
46
+ : null;
47
+ let selfClose = false;
48
+ switch (format) {
49
+ case "html": {
50
+ selfClose = this.htmlSelfClosingTags.has(tagName);
51
+ break;
52
+ }
53
+ case "xml": {
54
+ const children = slot.getChildren();
55
+ selfClose = children.length === 0;
56
+ break;
57
+ }
58
+ }
59
+ result += `<${tagName}`;
60
+ if (attr !== null) {
61
+ result += ` ${attr}`;
62
+ }
63
+ if (selfClose) {
64
+ result += " />";
65
+ }
66
+ else {
67
+ result += ">";
68
+ result += this.serializeChildren(slot, format);
69
+ result += `</${tagName}>`;
70
+ }
71
+ return result;
72
+ }
73
+ else if (slot.isNull()) {
74
+ return this.serializeChildren(slot, format);
75
+ }
76
+ return "";
77
+ }
78
+ static serializeChildren(slot, format) {
79
+ let result = "";
80
+ for (const child of slot.getChildren()) {
81
+ result += this.serializeSlot(child, format);
82
+ }
83
+ return result;
84
+ }
85
+ static escape(source) {
86
+ return source
87
+ .replaceAll("&", "&amp;")
88
+ .replaceAll('"', "&quot;")
89
+ .replaceAll("'", "&apos;")
90
+ .replaceAll("<", "&lt;")
91
+ .replaceAll(">", "&gt;");
92
+ }
93
+ static htmlSelfClosingTags = new Set([
94
+ "area",
95
+ "base",
96
+ "br",
97
+ "col",
98
+ "embed",
99
+ "hr",
100
+ "img",
101
+ "input",
102
+ "link",
103
+ "meta",
104
+ "param",
105
+ "source",
106
+ "track",
107
+ "wbr",
108
+ ]);
109
+ }
110
+ exports.Serializer = Serializer;
@@ -0,0 +1 @@
1
+ export { Serializer } from "./Serializer";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Serializer = void 0;
4
+ var Serializer_1 = require("./Serializer");
5
+ Object.defineProperty(exports, "Serializer", { enumerable: true, get: function () { return Serializer_1.Serializer; } });
@@ -0,0 +1,9 @@
1
+ import { Slot } from "untrue";
2
+ export type ContentFormat = "xml" | "html";
3
+ export declare class Serializer {
4
+ static serialize(slot: Slot, format?: ContentFormat): string;
5
+ private static serializeSlot;
6
+ private static serializeChildren;
7
+ private static escape;
8
+ private static htmlSelfClosingTags;
9
+ }
@@ -0,0 +1,106 @@
1
+ import { Slot } from "untrue";
2
+ export class Serializer {
3
+ static serialize(slot, format = "html") {
4
+ if (!["xml", "html"].includes(format)) {
5
+ throw new Error('Valid formats are: "xml", "html".');
6
+ }
7
+ if (!(slot instanceof Slot)) {
8
+ throw new Error("Invalid slot.");
9
+ }
10
+ return this.serializeSlot(slot, format);
11
+ }
12
+ static serializeSlot(slot, format) {
13
+ if (slot === null || slot === undefined || slot === false) {
14
+ return "";
15
+ }
16
+ if (!(slot instanceof Slot)) {
17
+ return this.escape(`${slot}`);
18
+ }
19
+ if (slot.isClass()) {
20
+ const ComponentClass = slot.getContentType();
21
+ const props = slot.getProps();
22
+ const component = new ComponentClass(props);
23
+ const children = component.render();
24
+ slot.setChildren(children);
25
+ return this.serializeChildren(slot, format);
26
+ }
27
+ else if (slot.isFunction()) {
28
+ const ComponentFunction = slot.getContentType();
29
+ const props = slot.getProps();
30
+ const children = ComponentFunction(props);
31
+ slot.setChildren(children);
32
+ return this.serializeChildren(slot, format);
33
+ }
34
+ else if (slot.isElement()) {
35
+ let result = "";
36
+ const tagName = slot.getContentType();
37
+ const attributes = slot.getAttributes() ?? {};
38
+ const keys = Object.keys(attributes);
39
+ const attr = keys.length !== 0
40
+ ? keys
41
+ .map((key) => `${key}="${this.escape(attributes[key])}"`)
42
+ .join(" ")
43
+ : null;
44
+ let selfClose = false;
45
+ switch (format) {
46
+ case "html": {
47
+ selfClose = this.htmlSelfClosingTags.has(tagName);
48
+ break;
49
+ }
50
+ case "xml": {
51
+ const children = slot.getChildren();
52
+ selfClose = children.length === 0;
53
+ break;
54
+ }
55
+ }
56
+ result += `<${tagName}`;
57
+ if (attr !== null) {
58
+ result += ` ${attr}`;
59
+ }
60
+ if (selfClose) {
61
+ result += " />";
62
+ }
63
+ else {
64
+ result += ">";
65
+ result += this.serializeChildren(slot, format);
66
+ result += `</${tagName}>`;
67
+ }
68
+ return result;
69
+ }
70
+ else if (slot.isNull()) {
71
+ return this.serializeChildren(slot, format);
72
+ }
73
+ return "";
74
+ }
75
+ static serializeChildren(slot, format) {
76
+ let result = "";
77
+ for (const child of slot.getChildren()) {
78
+ result += this.serializeSlot(child, format);
79
+ }
80
+ return result;
81
+ }
82
+ static escape(source) {
83
+ return source
84
+ .replaceAll("&", "&amp;")
85
+ .replaceAll('"', "&quot;")
86
+ .replaceAll("'", "&apos;")
87
+ .replaceAll("<", "&lt;")
88
+ .replaceAll(">", "&gt;");
89
+ }
90
+ static htmlSelfClosingTags = new Set([
91
+ "area",
92
+ "base",
93
+ "br",
94
+ "col",
95
+ "embed",
96
+ "hr",
97
+ "img",
98
+ "input",
99
+ "link",
100
+ "meta",
101
+ "param",
102
+ "source",
103
+ "track",
104
+ "wbr",
105
+ ]);
106
+ }
@@ -0,0 +1 @@
1
+ export { Serializer } from "./Serializer";
@@ -0,0 +1 @@
1
+ export { Serializer } from "./Serializer";
package/index.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,18 +1,38 @@
1
1
  {
2
2
  "name": "@untrue/serializer",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "XML and HTML serializer for Untrue.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/iconshot/untrue-serializer.git"
8
8
  },
9
- "main": "index.js",
9
+ "main": "./dist/cjs/index.js",
10
+ "module": "./dist/esm/index.js",
11
+ "types": "./dist/esm/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "require": "./dist/cjs/index.js",
15
+ "import": "./dist/esm/index.js"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "clean": "rm -rf dist",
20
+ "build-cjs": "tsc --project tsconfig.cjs.json",
21
+ "build-esm": "tsc --project tsconfig.esm.json",
22
+ "build": "npm run clean && npm run build-cjs && npm run build-esm",
23
+ "prepare": "npm run build"
24
+ },
10
25
  "keywords": [
11
26
  "serializer",
12
- "untrue"
27
+ "untrue",
28
+ "html",
29
+ "xml"
13
30
  ],
14
31
  "license": "MIT",
15
32
  "peerDependencies": {
16
- "untrue": "^4.4.0"
33
+ "untrue": "^5.18.0"
34
+ },
35
+ "devDependencies": {
36
+ "typescript": "^5.9.3"
17
37
  }
18
38
  }
package/src/Serializer.js DELETED
@@ -1,79 +0,0 @@
1
- const { Node } = require("untrue");
2
-
3
- class Serializer {
4
- static escape(string) {
5
- return string
6
- .replaceAll("&", "&amp;")
7
- .replaceAll('"', "&quot;")
8
- .replaceAll("'", "&apos;")
9
- .replaceAll("<", "&lt;")
10
- .replaceAll(">", "&gt;");
11
- }
12
-
13
- static serialize(node, content = this.content.xml) {
14
- if (content !== this.content.xml && content !== this.content.html) {
15
- throw new Error('Invalid content type. Valid values are: "xml", "html".');
16
- }
17
-
18
- if (node === null || node === undefined || node === false) {
19
- return "";
20
- }
21
-
22
- if (!(node instanceof Node)) {
23
- return this.escape(`${node}`);
24
- }
25
-
26
- let string = "";
27
-
28
- const type = node.getType();
29
- const attributes = node.getAttributes();
30
- const children = node.getChildren();
31
-
32
- const keys = Object.keys(attributes);
33
-
34
- const attr =
35
- keys.length > 0
36
- ? keys
37
- .map((key) => `${key}="${this.escape(attributes[key])}"`)
38
- .join(" ")
39
- : null;
40
-
41
- const selfClose =
42
- content === this.content.html && this.htmlSelfClosingTags.includes(type);
43
-
44
- if (selfClose) {
45
- string += `<${type}${attr !== null ? ` ${attr}` : ""}/>`;
46
- } else {
47
- string += `<${type}${attr !== null ? ` ${attr}` : ""}>`;
48
-
49
- for (const child of children) {
50
- string += this.serialize(child, content);
51
- }
52
-
53
- string += `</${type}>`;
54
- }
55
-
56
- return string;
57
- }
58
- }
59
-
60
- Serializer.content = { xml: "xml", html: "html" };
61
-
62
- Serializer.htmlSelfClosingTags = [
63
- "area",
64
- "base",
65
- "br",
66
- "col",
67
- "embed",
68
- "hr",
69
- "img",
70
- "input",
71
- "link",
72
- "meta",
73
- "param",
74
- "source",
75
- "track",
76
- "wbr",
77
- ];
78
-
79
- module.exports = Serializer;