-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (83 loc) · 3.41 KB
/
index.js
1
2
3
4
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
This code is classfree object oriented. No this. No prototype. No class.
Inspired by Douglas Crockford (see https://youtu.be/XFTOG895C7c?t=2562)
*/
import {
isHtmlString,
isArrayOfHtmlStrings,
isArrayOfHtmlElements,
inject2DOMTree,
ElemArray2HtmlString,
input2Collection,
setCollectionFromCssSelector,
truncateHtmlStr,
proxify,
addJQLStaticMethods,
createElementFromHtmlString,
insertPositions,
systemLog,
IS,
} from "./src/JQLExtensionHelpers.js";
export default addJQLStaticMethods(JQLFactory());
function JQLFactory() {
const logLineLength = 70;
return function JQLDefault(input, root, position = insertPositions.BeforeEnd) {
const isVirtual = IS(root, HTMLBRElement);
root = isVirtual && document.body || (root && root?.isJQL ? root[0] : root) || document.body;
position = position && Object.values(insertPositions).find(pos => position === pos) ? position : undefined;
const isRawHtml = isHtmlString(input);
const isRawHtmlArray = !isRawHtml && isArrayOfHtmlStrings(input);
const shouldCreateElements = isRawHtmlArray || isRawHtml;
let instance = {
collection: input2Collection(input) ?? [],
isVirtual,
isJQL: true,
};
const isRawElemCollection = isArrayOfHtmlElements(instance.collection);
if (location.host.startsWith(`dev`)) {
instance.params = { virtual: instance.isVirtual, jql: instance.isJQL, isRawHtml, isRawHtmlArray, isRawElemCollection };
}
const logStr = `input => ${
isRawHtmlArray
? `"${truncateHtmlStr(input.join(`, `), logLineLength)}"`
: !shouldCreateElements && isRawElemCollection ? `element collection [${
truncateHtmlStr( instance.collection.map(el => `${
IS(el, Comment, Text) ? `Comment|Text @` : ``} ${
el.outerHTML || el.textContent}`).join(`, `), logLineLength)}]`
: `"${truncateHtmlStr(input, logLineLength)}"`}`;
if (instance.collection.length && isRawElemCollection) {
systemLog(logStr);
if (!isVirtual) {
instance.collection.forEach(el => {
if (!root.contains(el)) {
inject2DOMTree([el], root, position);
}
});
}
return proxify(instance);
}
if (shouldCreateElements) {
[input].flat().forEach(htmlStringOrComment =>
instance.collection.push(createElementFromHtmlString(htmlStringOrComment)));
if (instance.collection.length > 0) {
const errors = instance.collection.filter( el => el?.dataset?.jqlcreationerror );
instance.collection = instance.collection.filter(el => !el?.dataset?.jqlcreationerror);
systemLog(`${logStr}`);
systemLog(`*Created ${instance.isVirtual ? `VIRTUAL ` : ``}[${
truncateHtmlStr(ElemArray2HtmlString(instance.collection) ||
"sanitized: no elements remaining", logLineLength)}]`);
if (errors.length) {
console.error(`JQL: illegal html, not rendered: "${
errors.reduce( (acc, el) => acc.concat(`${el.textContent}\n`), ``).trim()}"` );
}
if (!instance.isVirtual) {
inject2DOMTree(instance.collection, root, position);
}
}
return proxify(instance);
}
const forLog = setCollectionFromCssSelector(input, root, instance);
systemLog(`input => ${forLog}`);
return proxify(instance);
}
}