@design-edito/tools 0.1.22 → 0.1.24
Sign up to get free protection for your applications and to get access to all the features.
- package/agnostic/html/hyper-json/index.js +12 -12
- package/agnostic/html/hyper-json/transformers/index.js +11 -11
- package/agnostic/html/hyper-json/transformers/transformSelected/index.js +11 -11
- package/agnostic/html/hyper-json/tree/index.d.ts +3 -1
- package/agnostic/html/hyper-json/tree/index.js +11 -11
- package/agnostic/html/index.js +20 -20
- package/agnostic/misc/index.js +5 -5
- package/agnostic/misc/lorem-ipsum/index.js +2 -2
- package/agnostic/numbers/index.js +5 -5
- package/agnostic/objects/index.js +3 -3
- package/agnostic/strings/index.js +7 -7
- package/agnostic/time/index.js +3 -3
- package/chunks/chunk-6R54C426.js +35 -0
- package/chunks/chunk-APWPG7BU.js +647 -0
- package/chunks/chunk-HENMQK5Y.js +647 -0
- package/chunks/chunk-HFEOHBYG.js +647 -0
- package/chunks/chunk-HGECIEKD.js +35 -0
- package/chunks/chunk-JAST3E5G.js +647 -0
- package/chunks/chunk-L7SL3MND.js +35 -0
- package/chunks/chunk-OWRQ3QBI.js +211 -0
- package/chunks/chunk-RHYLRLKT.js +211 -0
- package/chunks/chunk-TRB6DPKJ.js +35 -0
- package/node/files/index.js +3 -3
- package/package.json +9 -8
@@ -0,0 +1,647 @@
|
|
1
|
+
import {
|
2
|
+
ref
|
3
|
+
} from "./chunk-3X6RXJBR.js";
|
4
|
+
import {
|
5
|
+
toRecord
|
6
|
+
} from "./chunk-F5YMP5WU.js";
|
7
|
+
import {
|
8
|
+
toString
|
9
|
+
} from "./chunk-DMDOCC72.js";
|
10
|
+
import {
|
11
|
+
toRef
|
12
|
+
} from "./chunk-4RDW62CU.js";
|
13
|
+
import {
|
14
|
+
toText
|
15
|
+
} from "./chunk-3PHPA25U.js";
|
16
|
+
import {
|
17
|
+
trim
|
18
|
+
} from "./chunk-7IGZP4TV.js";
|
19
|
+
import {
|
20
|
+
toTransformer
|
21
|
+
} from "./chunk-SMLKMOAX.js";
|
22
|
+
import {
|
23
|
+
replace
|
24
|
+
} from "./chunk-MDEDNPU5.js";
|
25
|
+
import {
|
26
|
+
split
|
27
|
+
} from "./chunk-6CM2SWSX.js";
|
28
|
+
import {
|
29
|
+
toArray
|
30
|
+
} from "./chunk-Q52UJOBC.js";
|
31
|
+
import {
|
32
|
+
toBoolean
|
33
|
+
} from "./chunk-SH3Y2I5P.js";
|
34
|
+
import {
|
35
|
+
toNodeList
|
36
|
+
} from "./chunk-DBAJ66BR.js";
|
37
|
+
import {
|
38
|
+
toElement
|
39
|
+
} from "./chunk-S5XDYLJH.js";
|
40
|
+
import {
|
41
|
+
toNull
|
42
|
+
} from "./chunk-RJ5E22H5.js";
|
43
|
+
import {
|
44
|
+
toNumber
|
45
|
+
} from "./chunk-F7XWUG2V.js";
|
46
|
+
import {
|
47
|
+
append
|
48
|
+
} from "./chunk-K3OQEJI3.js";
|
49
|
+
import {
|
50
|
+
join
|
51
|
+
} from "./chunk-RWUPHV54.js";
|
52
|
+
import {
|
53
|
+
length
|
54
|
+
} from "./chunk-S42P73SC.js";
|
55
|
+
import {
|
56
|
+
print
|
57
|
+
} from "./chunk-2DMWYLFG.js";
|
58
|
+
import {
|
59
|
+
push
|
60
|
+
} from "./chunk-FWHUASVA.js";
|
61
|
+
import {
|
62
|
+
classList
|
63
|
+
} from "./chunk-VRUJR6WR.js";
|
64
|
+
import {
|
65
|
+
querySelector
|
66
|
+
} from "./chunk-BNB7LEMQ.js";
|
67
|
+
import {
|
68
|
+
Cast
|
69
|
+
} from "./chunk-QO64L4KB.js";
|
70
|
+
import {
|
71
|
+
Serialize
|
72
|
+
} from "./chunk-DRNK3RLE.js";
|
73
|
+
import {
|
74
|
+
Crossenv
|
75
|
+
} from "./chunk-3LVR6KZH.js";
|
76
|
+
import {
|
77
|
+
Types
|
78
|
+
} from "./chunk-7QMAYGNV.js";
|
79
|
+
import {
|
80
|
+
Utils
|
81
|
+
} from "./chunk-YEBOL67V.js";
|
82
|
+
import {
|
83
|
+
replaceInElement
|
84
|
+
} from "./chunk-GBMHGF7P.js";
|
85
|
+
import {
|
86
|
+
isRecord
|
87
|
+
} from "./chunk-YDIBNEGA.js";
|
88
|
+
import {
|
89
|
+
isInEnum
|
90
|
+
} from "./chunk-E6MSDKON.js";
|
91
|
+
|
92
|
+
// src/agnostic/html/hyper-json/transformers/transformSelected/index.ts
|
93
|
+
var transformSelected = (callerTagName, ...args) => {
|
94
|
+
return Utils.toNamedTransformer(callerTagName, (currentValue, callerTree) => {
|
95
|
+
const { NodeList, Element, Text } = Crossenv.getWindow();
|
96
|
+
if (!(currentValue instanceof Element) && !(currentValue instanceof NodeList)) return Utils.makeTransformerError({
|
97
|
+
message: "Current value must be Element or NodeList",
|
98
|
+
input: currentValue
|
99
|
+
});
|
100
|
+
const [selector, ...transformerDescriptors] = args;
|
101
|
+
if (typeof selector !== "string" && !(selector instanceof Text)) return Utils.makeTransformerError({
|
102
|
+
message: "Selector argument must be string or text",
|
103
|
+
input: selector !== void 0 ? selector : "<undefined>"
|
104
|
+
});
|
105
|
+
if (transformerDescriptors.length === 0) return Utils.makeTransformerError("No transformer descriptor provided");
|
106
|
+
let transformers = [];
|
107
|
+
for (const descriptor of transformerDescriptors) {
|
108
|
+
const errorResponse = Utils.makeTransformerError({
|
109
|
+
message: "Transformer descriptors must be of shape { name: string, arguments: Value[] }",
|
110
|
+
input: descriptor
|
111
|
+
});
|
112
|
+
if (!isRecord(descriptor)) return errorResponse;
|
113
|
+
if (!("name" in descriptor) || typeof descriptor.name !== "string") return errorResponse;
|
114
|
+
if (!("arguments" in descriptor) || !Array.isArray(descriptor.arguments)) return errorResponse;
|
115
|
+
const { name, arguments: args2 } = descriptor;
|
116
|
+
const generator = callerTree.getGenerator(name);
|
117
|
+
if (generator === void 0) return Utils.makeTransformerError(`No transformer found under the name ${name}`);
|
118
|
+
transformers.push(generator(name, ...args2));
|
119
|
+
}
|
120
|
+
const elementInput = Cast.toElement(currentValue);
|
121
|
+
const strSelector = Cast.toString(selector);
|
122
|
+
const selected = Array.from(elementInput.querySelectorAll(strSelector));
|
123
|
+
const transformationErrors = [];
|
124
|
+
const transformed = selected.map((input) => {
|
125
|
+
const output = transformers.reduce((reduced, transformer, transformerPos) => {
|
126
|
+
const result = transformer(reduced, callerTree);
|
127
|
+
if (result.action === "REPLACE") return result.value;
|
128
|
+
else if (result.action === "MERGE") {
|
129
|
+
const merged = Tree.mergeValues(
|
130
|
+
reduced,
|
131
|
+
result.value,
|
132
|
+
transformerPos,
|
133
|
+
callerTree
|
134
|
+
);
|
135
|
+
return merged;
|
136
|
+
}
|
137
|
+
if (result.action === "ERROR") transformationErrors.push({
|
138
|
+
message: result.value,
|
139
|
+
transformerName: transformer.transformerName,
|
140
|
+
transformerPos,
|
141
|
+
transforming: reduced
|
142
|
+
});
|
143
|
+
return reduced;
|
144
|
+
}, input);
|
145
|
+
return { input, output };
|
146
|
+
});
|
147
|
+
if (transformationErrors.length !== 0) return Utils.makeTransformerError({
|
148
|
+
message: "Some nested transformers returned an error",
|
149
|
+
errors: transformationErrors
|
150
|
+
});
|
151
|
+
const nonElementTransformResult = transformed.find((item) => !(item.output instanceof Element));
|
152
|
+
if (nonElementTransformResult !== void 0) return Utils.makeTransformerError({
|
153
|
+
message: "Some transformations resulted in a non Element output",
|
154
|
+
inputElement: nonElementTransformResult.input,
|
155
|
+
outputValue: nonElementTransformResult.output
|
156
|
+
});
|
157
|
+
const transformMap = new Map(transformed.map((e) => [e.input, e.output]));
|
158
|
+
const elementOutput = replaceInElement(elementInput, transformMap);
|
159
|
+
return {
|
160
|
+
action: "REPLACE",
|
161
|
+
value: elementOutput
|
162
|
+
};
|
163
|
+
});
|
164
|
+
};
|
165
|
+
|
166
|
+
// src/agnostic/html/hyper-json/transformers/index.ts
|
167
|
+
var Transformers;
|
168
|
+
((Transformers2) => {
|
169
|
+
Transformers2.defaultGeneratorsMap = /* @__PURE__ */ new Map([
|
170
|
+
["append", append],
|
171
|
+
["classList", classList],
|
172
|
+
["join", join],
|
173
|
+
["length", length],
|
174
|
+
["print", print],
|
175
|
+
["push", push],
|
176
|
+
["querySelector", querySelector],
|
177
|
+
["ref", ref],
|
178
|
+
["replace", replace],
|
179
|
+
["split", split],
|
180
|
+
["toArray", toArray],
|
181
|
+
["toBoolean", toBoolean],
|
182
|
+
["toElement", toElement],
|
183
|
+
["toNodeList", toNodeList],
|
184
|
+
["toNull", toNull],
|
185
|
+
["toNumber", toNumber],
|
186
|
+
["toRecord", toRecord],
|
187
|
+
["toRef", toRef],
|
188
|
+
["toString", toString],
|
189
|
+
["toText", toText],
|
190
|
+
["toTransformer", toTransformer],
|
191
|
+
["transformSelected", transformSelected],
|
192
|
+
["trim", trim]
|
193
|
+
]);
|
194
|
+
})(Transformers || (Transformers = {}));
|
195
|
+
|
196
|
+
// src/agnostic/html/hyper-json/tree/index.ts
|
197
|
+
var getWindow = Crossenv.getWindow;
|
198
|
+
var isElement = (node) => node.nodeType === getWindow().Node.ELEMENT_NODE;
|
199
|
+
var isText = (node) => node.nodeType === getWindow().Node.TEXT_NODE;
|
200
|
+
var Tree;
|
201
|
+
((_Tree) => {
|
202
|
+
_Tree.defaultKeyAttribute = "_key";
|
203
|
+
_Tree.defaultActionAttribute = "_action";
|
204
|
+
_Tree.defaultRootKey = "_ROOT_";
|
205
|
+
function mergeValues(currentValue, incomingValue, mergeKey, initiatorTree) {
|
206
|
+
const { Element, Text, NodeList, document } = getWindow();
|
207
|
+
if (typeof incomingValue === "function") {
|
208
|
+
const evaluated = incomingValue(currentValue, initiatorTree);
|
209
|
+
if (evaluated.action === null) return currentValue;
|
210
|
+
if (evaluated.action === "REPLACE") return evaluated.value;
|
211
|
+
if (evaluated.action === "ERROR") {
|
212
|
+
const errorMessage = `Tranformer error:
|
213
|
+
from: ${incomingValue.transformerName}
|
214
|
+
at: ${initiatorTree.pathString.slice(1)}/${mergeKey}
|
215
|
+
message:`;
|
216
|
+
console.warn(errorMessage, evaluated.value);
|
217
|
+
return currentValue;
|
218
|
+
}
|
219
|
+
return mergeValues(
|
220
|
+
currentValue,
|
221
|
+
evaluated.value,
|
222
|
+
mergeKey,
|
223
|
+
initiatorTree
|
224
|
+
);
|
225
|
+
}
|
226
|
+
if (Array.isArray(currentValue)) {
|
227
|
+
if (typeof mergeKey === "string") return currentValue;
|
228
|
+
return [...currentValue, incomingValue];
|
229
|
+
}
|
230
|
+
if (currentValue === null || typeof currentValue === "boolean" || typeof currentValue === "number" || typeof currentValue === "string" || typeof currentValue === "function" || currentValue instanceof Text || currentValue instanceof Element) {
|
231
|
+
if (incomingValue === null || typeof incomingValue === "boolean" || typeof incomingValue === "number" || typeof incomingValue === "string" || Array.isArray(incomingValue) || isRecord(incomingValue)) {
|
232
|
+
return incomingValue;
|
233
|
+
}
|
234
|
+
const frag = document.createDocumentFragment();
|
235
|
+
if (currentValue instanceof Element || currentValue instanceof Text) frag.append(currentValue.cloneNode(true));
|
236
|
+
else frag.append(`${currentValue}`);
|
237
|
+
if (incomingValue instanceof Element || incomingValue instanceof Text) {
|
238
|
+
frag.append(incomingValue.cloneNode(true));
|
239
|
+
return frag.childNodes;
|
240
|
+
}
|
241
|
+
frag.append(...Array.from(incomingValue).map((e) => e.cloneNode(true)));
|
242
|
+
return frag.childNodes;
|
243
|
+
}
|
244
|
+
if (currentValue instanceof NodeList) {
|
245
|
+
if (incomingValue instanceof Element || incomingValue instanceof Text) {
|
246
|
+
const frag = document.createDocumentFragment();
|
247
|
+
frag.append(...Array.from(currentValue).map((e) => e.cloneNode(true)), incomingValue);
|
248
|
+
return frag.childNodes;
|
249
|
+
}
|
250
|
+
if (incomingValue instanceof NodeList) {
|
251
|
+
const frag = document.createDocumentFragment();
|
252
|
+
frag.append(
|
253
|
+
...Array.from(currentValue).map((e) => e.cloneNode(true)),
|
254
|
+
...Array.from(incomingValue).map((e) => e.cloneNode(true))
|
255
|
+
);
|
256
|
+
return frag.childNodes;
|
257
|
+
}
|
258
|
+
if (incomingValue === null || typeof incomingValue === "string" || typeof incomingValue === "number" || typeof incomingValue === "boolean") {
|
259
|
+
const frag = document.createDocumentFragment();
|
260
|
+
frag.append(...Array.from(currentValue).map((e) => e.cloneNode(true)), `${incomingValue}`);
|
261
|
+
return frag.childNodes;
|
262
|
+
}
|
263
|
+
if (Array.isArray(incomingValue)) return [
|
264
|
+
...Array.from(currentValue),
|
265
|
+
...incomingValue
|
266
|
+
];
|
267
|
+
return { ...incomingValue };
|
268
|
+
}
|
269
|
+
if (typeof mergeKey === "number") return currentValue;
|
270
|
+
return {
|
271
|
+
...currentValue,
|
272
|
+
[mergeKey]: incomingValue
|
273
|
+
};
|
274
|
+
}
|
275
|
+
_Tree.mergeValues = mergeValues;
|
276
|
+
function mergeNodes(nodes, options = {}) {
|
277
|
+
const [first, ...rest] = nodes;
|
278
|
+
const actionAttribute = options?.actionAttribute ?? _Tree.defaultActionAttribute;
|
279
|
+
const keyAttribute = options?.keyAttribute ?? _Tree.defaultKeyAttribute;
|
280
|
+
if (first === void 0) throw new Error("Expecting at least one node");
|
281
|
+
const { Text, Element, document } = getWindow();
|
282
|
+
function isTextOrElement(node) {
|
283
|
+
return node instanceof Text || node instanceof Element;
|
284
|
+
}
|
285
|
+
let CURRENT = first;
|
286
|
+
rest.forEach((node) => {
|
287
|
+
if (node instanceof Text) {
|
288
|
+
CURRENT.remove();
|
289
|
+
CURRENT = node;
|
290
|
+
return;
|
291
|
+
}
|
292
|
+
const actionRaw = node.getAttribute(actionAttribute);
|
293
|
+
const action = isInEnum(Types.ReductionAction, actionRaw) ? actionRaw : Types.ReductionAction.REPLACE;
|
294
|
+
if (action === Types.ReductionAction.REPLACE) {
|
295
|
+
CURRENT.remove();
|
296
|
+
CURRENT = node;
|
297
|
+
return;
|
298
|
+
}
|
299
|
+
if (CURRENT instanceof Text) {
|
300
|
+
if (node instanceof Text) {
|
301
|
+
const appended = action === Types.ReductionAction.APPEND ? document.createTextNode(`${CURRENT.textContent}${node.textContent}`) : document.createTextNode(`${node.textContent}${CURRENT.textContent}`);
|
302
|
+
CURRENT.remove();
|
303
|
+
node.remove();
|
304
|
+
CURRENT = appended;
|
305
|
+
return;
|
306
|
+
}
|
307
|
+
CURRENT.remove();
|
308
|
+
CURRENT = node;
|
309
|
+
return;
|
310
|
+
}
|
311
|
+
if (node instanceof Text) {
|
312
|
+
CURRENT.remove();
|
313
|
+
CURRENT = node;
|
314
|
+
return;
|
315
|
+
}
|
316
|
+
const currentAttributes = Array.from(CURRENT.attributes);
|
317
|
+
const nodeAttributes = Array.from(node.attributes);
|
318
|
+
const nodeChildren = Array.from(node.childNodes).filter(isTextOrElement);
|
319
|
+
const outputAttributes = action === Types.ReductionAction.APPEND ? [...currentAttributes, ...nodeAttributes] : [...nodeAttributes, ...currentAttributes];
|
320
|
+
if (action === Types.ReductionAction.APPEND) CURRENT.append(...nodeChildren);
|
321
|
+
else CURRENT.prepend(...nodeChildren);
|
322
|
+
outputAttributes.forEach((attr) => CURRENT.setAttribute(attr.name, attr.value));
|
323
|
+
node.remove();
|
324
|
+
return;
|
325
|
+
});
|
326
|
+
const wrapperChildren = Array.from(CURRENT.childNodes);
|
327
|
+
const subpaths = /* @__PURE__ */ new Map();
|
328
|
+
let positionnedChildrenCount = 0;
|
329
|
+
wrapperChildren.forEach((child) => {
|
330
|
+
if (child instanceof Text) {
|
331
|
+
const childKey = positionnedChildrenCount;
|
332
|
+
const found = subpaths.get(childKey) ?? [];
|
333
|
+
found.push(child);
|
334
|
+
subpaths.set(childKey, found);
|
335
|
+
positionnedChildrenCount += 1;
|
336
|
+
} else {
|
337
|
+
const rawChildKey = child.getAttribute(keyAttribute);
|
338
|
+
const childKey = rawChildKey ?? positionnedChildrenCount;
|
339
|
+
const found = subpaths.get(childKey) ?? [];
|
340
|
+
found.push(child);
|
341
|
+
subpaths.set(childKey, found);
|
342
|
+
if (rawChildKey === null) {
|
343
|
+
positionnedChildrenCount += 1;
|
344
|
+
}
|
345
|
+
}
|
346
|
+
});
|
347
|
+
subpaths.forEach((nodes2, subpath) => {
|
348
|
+
if (nodes2.length < 2) return;
|
349
|
+
return mergeNodes(nodes2, {
|
350
|
+
actionAttribute,
|
351
|
+
keyAttribute
|
352
|
+
});
|
353
|
+
});
|
354
|
+
return CURRENT;
|
355
|
+
}
|
356
|
+
_Tree.mergeNodes = mergeNodes;
|
357
|
+
function mergeRootNodes(...args) {
|
358
|
+
const [nodes, options] = args;
|
359
|
+
const actionAttribute = options?.actionAttribute ?? _Tree.defaultActionAttribute;
|
360
|
+
const keyAttribute = options?.keyAttribute ?? _Tree.defaultKeyAttribute;
|
361
|
+
const rootKey = options?.rootKey ?? _Tree.defaultRootKey;
|
362
|
+
const { Element } = getWindow();
|
363
|
+
const elements = nodes.filter((e) => e instanceof Element);
|
364
|
+
elements.forEach((element) => {
|
365
|
+
element.setAttribute(keyAttribute, rootKey);
|
366
|
+
const elementAction = element.getAttribute(actionAttribute) ?? Types.ReductionAction.APPEND;
|
367
|
+
element.setAttribute(actionAttribute, elementAction);
|
368
|
+
});
|
369
|
+
const merged = mergeNodes(elements, options);
|
370
|
+
return merged;
|
371
|
+
}
|
372
|
+
_Tree.mergeRootNodes = mergeRootNodes;
|
373
|
+
function from(nodes, options) {
|
374
|
+
const merged = mergeRootNodes(nodes, options);
|
375
|
+
return new Tree2(merged, options);
|
376
|
+
}
|
377
|
+
_Tree.from = from;
|
378
|
+
class Tree2 {
|
379
|
+
node;
|
380
|
+
name;
|
381
|
+
parent;
|
382
|
+
root;
|
383
|
+
isRoot;
|
384
|
+
path;
|
385
|
+
pathString;
|
386
|
+
tagName;
|
387
|
+
attributes;
|
388
|
+
subtrees = /* @__PURE__ */ new Map();
|
389
|
+
children;
|
390
|
+
type;
|
391
|
+
generators;
|
392
|
+
keyAttribute;
|
393
|
+
actionAttribute;
|
394
|
+
static fillOptions(options) {
|
395
|
+
const defaultOptions = {
|
396
|
+
generatorsMap: Transformers.defaultGeneratorsMap,
|
397
|
+
keyAttribute: _Tree.defaultKeyAttribute,
|
398
|
+
actionAttribute: _Tree.defaultActionAttribute
|
399
|
+
};
|
400
|
+
return {
|
401
|
+
...defaultOptions,
|
402
|
+
...options
|
403
|
+
};
|
404
|
+
}
|
405
|
+
constructor(node, parentOrOptions, pathFromParent, options) {
|
406
|
+
this.resolve = this.resolve.bind(this);
|
407
|
+
this.getGenerator = this.getGenerator.bind(this);
|
408
|
+
this.initValue = this.initValue.bind(this);
|
409
|
+
this.getInnerValue = this.getInnerValue.bind(this);
|
410
|
+
this.wrapInnerValue = this.wrapInnerValue.bind(this);
|
411
|
+
this.setCache = this.setCache.bind(this);
|
412
|
+
this.getPerfCounters = this.getPerfCounters.bind(this);
|
413
|
+
this.printPerfCounters = this.printPerfCounters.bind(this);
|
414
|
+
this.pushToEvalCallStack = this.pushToEvalCallStack.bind(this);
|
415
|
+
this.flushEvalCallStack = this.flushEvalCallStack.bind(this);
|
416
|
+
this.evaluate = this.evaluate.bind(this);
|
417
|
+
const { Element, Text } = getWindow();
|
418
|
+
const _node = node;
|
419
|
+
const _parent = parentOrOptions instanceof Tree2 ? parentOrOptions : void 0;
|
420
|
+
this.node = _node;
|
421
|
+
this.parent = _parent ?? null;
|
422
|
+
this.root = this.parent === null ? this : this.parent.root;
|
423
|
+
this.isRoot = this.root === this;
|
424
|
+
const _options = options ?? parentOrOptions instanceof Tree2 ? Tree2.fillOptions({}) : Tree2.fillOptions(parentOrOptions ?? {});
|
425
|
+
this.keyAttribute = _options.keyAttribute;
|
426
|
+
this.actionAttribute = _options.actionAttribute;
|
427
|
+
this.generators = this.isRoot ? _options.generatorsMap : this.root.generators;
|
428
|
+
this.name = this.node instanceof Element ? this.node.getAttribute(this.keyAttribute) : null;
|
429
|
+
const _pathFromParent = pathFromParent !== void 0 ? pathFromParent : void 0;
|
430
|
+
if (this.parent === null) this.path = [];
|
431
|
+
else if (_pathFromParent === void 0) {
|
432
|
+
this.path = [...this.parent.path, 0];
|
433
|
+
} else {
|
434
|
+
this.path = [...this.parent.path, _pathFromParent];
|
435
|
+
}
|
436
|
+
this.pathString = `/${this.path.join("/")}`;
|
437
|
+
this.tagName = node instanceof Element ? node.tagName.toLowerCase() : null;
|
438
|
+
this.attributes = isElement(node) ? Array.from(node.attributes) : null;
|
439
|
+
const { childNodes } = node;
|
440
|
+
let positionnedChildrenCount = 0;
|
441
|
+
const mutableSubtrees = /* @__PURE__ */ new Map();
|
442
|
+
Array.from(childNodes).filter((node2, _, nodes) => {
|
443
|
+
if (isElement(node2)) return true;
|
444
|
+
if (isText(node2)) {
|
445
|
+
const hasContent = (node2.textContent ?? "").trim() !== "";
|
446
|
+
if (hasContent) return true;
|
447
|
+
if (nodes.some((n) => n instanceof Element)) return false;
|
448
|
+
return true;
|
449
|
+
}
|
450
|
+
return false;
|
451
|
+
}).forEach((childNode) => {
|
452
|
+
if (childNode instanceof Text) {
|
453
|
+
mutableSubtrees.set(
|
454
|
+
positionnedChildrenCount,
|
455
|
+
new Tree2(childNode, this, positionnedChildrenCount)
|
456
|
+
);
|
457
|
+
positionnedChildrenCount += 1;
|
458
|
+
} else {
|
459
|
+
const propertyName = childNode.getAttribute(this.keyAttribute);
|
460
|
+
if (propertyName === null) {
|
461
|
+
mutableSubtrees.set(
|
462
|
+
positionnedChildrenCount,
|
463
|
+
new Tree2(childNode, this, positionnedChildrenCount)
|
464
|
+
);
|
465
|
+
positionnedChildrenCount += 1;
|
466
|
+
} else {
|
467
|
+
mutableSubtrees.set(
|
468
|
+
propertyName,
|
469
|
+
new Tree2(childNode, this, propertyName)
|
470
|
+
);
|
471
|
+
}
|
472
|
+
}
|
473
|
+
});
|
474
|
+
this.subtrees = mutableSubtrees;
|
475
|
+
this.children = Array.from(this.subtrees.values());
|
476
|
+
if (this.tagName === null) {
|
477
|
+
this.type = "text";
|
478
|
+
} else if (this.tagName === Types.TyperTagName.NULL) {
|
479
|
+
this.type = "null";
|
480
|
+
} else if (this.tagName === Types.TyperTagName.BOOLEAN) {
|
481
|
+
this.type = "boolean";
|
482
|
+
} else if (this.tagName === Types.TyperTagName.NUMBER) {
|
483
|
+
this.type = "number";
|
484
|
+
} else if (this.tagName === Types.TyperTagName.STRING) {
|
485
|
+
this.type = "string";
|
486
|
+
} else if (this.tagName === Types.TyperTagName.TEXT) {
|
487
|
+
this.type = "text";
|
488
|
+
} else if (this.tagName === Types.TyperTagName.NODELIST) {
|
489
|
+
this.type = "nodelist";
|
490
|
+
} else if (this.tagName === Types.TyperTagName.ARRAY) {
|
491
|
+
this.type = "array";
|
492
|
+
} else if (this.tagName === Types.TyperTagName.RECORD) {
|
493
|
+
this.type = "record";
|
494
|
+
} else if (this.generators.get(this.tagName) !== void 0) {
|
495
|
+
this.type = "transformer";
|
496
|
+
} else {
|
497
|
+
this.type = "element";
|
498
|
+
}
|
499
|
+
}
|
500
|
+
resolve = function(path) {
|
501
|
+
let currentTree = this.root;
|
502
|
+
for (const chunk of path) {
|
503
|
+
const { subtrees } = currentTree;
|
504
|
+
const foundSubtree = subtrees.get(chunk);
|
505
|
+
if (foundSubtree === void 0) return void 0;
|
506
|
+
currentTree = foundSubtree;
|
507
|
+
}
|
508
|
+
return currentTree;
|
509
|
+
};
|
510
|
+
getGenerator(name) {
|
511
|
+
return this.generators.get(name);
|
512
|
+
}
|
513
|
+
initValue() {
|
514
|
+
const { type } = this;
|
515
|
+
let currentValue;
|
516
|
+
if (type === "null") {
|
517
|
+
currentValue = null;
|
518
|
+
} else if (type === "boolean") {
|
519
|
+
currentValue = false;
|
520
|
+
} else if (type === "number") {
|
521
|
+
currentValue = 0;
|
522
|
+
} else if (type === "string") {
|
523
|
+
currentValue = "";
|
524
|
+
} else if (type === "text") {
|
525
|
+
currentValue = this.node.textContent;
|
526
|
+
} else if (type === "element") {
|
527
|
+
currentValue = getWindow().document.createDocumentFragment().childNodes;
|
528
|
+
} else if (type === "nodelist") {
|
529
|
+
currentValue = getWindow().document.createDocumentFragment().childNodes;
|
530
|
+
} else if (type === "array") {
|
531
|
+
currentValue = [];
|
532
|
+
} else if (type === "record") {
|
533
|
+
currentValue = {};
|
534
|
+
} else if (type === "transformer") {
|
535
|
+
currentValue = [];
|
536
|
+
} else {
|
537
|
+
currentValue = null;
|
538
|
+
}
|
539
|
+
return currentValue;
|
540
|
+
}
|
541
|
+
getInnerValue(initialValue) {
|
542
|
+
const { subtrees } = this;
|
543
|
+
const innerValue = Array.from(subtrees.entries()).reduce((currentValue, [subpath, subtree]) => mergeValues(
|
544
|
+
currentValue,
|
545
|
+
subtree.evaluate(),
|
546
|
+
subpath,
|
547
|
+
this
|
548
|
+
), initialValue);
|
549
|
+
return innerValue;
|
550
|
+
}
|
551
|
+
wrapInnerValue(innerValue) {
|
552
|
+
const { type } = this;
|
553
|
+
if (type === "transformer") {
|
554
|
+
const transformerName = this.tagName;
|
555
|
+
if (transformerName === null) return innerValue;
|
556
|
+
const generator = this.getGenerator(transformerName);
|
557
|
+
if (generator === void 0) return innerValue;
|
558
|
+
const transformer = Array.isArray(innerValue) ? generator(transformerName, ...innerValue) : generator(transformerName, innerValue);
|
559
|
+
return transformer;
|
560
|
+
}
|
561
|
+
if (type === "null") return Cast.toNull();
|
562
|
+
if (type === "boolean") return Cast.toBoolean(innerValue);
|
563
|
+
if (type === "number") return Cast.toNumber(innerValue);
|
564
|
+
if (type === "string") return Cast.toString(innerValue);
|
565
|
+
if (type === "array") return Cast.toArray(innerValue);
|
566
|
+
if (type === "record") return Cast.toRecord(innerValue);
|
567
|
+
if (type === "text") return Cast.toText(innerValue);
|
568
|
+
if (type === "element") return Cast.toElement(innerValue);
|
569
|
+
if (type === "nodelist") return Cast.toNodeList(innerValue);
|
570
|
+
return Cast.toNull();
|
571
|
+
}
|
572
|
+
cache = void 0;
|
573
|
+
setCache(value) {
|
574
|
+
this.cache = Serialize.serialize(value);
|
575
|
+
}
|
576
|
+
perfCounters = {
|
577
|
+
computed: 0,
|
578
|
+
computeTime: 0,
|
579
|
+
computeTimeAvg: 0,
|
580
|
+
cached: 0,
|
581
|
+
cacheTime: 0,
|
582
|
+
cacheTimeAvg: 0,
|
583
|
+
totalTime: 0
|
584
|
+
};
|
585
|
+
getPerfCounters() {
|
586
|
+
const { subtrees } = this;
|
587
|
+
const subCounters = [];
|
588
|
+
subCounters.push([this.pathString, this.perfCounters]);
|
589
|
+
subtrees.forEach((subtree) => subCounters.push(...subtree.getPerfCounters()));
|
590
|
+
return subCounters;
|
591
|
+
}
|
592
|
+
printPerfCounters() {
|
593
|
+
const perfCounters = this.getPerfCounters().sort((a, b) => b[1].totalTime - a[1].totalTime).map((e) => ({
|
594
|
+
path: e[0],
|
595
|
+
totalMs: e[1].totalTime,
|
596
|
+
computeMs: e[1].computeTime,
|
597
|
+
cacheMs: e[1].cacheTime,
|
598
|
+
ops: `${e[1].computed}/${e[1].cached}`
|
599
|
+
}));
|
600
|
+
console.table(perfCounters);
|
601
|
+
}
|
602
|
+
callstack = [];
|
603
|
+
pushToEvalCallStack(path) {
|
604
|
+
this.callstack.push(path);
|
605
|
+
this.parent?.pushToEvalCallStack(path);
|
606
|
+
}
|
607
|
+
flushEvalCallStack() {
|
608
|
+
this.callstack.length = 0;
|
609
|
+
}
|
610
|
+
evaluate() {
|
611
|
+
const start = Date.now();
|
612
|
+
const circularPatternDetected = this.callstack.some((p) => p.startsWith(this.pathString));
|
613
|
+
if (circularPatternDetected) throw new Error(`Circular reference pattern detected @ ${this.pathString}`);
|
614
|
+
this.pushToEvalCallStack(this.pathString);
|
615
|
+
const { perfCounters, cache } = this;
|
616
|
+
if (cache !== void 0) {
|
617
|
+
const deserialized = Serialize.deserialize(cache);
|
618
|
+
const end2 = Date.now();
|
619
|
+
const time2 = end2 - start;
|
620
|
+
perfCounters.cached++;
|
621
|
+
perfCounters.cacheTime += time2;
|
622
|
+
perfCounters.cacheTimeAvg = perfCounters.cacheTime / perfCounters.cached;
|
623
|
+
perfCounters.totalTime = perfCounters.computeTime + perfCounters.cacheTime;
|
624
|
+
return deserialized;
|
625
|
+
}
|
626
|
+
const init = this.initValue();
|
627
|
+
const inner = this.getInnerValue(init);
|
628
|
+
const wrapped = this.wrapInnerValue(inner);
|
629
|
+
this.setCache(wrapped);
|
630
|
+
const end = Date.now();
|
631
|
+
const time = end - start;
|
632
|
+
perfCounters.computed++;
|
633
|
+
perfCounters.computeTime += time;
|
634
|
+
perfCounters.computeTimeAvg = perfCounters.computeTime / perfCounters.computed;
|
635
|
+
perfCounters.totalTime = perfCounters.computeTime + perfCounters.cacheTime;
|
636
|
+
this.flushEvalCallStack();
|
637
|
+
return wrapped;
|
638
|
+
}
|
639
|
+
}
|
640
|
+
_Tree.Tree = Tree2;
|
641
|
+
})(Tree || (Tree = {}));
|
642
|
+
|
643
|
+
export {
|
644
|
+
Tree,
|
645
|
+
transformSelected,
|
646
|
+
Transformers
|
647
|
+
};
|