jj 2.3.0 → 2.4.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/lib/JJN.d.ts +25 -2
- package/lib/JJN.js +44 -7
- package/lib/JJN.js.map +1 -1
- package/lib/bundle.js +61 -30
- package/lib/bundle.js.map +2 -2
- package/lib/bundle.min.js +2 -2
- package/lib/mixin-types.d.ts +1 -1
- package/lib/mixins.d.ts +2 -19
- package/lib/mixins.js +17 -40
- package/lib/mixins.js.map +1 -1
- package/package.json +1 -1
package/lib/JJN.d.ts
CHANGED
|
@@ -23,6 +23,16 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
23
23
|
* @returns A new JJN instance.
|
|
24
24
|
*/
|
|
25
25
|
static from(node: Node): JJN;
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a value can be passed to the `wrap()` or `unwrap()` function.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* This is useful for filtering the array that is passed to `append()`, `prepend()` or `setChildren()`
|
|
31
|
+
*
|
|
32
|
+
* @param x an unknown value
|
|
33
|
+
* @returns true if `x` is a string, Node (or its descendents), JJN (or its descendents)
|
|
34
|
+
*/
|
|
35
|
+
static isWrapable(x: unknown): x is Wrappable;
|
|
26
36
|
/**
|
|
27
37
|
* Wraps a native DOM node or string into the most specific JJ wrapper available.
|
|
28
38
|
*
|
|
@@ -104,6 +114,9 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
104
114
|
/**
|
|
105
115
|
* Appends children to this node.
|
|
106
116
|
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
119
|
+
*
|
|
107
120
|
* @param children - The children to append (Nodes, strings, or Wrappers).
|
|
108
121
|
* @returns This instance for chaining.
|
|
109
122
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/append | Element.append}
|
|
@@ -118,6 +131,9 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
118
131
|
* list.mapAppend(['a', 'b'], item => h('li', null, item))
|
|
119
132
|
* ```
|
|
120
133
|
*
|
|
134
|
+
* @remarks
|
|
135
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
136
|
+
*
|
|
121
137
|
* @param array - The source array.
|
|
122
138
|
* @param mapFn - The mapping function returning a Wrappable.
|
|
123
139
|
* @returns This instance for chaining.
|
|
@@ -126,6 +142,9 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
126
142
|
/**
|
|
127
143
|
* Prepends children to this node.
|
|
128
144
|
*
|
|
145
|
+
* @remarks
|
|
146
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
147
|
+
*
|
|
129
148
|
* @param children - The children to prepend.
|
|
130
149
|
* @returns This instance for chaining.
|
|
131
150
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend | Element.prepend}
|
|
@@ -139,6 +158,9 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
139
158
|
* list.mapPrepend(['a', 'b'], item => JJHE.fromTag('li').setText(item))
|
|
140
159
|
* ```
|
|
141
160
|
*
|
|
161
|
+
* @remarks
|
|
162
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
163
|
+
*
|
|
142
164
|
* @param array - The source array.
|
|
143
165
|
* @param mapFn - The mapping function.
|
|
144
166
|
* @returns This instance for chaining.
|
|
@@ -149,12 +171,13 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
149
171
|
*
|
|
150
172
|
* @remarks
|
|
151
173
|
* If no children are specified, it essentially empties the node
|
|
174
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
152
175
|
*
|
|
153
176
|
* @param children - The new children to set.
|
|
154
177
|
* @returns This instance for chaining.
|
|
155
178
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren | Element.replaceChildren}
|
|
156
179
|
*/
|
|
157
|
-
|
|
180
|
+
setChildren(...children: Wrappable[]): this;
|
|
158
181
|
/**
|
|
159
182
|
* Adds an event listener.
|
|
160
183
|
*
|
|
@@ -198,7 +221,7 @@ export declare class JJN<T extends Node = Node> implements IAppendPrepend {
|
|
|
198
221
|
* })
|
|
199
222
|
* ```
|
|
200
223
|
* @remarks
|
|
201
|
-
* If you want to access the current JJ* instance, you SHOULD use a `function` not an arrow function.
|
|
224
|
+
* If you want to access the current JJ* instance using `this` keyword, you SHOULD use a `function` not an arrow function.
|
|
202
225
|
* If the function throws, `run()` doesn't swallow the exception.
|
|
203
226
|
* So if you're expecting an error, make sure to wrap it in a `try..catch` block and handle the exception.
|
|
204
227
|
* If the function returns a promise, you can `await` on the response.
|
package/lib/JJN.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _JJN_ref;
|
|
13
|
-
import { isA } from 'jty';
|
|
13
|
+
import { isA, isObj, isStr } from 'jty';
|
|
14
14
|
import { off, on } from './util.js';
|
|
15
15
|
/**
|
|
16
16
|
* Wraps a DOM Node.
|
|
@@ -36,6 +36,18 @@ export class JJN {
|
|
|
36
36
|
static from(node) {
|
|
37
37
|
return new JJN(node);
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a value can be passed to the `wrap()` or `unwrap()` function.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* This is useful for filtering the array that is passed to `append()`, `prepend()` or `setChildren()`
|
|
44
|
+
*
|
|
45
|
+
* @param x an unknown value
|
|
46
|
+
* @returns true if `x` is a string, Node (or its descendents), JJN (or its descendents)
|
|
47
|
+
*/
|
|
48
|
+
static isWrapable(x) {
|
|
49
|
+
return isStr(x) || isA(x, Node) || isA(x, JJN);
|
|
50
|
+
}
|
|
39
51
|
/**
|
|
40
52
|
* Wraps a native DOM node or string into the most specific JJ wrapper available.
|
|
41
53
|
*
|
|
@@ -73,7 +85,19 @@ export class JJN {
|
|
|
73
85
|
* @throws {TypeError} If the input cannot be unwrapped.
|
|
74
86
|
*/
|
|
75
87
|
static unwrap(obj) {
|
|
76
|
-
|
|
88
|
+
if (isStr(obj)) {
|
|
89
|
+
return document.createTextNode(obj);
|
|
90
|
+
}
|
|
91
|
+
if (!isObj(obj)) {
|
|
92
|
+
throw new TypeError(`Expected an object. Got ${obj} (${typeof obj})`);
|
|
93
|
+
}
|
|
94
|
+
if (isA(obj, Node)) {
|
|
95
|
+
return obj;
|
|
96
|
+
}
|
|
97
|
+
if (isA(obj, JJN)) {
|
|
98
|
+
return obj.ref;
|
|
99
|
+
}
|
|
100
|
+
throw new TypeError(`Could not unwrap ${obj} (${typeof obj})`);
|
|
77
101
|
}
|
|
78
102
|
/**
|
|
79
103
|
* Wraps an iterable object (e.g. an array of wrapped or DOM elements).
|
|
@@ -135,13 +159,16 @@ export class JJN {
|
|
|
135
159
|
/**
|
|
136
160
|
* Appends children to this node.
|
|
137
161
|
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
164
|
+
*
|
|
138
165
|
* @param children - The children to append (Nodes, strings, or Wrappers).
|
|
139
166
|
* @returns This instance for chaining.
|
|
140
167
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/append | Element.append}
|
|
141
168
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild | Node.appendChild}
|
|
142
169
|
*/
|
|
143
170
|
append(...children) {
|
|
144
|
-
const nodes = JJN.unwrapAll(children);
|
|
171
|
+
const nodes = JJN.unwrapAll(children.filter(JJN.isWrapable));
|
|
145
172
|
const ref = this.ref;
|
|
146
173
|
for (const node of nodes) {
|
|
147
174
|
if (node) {
|
|
@@ -158,6 +185,9 @@ export class JJN {
|
|
|
158
185
|
* list.mapAppend(['a', 'b'], item => h('li', null, item))
|
|
159
186
|
* ```
|
|
160
187
|
*
|
|
188
|
+
* @remarks
|
|
189
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
190
|
+
*
|
|
161
191
|
* @param array - The source array.
|
|
162
192
|
* @param mapFn - The mapping function returning a Wrappable.
|
|
163
193
|
* @returns This instance for chaining.
|
|
@@ -168,12 +198,15 @@ export class JJN {
|
|
|
168
198
|
/**
|
|
169
199
|
* Prepends children to this node.
|
|
170
200
|
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
203
|
+
*
|
|
171
204
|
* @param children - The children to prepend.
|
|
172
205
|
* @returns This instance for chaining.
|
|
173
206
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend | Element.prepend}
|
|
174
207
|
*/
|
|
175
208
|
prepend(...children) {
|
|
176
|
-
const nodes = JJN.unwrapAll(children);
|
|
209
|
+
const nodes = JJN.unwrapAll(children.filter(JJN.isWrapable));
|
|
177
210
|
const ref = this.ref;
|
|
178
211
|
const first = ref.firstChild;
|
|
179
212
|
for (const node of nodes) {
|
|
@@ -191,6 +224,9 @@ export class JJN {
|
|
|
191
224
|
* list.mapPrepend(['a', 'b'], item => JJHE.fromTag('li').setText(item))
|
|
192
225
|
* ```
|
|
193
226
|
*
|
|
227
|
+
* @remarks
|
|
228
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
229
|
+
*
|
|
194
230
|
* @param array - The source array.
|
|
195
231
|
* @param mapFn - The mapping function.
|
|
196
232
|
* @returns This instance for chaining.
|
|
@@ -203,13 +239,14 @@ export class JJN {
|
|
|
203
239
|
*
|
|
204
240
|
* @remarks
|
|
205
241
|
* If no children are specified, it essentially empties the node
|
|
242
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
206
243
|
*
|
|
207
244
|
* @param children - The new children to set.
|
|
208
245
|
* @returns This instance for chaining.
|
|
209
246
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren | Element.replaceChildren}
|
|
210
247
|
*/
|
|
211
|
-
|
|
212
|
-
return this.empty().append(...children);
|
|
248
|
+
setChildren(...children) {
|
|
249
|
+
return this.empty().append(...children.filter(JJN.isWrapable));
|
|
213
250
|
}
|
|
214
251
|
/**
|
|
215
252
|
* Adds an event listener.
|
|
@@ -269,7 +306,7 @@ export class JJN {
|
|
|
269
306
|
* })
|
|
270
307
|
* ```
|
|
271
308
|
* @remarks
|
|
272
|
-
* If you want to access the current JJ* instance, you SHOULD use a `function` not an arrow function.
|
|
309
|
+
* If you want to access the current JJ* instance using `this` keyword, you SHOULD use a `function` not an arrow function.
|
|
273
310
|
* If the function throws, `run()` doesn't swallow the exception.
|
|
274
311
|
* So if you're expecting an error, make sure to wrap it in a `try..catch` block and handle the exception.
|
|
275
312
|
* If the function returns a promise, you can `await` on the response.
|
package/lib/JJN.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JJN.js","sourceRoot":"","sources":["../src/JJN.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAA;
|
|
1
|
+
{"version":3,"file":"JJN.js","sourceRoot":"","sources":["../src/JJN.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,CAAA;AAEvC,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,WAAW,CAAA;AAGnC;;;;;;;;GAQG;AACH,MAAM,OAAO,GAAG;IACZ;;;;;;;;;;OAUG;IACH,MAAM,CAAC,IAAI,CAAC,I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G,CAAA;QACxB,OAAO,OAAO,CAAC,UAAU,EAAE,CAAC;YACxB,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;QAC3C,CAAC;QACD,OAAO,IAAI,CAAA;IACf,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAwB,EAAoC,EAAE,GAAG,IAAU;QAC1E,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAA;IACjC,CAAC;CACJ"}
|
package/lib/bundle.js
CHANGED
|
@@ -148,6 +148,18 @@ var JJN = class _JJN {
|
|
|
148
148
|
static from(node) {
|
|
149
149
|
return new _JJN(node);
|
|
150
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Checks if a value can be passed to the `wrap()` or `unwrap()` function.
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* This is useful for filtering the array that is passed to `append()`, `prepend()` or `setChildren()`
|
|
156
|
+
*
|
|
157
|
+
* @param x an unknown value
|
|
158
|
+
* @returns true if `x` is a string, Node (or its descendents), JJN (or its descendents)
|
|
159
|
+
*/
|
|
160
|
+
static isWrapable(x) {
|
|
161
|
+
return isStr(x) || isA(x, Node) || isA(x, _JJN);
|
|
162
|
+
}
|
|
151
163
|
/**
|
|
152
164
|
* Wraps a native DOM node or string into the most specific JJ wrapper available.
|
|
153
165
|
*
|
|
@@ -185,7 +197,19 @@ var JJN = class _JJN {
|
|
|
185
197
|
* @throws {TypeError} If the input cannot be unwrapped.
|
|
186
198
|
*/
|
|
187
199
|
static unwrap(obj) {
|
|
188
|
-
|
|
200
|
+
if (isStr(obj)) {
|
|
201
|
+
return document.createTextNode(obj);
|
|
202
|
+
}
|
|
203
|
+
if (!isObj(obj)) {
|
|
204
|
+
throw new TypeError(`Expected an object. Got ${obj} (${typeof obj})`);
|
|
205
|
+
}
|
|
206
|
+
if (isA(obj, Node)) {
|
|
207
|
+
return obj;
|
|
208
|
+
}
|
|
209
|
+
if (isA(obj, _JJN)) {
|
|
210
|
+
return obj.ref;
|
|
211
|
+
}
|
|
212
|
+
throw new TypeError(`Could not unwrap ${obj} (${typeof obj})`);
|
|
189
213
|
}
|
|
190
214
|
/**
|
|
191
215
|
* Wraps an iterable object (e.g. an array of wrapped or DOM elements).
|
|
@@ -247,13 +271,16 @@ var JJN = class _JJN {
|
|
|
247
271
|
/**
|
|
248
272
|
* Appends children to this node.
|
|
249
273
|
*
|
|
274
|
+
* @remarks
|
|
275
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
276
|
+
*
|
|
250
277
|
* @param children - The children to append (Nodes, strings, or Wrappers).
|
|
251
278
|
* @returns This instance for chaining.
|
|
252
279
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/append | Element.append}
|
|
253
280
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild | Node.appendChild}
|
|
254
281
|
*/
|
|
255
282
|
append(...children) {
|
|
256
|
-
const nodes = _JJN.unwrapAll(children);
|
|
283
|
+
const nodes = _JJN.unwrapAll(children.filter(_JJN.isWrapable));
|
|
257
284
|
const ref = this.ref;
|
|
258
285
|
for (const node of nodes) {
|
|
259
286
|
if (node) {
|
|
@@ -270,6 +297,9 @@ var JJN = class _JJN {
|
|
|
270
297
|
* list.mapAppend(['a', 'b'], item => h('li', null, item))
|
|
271
298
|
* ```
|
|
272
299
|
*
|
|
300
|
+
* @remarks
|
|
301
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
302
|
+
*
|
|
273
303
|
* @param array - The source array.
|
|
274
304
|
* @param mapFn - The mapping function returning a Wrappable.
|
|
275
305
|
* @returns This instance for chaining.
|
|
@@ -280,12 +310,15 @@ var JJN = class _JJN {
|
|
|
280
310
|
/**
|
|
281
311
|
* Prepends children to this node.
|
|
282
312
|
*
|
|
313
|
+
* @remarks
|
|
314
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
315
|
+
*
|
|
283
316
|
* @param children - The children to prepend.
|
|
284
317
|
* @returns This instance for chaining.
|
|
285
318
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend | Element.prepend}
|
|
286
319
|
*/
|
|
287
320
|
prepend(...children) {
|
|
288
|
-
const nodes = _JJN.unwrapAll(children);
|
|
321
|
+
const nodes = _JJN.unwrapAll(children.filter(_JJN.isWrapable));
|
|
289
322
|
const ref = this.ref;
|
|
290
323
|
const first = ref.firstChild;
|
|
291
324
|
for (const node of nodes) {
|
|
@@ -303,6 +336,9 @@ var JJN = class _JJN {
|
|
|
303
336
|
* list.mapPrepend(['a', 'b'], item => JJHE.fromTag('li').setText(item))
|
|
304
337
|
* ```
|
|
305
338
|
*
|
|
339
|
+
* @remarks
|
|
340
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
341
|
+
*
|
|
306
342
|
* @param array - The source array.
|
|
307
343
|
* @param mapFn - The mapping function.
|
|
308
344
|
* @returns This instance for chaining.
|
|
@@ -315,13 +351,14 @@ var JJN = class _JJN {
|
|
|
315
351
|
*
|
|
316
352
|
* @remarks
|
|
317
353
|
* If no children are specified, it essentially empties the node
|
|
354
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
318
355
|
*
|
|
319
356
|
* @param children - The new children to set.
|
|
320
357
|
* @returns This instance for chaining.
|
|
321
358
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren | Element.replaceChildren}
|
|
322
359
|
*/
|
|
323
|
-
|
|
324
|
-
return this.empty().append(...children);
|
|
360
|
+
setChildren(...children) {
|
|
361
|
+
return this.empty().append(...children.filter(_JJN.isWrapable));
|
|
325
362
|
}
|
|
326
363
|
/**
|
|
327
364
|
* Adds an event listener.
|
|
@@ -381,7 +418,7 @@ var JJN = class _JJN {
|
|
|
381
418
|
* })
|
|
382
419
|
* ```
|
|
383
420
|
* @remarks
|
|
384
|
-
* If you want to access the current JJ* instance, you SHOULD use a `function` not an arrow function.
|
|
421
|
+
* If you want to access the current JJ* instance using `this` keyword, you SHOULD use a `function` not an arrow function.
|
|
385
422
|
* If the function throws, `run()` doesn't swallow the exception.
|
|
386
423
|
* So if you're expecting an error, make sure to wrap it in a `try..catch` block and handle the exception.
|
|
387
424
|
* If the function returns a promise, you can `await` on the response.
|
|
@@ -1418,7 +1455,7 @@ var JJD = class _JJD extends JJN {
|
|
|
1418
1455
|
};
|
|
1419
1456
|
|
|
1420
1457
|
// src/mixins.ts
|
|
1421
|
-
var { wrapAll, unwrapAll } = JJN;
|
|
1458
|
+
var { wrapAll, unwrap, unwrapAll, isWrapable } = JJN;
|
|
1422
1459
|
function wrap(raw) {
|
|
1423
1460
|
if (isStr(raw)) {
|
|
1424
1461
|
return JJT.from(document.createTextNode(raw));
|
|
@@ -1455,21 +1492,6 @@ function wrap(raw) {
|
|
|
1455
1492
|
}
|
|
1456
1493
|
throw new TypeError(`Expected a Node to wrap. Got ${raw} (${typeof raw})`);
|
|
1457
1494
|
}
|
|
1458
|
-
function unwrap(obj) {
|
|
1459
|
-
if (isStr(obj)) {
|
|
1460
|
-
return document.createTextNode(obj);
|
|
1461
|
-
}
|
|
1462
|
-
if (!isObj(obj)) {
|
|
1463
|
-
throw new TypeError(`Expected an object. Got ${obj} (${typeof obj})`);
|
|
1464
|
-
}
|
|
1465
|
-
if (isA(obj, Node)) {
|
|
1466
|
-
return obj;
|
|
1467
|
-
}
|
|
1468
|
-
if (isA(obj, JJN)) {
|
|
1469
|
-
return obj.ref;
|
|
1470
|
-
}
|
|
1471
|
-
throw new TypeError(`Could not unwrap ${obj} (${typeof obj})`);
|
|
1472
|
-
}
|
|
1473
1495
|
function byId(id, throwIfNotFound = true) {
|
|
1474
1496
|
const el = document.getElementById(id);
|
|
1475
1497
|
if (el) {
|
|
@@ -1577,6 +1599,9 @@ var EDDF = {
|
|
|
1577
1599
|
* myDiv.append(h('span', null, 'hello'))
|
|
1578
1600
|
* ```
|
|
1579
1601
|
*
|
|
1602
|
+
* @remarks
|
|
1603
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
1604
|
+
*
|
|
1580
1605
|
* @param this - The JJE, JJD or JJDF instance.
|
|
1581
1606
|
* @param children - The children to append.
|
|
1582
1607
|
* @returns This instance for chaining.
|
|
@@ -1585,7 +1610,7 @@ var EDDF = {
|
|
|
1585
1610
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment/append | DocumentFragment.append}
|
|
1586
1611
|
*/
|
|
1587
1612
|
append(...children) {
|
|
1588
|
-
const nodes = unwrapAll(children);
|
|
1613
|
+
const nodes = unwrapAll(children.filter(isWrapable));
|
|
1589
1614
|
this.ref.append(...nodes);
|
|
1590
1615
|
return this;
|
|
1591
1616
|
},
|
|
@@ -1597,6 +1622,9 @@ var EDDF = {
|
|
|
1597
1622
|
* div.prepend(h('span', null, 'first'))
|
|
1598
1623
|
* ```
|
|
1599
1624
|
*
|
|
1625
|
+
* @remarks
|
|
1626
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
1627
|
+
*
|
|
1600
1628
|
* @param this - The JJE, JJD or JJDF instance.
|
|
1601
1629
|
* @param children - The children to prepend.
|
|
1602
1630
|
* @returns This instance for chaining.
|
|
@@ -1605,7 +1633,7 @@ var EDDF = {
|
|
|
1605
1633
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment/prepend | DocumentFragment.prepend}
|
|
1606
1634
|
*/
|
|
1607
1635
|
prepend(...children) {
|
|
1608
|
-
const nodes = unwrapAll(children);
|
|
1636
|
+
const nodes = unwrapAll(children.filter(isWrapable));
|
|
1609
1637
|
this.ref.prepend(...nodes);
|
|
1610
1638
|
return this;
|
|
1611
1639
|
},
|
|
@@ -1617,9 +1645,12 @@ var EDDF = {
|
|
|
1617
1645
|
*
|
|
1618
1646
|
* @example
|
|
1619
1647
|
* ```ts
|
|
1620
|
-
* div.
|
|
1648
|
+
* div.setChildren(h('p', null, 'New Content'))
|
|
1621
1649
|
* ```
|
|
1622
1650
|
*
|
|
1651
|
+
* @remarks
|
|
1652
|
+
* To make template codes easier, this function ignores any child that is not possible to `wrap()` (e.g. undefined, null, false).
|
|
1653
|
+
*
|
|
1623
1654
|
* @param this - The JJE, JJD or JJDF instance.
|
|
1624
1655
|
* @param children - The children to replace with.
|
|
1625
1656
|
* @returns This instance for chaining.
|
|
@@ -1627,8 +1658,8 @@ var EDDF = {
|
|
|
1627
1658
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/replaceChildren | Document.replaceChildren}
|
|
1628
1659
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment/replaceChildren | DocumentFragment.replaceChildren}
|
|
1629
1660
|
*/
|
|
1630
|
-
|
|
1631
|
-
const nodes = unwrapAll(children);
|
|
1661
|
+
setChildren(...children) {
|
|
1662
|
+
const nodes = unwrapAll(children.filter(isWrapable));
|
|
1632
1663
|
this.ref.replaceChildren(...nodes);
|
|
1633
1664
|
return this;
|
|
1634
1665
|
},
|
|
@@ -1641,10 +1672,10 @@ var EDDF = {
|
|
|
1641
1672
|
* ```
|
|
1642
1673
|
*
|
|
1643
1674
|
* @returns This instance for chaining.
|
|
1644
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren | Element.
|
|
1675
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren | Element.setChildren}
|
|
1645
1676
|
*/
|
|
1646
1677
|
empty() {
|
|
1647
|
-
this.
|
|
1678
|
+
this.setChildren();
|
|
1648
1679
|
return this;
|
|
1649
1680
|
}
|
|
1650
1681
|
};
|
|
@@ -1676,7 +1707,6 @@ function assignPrototype(Class, ...mixins) {
|
|
|
1676
1707
|
}
|
|
1677
1708
|
}
|
|
1678
1709
|
JJN.wrap = wrap;
|
|
1679
|
-
JJN.unwrap = unwrap;
|
|
1680
1710
|
assignPrototype(JJE, EDDF);
|
|
1681
1711
|
assignPrototype(JJD, DDF, EDDF);
|
|
1682
1712
|
assignPrototype(JJDF, DDF, EDDF);
|
|
@@ -1914,6 +1944,7 @@ export {
|
|
|
1914
1944
|
fetchText,
|
|
1915
1945
|
fileExt,
|
|
1916
1946
|
h,
|
|
1947
|
+
isWrapable,
|
|
1917
1948
|
keb2cam,
|
|
1918
1949
|
keb2pas,
|
|
1919
1950
|
nextAnimationFrame,
|