@thi.ng/hdom-mock 2.1.66 → 2.1.67

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 (3) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/index.js +199 -201
  3. package/package.json +9 -6
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/index.js CHANGED
@@ -2,209 +2,207 @@ import { isFunction } from "@thi.ng/checks/is-function";
2
2
  import { diffTree } from "@thi.ng/hdom/diff";
3
3
  import { createTree, hydrateTree } from "@thi.ng/hdom/dom";
4
4
  import { normalizeTree } from "@thi.ng/hdom/normalize";
5
- export const TEXT = Symbol();
6
- export class HDOMNode {
7
- /**
8
- * Only real child nodes
9
- */
10
- children;
11
- /**
12
- * Includes real children AND text nodes
13
- */
14
- _children;
15
- listeners;
16
- value;
17
- checked;
18
- tag;
19
- attribs;
20
- style;
21
- body;
22
- constructor(tag, attribs = {}) {
23
- this.tag = tag;
24
- this.children = [];
25
- this._children = [];
26
- this.attribs = attribs;
27
- this.listeners = {};
28
- }
29
- get textContent() {
30
- const res = [];
31
- for (let c of this._children) {
32
- if (c.isText()) {
33
- res.push(c.body);
34
- }
35
- }
36
- return res.join("");
37
- }
38
- set textContent(body) {
39
- const txt = new HDOMNode(TEXT);
40
- txt.body = body;
41
- this._children = [txt];
42
- this.children = [];
43
- }
44
- isText() {
45
- return this.tag === TEXT;
46
- }
47
- insertBefore(c, i) {
48
- const existing = this.children[i];
49
- if (existing) {
50
- !this.isText() && this.children.splice(i, 0, c);
51
- this._children.splice(this._children.indexOf(existing), 0, c);
52
- }
53
- else {
54
- this.appendChild(c);
55
- }
5
+ const TEXT = Symbol();
6
+ class HDOMNode {
7
+ /**
8
+ * Only real child nodes
9
+ */
10
+ children;
11
+ /**
12
+ * Includes real children AND text nodes
13
+ */
14
+ _children;
15
+ listeners;
16
+ value;
17
+ checked;
18
+ tag;
19
+ attribs;
20
+ style;
21
+ body;
22
+ constructor(tag, attribs = {}) {
23
+ this.tag = tag;
24
+ this.children = [];
25
+ this._children = [];
26
+ this.attribs = attribs;
27
+ this.listeners = {};
28
+ }
29
+ get textContent() {
30
+ const res = [];
31
+ for (let c of this._children) {
32
+ if (c.isText()) {
33
+ res.push(c.body);
34
+ }
35
+ }
36
+ return res.join("");
37
+ }
38
+ set textContent(body) {
39
+ const txt = new HDOMNode(TEXT);
40
+ txt.body = body;
41
+ this._children = [txt];
42
+ this.children = [];
43
+ }
44
+ isText() {
45
+ return this.tag === TEXT;
46
+ }
47
+ insertBefore(c, i) {
48
+ const existing = this.children[i];
49
+ if (existing) {
50
+ !this.isText() && this.children.splice(i, 0, c);
51
+ this._children.splice(this._children.indexOf(existing), 0, c);
52
+ } else {
53
+ this.appendChild(c);
54
+ }
55
+ return c;
56
+ }
57
+ appendChild(c) {
58
+ !c.isText() && this.children.push(c);
59
+ this._children.push(c);
60
+ return c;
61
+ }
62
+ removeChild(i) {
63
+ const c = this.children[i];
64
+ if (c) {
65
+ this.children.splice(i, 1);
66
+ this._children.splice(this._children.indexOf(c), 1);
67
+ }
68
+ }
69
+ getElementById(id) {
70
+ if (this.attribs.id === id)
71
+ return this;
72
+ let c;
73
+ for (c of this.children) {
74
+ c = c.getElementById(id);
75
+ if (c)
56
76
  return c;
57
77
  }
58
- appendChild(c) {
59
- !c.isText() && this.children.push(c);
60
- this._children.push(c);
61
- return c;
62
- }
63
- removeChild(i) {
64
- const c = this.children[i];
65
- if (c) {
66
- this.children.splice(i, 1);
67
- this._children.splice(this._children.indexOf(c), 1);
68
- }
69
- }
70
- getElementById(id) {
71
- if (this.attribs.id === id)
72
- return this;
73
- let c;
74
- for (c of this.children) {
75
- c = c.getElementById(id);
76
- if (c)
77
- return c;
78
- }
79
- return null;
80
- }
81
- toHiccup() {
82
- if (this.isText()) {
83
- return this.body;
84
- }
85
- const attr = { ...this.attribs };
86
- this.style && (attr.style = this.style);
87
- this.value != null && (attr.value = this.value);
88
- this.checked && (attr.checked = true);
89
- return [this.tag, attr, ...this._children.map((c) => c.toHiccup())];
90
- }
78
+ return null;
79
+ }
80
+ toHiccup() {
81
+ if (this.isText()) {
82
+ return this.body;
83
+ }
84
+ const attr = { ...this.attribs };
85
+ this.style && (attr.style = this.style);
86
+ this.value != null && (attr.value = this.value);
87
+ this.checked && (attr.checked = true);
88
+ return [this.tag, attr, ...this._children.map((c) => c.toHiccup())];
89
+ }
91
90
  }
92
- export class MockHDOM {
93
- root;
94
- constructor(root) {
95
- this.root = root;
96
- }
97
- normalizeTree(opts, tree) {
98
- return normalizeTree(opts, tree);
99
- }
100
- createTree(opts, parent, tree, child) {
101
- return createTree(opts, this, parent, tree, child);
102
- }
103
- hydrateTree(opts, parent, tree, child) {
104
- return hydrateTree(opts, this, parent, tree, child);
105
- }
106
- diffTree(opts, parent, prev, curr, child) {
107
- diffTree(opts, this, parent, prev, curr, child);
108
- }
109
- createElement(parent, tag, attribs, insert) {
110
- const el = new HDOMNode(tag);
111
- if (parent) {
112
- if (insert == null) {
113
- parent.appendChild(el);
114
- }
115
- else {
116
- parent.insertBefore(el, insert);
117
- }
118
- }
119
- if (attribs) {
120
- this.setAttribs(el, attribs);
121
- }
122
- return el;
123
- }
124
- createTextElement(parent, content) {
125
- const el = new HDOMNode(TEXT);
126
- el.body = content;
127
- parent && parent.appendChild(el);
128
- return el;
129
- }
130
- getElementById(id) {
131
- return this.root.getElementById(id);
132
- }
133
- replaceChild(opts, parent, child, tree) {
134
- this.removeChild(parent, child);
135
- return this.createTree(opts, parent, tree, child);
136
- }
137
- getChild(parent, i) {
138
- return parent.children[i];
139
- }
140
- removeChild(parent, i) {
141
- parent.removeChild(i);
142
- }
143
- setAttribs(el, attribs) {
144
- for (let k in attribs) {
145
- this.setAttrib(el, k, attribs[k], attribs);
91
+ class MockHDOM {
92
+ root;
93
+ constructor(root) {
94
+ this.root = root;
95
+ }
96
+ normalizeTree(opts, tree) {
97
+ return normalizeTree(opts, tree);
98
+ }
99
+ createTree(opts, parent, tree, child) {
100
+ return createTree(opts, this, parent, tree, child);
101
+ }
102
+ hydrateTree(opts, parent, tree, child) {
103
+ return hydrateTree(opts, this, parent, tree, child);
104
+ }
105
+ diffTree(opts, parent, prev, curr, child) {
106
+ diffTree(opts, this, parent, prev, curr, child);
107
+ }
108
+ createElement(parent, tag, attribs, insert) {
109
+ const el = new HDOMNode(tag);
110
+ if (parent) {
111
+ if (insert == null) {
112
+ parent.appendChild(el);
113
+ } else {
114
+ parent.insertBefore(el, insert);
115
+ }
116
+ }
117
+ if (attribs) {
118
+ this.setAttribs(el, attribs);
119
+ }
120
+ return el;
121
+ }
122
+ createTextElement(parent, content) {
123
+ const el = new HDOMNode(TEXT);
124
+ el.body = content;
125
+ parent && parent.appendChild(el);
126
+ return el;
127
+ }
128
+ getElementById(id) {
129
+ return this.root.getElementById(id);
130
+ }
131
+ replaceChild(opts, parent, child, tree) {
132
+ this.removeChild(parent, child);
133
+ return this.createTree(opts, parent, tree, child);
134
+ }
135
+ getChild(parent, i) {
136
+ return parent.children[i];
137
+ }
138
+ removeChild(parent, i) {
139
+ parent.removeChild(i);
140
+ }
141
+ setAttribs(el, attribs) {
142
+ for (let k in attribs) {
143
+ this.setAttrib(el, k, attribs[k], attribs);
144
+ }
145
+ return el;
146
+ }
147
+ setAttrib(el, id, val, attribs) {
148
+ if (id.startsWith("__"))
149
+ return;
150
+ const isListener = id.indexOf("on") === 0;
151
+ if (!isListener && typeof val === "function") {
152
+ val = val(attribs);
153
+ }
154
+ if (val !== void 0 && val !== false) {
155
+ switch (id) {
156
+ case "style":
157
+ this.setStyle(el, val);
158
+ break;
159
+ case "value":
160
+ el.value = val;
161
+ break;
162
+ case "checked":
163
+ el[id] = val;
164
+ break;
165
+ default:
166
+ if (isListener) {
167
+ const lid = id.substring(2);
168
+ const listeners = el.listeners[lid];
169
+ (listeners || (el.listeners[lid] = [])).push(val);
170
+ } else {
171
+ el.attribs[id] = val;
172
+ }
173
+ }
174
+ } else {
175
+ el[id] != null ? el[id] = null : delete el.attribs[id];
176
+ }
177
+ return el;
178
+ }
179
+ removeAttribs(el, attribs, prev) {
180
+ for (let i = attribs.length; i-- > 0; ) {
181
+ const a = attribs[i];
182
+ if (a.indexOf("on") === 0) {
183
+ const listeners = el.listeners[a.substring(2)];
184
+ if (listeners) {
185
+ const i2 = listeners.indexOf(prev[a]);
186
+ i2 >= 0 && listeners.splice(i2, 1);
146
187
  }
147
- return el;
148
- }
149
- setAttrib(el, id, val, attribs) {
150
- if (id.startsWith("__"))
151
- return;
152
- const isListener = id.indexOf("on") === 0;
153
- if (!isListener && typeof val === "function") {
154
- val = val(attribs);
155
- }
156
- if (val !== undefined && val !== false) {
157
- switch (id) {
158
- case "style":
159
- this.setStyle(el, val);
160
- break;
161
- case "value":
162
- el.value = val;
163
- break;
164
- case "checked":
165
- el[id] = val;
166
- break;
167
- default:
168
- if (isListener) {
169
- const lid = id.substring(2);
170
- const listeners = el.listeners[lid];
171
- (listeners || (el.listeners[lid] = [])).push(val);
172
- }
173
- else {
174
- el.attribs[id] = val;
175
- }
176
- }
177
- }
178
- else {
179
- el[id] != null
180
- ? (el[id] = null)
181
- : delete el.attribs[id];
182
- }
183
- return el;
184
- }
185
- removeAttribs(el, attribs, prev) {
186
- for (let i = attribs.length; i-- > 0;) {
187
- const a = attribs[i];
188
- if (a.indexOf("on") === 0) {
189
- const listeners = el.listeners[a.substring(2)];
190
- if (listeners) {
191
- const i = listeners.indexOf(prev[a]);
192
- i >= 0 && listeners.splice(i, 1);
193
- }
194
- }
195
- else {
196
- el[a] ? (el[a] = null) : delete el.attribs[a];
197
- }
198
- }
199
- }
200
- setContent(el, value) {
201
- el.textContent = value;
202
- }
203
- setStyle(el, rules) {
204
- for (let r in rules) {
205
- let v = rules[r];
206
- isFunction(v) && (v = v(rules));
207
- v != null && ((el.style || (el.style = {}))[r] = v);
208
- }
209
- }
188
+ } else {
189
+ el[a] ? el[a] = null : delete el.attribs[a];
190
+ }
191
+ }
192
+ }
193
+ setContent(el, value) {
194
+ el.textContent = value;
195
+ }
196
+ setStyle(el, rules) {
197
+ for (let r in rules) {
198
+ let v = rules[r];
199
+ isFunction(v) && (v = v(rules));
200
+ v != null && ((el.style || (el.style = {}))[r] = v);
201
+ }
202
+ }
210
203
  }
204
+ export {
205
+ HDOMNode,
206
+ MockHDOM,
207
+ TEXT
208
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/hdom-mock",
3
- "version": "2.1.66",
3
+ "version": "2.1.67",
4
4
  "description": "Mock base implementation for @thi.ng/hdom API",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,12 +35,13 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.11",
37
- "@thi.ng/checks": "^3.4.11",
38
- "@thi.ng/hdom": "^9.3.31"
38
+ "@thi.ng/api": "^8.9.12",
39
+ "@thi.ng/checks": "^3.4.12",
40
+ "@thi.ng/hdom": "^9.4.0"
39
41
  },
40
42
  "devDependencies": {
41
43
  "@microsoft/api-extractor": "^7.38.3",
44
+ "esbuild": "^0.19.8",
42
45
  "rimraf": "^5.0.5",
43
46
  "tools": "^0.0.1",
44
47
  "typedoc": "^0.25.4",
@@ -68,5 +71,5 @@
68
71
  "status": "alpha",
69
72
  "year": 2018
70
73
  },
71
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
74
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
72
75
  }