@thi.ng/hdom-mock 2.1.66 → 2.1.68
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/CHANGELOG.md +1 -1
- package/index.js +199 -201
- package/package.json +10 -7
package/CHANGELOG.md
CHANGED
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
getElementById(id)
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
removeChild(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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.
|
|
3
|
+
"version": "2.1.68",
|
|
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
|
|
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.
|
|
37
|
-
"@thi.ng/checks": "^3.4.
|
|
38
|
-
"@thi.ng/hdom": "^9.
|
|
38
|
+
"@thi.ng/api": "^8.9.13",
|
|
39
|
+
"@thi.ng/checks": "^3.4.13",
|
|
40
|
+
"@thi.ng/hdom": "^9.4.1"
|
|
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",
|
|
@@ -52,7 +55,7 @@
|
|
|
52
55
|
"access": "public"
|
|
53
56
|
},
|
|
54
57
|
"engines": {
|
|
55
|
-
"node": ">=
|
|
58
|
+
"node": ">=18"
|
|
56
59
|
},
|
|
57
60
|
"files": [
|
|
58
61
|
"./*.js",
|
|
@@ -68,5 +71,5 @@
|
|
|
68
71
|
"status": "alpha",
|
|
69
72
|
"year": 2018
|
|
70
73
|
},
|
|
71
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
|
|
72
75
|
}
|