@mgdis/core-ui-helpers 1.2.0 → 1.2.2
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 +12 -0
- package/package.json +8 -8
- package/rollup.config.mjs +3 -3
- package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +1 -1
- package/src/angular/mg-angular-logger/mg-angular-logger.ts +2 -1
- package/src/locale/index.spec.ts +10 -1
- package/src/locale/locale.spec.ts +43 -18
- package/src/locale/locale.ts +13 -5
- package/src/playwright/utils/utils.spec.ts +16 -9
- package/src/playwright/utils/utils.ts +14 -6
- package/src/stencil/ide/cem.spec.ts +4 -4
- package/src/stencil/ide/cem.ts +9 -9
- package/src/stencil/ide/ide.ts +26 -26
- package/src/storybook/stencil/stencil.spec.ts +30 -6
- package/src/storybook/stencil/stencil.ts +24 -13
- package/src/tests/index.spec.ts +7 -1
- package/src/tests/unit/unit.conf.ts +9 -2
- package/src/tests/unit/unit.spec.ts +103 -61
- package/src/tests/unit/unit.ts +38 -11
- package/src/utils/attributes/id/id.spec.ts +9 -6
- package/src/utils/attributes/id/id.ts +3 -2
- package/src/utils/components/components.spec.ts +6 -6
- package/src/utils/components/components.ts +19 -5
- package/src/utils/elements/elements.ts +4 -2
- package/src/utils/number/number.spec.ts +2 -2
- package/src/utils/object/object.spec.ts +2 -2
- package/src/utils/object/object.ts +2 -1
- package/src/utils/string/string.spec.ts +12 -5
- package/src/utils/string/string.ts +2 -1
- package/src/utils/windows/windows.spec.ts +6 -2
- package/src/vue/mg-model/mg-model.ts +7 -2
- package/vitest.config.mts +1 -1
- package/.turbo/turbo-build.log +0 -52
- package/dist/angular/index.cjs +0 -27
- package/dist/angular/index.cjs.map +0 -1
- package/dist/angular/index.d.ts +0 -21
- package/dist/angular/index.js +0 -25
- package/dist/angular/index.js.map +0 -1
- package/dist/angular/index.umd.js +0 -33
- package/dist/angular/index.umd.js.map +0 -1
- package/dist/locale/index.cjs +0 -188
- package/dist/locale/index.cjs.map +0 -1
- package/dist/locale/index.d.ts +0 -110
- package/dist/locale/index.js +0 -179
- package/dist/locale/index.js.map +0 -1
- package/dist/locale/index.umd.js +0 -194
- package/dist/locale/index.umd.js.map +0 -1
- package/dist/playwright/index.cjs +0 -130
- package/dist/playwright/index.cjs.map +0 -1
- package/dist/playwright/index.d.ts +0 -39
- package/dist/playwright/index.js +0 -126
- package/dist/playwright/index.js.map +0 -1
- package/dist/playwright/index.umd.js +0 -136
- package/dist/playwright/index.umd.js.map +0 -1
- package/dist/stencil/index.cjs +0 -363
- package/dist/stencil/index.cjs.map +0 -1
- package/dist/stencil/index.d.ts +0 -222
- package/dist/stencil/index.js +0 -357
- package/dist/stencil/index.js.map +0 -1
- package/dist/stencil/index.umd.js +0 -369
- package/dist/stencil/index.umd.js.map +0 -1
- package/dist/storybook/index.cjs +0 -2333
- package/dist/storybook/index.cjs.map +0 -1
- package/dist/storybook/index.d.ts +0 -85
- package/dist/storybook/index.js +0 -2327
- package/dist/storybook/index.js.map +0 -1
- package/dist/storybook/index.umd.js +0 -2339
- package/dist/storybook/index.umd.js.map +0 -1
- package/dist/tests/index.cjs +0 -300
- package/dist/tests/index.cjs.map +0 -1
- package/dist/tests/index.d.ts +0 -100
- package/dist/tests/index.js +0 -294
- package/dist/tests/index.js.map +0 -1
- package/dist/tests/index.umd.js +0 -306
- package/dist/tests/index.umd.js.map +0 -1
- package/dist/utils/index.cjs +0 -433
- package/dist/utils/index.cjs.map +0 -1
- package/dist/utils/index.d.ts +0 -265
- package/dist/utils/index.js +0 -409
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/index.umd.js +0 -439
- package/dist/utils/index.umd.js.map +0 -1
- package/dist/vue/index.cjs +0 -1934
- package/dist/vue/index.cjs.map +0 -1
- package/dist/vue/index.d.ts +0 -35
- package/dist/vue/index.js +0 -1932
- package/dist/vue/index.js.map +0 -1
- package/dist/vue/index.umd.js +0 -1940
- package/dist/vue/index.umd.js.map +0 -1
package/dist/storybook/index.js
DELETED
|
@@ -1,2327 +0,0 @@
|
|
|
1
|
-
// src/app-data/index.ts
|
|
2
|
-
var BUILD = {
|
|
3
|
-
updatable: true,
|
|
4
|
-
slotRelocation: true};
|
|
5
|
-
|
|
6
|
-
/*
|
|
7
|
-
Stencil Client Platform v4.43.5 | MIT Licensed | https://stenciljs.com
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// src/utils/constants.ts
|
|
12
|
-
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
13
|
-
var HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
14
|
-
|
|
15
|
-
// src/client/client-host-ref.ts
|
|
16
|
-
var getHostRef = (ref) => {
|
|
17
|
-
if (ref.__stencil__getHostRef) {
|
|
18
|
-
return ref.__stencil__getHostRef();
|
|
19
|
-
}
|
|
20
|
-
return void 0;
|
|
21
|
-
};
|
|
22
|
-
var isMemberInElement = (elm, memberName) => memberName in elm;
|
|
23
|
-
var XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
24
|
-
var win = typeof window !== "undefined" ? window : {};
|
|
25
|
-
var plt = {
|
|
26
|
-
$flags$: 0,
|
|
27
|
-
$resourcesUrl$: "",
|
|
28
|
-
jmp: (h2) => h2(),
|
|
29
|
-
raf: (h2) => requestAnimationFrame(h2),
|
|
30
|
-
ael: (el, eventName, listener, opts) => el.addEventListener(eventName, listener, opts),
|
|
31
|
-
rel: (el, eventName, listener, opts) => el.removeEventListener(eventName, listener, opts),
|
|
32
|
-
ce: (eventName, opts) => new CustomEvent(eventName, opts)
|
|
33
|
-
};
|
|
34
|
-
var updateFallbackSlotVisibility = (elm) => {
|
|
35
|
-
const childNodes = internalCall(elm, "childNodes");
|
|
36
|
-
if (elm.tagName && elm.tagName.includes("-") && elm["s-cr"] && elm.tagName !== "SLOT-FB") {
|
|
37
|
-
getHostSlotNodes(childNodes, elm.tagName).forEach((slotNode) => {
|
|
38
|
-
if (slotNode.nodeType === 1 /* ElementNode */ && slotNode.tagName === "SLOT-FB") {
|
|
39
|
-
if (getSlotChildSiblings(slotNode, getSlotName(slotNode), false).length) {
|
|
40
|
-
slotNode.hidden = true;
|
|
41
|
-
} else {
|
|
42
|
-
slotNode.hidden = false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
let i2 = 0;
|
|
48
|
-
for (i2 = 0; i2 < childNodes.length; i2++) {
|
|
49
|
-
const childNode = childNodes[i2];
|
|
50
|
-
if (childNode.nodeType === 1 /* ElementNode */ && internalCall(childNode, "childNodes").length) {
|
|
51
|
-
updateFallbackSlotVisibility(childNode);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
var getSlottedChildNodes = (childNodes) => {
|
|
56
|
-
const result = [];
|
|
57
|
-
for (let i2 = 0; i2 < childNodes.length; i2++) {
|
|
58
|
-
const slottedNode = childNodes[i2]["s-nr"] || void 0;
|
|
59
|
-
if (slottedNode && slottedNode.isConnected) {
|
|
60
|
-
result.push(slottedNode);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
};
|
|
65
|
-
function getHostSlotNodes(childNodes, hostName, slotName) {
|
|
66
|
-
let i2 = 0;
|
|
67
|
-
let slottedNodes = [];
|
|
68
|
-
let childNode;
|
|
69
|
-
for (; i2 < childNodes.length; i2++) {
|
|
70
|
-
childNode = childNodes[i2];
|
|
71
|
-
if (childNode["s-sr"] && (!hostName || childNode["s-hn"] === hostName) && (slotName === void 0 || getSlotName(childNode) === slotName)) {
|
|
72
|
-
slottedNodes.push(childNode);
|
|
73
|
-
if (typeof slotName !== "undefined") return slottedNodes;
|
|
74
|
-
}
|
|
75
|
-
slottedNodes = [...slottedNodes, ...getHostSlotNodes(childNode.childNodes, hostName, slotName)];
|
|
76
|
-
}
|
|
77
|
-
return slottedNodes;
|
|
78
|
-
}
|
|
79
|
-
var getSlotChildSiblings = (slot, slotName, includeSlot = true) => {
|
|
80
|
-
const childNodes = [];
|
|
81
|
-
if (includeSlot && slot["s-sr"] || !slot["s-sr"]) childNodes.push(slot);
|
|
82
|
-
let node = slot;
|
|
83
|
-
while (node = node.nextSibling) {
|
|
84
|
-
if (getSlotName(node) === slotName && (includeSlot || !node["s-sr"])) childNodes.push(node);
|
|
85
|
-
}
|
|
86
|
-
return childNodes;
|
|
87
|
-
};
|
|
88
|
-
var isNodeLocatedInSlot = (nodeToRelocate, slotName) => {
|
|
89
|
-
if (nodeToRelocate.nodeType === 1 /* ElementNode */) {
|
|
90
|
-
if (nodeToRelocate.getAttribute("slot") === null && slotName === "") {
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
if (nodeToRelocate.getAttribute("slot") === slotName) {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
if (nodeToRelocate["s-sn"] === slotName) {
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
return slotName === "";
|
|
102
|
-
};
|
|
103
|
-
var getSlotName = (node) => typeof node["s-sn"] === "string" ? node["s-sn"] : node.nodeType === 1 && node.getAttribute("slot") || void 0;
|
|
104
|
-
function patchSlotNode(node) {
|
|
105
|
-
if (node.assignedElements || node.assignedNodes || !node["s-sr"]) return;
|
|
106
|
-
const assignedFactory = (elementsOnly) => (function(opts) {
|
|
107
|
-
const toReturn = [];
|
|
108
|
-
const slotName = this["s-sn"];
|
|
109
|
-
if (opts == null ? void 0 : opts.flatten) {
|
|
110
|
-
console.error(`
|
|
111
|
-
Flattening is not supported for Stencil non-shadow slots.
|
|
112
|
-
You can use \`.childNodes\` to nested slot fallback content.
|
|
113
|
-
If you have a particular use case, please open an issue on the Stencil repo.
|
|
114
|
-
`);
|
|
115
|
-
}
|
|
116
|
-
const parent = this["s-cr"].parentElement;
|
|
117
|
-
const slottedNodes = parent.__childNodes ? parent.childNodes : getSlottedChildNodes(parent.childNodes);
|
|
118
|
-
slottedNodes.forEach((n) => {
|
|
119
|
-
if (slotName === getSlotName(n)) {
|
|
120
|
-
toReturn.push(n);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
if (elementsOnly) {
|
|
124
|
-
return toReturn.filter((n) => n.nodeType === 1 /* ElementNode */);
|
|
125
|
-
}
|
|
126
|
-
return toReturn;
|
|
127
|
-
}).bind(node);
|
|
128
|
-
node.assignedElements = assignedFactory(true);
|
|
129
|
-
node.assignedNodes = assignedFactory(false);
|
|
130
|
-
}
|
|
131
|
-
function dispatchSlotChangeEvent(elm) {
|
|
132
|
-
elm.dispatchEvent(new CustomEvent("slotchange", { bubbles: false, cancelable: false, composed: false }));
|
|
133
|
-
}
|
|
134
|
-
function findSlotFromSlottedNode(slottedNode, parentHost) {
|
|
135
|
-
var _a;
|
|
136
|
-
parentHost = parentHost || ((_a = slottedNode["s-ol"]) == null ? void 0 : _a.parentElement);
|
|
137
|
-
if (!parentHost) return { slotNode: null, slotName: "" };
|
|
138
|
-
const slotName = slottedNode["s-sn"] = getSlotName(slottedNode) || "";
|
|
139
|
-
const childNodes = internalCall(parentHost, "childNodes");
|
|
140
|
-
const slotNode = getHostSlotNodes(childNodes, parentHost.tagName, slotName)[0];
|
|
141
|
-
return { slotNode, slotName };
|
|
142
|
-
}
|
|
143
|
-
function internalCall(node, method) {
|
|
144
|
-
if ("__" + method in node) {
|
|
145
|
-
const toReturn = node["__" + method];
|
|
146
|
-
if (typeof toReturn !== "function") return toReturn;
|
|
147
|
-
return toReturn.bind(node);
|
|
148
|
-
} else {
|
|
149
|
-
if (typeof node[method] !== "function") return node[method];
|
|
150
|
-
return node[method].bind(node);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// src/utils/helpers.ts
|
|
155
|
-
var isDef = (v) => v != null && v !== void 0;
|
|
156
|
-
var isComplexType = (o) => {
|
|
157
|
-
o = typeof o;
|
|
158
|
-
return o === "object" || o === "function";
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// src/runtime/vdom/h.ts
|
|
162
|
-
var h = (nodeName, vnodeData, ...children) => {
|
|
163
|
-
let child = null;
|
|
164
|
-
let key = null;
|
|
165
|
-
let slotName = null;
|
|
166
|
-
let simple = false;
|
|
167
|
-
let lastSimple = false;
|
|
168
|
-
const vNodeChildren = [];
|
|
169
|
-
const walk = (c) => {
|
|
170
|
-
for (let i2 = 0; i2 < c.length; i2++) {
|
|
171
|
-
child = c[i2];
|
|
172
|
-
if (Array.isArray(child)) {
|
|
173
|
-
walk(child);
|
|
174
|
-
} else if (child != null && typeof child !== "boolean") {
|
|
175
|
-
if (simple = !isComplexType(child)) {
|
|
176
|
-
child = String(child);
|
|
177
|
-
}
|
|
178
|
-
if (simple && lastSimple) {
|
|
179
|
-
vNodeChildren[vNodeChildren.length - 1].$text$ += child;
|
|
180
|
-
} else {
|
|
181
|
-
vNodeChildren.push(simple ? newVNode(null, child) : child);
|
|
182
|
-
}
|
|
183
|
-
lastSimple = simple;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
walk(children);
|
|
188
|
-
const vnode = newVNode(nodeName, null);
|
|
189
|
-
vnode.$attrs$ = vnodeData;
|
|
190
|
-
if (vNodeChildren.length > 0) {
|
|
191
|
-
vnode.$children$ = vNodeChildren;
|
|
192
|
-
}
|
|
193
|
-
{
|
|
194
|
-
vnode.$key$ = key;
|
|
195
|
-
}
|
|
196
|
-
{
|
|
197
|
-
vnode.$name$ = slotName;
|
|
198
|
-
}
|
|
199
|
-
return vnode;
|
|
200
|
-
};
|
|
201
|
-
var newVNode = (tag, text) => {
|
|
202
|
-
const vnode = {
|
|
203
|
-
$flags$: 0,
|
|
204
|
-
$tag$: tag,
|
|
205
|
-
// Normalize undefined to null to prevent rendering "undefined" as text
|
|
206
|
-
$text$: text != null ? text : null,
|
|
207
|
-
$elm$: null,
|
|
208
|
-
$children$: null
|
|
209
|
-
};
|
|
210
|
-
{
|
|
211
|
-
vnode.$attrs$ = null;
|
|
212
|
-
}
|
|
213
|
-
{
|
|
214
|
-
vnode.$key$ = null;
|
|
215
|
-
}
|
|
216
|
-
{
|
|
217
|
-
vnode.$name$ = null;
|
|
218
|
-
}
|
|
219
|
-
return vnode;
|
|
220
|
-
};
|
|
221
|
-
var Host = {};
|
|
222
|
-
var isHost = (node) => node && node.$tag$ === Host;
|
|
223
|
-
var setAccessor = (elm, memberName, oldValue, newValue, isSvg, flags, initialRender) => {
|
|
224
|
-
if (oldValue === newValue) {
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
let isProp = isMemberInElement(elm, memberName);
|
|
228
|
-
let ln = memberName.toLowerCase();
|
|
229
|
-
if (memberName === "class") {
|
|
230
|
-
const classList = elm.classList;
|
|
231
|
-
const oldClasses = parseClassList(oldValue);
|
|
232
|
-
let newClasses = parseClassList(newValue);
|
|
233
|
-
{
|
|
234
|
-
classList.remove(...oldClasses.filter((c) => c && !newClasses.includes(c)));
|
|
235
|
-
classList.add(...newClasses.filter((c) => c && !oldClasses.includes(c)));
|
|
236
|
-
}
|
|
237
|
-
} else if (memberName === "style") {
|
|
238
|
-
{
|
|
239
|
-
for (const prop in oldValue) {
|
|
240
|
-
if (!newValue || newValue[prop] == null) {
|
|
241
|
-
if (prop.includes("-")) {
|
|
242
|
-
elm.style.removeProperty(prop);
|
|
243
|
-
} else {
|
|
244
|
-
elm.style[prop] = "";
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
for (const prop in newValue) {
|
|
250
|
-
if (!oldValue || newValue[prop] !== oldValue[prop]) {
|
|
251
|
-
if (prop.includes("-")) {
|
|
252
|
-
elm.style.setProperty(prop, newValue[prop]);
|
|
253
|
-
} else {
|
|
254
|
-
elm.style[prop] = newValue[prop];
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
} else if (memberName === "key") ; else if (memberName === "ref") {
|
|
259
|
-
if (newValue) {
|
|
260
|
-
queueRefAttachment(newValue, elm);
|
|
261
|
-
}
|
|
262
|
-
} else if ((!elm.__lookupSetter__(memberName)) && memberName[0] === "o" && memberName[1] === "n") {
|
|
263
|
-
if (memberName[2] === "-") {
|
|
264
|
-
memberName = memberName.slice(3);
|
|
265
|
-
} else if (isMemberInElement(win, ln)) {
|
|
266
|
-
memberName = ln.slice(2);
|
|
267
|
-
} else {
|
|
268
|
-
memberName = ln[2] + memberName.slice(3);
|
|
269
|
-
}
|
|
270
|
-
if (oldValue || newValue) {
|
|
271
|
-
const capture = memberName.endsWith(CAPTURE_EVENT_SUFFIX);
|
|
272
|
-
memberName = memberName.replace(CAPTURE_EVENT_REGEX, "");
|
|
273
|
-
if (oldValue) {
|
|
274
|
-
plt.rel(elm, memberName, oldValue, capture);
|
|
275
|
-
}
|
|
276
|
-
if (newValue) {
|
|
277
|
-
plt.ael(elm, memberName, newValue, capture);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
} else if (memberName[0] === "a" && memberName.startsWith("attr:")) {
|
|
281
|
-
const propName = memberName.slice(5);
|
|
282
|
-
let attrName;
|
|
283
|
-
{
|
|
284
|
-
const hostRef = getHostRef(elm);
|
|
285
|
-
if (hostRef && hostRef.$cmpMeta$ && hostRef.$cmpMeta$.$members$) {
|
|
286
|
-
const memberMeta = hostRef.$cmpMeta$.$members$[propName];
|
|
287
|
-
if (memberMeta && memberMeta[1]) {
|
|
288
|
-
attrName = memberMeta[1];
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
if (!attrName) {
|
|
293
|
-
attrName = propName.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
294
|
-
}
|
|
295
|
-
if (newValue == null || newValue === false) {
|
|
296
|
-
if (newValue !== false || elm.getAttribute(attrName) === "") {
|
|
297
|
-
elm.removeAttribute(attrName);
|
|
298
|
-
}
|
|
299
|
-
} else {
|
|
300
|
-
elm.setAttribute(attrName, newValue === true ? "" : newValue);
|
|
301
|
-
}
|
|
302
|
-
return;
|
|
303
|
-
} else if (memberName[0] === "p" && memberName.startsWith("prop:")) {
|
|
304
|
-
const propName = memberName.slice(5);
|
|
305
|
-
try {
|
|
306
|
-
elm[propName] = newValue;
|
|
307
|
-
} catch (e) {
|
|
308
|
-
}
|
|
309
|
-
return;
|
|
310
|
-
} else {
|
|
311
|
-
const isComplex = isComplexType(newValue);
|
|
312
|
-
if ((isProp || isComplex && newValue !== null) && !isSvg) {
|
|
313
|
-
try {
|
|
314
|
-
if (!elm.tagName.includes("-")) {
|
|
315
|
-
const n = newValue == null ? "" : newValue;
|
|
316
|
-
if (memberName === "list") {
|
|
317
|
-
isProp = false;
|
|
318
|
-
} else if (oldValue == null || elm[memberName] !== n) {
|
|
319
|
-
if (typeof elm.__lookupSetter__(memberName) === "function") {
|
|
320
|
-
elm[memberName] = n;
|
|
321
|
-
} else {
|
|
322
|
-
elm.setAttribute(memberName, n);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
} else if (elm[memberName] !== newValue) {
|
|
326
|
-
elm[memberName] = newValue;
|
|
327
|
-
}
|
|
328
|
-
} catch (e) {
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
let xlink = false;
|
|
332
|
-
{
|
|
333
|
-
if (ln !== (ln = ln.replace(/^xlink\:?/, ""))) {
|
|
334
|
-
memberName = ln;
|
|
335
|
-
xlink = true;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
if (newValue == null || newValue === false) {
|
|
339
|
-
if (newValue !== false || elm.getAttribute(memberName) === "") {
|
|
340
|
-
if (xlink) {
|
|
341
|
-
elm.removeAttributeNS(XLINK_NS, memberName);
|
|
342
|
-
} else {
|
|
343
|
-
elm.removeAttribute(memberName);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
} else if ((!isProp || flags & 4 /* isHost */ || isSvg) && !isComplex && elm.nodeType === 1 /* ElementNode */) {
|
|
347
|
-
newValue = newValue === true ? "" : newValue;
|
|
348
|
-
if (xlink) {
|
|
349
|
-
elm.setAttributeNS(XLINK_NS, memberName, newValue);
|
|
350
|
-
} else {
|
|
351
|
-
elm.setAttribute(memberName, newValue);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
var parseClassListRegex = /\s/;
|
|
357
|
-
var parseClassList = (value) => {
|
|
358
|
-
if (typeof value === "object" && value && "baseVal" in value) {
|
|
359
|
-
value = value.baseVal;
|
|
360
|
-
}
|
|
361
|
-
if (!value || typeof value !== "string") {
|
|
362
|
-
return [];
|
|
363
|
-
}
|
|
364
|
-
return value.split(parseClassListRegex);
|
|
365
|
-
};
|
|
366
|
-
var CAPTURE_EVENT_SUFFIX = "Capture";
|
|
367
|
-
var CAPTURE_EVENT_REGEX = new RegExp(CAPTURE_EVENT_SUFFIX + "$");
|
|
368
|
-
|
|
369
|
-
// src/runtime/vdom/update-element.ts
|
|
370
|
-
var updateElement = (oldVnode, newVnode, isSvgMode2, isInitialRender) => {
|
|
371
|
-
const elm = newVnode.$elm$.nodeType === 11 /* DocumentFragment */ && newVnode.$elm$.host ? newVnode.$elm$.host : newVnode.$elm$;
|
|
372
|
-
const oldVnodeAttrs = oldVnode && oldVnode.$attrs$ || {};
|
|
373
|
-
const newVnodeAttrs = newVnode.$attrs$ || {};
|
|
374
|
-
{
|
|
375
|
-
for (const memberName of sortedAttrNames(Object.keys(oldVnodeAttrs))) {
|
|
376
|
-
if (!(memberName in newVnodeAttrs)) {
|
|
377
|
-
setAccessor(
|
|
378
|
-
elm,
|
|
379
|
-
memberName,
|
|
380
|
-
oldVnodeAttrs[memberName],
|
|
381
|
-
void 0,
|
|
382
|
-
isSvgMode2,
|
|
383
|
-
newVnode.$flags$);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
for (const memberName of sortedAttrNames(Object.keys(newVnodeAttrs))) {
|
|
388
|
-
setAccessor(
|
|
389
|
-
elm,
|
|
390
|
-
memberName,
|
|
391
|
-
oldVnodeAttrs[memberName],
|
|
392
|
-
newVnodeAttrs[memberName],
|
|
393
|
-
isSvgMode2,
|
|
394
|
-
newVnode.$flags$);
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
function sortedAttrNames(attrNames) {
|
|
398
|
-
return attrNames.includes("ref") ? (
|
|
399
|
-
// we need to sort these to ensure that `'ref'` is the last attr
|
|
400
|
-
[...attrNames.filter((attr) => attr !== "ref"), "ref"]
|
|
401
|
-
) : (
|
|
402
|
-
// no need to sort, return the original array
|
|
403
|
-
attrNames
|
|
404
|
-
);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// src/runtime/vdom/vdom-render.ts
|
|
408
|
-
var scopeId;
|
|
409
|
-
var contentRef;
|
|
410
|
-
var hostTagName;
|
|
411
|
-
var useNativeShadowDom = false;
|
|
412
|
-
var checkSlotFallbackVisibility = false;
|
|
413
|
-
var checkSlotRelocate = false;
|
|
414
|
-
var isSvgMode = false;
|
|
415
|
-
var refCallbacksToRemove = [];
|
|
416
|
-
var refCallbacksToAttach = [];
|
|
417
|
-
var createElm = (oldParentVNode, newParentVNode, childIndex) => {
|
|
418
|
-
var _a;
|
|
419
|
-
const newVNode2 = newParentVNode.$children$[childIndex];
|
|
420
|
-
let i2 = 0;
|
|
421
|
-
let elm;
|
|
422
|
-
let childNode;
|
|
423
|
-
let oldVNode;
|
|
424
|
-
if (!useNativeShadowDom) {
|
|
425
|
-
checkSlotRelocate = true;
|
|
426
|
-
if (newVNode2.$tag$ === "slot") {
|
|
427
|
-
newVNode2.$flags$ |= newVNode2.$children$ ? (
|
|
428
|
-
// slot element has fallback content
|
|
429
|
-
// still create an element that "mocks" the slot element
|
|
430
|
-
2 /* isSlotFallback */
|
|
431
|
-
) : (
|
|
432
|
-
// slot element does not have fallback content
|
|
433
|
-
// create an html comment we'll use to always reference
|
|
434
|
-
// where actual slot content should sit next to
|
|
435
|
-
1 /* isSlotReference */
|
|
436
|
-
);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
if (newVNode2.$text$ != null) {
|
|
440
|
-
elm = newVNode2.$elm$ = win.document.createTextNode(newVNode2.$text$);
|
|
441
|
-
} else if (newVNode2.$flags$ & 1 /* isSlotReference */) {
|
|
442
|
-
elm = newVNode2.$elm$ = win.document.createTextNode("");
|
|
443
|
-
{
|
|
444
|
-
updateElement(null, newVNode2, isSvgMode);
|
|
445
|
-
}
|
|
446
|
-
} else {
|
|
447
|
-
if (!isSvgMode) {
|
|
448
|
-
isSvgMode = newVNode2.$tag$ === "svg";
|
|
449
|
-
}
|
|
450
|
-
if (!win.document) {
|
|
451
|
-
throw new Error("You are trying to render a Stencil component in an environment that doesn't support the DOM.");
|
|
452
|
-
}
|
|
453
|
-
elm = newVNode2.$elm$ = win.document.createElementNS(
|
|
454
|
-
isSvgMode ? SVG_NS : HTML_NS,
|
|
455
|
-
!useNativeShadowDom && BUILD.slotRelocation && newVNode2.$flags$ & 2 /* isSlotFallback */ ? "slot-fb" : newVNode2.$tag$
|
|
456
|
-
) ;
|
|
457
|
-
if (isSvgMode && newVNode2.$tag$ === "foreignObject") {
|
|
458
|
-
isSvgMode = false;
|
|
459
|
-
}
|
|
460
|
-
{
|
|
461
|
-
updateElement(null, newVNode2, isSvgMode);
|
|
462
|
-
}
|
|
463
|
-
if (isDef(scopeId) && elm["s-si"] !== scopeId) {
|
|
464
|
-
elm.classList.add(elm["s-si"] = scopeId);
|
|
465
|
-
}
|
|
466
|
-
if (newVNode2.$children$) {
|
|
467
|
-
const appendTarget = newVNode2.$tag$ === "template" ? elm.content : elm;
|
|
468
|
-
for (i2 = 0; i2 < newVNode2.$children$.length; ++i2) {
|
|
469
|
-
childNode = createElm(oldParentVNode, newVNode2, i2);
|
|
470
|
-
if (childNode) {
|
|
471
|
-
appendTarget.appendChild(childNode);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
{
|
|
476
|
-
if (newVNode2.$tag$ === "svg") {
|
|
477
|
-
isSvgMode = false;
|
|
478
|
-
} else if (elm.tagName === "foreignObject") {
|
|
479
|
-
isSvgMode = true;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
elm["s-hn"] = hostTagName;
|
|
484
|
-
{
|
|
485
|
-
if (newVNode2.$flags$ & (2 /* isSlotFallback */ | 1 /* isSlotReference */)) {
|
|
486
|
-
elm["s-sr"] = true;
|
|
487
|
-
elm["s-cr"] = contentRef;
|
|
488
|
-
elm["s-sn"] = newVNode2.$name$ || "";
|
|
489
|
-
elm["s-rf"] = (_a = newVNode2.$attrs$) == null ? void 0 : _a.ref;
|
|
490
|
-
patchSlotNode(elm);
|
|
491
|
-
oldVNode = oldParentVNode && oldParentVNode.$children$ && oldParentVNode.$children$[childIndex];
|
|
492
|
-
if (oldVNode && oldVNode.$tag$ === newVNode2.$tag$ && oldParentVNode.$elm$) {
|
|
493
|
-
relocateToHostRoot(oldParentVNode.$elm$);
|
|
494
|
-
}
|
|
495
|
-
{
|
|
496
|
-
addRemoveSlotScopedClass(contentRef, elm, newParentVNode.$elm$, oldParentVNode == null ? void 0 : oldParentVNode.$elm$);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
return elm;
|
|
501
|
-
};
|
|
502
|
-
var relocateToHostRoot = (parentElm) => {
|
|
503
|
-
plt.$flags$ |= 1 /* isTmpDisconnected */;
|
|
504
|
-
const host = parentElm.closest(hostTagName.toLowerCase());
|
|
505
|
-
if (host != null) {
|
|
506
|
-
const contentRefNode = Array.from(host.__childNodes || host.childNodes).find(
|
|
507
|
-
(ref) => ref["s-cr"]
|
|
508
|
-
);
|
|
509
|
-
const childNodeArray = Array.from(
|
|
510
|
-
parentElm.__childNodes || parentElm.childNodes
|
|
511
|
-
);
|
|
512
|
-
for (const childNode of contentRefNode ? childNodeArray.reverse() : childNodeArray) {
|
|
513
|
-
if (childNode["s-sh"] != null) {
|
|
514
|
-
insertBefore(host, childNode, contentRefNode != null ? contentRefNode : null);
|
|
515
|
-
childNode["s-sh"] = void 0;
|
|
516
|
-
checkSlotRelocate = true;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
plt.$flags$ &= -2 /* isTmpDisconnected */;
|
|
521
|
-
};
|
|
522
|
-
var putBackInOriginalLocation = (parentElm, recursive) => {
|
|
523
|
-
plt.$flags$ |= 1 /* isTmpDisconnected */;
|
|
524
|
-
const oldSlotChildNodes = Array.from(parentElm.__childNodes || parentElm.childNodes);
|
|
525
|
-
if (parentElm["s-sr"]) {
|
|
526
|
-
let node = parentElm;
|
|
527
|
-
while (node = node.nextSibling) {
|
|
528
|
-
if (node && node["s-sn"] === parentElm["s-sn"] && node["s-sh"] === hostTagName) {
|
|
529
|
-
oldSlotChildNodes.push(node);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
for (let i2 = oldSlotChildNodes.length - 1; i2 >= 0; i2--) {
|
|
534
|
-
const childNode = oldSlotChildNodes[i2];
|
|
535
|
-
if (childNode["s-hn"] !== hostTagName && childNode["s-ol"]) {
|
|
536
|
-
insertBefore(referenceNode(childNode).parentNode, childNode, referenceNode(childNode));
|
|
537
|
-
childNode["s-ol"].remove();
|
|
538
|
-
childNode["s-ol"] = void 0;
|
|
539
|
-
childNode["s-sh"] = void 0;
|
|
540
|
-
checkSlotRelocate = true;
|
|
541
|
-
}
|
|
542
|
-
if (recursive) {
|
|
543
|
-
putBackInOriginalLocation(childNode, recursive);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
plt.$flags$ &= -2 /* isTmpDisconnected */;
|
|
547
|
-
};
|
|
548
|
-
var addVnodes = (parentElm, before, parentVNode, vnodes, startIdx, endIdx) => {
|
|
549
|
-
let containerElm = parentElm["s-cr"] && parentElm["s-cr"].parentNode || parentElm;
|
|
550
|
-
let childNode;
|
|
551
|
-
if (containerElm.shadowRoot && containerElm.tagName === hostTagName) {
|
|
552
|
-
containerElm = containerElm.shadowRoot;
|
|
553
|
-
}
|
|
554
|
-
if (parentVNode.$tag$ === "template") {
|
|
555
|
-
containerElm = containerElm.content;
|
|
556
|
-
}
|
|
557
|
-
for (; startIdx <= endIdx; ++startIdx) {
|
|
558
|
-
if (vnodes[startIdx]) {
|
|
559
|
-
childNode = createElm(null, parentVNode, startIdx);
|
|
560
|
-
if (childNode) {
|
|
561
|
-
vnodes[startIdx].$elm$ = childNode;
|
|
562
|
-
insertBefore(containerElm, childNode, referenceNode(before) );
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
};
|
|
567
|
-
var removeVnodes = (vnodes, startIdx, endIdx) => {
|
|
568
|
-
for (let index = startIdx; index <= endIdx; ++index) {
|
|
569
|
-
const vnode = vnodes[index];
|
|
570
|
-
if (vnode) {
|
|
571
|
-
const elm = vnode.$elm$;
|
|
572
|
-
nullifyVNodeRefs(vnode);
|
|
573
|
-
if (elm) {
|
|
574
|
-
{
|
|
575
|
-
checkSlotFallbackVisibility = true;
|
|
576
|
-
if (elm["s-ol"]) {
|
|
577
|
-
elm["s-ol"].remove();
|
|
578
|
-
} else {
|
|
579
|
-
putBackInOriginalLocation(elm, true);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
elm.remove();
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
};
|
|
587
|
-
var updateChildren = (parentElm, oldCh, newVNode2, newCh, isInitialRender = false) => {
|
|
588
|
-
let oldStartIdx = 0;
|
|
589
|
-
let newStartIdx = 0;
|
|
590
|
-
let idxInOld = 0;
|
|
591
|
-
let i2 = 0;
|
|
592
|
-
let oldEndIdx = oldCh.length - 1;
|
|
593
|
-
let oldStartVnode = oldCh[0];
|
|
594
|
-
let oldEndVnode = oldCh[oldEndIdx];
|
|
595
|
-
let newEndIdx = newCh.length - 1;
|
|
596
|
-
let newStartVnode = newCh[0];
|
|
597
|
-
let newEndVnode = newCh[newEndIdx];
|
|
598
|
-
let node;
|
|
599
|
-
let elmToMove;
|
|
600
|
-
const containerElm = newVNode2.$tag$ === "template" ? parentElm.content : parentElm;
|
|
601
|
-
while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
|
|
602
|
-
if (oldStartVnode == null) {
|
|
603
|
-
oldStartVnode = oldCh[++oldStartIdx];
|
|
604
|
-
} else if (oldEndVnode == null) {
|
|
605
|
-
oldEndVnode = oldCh[--oldEndIdx];
|
|
606
|
-
} else if (newStartVnode == null) {
|
|
607
|
-
newStartVnode = newCh[++newStartIdx];
|
|
608
|
-
} else if (newEndVnode == null) {
|
|
609
|
-
newEndVnode = newCh[--newEndIdx];
|
|
610
|
-
} else if (isSameVnode(oldStartVnode, newStartVnode, isInitialRender)) {
|
|
611
|
-
patch(oldStartVnode, newStartVnode, isInitialRender);
|
|
612
|
-
oldStartVnode = oldCh[++oldStartIdx];
|
|
613
|
-
newStartVnode = newCh[++newStartIdx];
|
|
614
|
-
} else if (isSameVnode(oldEndVnode, newEndVnode, isInitialRender)) {
|
|
615
|
-
patch(oldEndVnode, newEndVnode, isInitialRender);
|
|
616
|
-
oldEndVnode = oldCh[--oldEndIdx];
|
|
617
|
-
newEndVnode = newCh[--newEndIdx];
|
|
618
|
-
} else if (isSameVnode(oldStartVnode, newEndVnode, isInitialRender)) {
|
|
619
|
-
if ((oldStartVnode.$tag$ === "slot" || newEndVnode.$tag$ === "slot")) {
|
|
620
|
-
putBackInOriginalLocation(oldStartVnode.$elm$.parentNode, false);
|
|
621
|
-
}
|
|
622
|
-
patch(oldStartVnode, newEndVnode, isInitialRender);
|
|
623
|
-
insertBefore(containerElm, oldStartVnode.$elm$, oldEndVnode.$elm$.nextSibling);
|
|
624
|
-
oldStartVnode = oldCh[++oldStartIdx];
|
|
625
|
-
newEndVnode = newCh[--newEndIdx];
|
|
626
|
-
} else if (isSameVnode(oldEndVnode, newStartVnode, isInitialRender)) {
|
|
627
|
-
if ((oldStartVnode.$tag$ === "slot" || newEndVnode.$tag$ === "slot")) {
|
|
628
|
-
putBackInOriginalLocation(oldEndVnode.$elm$.parentNode, false);
|
|
629
|
-
}
|
|
630
|
-
patch(oldEndVnode, newStartVnode, isInitialRender);
|
|
631
|
-
insertBefore(containerElm, oldEndVnode.$elm$, oldStartVnode.$elm$);
|
|
632
|
-
oldEndVnode = oldCh[--oldEndIdx];
|
|
633
|
-
newStartVnode = newCh[++newStartIdx];
|
|
634
|
-
} else {
|
|
635
|
-
idxInOld = -1;
|
|
636
|
-
{
|
|
637
|
-
for (i2 = oldStartIdx; i2 <= oldEndIdx; ++i2) {
|
|
638
|
-
if (oldCh[i2] && oldCh[i2].$key$ !== null && oldCh[i2].$key$ === newStartVnode.$key$) {
|
|
639
|
-
idxInOld = i2;
|
|
640
|
-
break;
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
if (idxInOld >= 0) {
|
|
645
|
-
elmToMove = oldCh[idxInOld];
|
|
646
|
-
if (elmToMove.$tag$ !== newStartVnode.$tag$) {
|
|
647
|
-
node = createElm(oldCh && oldCh[newStartIdx], newVNode2, idxInOld);
|
|
648
|
-
} else {
|
|
649
|
-
patch(elmToMove, newStartVnode, isInitialRender);
|
|
650
|
-
oldCh[idxInOld] = void 0;
|
|
651
|
-
node = elmToMove.$elm$;
|
|
652
|
-
}
|
|
653
|
-
newStartVnode = newCh[++newStartIdx];
|
|
654
|
-
} else {
|
|
655
|
-
node = createElm(oldCh && oldCh[newStartIdx], newVNode2, newStartIdx);
|
|
656
|
-
newStartVnode = newCh[++newStartIdx];
|
|
657
|
-
}
|
|
658
|
-
if (node) {
|
|
659
|
-
{
|
|
660
|
-
insertBefore(
|
|
661
|
-
referenceNode(oldStartVnode.$elm$).parentNode,
|
|
662
|
-
node,
|
|
663
|
-
referenceNode(oldStartVnode.$elm$)
|
|
664
|
-
);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
if (oldStartIdx > oldEndIdx) {
|
|
670
|
-
addVnodes(
|
|
671
|
-
parentElm,
|
|
672
|
-
newCh[newEndIdx + 1] == null ? null : newCh[newEndIdx + 1].$elm$,
|
|
673
|
-
newVNode2,
|
|
674
|
-
newCh,
|
|
675
|
-
newStartIdx,
|
|
676
|
-
newEndIdx
|
|
677
|
-
);
|
|
678
|
-
} else if (newStartIdx > newEndIdx) {
|
|
679
|
-
removeVnodes(oldCh, oldStartIdx, oldEndIdx);
|
|
680
|
-
}
|
|
681
|
-
};
|
|
682
|
-
var isSameVnode = (leftVNode, rightVNode, isInitialRender = false) => {
|
|
683
|
-
if (leftVNode.$tag$ === rightVNode.$tag$) {
|
|
684
|
-
if (leftVNode.$tag$ === "slot") {
|
|
685
|
-
return leftVNode.$name$ === rightVNode.$name$;
|
|
686
|
-
}
|
|
687
|
-
if (!isInitialRender) {
|
|
688
|
-
return leftVNode.$key$ === rightVNode.$key$;
|
|
689
|
-
}
|
|
690
|
-
if (isInitialRender && !leftVNode.$key$ && rightVNode.$key$) {
|
|
691
|
-
leftVNode.$key$ = rightVNode.$key$;
|
|
692
|
-
}
|
|
693
|
-
return true;
|
|
694
|
-
}
|
|
695
|
-
return false;
|
|
696
|
-
};
|
|
697
|
-
var referenceNode = (node) => node && node["s-ol"] || node;
|
|
698
|
-
var patch = (oldVNode, newVNode2, isInitialRender = false) => {
|
|
699
|
-
const elm = newVNode2.$elm$ = oldVNode.$elm$;
|
|
700
|
-
const oldChildren = oldVNode.$children$;
|
|
701
|
-
const newChildren = newVNode2.$children$;
|
|
702
|
-
const tag = newVNode2.$tag$;
|
|
703
|
-
const text = newVNode2.$text$;
|
|
704
|
-
let defaultHolder;
|
|
705
|
-
if (text == null) {
|
|
706
|
-
{
|
|
707
|
-
isSvgMode = tag === "svg" ? true : tag === "foreignObject" ? false : isSvgMode;
|
|
708
|
-
}
|
|
709
|
-
{
|
|
710
|
-
if (tag === "slot" && !useNativeShadowDom) {
|
|
711
|
-
if (oldVNode.$name$ !== newVNode2.$name$) {
|
|
712
|
-
newVNode2.$elm$["s-sn"] = newVNode2.$name$ || "";
|
|
713
|
-
relocateToHostRoot(newVNode2.$elm$.parentElement);
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
updateElement(oldVNode, newVNode2, isSvgMode);
|
|
717
|
-
}
|
|
718
|
-
if (oldChildren !== null && newChildren !== null) {
|
|
719
|
-
updateChildren(elm, oldChildren, newVNode2, newChildren, isInitialRender);
|
|
720
|
-
} else if (newChildren !== null) {
|
|
721
|
-
if (oldVNode.$text$ !== null) {
|
|
722
|
-
elm.textContent = "";
|
|
723
|
-
}
|
|
724
|
-
addVnodes(elm, null, newVNode2, newChildren, 0, newChildren.length - 1);
|
|
725
|
-
} else if (
|
|
726
|
-
// don't do this on initial render as it can cause non-hydrated content to be removed
|
|
727
|
-
!isInitialRender && BUILD.updatable && oldChildren !== null
|
|
728
|
-
) {
|
|
729
|
-
removeVnodes(oldChildren, 0, oldChildren.length - 1);
|
|
730
|
-
} else ;
|
|
731
|
-
if (isSvgMode && tag === "svg") {
|
|
732
|
-
isSvgMode = false;
|
|
733
|
-
}
|
|
734
|
-
} else if ((defaultHolder = elm["s-cr"])) {
|
|
735
|
-
defaultHolder.parentNode.textContent = text;
|
|
736
|
-
} else if (oldVNode.$text$ !== text) {
|
|
737
|
-
elm.data = text;
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
var relocateNodes = [];
|
|
741
|
-
var markSlotContentForRelocation = (elm) => {
|
|
742
|
-
let node;
|
|
743
|
-
let hostContentNodes;
|
|
744
|
-
let j;
|
|
745
|
-
const children = elm.__childNodes || elm.childNodes;
|
|
746
|
-
for (const childNode of children) {
|
|
747
|
-
if (childNode["s-sr"] && (node = childNode["s-cr"]) && node.parentNode) {
|
|
748
|
-
hostContentNodes = node.parentNode.__childNodes || node.parentNode.childNodes;
|
|
749
|
-
const slotName = childNode["s-sn"];
|
|
750
|
-
for (j = hostContentNodes.length - 1; j >= 0; j--) {
|
|
751
|
-
node = hostContentNodes[j];
|
|
752
|
-
if (!node["s-cn"] && !node["s-nr"] && node["s-hn"] !== childNode["s-hn"] && (!node["s-sh"] || node["s-sh"] !== childNode["s-hn"])) {
|
|
753
|
-
if (isNodeLocatedInSlot(node, slotName)) {
|
|
754
|
-
let relocateNodeData = relocateNodes.find((r) => r.$nodeToRelocate$ === node);
|
|
755
|
-
checkSlotFallbackVisibility = true;
|
|
756
|
-
node["s-sn"] = node["s-sn"] || slotName;
|
|
757
|
-
if (relocateNodeData) {
|
|
758
|
-
relocateNodeData.$nodeToRelocate$["s-sh"] = childNode["s-hn"];
|
|
759
|
-
relocateNodeData.$slotRefNode$ = childNode;
|
|
760
|
-
} else {
|
|
761
|
-
node["s-sh"] = childNode["s-hn"];
|
|
762
|
-
relocateNodes.push({
|
|
763
|
-
$slotRefNode$: childNode,
|
|
764
|
-
$nodeToRelocate$: node
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
if (node["s-sr"]) {
|
|
768
|
-
relocateNodes.map((relocateNode) => {
|
|
769
|
-
if (isNodeLocatedInSlot(relocateNode.$nodeToRelocate$, node["s-sn"])) {
|
|
770
|
-
relocateNodeData = relocateNodes.find((r) => r.$nodeToRelocate$ === node);
|
|
771
|
-
if (relocateNodeData && !relocateNode.$slotRefNode$) {
|
|
772
|
-
relocateNode.$slotRefNode$ = relocateNodeData.$slotRefNode$;
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
} else if (!relocateNodes.some((r) => r.$nodeToRelocate$ === node)) {
|
|
778
|
-
relocateNodes.push({
|
|
779
|
-
$nodeToRelocate$: node
|
|
780
|
-
});
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
if (childNode.nodeType === 1 /* ElementNode */) {
|
|
786
|
-
markSlotContentForRelocation(childNode);
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
};
|
|
790
|
-
var nullifyVNodeRefs = (vNode) => {
|
|
791
|
-
{
|
|
792
|
-
if (vNode.$attrs$ && vNode.$attrs$.ref) {
|
|
793
|
-
refCallbacksToRemove.push(() => vNode.$attrs$.ref(null));
|
|
794
|
-
}
|
|
795
|
-
vNode.$children$ && vNode.$children$.map(nullifyVNodeRefs);
|
|
796
|
-
}
|
|
797
|
-
};
|
|
798
|
-
var queueRefAttachment = (refCallback, elm) => {
|
|
799
|
-
{
|
|
800
|
-
refCallbacksToAttach.push(() => refCallback(elm));
|
|
801
|
-
}
|
|
802
|
-
};
|
|
803
|
-
var flushQueuedRefCallbacks = () => {
|
|
804
|
-
{
|
|
805
|
-
refCallbacksToRemove.forEach((cb) => cb());
|
|
806
|
-
refCallbacksToRemove.length = 0;
|
|
807
|
-
refCallbacksToAttach.forEach((cb) => cb());
|
|
808
|
-
refCallbacksToAttach.length = 0;
|
|
809
|
-
}
|
|
810
|
-
};
|
|
811
|
-
var insertBefore = (parent, newNode, reference, isInitialLoad) => {
|
|
812
|
-
{
|
|
813
|
-
if (typeof newNode["s-sn"] === "string" && !!newNode["s-sr"] && !!newNode["s-cr"]) {
|
|
814
|
-
addRemoveSlotScopedClass(newNode["s-cr"], newNode, parent, newNode.parentElement);
|
|
815
|
-
} else if (typeof newNode["s-sn"] === "string") {
|
|
816
|
-
parent.insertBefore(newNode, reference);
|
|
817
|
-
const { slotNode } = findSlotFromSlottedNode(newNode);
|
|
818
|
-
if (slotNode && !isInitialLoad) dispatchSlotChangeEvent(slotNode);
|
|
819
|
-
return newNode;
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
if (parent.__insertBefore) {
|
|
823
|
-
return parent.__insertBefore(newNode, reference);
|
|
824
|
-
} else {
|
|
825
|
-
return parent == null ? void 0 : parent.insertBefore(newNode, reference);
|
|
826
|
-
}
|
|
827
|
-
};
|
|
828
|
-
function addRemoveSlotScopedClass(reference, slotNode, newParent, oldParent) {
|
|
829
|
-
var _a, _b;
|
|
830
|
-
let scopeId2;
|
|
831
|
-
if (reference && typeof slotNode["s-sn"] === "string" && !!slotNode["s-sr"] && reference.parentNode && reference.parentNode["s-sc"] && (scopeId2 = slotNode["s-si"] || reference.parentNode["s-sc"])) {
|
|
832
|
-
const scopeName = slotNode["s-sn"];
|
|
833
|
-
const hostName = slotNode["s-hn"];
|
|
834
|
-
(_a = newParent.classList) == null ? void 0 : _a.add(scopeId2 + "-s");
|
|
835
|
-
if (oldParent && ((_b = oldParent.classList) == null ? void 0 : _b.contains(scopeId2 + "-s"))) {
|
|
836
|
-
let child = (oldParent.__childNodes || oldParent.childNodes)[0];
|
|
837
|
-
let found = false;
|
|
838
|
-
while (child) {
|
|
839
|
-
if (child["s-sn"] !== scopeName && child["s-hn"] === hostName && !!child["s-sr"]) {
|
|
840
|
-
found = true;
|
|
841
|
-
break;
|
|
842
|
-
}
|
|
843
|
-
child = child.nextSibling;
|
|
844
|
-
}
|
|
845
|
-
if (!found) oldParent.classList.remove(scopeId2 + "-s");
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
var renderVdom = (hostRef, renderFnResults, isInitialLoad = false) => {
|
|
850
|
-
var _a, _b, _c, _d, _e;
|
|
851
|
-
const hostElm = hostRef.$hostElement$;
|
|
852
|
-
const cmpMeta = hostRef.$cmpMeta$;
|
|
853
|
-
const oldVNode = hostRef.$vnode$ || newVNode(null, null);
|
|
854
|
-
const isHostElement = isHost(renderFnResults);
|
|
855
|
-
const rootVnode = isHostElement ? renderFnResults : h(null, null, renderFnResults);
|
|
856
|
-
hostTagName = hostElm.tagName;
|
|
857
|
-
if (cmpMeta.$attrsToReflect$) {
|
|
858
|
-
rootVnode.$attrs$ = rootVnode.$attrs$ || {};
|
|
859
|
-
cmpMeta.$attrsToReflect$.forEach(([propName, attribute]) => {
|
|
860
|
-
if (BUILD.serializer && hostRef.$serializerValues$.has(propName)) {
|
|
861
|
-
rootVnode.$attrs$[attribute] = hostRef.$serializerValues$.get(propName);
|
|
862
|
-
} else {
|
|
863
|
-
rootVnode.$attrs$[attribute] = hostElm[propName];
|
|
864
|
-
}
|
|
865
|
-
});
|
|
866
|
-
}
|
|
867
|
-
if (isInitialLoad && rootVnode.$attrs$) {
|
|
868
|
-
for (const key of Object.keys(rootVnode.$attrs$)) {
|
|
869
|
-
if (hostElm.hasAttribute(key) && !["key", "ref", "style", "class"].includes(key)) {
|
|
870
|
-
rootVnode.$attrs$[key] = hostElm[key];
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
rootVnode.$tag$ = null;
|
|
875
|
-
rootVnode.$flags$ |= 4 /* isHost */;
|
|
876
|
-
hostRef.$vnode$ = rootVnode;
|
|
877
|
-
rootVnode.$elm$ = oldVNode.$elm$ = hostElm.shadowRoot || hostElm ;
|
|
878
|
-
{
|
|
879
|
-
scopeId = hostElm["s-sc"];
|
|
880
|
-
}
|
|
881
|
-
useNativeShadowDom = !!(cmpMeta.$flags$ & 1 /* shadowDomEncapsulation */) && !(cmpMeta.$flags$ & 128 /* shadowNeedsScopedCss */);
|
|
882
|
-
{
|
|
883
|
-
contentRef = hostElm["s-cr"];
|
|
884
|
-
checkSlotFallbackVisibility = false;
|
|
885
|
-
}
|
|
886
|
-
patch(oldVNode, rootVnode, isInitialLoad);
|
|
887
|
-
{
|
|
888
|
-
plt.$flags$ |= 1 /* isTmpDisconnected */;
|
|
889
|
-
if (checkSlotRelocate) {
|
|
890
|
-
markSlotContentForRelocation(rootVnode.$elm$);
|
|
891
|
-
for (const relocateData of relocateNodes) {
|
|
892
|
-
const nodeToRelocate = relocateData.$nodeToRelocate$;
|
|
893
|
-
if (!nodeToRelocate["s-ol"] && win.document) {
|
|
894
|
-
const orgLocationNode = win.document.createTextNode("");
|
|
895
|
-
orgLocationNode["s-nr"] = nodeToRelocate;
|
|
896
|
-
insertBefore(
|
|
897
|
-
nodeToRelocate.parentNode,
|
|
898
|
-
nodeToRelocate["s-ol"] = orgLocationNode,
|
|
899
|
-
nodeToRelocate,
|
|
900
|
-
isInitialLoad
|
|
901
|
-
);
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
for (const relocateData of relocateNodes) {
|
|
905
|
-
const nodeToRelocate = relocateData.$nodeToRelocate$;
|
|
906
|
-
const slotRefNode = relocateData.$slotRefNode$;
|
|
907
|
-
if (nodeToRelocate.nodeType === 1 /* ElementNode */ && isInitialLoad) {
|
|
908
|
-
nodeToRelocate["s-ih"] = (_a = nodeToRelocate.hidden) != null ? _a : false;
|
|
909
|
-
}
|
|
910
|
-
if (slotRefNode) {
|
|
911
|
-
const parentNodeRef = slotRefNode.parentNode;
|
|
912
|
-
let insertBeforeNode = slotRefNode.nextSibling;
|
|
913
|
-
if (insertBeforeNode && insertBeforeNode.nodeType === 1 /* ElementNode */) {
|
|
914
|
-
let orgLocationNode = (_b = nodeToRelocate["s-ol"]) == null ? void 0 : _b.previousSibling;
|
|
915
|
-
while (orgLocationNode) {
|
|
916
|
-
let refNode = (_c = orgLocationNode["s-nr"]) != null ? _c : null;
|
|
917
|
-
if (refNode && refNode["s-sn"] === nodeToRelocate["s-sn"] && parentNodeRef === (refNode.__parentNode || refNode.parentNode)) {
|
|
918
|
-
refNode = refNode.nextSibling;
|
|
919
|
-
while (refNode === nodeToRelocate || (refNode == null ? void 0 : refNode["s-sr"])) {
|
|
920
|
-
refNode = refNode == null ? void 0 : refNode.nextSibling;
|
|
921
|
-
}
|
|
922
|
-
if (!refNode || !refNode["s-nr"]) {
|
|
923
|
-
insertBeforeNode = refNode;
|
|
924
|
-
break;
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
orgLocationNode = orgLocationNode.previousSibling;
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
const parent = nodeToRelocate.__parentNode || nodeToRelocate.parentNode;
|
|
931
|
-
const nextSibling = nodeToRelocate.__nextSibling || nodeToRelocate.nextSibling;
|
|
932
|
-
if (!insertBeforeNode && parentNodeRef !== parent || nextSibling !== insertBeforeNode) {
|
|
933
|
-
if (nodeToRelocate !== insertBeforeNode) {
|
|
934
|
-
insertBefore(parentNodeRef, nodeToRelocate, insertBeforeNode, isInitialLoad);
|
|
935
|
-
if (nodeToRelocate.nodeType === 8 /* CommentNode */ && nodeToRelocate.nodeValue.startsWith("s-nt-")) {
|
|
936
|
-
const textNode = win.document.createTextNode(nodeToRelocate.nodeValue.replace(/^s-nt-/, ""));
|
|
937
|
-
textNode["s-hn"] = nodeToRelocate["s-hn"];
|
|
938
|
-
textNode["s-sn"] = nodeToRelocate["s-sn"];
|
|
939
|
-
textNode["s-sh"] = nodeToRelocate["s-sh"];
|
|
940
|
-
textNode["s-sr"] = nodeToRelocate["s-sr"];
|
|
941
|
-
textNode["s-ol"] = nodeToRelocate["s-ol"];
|
|
942
|
-
textNode["s-ol"]["s-nr"] = textNode;
|
|
943
|
-
insertBefore(nodeToRelocate.parentNode, textNode, nodeToRelocate, isInitialLoad);
|
|
944
|
-
nodeToRelocate.parentNode.removeChild(nodeToRelocate);
|
|
945
|
-
}
|
|
946
|
-
if (nodeToRelocate.nodeType === 1 /* ElementNode */ && nodeToRelocate.tagName !== "SLOT-FB") {
|
|
947
|
-
nodeToRelocate.hidden = (_d = nodeToRelocate["s-ih"]) != null ? _d : false;
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
nodeToRelocate && typeof slotRefNode["s-rf"] === "function" && slotRefNode["s-rf"](slotRefNode);
|
|
952
|
-
} else if (nodeToRelocate.nodeType === 1 /* ElementNode */) {
|
|
953
|
-
nodeToRelocate.hidden = true;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
if (checkSlotFallbackVisibility) {
|
|
958
|
-
updateFallbackSlotVisibility(rootVnode.$elm$);
|
|
959
|
-
}
|
|
960
|
-
plt.$flags$ &= -2 /* isTmpDisconnected */;
|
|
961
|
-
relocateNodes.length = 0;
|
|
962
|
-
}
|
|
963
|
-
if (!useNativeShadowDom && !(cmpMeta.$flags$ & 1 /* shadowDomEncapsulation */) && hostElm["s-cr"]) {
|
|
964
|
-
const children = rootVnode.$elm$.__childNodes || rootVnode.$elm$.childNodes;
|
|
965
|
-
for (const childNode of children) {
|
|
966
|
-
if (childNode["s-hn"] !== hostTagName && !childNode["s-sh"]) {
|
|
967
|
-
if (isInitialLoad && childNode["s-ih"] == null) {
|
|
968
|
-
childNode["s-ih"] = (_e = childNode.hidden) != null ? _e : false;
|
|
969
|
-
}
|
|
970
|
-
if (childNode.nodeType === 1 /* ElementNode */) {
|
|
971
|
-
childNode.hidden = true;
|
|
972
|
-
} else if (childNode.nodeType === 3 /* TextNode */ && !!childNode.nodeValue.trim()) {
|
|
973
|
-
const textCommentNode = win.document.createComment("s-nt-" + childNode.nodeValue);
|
|
974
|
-
textCommentNode["s-sn"] = childNode["s-sn"];
|
|
975
|
-
insertBefore(childNode.parentNode, textCommentNode, childNode, isInitialLoad);
|
|
976
|
-
childNode.parentNode.removeChild(childNode);
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
contentRef = void 0;
|
|
982
|
-
flushQueuedRefCallbacks();
|
|
983
|
-
};
|
|
984
|
-
|
|
985
|
-
/**
|
|
986
|
-
* @type {import('htmlfy').Config}
|
|
987
|
-
*/
|
|
988
|
-
const CONFIG = {
|
|
989
|
-
content_wrap: 0,
|
|
990
|
-
ignore: [],
|
|
991
|
-
ignore_with: '!i-£___£%_',
|
|
992
|
-
strict: false,
|
|
993
|
-
tab_size: 2,
|
|
994
|
-
tag_wrap: 0,
|
|
995
|
-
trim: []
|
|
996
|
-
};
|
|
997
|
-
|
|
998
|
-
const VOID_ELEMENTS = [
|
|
999
|
-
'area', 'base', 'br', 'col', 'embed', 'hr',
|
|
1000
|
-
'img', 'input', 'link', 'meta',
|
|
1001
|
-
'param', 'source', 'track', 'wbr'
|
|
1002
|
-
];
|
|
1003
|
-
|
|
1004
|
-
/**
|
|
1005
|
-
* Defined by state.js and configuration.
|
|
1006
|
-
*
|
|
1007
|
-
* CONTENT_IGNORE_PLACEHOLDER
|
|
1008
|
-
* SELF_CLOSING_PLACEHOLDER
|
|
1009
|
-
* ATTRIBUTE_IGNORE_PLACEHOLDER
|
|
1010
|
-
*/
|
|
1011
|
-
|
|
1012
|
-
/**
|
|
1013
|
-
* @typedef {object} Constants
|
|
1014
|
-
* @property {string} CONTENT_IGNORE_PLACEHOLDER
|
|
1015
|
-
* @property {string} SELF_CLOSING_PLACEHOLDER
|
|
1016
|
-
* @property {string} ATTRIBUTE_IGNORE_PLACEHOLDER
|
|
1017
|
-
*/
|
|
1018
|
-
/**
|
|
1019
|
-
* @typedef {object} State
|
|
1020
|
-
* @property {boolean} checked_html - If passed in HTML has been checked for HTML within it.
|
|
1021
|
-
* @property {import("htmlfy").Config} config - Validated configuration.
|
|
1022
|
-
* @property {boolean} ignored
|
|
1023
|
-
* @property {Constants} constants - Constant strings, influenced by ignore_with.
|
|
1024
|
-
*/
|
|
1025
|
-
|
|
1026
|
-
/**
|
|
1027
|
-
* @type State
|
|
1028
|
-
*
|
|
1029
|
-
* `constants` prefixes and suffixes must be in sync with those in utils.js
|
|
1030
|
-
*/
|
|
1031
|
-
const state = {
|
|
1032
|
-
checked_html: false,
|
|
1033
|
-
config: { ...CONFIG },
|
|
1034
|
-
ignored: false,
|
|
1035
|
-
constants: {
|
|
1036
|
-
CONTENT_IGNORE_PLACEHOLDER: `${CONFIG.ignore_with}_`,
|
|
1037
|
-
SELF_CLOSING_PLACEHOLDER: `${CONFIG.ignore_with}/_>`,
|
|
1038
|
-
ATTRIBUTE_IGNORE_PLACEHOLDER: `${CONFIG.ignore_with}=_`
|
|
1039
|
-
}
|
|
1040
|
-
};
|
|
1041
|
-
|
|
1042
|
-
/**
|
|
1043
|
-
*
|
|
1044
|
-
* @returns {State}
|
|
1045
|
-
*/
|
|
1046
|
-
const getState = () => state;
|
|
1047
|
-
|
|
1048
|
-
/**
|
|
1049
|
-
*
|
|
1050
|
-
* @param {Partial<State>} new_state
|
|
1051
|
-
*/
|
|
1052
|
-
const setState = (new_state) => Object.assign(state, new_state);
|
|
1053
|
-
|
|
1054
|
-
/**
|
|
1055
|
-
* Checks if content contains at least one HTML element or custom HTML element.
|
|
1056
|
-
*
|
|
1057
|
-
* The first regex matches void and self-closing elements.
|
|
1058
|
-
* The second regex matches normal HTML elements, plus they can have a namespace.
|
|
1059
|
-
* The third regex matches custom HTML elemtns, plus they can have a namespace.
|
|
1060
|
-
*
|
|
1061
|
-
* HTML elements should begin with a letter, and can end with a letter or number.
|
|
1062
|
-
*
|
|
1063
|
-
* Custom elements must begin with a letter, and can end with a letter, number,
|
|
1064
|
-
* hyphen, underscore, or period. However, all letters must be lowercase.
|
|
1065
|
-
* They must have at least one hyphen, and can only have periods and underscores if there is a hyphen.
|
|
1066
|
-
*
|
|
1067
|
-
* These regexes are based on
|
|
1068
|
-
* https://w3c.github.io/html-reference/syntax.html#tag-name
|
|
1069
|
-
* and
|
|
1070
|
-
* https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
|
|
1071
|
-
* respectively.
|
|
1072
|
-
*
|
|
1073
|
-
* @param {string} content Content to evaluate.
|
|
1074
|
-
* @returns {boolean} A boolean.
|
|
1075
|
-
*/
|
|
1076
|
-
const isHtml = (content) => {
|
|
1077
|
-
setState({ checked_html: true });
|
|
1078
|
-
|
|
1079
|
-
return /<(?:[A-Za-z]+[A-Za-z0-9]*)(?:\s+.*?)*?\/{0,1}>/.test(content) ||
|
|
1080
|
-
/<(?<Element>(?:[A-Za-z]+[A-Za-z0-9]*:)?(?:[A-Za-z]+[A-Za-z0-9]*))(?:\s+.*?)*?>(?:.|\n)*?<\/{1}\k<Element>>/.test(content) ||
|
|
1081
|
-
/<(?<Element>(?:[a-z][a-z0-9._]*:)?[a-z][a-z0-9._]*-[a-z0-9._-]+)(?:\s+.*?)*?>(?:.|\n)*?<\/{1}\k<Element>>/.test(content)
|
|
1082
|
-
};
|
|
1083
|
-
|
|
1084
|
-
/**
|
|
1085
|
-
* Generic utility which merges two objects.
|
|
1086
|
-
*
|
|
1087
|
-
* @param {any} current Original object.
|
|
1088
|
-
* @param {any} updates Object to merge with original.
|
|
1089
|
-
* @returns {any}
|
|
1090
|
-
*/
|
|
1091
|
-
const mergeObjects = (current, updates) => {
|
|
1092
|
-
if (!current || !updates)
|
|
1093
|
-
throw new Error("Both 'current' and 'updates' must be passed-in to mergeObjects()")
|
|
1094
|
-
|
|
1095
|
-
/**
|
|
1096
|
-
* @type {any}
|
|
1097
|
-
*/
|
|
1098
|
-
let merged;
|
|
1099
|
-
|
|
1100
|
-
if (Array.isArray(current)) {
|
|
1101
|
-
merged = structuredClone(current).concat(updates);
|
|
1102
|
-
} else if (typeof current === 'object') {
|
|
1103
|
-
merged = { ...current };
|
|
1104
|
-
for (let key of Object.keys(updates)) {
|
|
1105
|
-
if (typeof updates[key] !== 'object') {
|
|
1106
|
-
merged[key] = updates[key];
|
|
1107
|
-
} else {
|
|
1108
|
-
/* key is an object, run mergeObjects again. */
|
|
1109
|
-
merged[key] = mergeObjects(merged[key] || {}, updates[key]);
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
|
-
return merged
|
|
1115
|
-
};
|
|
1116
|
-
|
|
1117
|
-
/**
|
|
1118
|
-
* Merge a user config with the default config.
|
|
1119
|
-
*
|
|
1120
|
-
* @param {import('htmlfy').Config} default_config The default config.
|
|
1121
|
-
* @param {import('htmlfy').UserConfig} config The user config.
|
|
1122
|
-
* @returns {import('htmlfy').Config}
|
|
1123
|
-
*/
|
|
1124
|
-
const mergeConfig = (default_config, config) => {
|
|
1125
|
-
const validated_config = mergeObjects(default_config, config);
|
|
1126
|
-
|
|
1127
|
-
/* Below `constants` prefixes and suffixes must be in sync with those in state.js */
|
|
1128
|
-
setState({
|
|
1129
|
-
config: validated_config,
|
|
1130
|
-
constants: {
|
|
1131
|
-
CONTENT_IGNORE_PLACEHOLDER: `${validated_config.ignore_with}_`,
|
|
1132
|
-
SELF_CLOSING_PLACEHOLDER: `${validated_config.ignore_with}/_>`,
|
|
1133
|
-
ATTRIBUTE_IGNORE_PLACEHOLDER: `${validated_config.ignore_with}=_`
|
|
1134
|
-
}
|
|
1135
|
-
});
|
|
1136
|
-
return validated_config
|
|
1137
|
-
};
|
|
1138
|
-
|
|
1139
|
-
/**
|
|
1140
|
-
*
|
|
1141
|
-
* @param {string} html
|
|
1142
|
-
*/
|
|
1143
|
-
const protectAttributes = (html) => {
|
|
1144
|
-
const { constants } = getState();
|
|
1145
|
-
|
|
1146
|
-
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1147
|
-
return match.replace(capture, (match) => {
|
|
1148
|
-
return match
|
|
1149
|
-
.replace(/\n/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'nl!')
|
|
1150
|
-
.replace(/\r/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'cr!')
|
|
1151
|
-
.replace(/\s/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'ws!')
|
|
1152
|
-
})
|
|
1153
|
-
});
|
|
1154
|
-
|
|
1155
|
-
return html
|
|
1156
|
-
};
|
|
1157
|
-
|
|
1158
|
-
/**
|
|
1159
|
-
*
|
|
1160
|
-
* @param {string} html
|
|
1161
|
-
*/
|
|
1162
|
-
const protectContent = (html) => {
|
|
1163
|
-
const { constants } = getState();
|
|
1164
|
-
|
|
1165
|
-
return html
|
|
1166
|
-
.replace(/\n/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!')
|
|
1167
|
-
.replace(/\r/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!')
|
|
1168
|
-
.replace(/\s/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'ws!')
|
|
1169
|
-
};
|
|
1170
|
-
|
|
1171
|
-
/**
|
|
1172
|
-
*
|
|
1173
|
-
* @param {string} html
|
|
1174
|
-
*/
|
|
1175
|
-
const finalProtectContent = (html) => {
|
|
1176
|
-
const regex = /\s*<([a-zA-Z0-9:-]+)[^>]*>\n\s*<\/\1>(?=\n[ ]*[^\n]*__!i-£___£%__[^\n]*\n)(\n[ ]*\S[^\n]*\n)|<([a-zA-Z0-9:-]+)[^>]*>(?=\n[ ]*[^\n]*__!i-£___£%__[^\n]*\n)(\n[ ]*\S[^\n]*\n\s*)<\/\3>/g;
|
|
1177
|
-
const { constants } = getState();
|
|
1178
|
-
|
|
1179
|
-
return html
|
|
1180
|
-
.replace(regex, (/** @type {string} */match, p1, p2, p3, p4) => {
|
|
1181
|
-
const text_to_protect = p2 || p4;
|
|
1182
|
-
|
|
1183
|
-
if (!text_to_protect)
|
|
1184
|
-
return match
|
|
1185
|
-
|
|
1186
|
-
const protected_text = text_to_protect
|
|
1187
|
-
.replace(/\n/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!')
|
|
1188
|
-
.replace(/\r/g, constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!')
|
|
1189
|
-
.replace(/\s/g, constants.CONTENT_IGNORE_PLACEHOLDER + "ws!");
|
|
1190
|
-
|
|
1191
|
-
return match.replace(text_to_protect, protected_text)
|
|
1192
|
-
})
|
|
1193
|
-
};
|
|
1194
|
-
|
|
1195
|
-
/**
|
|
1196
|
-
* Replace html brackets with ignore string.
|
|
1197
|
-
*
|
|
1198
|
-
* @param {string} html
|
|
1199
|
-
* @returns {string}
|
|
1200
|
-
*/
|
|
1201
|
-
const setIgnoreAttribute = (html) => {
|
|
1202
|
-
const regex = /<([A-Za-z][A-Za-z0-9]*|[a-z][a-z0-9._]*-[a-z0-9._-]+)((?:\s+[A-Za-z0-9_-]+="[^"]*"|\s*[a-z]*)*)>/g;
|
|
1203
|
-
const { constants } = getState();
|
|
1204
|
-
|
|
1205
|
-
html = html.replace(regex, (/** @type {string} */match, p1, p2) => {
|
|
1206
|
-
return match.replace(p2, (match) => {
|
|
1207
|
-
return match
|
|
1208
|
-
.replace(/</g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'lt!')
|
|
1209
|
-
.replace(/>/g, constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'gt!')
|
|
1210
|
-
})
|
|
1211
|
-
});
|
|
1212
|
-
|
|
1213
|
-
return html
|
|
1214
|
-
};
|
|
1215
|
-
|
|
1216
|
-
/**
|
|
1217
|
-
* Trim leading and trailing whitespace characters.
|
|
1218
|
-
*
|
|
1219
|
-
* @param {string} html
|
|
1220
|
-
* @param {string[]} trim
|
|
1221
|
-
* @returns {string}
|
|
1222
|
-
*/
|
|
1223
|
-
const trimify = (html, trim) => {
|
|
1224
|
-
for (let e = 0; e < trim.length; e++) {
|
|
1225
|
-
/* Whitespace character must be escaped with '\' or RegExp() won't include it. */
|
|
1226
|
-
const leading_whitespace = new RegExp(`(<${trim[e]}[^>]*>)\\s+`, "g");
|
|
1227
|
-
const trailing_whitespace = new RegExp(`\\s+(</${trim[e]}>)`, "g");
|
|
1228
|
-
|
|
1229
|
-
html = html
|
|
1230
|
-
.replace(leading_whitespace, '$1')
|
|
1231
|
-
.replace(trailing_whitespace, '$1');
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
return html
|
|
1235
|
-
};
|
|
1236
|
-
|
|
1237
|
-
/**
|
|
1238
|
-
*
|
|
1239
|
-
* @param {string} html
|
|
1240
|
-
*/
|
|
1241
|
-
const unprotectAttributes = (html) => {
|
|
1242
|
-
const { constants } = getState();
|
|
1243
|
-
|
|
1244
|
-
html = html.replace(/<[\w:\-]+([^>]*[^\/])>/g, (/** @type {string} */match, /** @type {any} */capture) => {
|
|
1245
|
-
return match.replace(capture, (match) => {
|
|
1246
|
-
return match
|
|
1247
|
-
.replace(new RegExp(constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'nl!', "g"), '\n')
|
|
1248
|
-
.replace(new RegExp(constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'cr!', "g"), '\r')
|
|
1249
|
-
.replace(new RegExp(constants.ATTRIBUTE_IGNORE_PLACEHOLDER + 'ws!', "g"), ' ')
|
|
1250
|
-
})
|
|
1251
|
-
});
|
|
1252
|
-
|
|
1253
|
-
return html
|
|
1254
|
-
};
|
|
1255
|
-
|
|
1256
|
-
/**
|
|
1257
|
-
*
|
|
1258
|
-
* @param {string} html
|
|
1259
|
-
*/
|
|
1260
|
-
const unprotectContent = (html) => {
|
|
1261
|
-
const { constants } = getState();
|
|
1262
|
-
|
|
1263
|
-
html = html.replace(new RegExp(`.*${constants.CONTENT_IGNORE_PLACEHOLDER}[a-z]{2}!.*`, "g"), (/** @type {string} */match) => {
|
|
1264
|
-
return match.replace(new RegExp(`${constants.CONTENT_IGNORE_PLACEHOLDER}[a-z]{2}!`, "g"), (match) => {
|
|
1265
|
-
return match
|
|
1266
|
-
.replace(new RegExp(constants.CONTENT_IGNORE_PLACEHOLDER + 'nl!', "g"), '\n')
|
|
1267
|
-
.replace(new RegExp(constants.CONTENT_IGNORE_PLACEHOLDER + 'cr!', "g"), '\r')
|
|
1268
|
-
.replace(new RegExp(constants.CONTENT_IGNORE_PLACEHOLDER + 'ws!', "g"), ' ')
|
|
1269
|
-
})
|
|
1270
|
-
});
|
|
1271
|
-
|
|
1272
|
-
return html
|
|
1273
|
-
};
|
|
1274
|
-
|
|
1275
|
-
/**
|
|
1276
|
-
* Replace ignore string with html brackets.
|
|
1277
|
-
*
|
|
1278
|
-
* @param {string} html
|
|
1279
|
-
* @returns {string}
|
|
1280
|
-
*/
|
|
1281
|
-
const unsetIgnoreAttribute = (html) => {
|
|
1282
|
-
/* Regex to find opening tags and capture their attributes. */
|
|
1283
|
-
const tagRegex = /<([\w:\-]+)([^>]*)>/g;
|
|
1284
|
-
const { constants } = getState();
|
|
1285
|
-
const escapedIgnoreString = constants.ATTRIBUTE_IGNORE_PLACEHOLDER.replace(
|
|
1286
|
-
/[-\/\\^$*+?.()|[\]{}]/g,
|
|
1287
|
-
"\\$&"
|
|
1288
|
-
);
|
|
1289
|
-
const ltPlaceholderRegex = new RegExp(escapedIgnoreString + "lt!", "g");
|
|
1290
|
-
const gtPlaceholderRegex = new RegExp(escapedIgnoreString + "gt!", "g");
|
|
1291
|
-
|
|
1292
|
-
return html.replace(
|
|
1293
|
-
tagRegex,
|
|
1294
|
-
(
|
|
1295
|
-
/** @type {string} */ fullMatch,
|
|
1296
|
-
/** @type {string} */ tagName,
|
|
1297
|
-
/** @type {string} */ attributesCapture
|
|
1298
|
-
) => {
|
|
1299
|
-
const processedAttributes = attributesCapture
|
|
1300
|
-
.replace(ltPlaceholderRegex, "<")
|
|
1301
|
-
.replace(gtPlaceholderRegex, ">");
|
|
1302
|
-
|
|
1303
|
-
/* Reconstruct the tag. */
|
|
1304
|
-
return `<${tagName}${processedAttributes}>`
|
|
1305
|
-
}
|
|
1306
|
-
)
|
|
1307
|
-
};
|
|
1308
|
-
|
|
1309
|
-
/**
|
|
1310
|
-
* Validate any passed-in config options and merge with CONFIG.
|
|
1311
|
-
*
|
|
1312
|
-
* @param {import('htmlfy').UserConfig} config A user config.
|
|
1313
|
-
* @returns {import('htmlfy').Config} A validated config.
|
|
1314
|
-
*/
|
|
1315
|
-
const validateConfig = (config) => {
|
|
1316
|
-
if (typeof config !== 'object') throw new Error('Config must be an object.')
|
|
1317
|
-
|
|
1318
|
-
const default_config = { ...CONFIG };
|
|
1319
|
-
|
|
1320
|
-
const config_empty = !(
|
|
1321
|
-
Object.hasOwn(config, 'content_wrap') ||
|
|
1322
|
-
Object.hasOwn(config, 'ignore') ||
|
|
1323
|
-
Object.hasOwn(config, 'ignore_with') ||
|
|
1324
|
-
Object.hasOwn(config, 'strict') ||
|
|
1325
|
-
Object.hasOwn(config, 'tab_size') ||
|
|
1326
|
-
Object.hasOwn(config, 'tag_wrap') ||
|
|
1327
|
-
Object.hasOwn(config, 'trim')
|
|
1328
|
-
);
|
|
1329
|
-
|
|
1330
|
-
if (config_empty) {
|
|
1331
|
-
setState({ config: default_config });
|
|
1332
|
-
return default_config
|
|
1333
|
-
}
|
|
1334
|
-
|
|
1335
|
-
let tab_size = config.tab_size;
|
|
1336
|
-
|
|
1337
|
-
if (tab_size) {
|
|
1338
|
-
if (typeof tab_size !== 'number') throw new Error(`tab_size must be a number, not ${typeof config.tab_size}.`)
|
|
1339
|
-
|
|
1340
|
-
const safe = Number.isSafeInteger(tab_size);
|
|
1341
|
-
if (!safe) throw new Error(`Tab size ${tab_size} is not safe. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger for more info.`)
|
|
1342
|
-
|
|
1343
|
-
/**
|
|
1344
|
-
* Round down, just in case a safe floating point,
|
|
1345
|
-
* like 4.0, was passed.
|
|
1346
|
-
*/
|
|
1347
|
-
tab_size = Math.floor(tab_size);
|
|
1348
|
-
if (tab_size < 1 || tab_size > 16) throw new Error('Tab size out of range. Expecting 1 to 16.')
|
|
1349
|
-
|
|
1350
|
-
config.tab_size = tab_size;
|
|
1351
|
-
}
|
|
1352
|
-
|
|
1353
|
-
if (Object.hasOwn(config, 'content_wrap') && typeof config.content_wrap !== 'number')
|
|
1354
|
-
throw new Error(`content_wrap config must be a number, not ${typeof config.content_wrap}.`)
|
|
1355
|
-
|
|
1356
|
-
if (Object.hasOwn(config, 'ignore') && (!Array.isArray(config.ignore) || !config.ignore?.every((e) => typeof e === 'string')))
|
|
1357
|
-
throw new Error('Ignore config must be an array of strings.')
|
|
1358
|
-
|
|
1359
|
-
if (Object.hasOwn(config, 'ignore_with')) {
|
|
1360
|
-
if (typeof config.ignore_with !== 'string')
|
|
1361
|
-
throw new Error(`ignore_with must be a string, not ${typeof config.ignore_with}.`)
|
|
1362
|
-
else if (config.ignore_with.startsWith('_'))
|
|
1363
|
-
/**
|
|
1364
|
-
* This negatively affects processing of preserved tag attributes,
|
|
1365
|
-
* because tag names can end with an underscore, so the regex
|
|
1366
|
-
* does not capture them.
|
|
1367
|
-
*/
|
|
1368
|
-
throw new Error(`ignore_with cannot start with an underscore.`)
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
if (Object.hasOwn(config, 'strict') && typeof config.strict !== 'boolean')
|
|
1372
|
-
throw new Error(`Strict config must be a boolean, not ${typeof config.strict}.`)
|
|
1373
|
-
|
|
1374
|
-
if (Object.hasOwn(config, 'tag_wrap') && typeof config.tag_wrap !== 'number')
|
|
1375
|
-
throw new Error(`tag_wrap config must be a number, not ${typeof config.tag_wrap}.`)
|
|
1376
|
-
|
|
1377
|
-
if (Object.hasOwn(config, 'trim') && (!Array.isArray(config.trim) || !config.trim?.every((e) => typeof e === 'string')))
|
|
1378
|
-
throw new Error('Trim config must be an array of strings.')
|
|
1379
|
-
|
|
1380
|
-
return mergeConfig(default_config, config)
|
|
1381
|
-
|
|
1382
|
-
};
|
|
1383
|
-
|
|
1384
|
-
/**
|
|
1385
|
-
*
|
|
1386
|
-
* @param {string} text
|
|
1387
|
-
* @param {number} width
|
|
1388
|
-
* @param {string} indent
|
|
1389
|
-
*/
|
|
1390
|
-
const wordWrap = (text, width, indent) => {
|
|
1391
|
-
const words = text.trim().split(/\s+/);
|
|
1392
|
-
|
|
1393
|
-
if (words.length === 0 || (words.length === 1 && words[0] === ''))
|
|
1394
|
-
return ""
|
|
1395
|
-
|
|
1396
|
-
const lines = [];
|
|
1397
|
-
let current_line = "";
|
|
1398
|
-
const padding_string = indent;
|
|
1399
|
-
|
|
1400
|
-
words.forEach((word) => {
|
|
1401
|
-
if (word === "") return
|
|
1402
|
-
|
|
1403
|
-
if (word.length >= width) {
|
|
1404
|
-
/* If there's content on the current line, push it first with correct padding. */
|
|
1405
|
-
if (current_line !== "")
|
|
1406
|
-
lines.push(lines.length === 0 ? indent + current_line : padding_string + current_line);
|
|
1407
|
-
|
|
1408
|
-
/* Push a long word on its own line with correct padding. */
|
|
1409
|
-
lines.push(lines.length === 0 ? indent + word : padding_string + word);
|
|
1410
|
-
current_line = ""; // Reset current line
|
|
1411
|
-
return // Move to the next word
|
|
1412
|
-
}
|
|
1413
|
-
|
|
1414
|
-
/* Check if adding the next word exceeds the wrap width. */
|
|
1415
|
-
const test_line = current_line === "" ? word : current_line + " " + word;
|
|
1416
|
-
|
|
1417
|
-
if (test_line.length <= width) {
|
|
1418
|
-
current_line = test_line;
|
|
1419
|
-
} else {
|
|
1420
|
-
/* Word doesn't fit, finish the current line and push it. */
|
|
1421
|
-
if (current_line !== "") {
|
|
1422
|
-
/* Add padding based on whether it's the first line added or not. */
|
|
1423
|
-
lines.push(lines.length === 0 ? indent + current_line : padding_string + current_line);
|
|
1424
|
-
}
|
|
1425
|
-
/* Start a new line with the current word. */
|
|
1426
|
-
current_line = word;
|
|
1427
|
-
}
|
|
1428
|
-
});
|
|
1429
|
-
|
|
1430
|
-
/* Add the last remaining line with appropriate padding. */
|
|
1431
|
-
if (current_line !== "")
|
|
1432
|
-
lines.push(lines.length === 0 ? indent + current_line : padding_string + current_line);
|
|
1433
|
-
|
|
1434
|
-
const result = lines.join("\n");
|
|
1435
|
-
|
|
1436
|
-
return protectContent(result)
|
|
1437
|
-
};
|
|
1438
|
-
|
|
1439
|
-
/**
|
|
1440
|
-
* Extract any HTML blocks to be ignored,
|
|
1441
|
-
* and replace them with a placeholder
|
|
1442
|
-
* for re-insertion later.
|
|
1443
|
-
*
|
|
1444
|
-
* @param {string} html
|
|
1445
|
-
* @returns {{ html_with_markers: string, extracted_map: Map<any,any> }}
|
|
1446
|
-
*/
|
|
1447
|
-
function extractIgnoredBlocks(html) {
|
|
1448
|
-
setState({ ignored: true });
|
|
1449
|
-
const config = (getState()).config;
|
|
1450
|
-
let current_html = html;
|
|
1451
|
-
const extracted_blocks = new Map();
|
|
1452
|
-
let marker_id = 0;
|
|
1453
|
-
const MARKER_PREFIX = "___HTMLFY_SPECIAL_IGNORE_MARKER_";
|
|
1454
|
-
|
|
1455
|
-
for (const tag of config.ignore) {
|
|
1456
|
-
/* Ensure tag is escaped if it can contain regex special chars. */
|
|
1457
|
-
const safe_tag_name = tag.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
1458
|
-
|
|
1459
|
-
const regex = new RegExp(
|
|
1460
|
-
`(<\\s*${safe_tag_name}[^>]*>)(.*?)(<\\s*\/\\s*${safe_tag_name}\\s*>)`,
|
|
1461
|
-
"gs" // global and dotAll
|
|
1462
|
-
);
|
|
1463
|
-
|
|
1464
|
-
/** @type RegExpExecArray | null */
|
|
1465
|
-
let match;
|
|
1466
|
-
|
|
1467
|
-
/**
|
|
1468
|
-
* @type {{ start: number; end: number; marker: string }[]}
|
|
1469
|
-
*/
|
|
1470
|
-
const replacements = [];
|
|
1471
|
-
|
|
1472
|
-
while ((match = regex.exec(current_html)) !== null) {
|
|
1473
|
-
const marker = `${MARKER_PREFIX}${marker_id++}___`;
|
|
1474
|
-
|
|
1475
|
-
/* Only store content, and minify tags later. */
|
|
1476
|
-
extracted_blocks.set(marker, match[2]);
|
|
1477
|
-
|
|
1478
|
-
replacements.push({
|
|
1479
|
-
start: match.index + match[1].length, // start of content
|
|
1480
|
-
end: match.index + match[1].length + match[2].length, // end of content
|
|
1481
|
-
marker: marker,
|
|
1482
|
-
});
|
|
1483
|
-
}
|
|
1484
|
-
|
|
1485
|
-
/* Apply replacements from the end to the beginning to keep indices valid. */
|
|
1486
|
-
for (let i = replacements.length - 1; i >= 0; i--) {
|
|
1487
|
-
const rep = replacements[i];
|
|
1488
|
-
current_html =
|
|
1489
|
-
current_html.substring(0, rep.start) +
|
|
1490
|
-
rep.marker +
|
|
1491
|
-
current_html.substring(rep.end);
|
|
1492
|
-
}
|
|
1493
|
-
}
|
|
1494
|
-
|
|
1495
|
-
return { html_with_markers: current_html, extracted_map: extracted_blocks }
|
|
1496
|
-
}
|
|
1497
|
-
|
|
1498
|
-
/**
|
|
1499
|
-
* Re-insert ignored HTML blocks.
|
|
1500
|
-
*
|
|
1501
|
-
* @param {string} html_with_markers
|
|
1502
|
-
* @param {Map<any,any>} extracted_map
|
|
1503
|
-
* @returns
|
|
1504
|
-
*/
|
|
1505
|
-
function reinsertIgnoredBlocks(html_with_markers, extracted_map) {
|
|
1506
|
-
setState({ ignored: false });
|
|
1507
|
-
let final_html = html_with_markers;
|
|
1508
|
-
|
|
1509
|
-
for (const [marker, original_block] of extracted_map) {
|
|
1510
|
-
final_html = final_html.split(marker).join(original_block);
|
|
1511
|
-
}
|
|
1512
|
-
return final_html
|
|
1513
|
-
}
|
|
1514
|
-
|
|
1515
|
-
const void_element_regex = new RegExp(`<(${VOID_ELEMENTS.join("|")})(?:\\s(?:[^/>]|/(?!>))*)*>`, 'g');
|
|
1516
|
-
|
|
1517
|
-
/**
|
|
1518
|
-
* Add a placeholder for void elements that are not self-closing.
|
|
1519
|
-
* This is for internal processing only.
|
|
1520
|
-
*
|
|
1521
|
-
* @param {string} html
|
|
1522
|
-
* @returns
|
|
1523
|
-
*/
|
|
1524
|
-
function setSelfClosing(html) {
|
|
1525
|
-
const { constants } = getState();
|
|
1526
|
-
|
|
1527
|
-
return html.replace(
|
|
1528
|
-
// match only void elements that are not self-closing
|
|
1529
|
-
void_element_regex,
|
|
1530
|
-
match => match.replace(/>$/, constants.SELF_CLOSING_PLACEHOLDER)
|
|
1531
|
-
)
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
/**
|
|
1535
|
-
* Remove internal placeholder for non-native self-closing void elements.
|
|
1536
|
-
*
|
|
1537
|
-
* @param {string} html
|
|
1538
|
-
* @returns
|
|
1539
|
-
*/
|
|
1540
|
-
function unsetSelfClosing(html) {
|
|
1541
|
-
const { constants } = getState();
|
|
1542
|
-
|
|
1543
|
-
return html.replace(constants.SELF_CLOSING_PLACEHOLDER, ">")
|
|
1544
|
-
}
|
|
1545
|
-
|
|
1546
|
-
/**
|
|
1547
|
-
* Enforce entity characters for textarea content.
|
|
1548
|
-
* To also minifiy tags, pass `minify` as `true`.
|
|
1549
|
-
*
|
|
1550
|
-
* @param {string} html The HTML string to evaluate.
|
|
1551
|
-
* @param {boolean} [minify] Minifies the textarea tags themselves.
|
|
1552
|
-
* Defaults to `false`. We recommend a value of `true` if you're running `entify()`
|
|
1553
|
-
* as a standalone function.
|
|
1554
|
-
* @returns {string}
|
|
1555
|
-
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1556
|
-
* @example With minify.
|
|
1557
|
-
* <textarea >3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1558
|
-
*/
|
|
1559
|
-
const entify = (html, minify = false) => {
|
|
1560
|
-
/**
|
|
1561
|
-
* Use entities inside textarea content.
|
|
1562
|
-
*/
|
|
1563
|
-
html = html.replace(/<\s*textarea[^>]*>((.|\n)*?)<s*\/\s*textarea\s*>/g, (match, capture) => {
|
|
1564
|
-
return match.replace(capture, (match) => {
|
|
1565
|
-
return match
|
|
1566
|
-
.replace(/</g, '<')
|
|
1567
|
-
.replace(/>/g, '>')
|
|
1568
|
-
.replace(/"/g, '"')
|
|
1569
|
-
.replace(/'/g, ''')
|
|
1570
|
-
.replace(/\n/g, ' ')
|
|
1571
|
-
.replace(/\r/g, ' ')
|
|
1572
|
-
.replace(/\s/g, ' ')
|
|
1573
|
-
})
|
|
1574
|
-
});
|
|
1575
|
-
|
|
1576
|
-
if (minify) {
|
|
1577
|
-
html = html.replace(/<\s*textarea[^>]*>(.|\n)*?<\s*\/\s*textarea\s*>/g, (match) => {
|
|
1578
|
-
/* This only affects the html tags, since everything else has been entified. */
|
|
1579
|
-
return match
|
|
1580
|
-
.replace(/\s+/g, ' ')
|
|
1581
|
-
.replace(/\s>/g, '>')
|
|
1582
|
-
.replace(/>\s/g, '>')
|
|
1583
|
-
.replace(/\s</g, '<')
|
|
1584
|
-
.replace(/<\s/g, '<')
|
|
1585
|
-
.replace(/<\/\s/g, '<\/')
|
|
1586
|
-
.replace(/class=["']\s/g, (match) => match.replace(/\s/g, ''))
|
|
1587
|
-
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
|
|
1588
|
-
});
|
|
1589
|
-
}
|
|
1590
|
-
|
|
1591
|
-
return html
|
|
1592
|
-
};
|
|
1593
|
-
|
|
1594
|
-
/**
|
|
1595
|
-
* Remove entity characters for textarea content.
|
|
1596
|
-
* Currently internal use only.
|
|
1597
|
-
*
|
|
1598
|
-
* @param {string} html The HTML string to evaluate.
|
|
1599
|
-
* @returns {string}
|
|
1600
|
-
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
|
|
1601
|
-
*/
|
|
1602
|
-
const dentify = (html) => {
|
|
1603
|
-
/**
|
|
1604
|
-
* Remove entities inside textarea content.
|
|
1605
|
-
*/
|
|
1606
|
-
return html = html.replace(/<textarea[^>]*>((.|\n)*?)<\/textarea>/g, (match, capture) => {
|
|
1607
|
-
return match.replace(capture, (match) => {
|
|
1608
|
-
match = match
|
|
1609
|
-
.replace(/</g, '<')
|
|
1610
|
-
.replace(/>/g, '>')
|
|
1611
|
-
.replace(/"/g, '"')
|
|
1612
|
-
.replace(/'/g, "'")
|
|
1613
|
-
.replace(/ /g, '\n')
|
|
1614
|
-
.replace(/ /g, '\r')
|
|
1615
|
-
.replace(/ /g, ' ')
|
|
1616
|
-
// Ensure we collapse consecutive spaces, or they'll be completely removed later.
|
|
1617
|
-
.replace(/\s+/g, ' ');
|
|
1618
|
-
|
|
1619
|
-
return match
|
|
1620
|
-
})
|
|
1621
|
-
})
|
|
1622
|
-
};
|
|
1623
|
-
|
|
1624
|
-
/**
|
|
1625
|
-
* @type {Map<any,any>}
|
|
1626
|
-
*/
|
|
1627
|
-
let ignore_map$1;
|
|
1628
|
-
|
|
1629
|
-
/**
|
|
1630
|
-
* Creates a single-line HTML string
|
|
1631
|
-
* by removing line returns, tabs, and relevant spaces.
|
|
1632
|
-
*
|
|
1633
|
-
* @param {string} html The HTML string to minify.
|
|
1634
|
-
* @param {import('htmlfy').UserConfig} [config] A user configuration object.
|
|
1635
|
-
* @returns {string} A minified HTML string.
|
|
1636
|
-
*/
|
|
1637
|
-
const minify = (html, config) => {
|
|
1638
|
-
let reinsert_ignored = false;
|
|
1639
|
-
const { checked_html, ignored, constants } = getState();
|
|
1640
|
-
|
|
1641
|
-
if (!checked_html && !isHtml(html)) return html
|
|
1642
|
-
|
|
1643
|
-
const validated_config = (getState()).config;
|
|
1644
|
-
const ignore = validated_config.ignore.length > 0;
|
|
1645
|
-
|
|
1646
|
-
/* Extract ignored elements. Skipped if prettify has already ignored blocks. */
|
|
1647
|
-
if (!ignored && ignore) {
|
|
1648
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1649
|
-
html = html_with_markers;
|
|
1650
|
-
ignore_map$1 = extracted_map;
|
|
1651
|
-
reinsert_ignored = true;
|
|
1652
|
-
}
|
|
1653
|
-
|
|
1654
|
-
/**
|
|
1655
|
-
* Ensure textarea content is protected
|
|
1656
|
-
* before general minification.
|
|
1657
|
-
*/
|
|
1658
|
-
html = entify(html, true);
|
|
1659
|
-
|
|
1660
|
-
/* All other minification. */
|
|
1661
|
-
// Remove ALL newlines and tabs explicitly.
|
|
1662
|
-
html = html.replace(/\n|\t/g, '');
|
|
1663
|
-
|
|
1664
|
-
// Remove whitespace ONLY between tags.
|
|
1665
|
-
html = html.replace(/>\s+</g, "><");
|
|
1666
|
-
|
|
1667
|
-
// Collapse any remaining multiple spaces to single spaces.
|
|
1668
|
-
html = html.replace(/ {2,}/g, ' ');
|
|
1669
|
-
|
|
1670
|
-
// Protect space between text content and an opening tag (e.g., "text <a>")
|
|
1671
|
-
html = html.replace(
|
|
1672
|
-
/(\S) (<[a-zA-Z][a-zA-Z0-9_:-]*)/g,
|
|
1673
|
-
`$1___MINIFY-PROTECTED-SPACE___$2`
|
|
1674
|
-
);
|
|
1675
|
-
|
|
1676
|
-
// Protect space between a closing tag and text content (e.g., "</a> text")
|
|
1677
|
-
html = html.replace(
|
|
1678
|
-
/(<\/[a-zA-Z][a-zA-Z0-9_:-]*>) (\S)/g,
|
|
1679
|
-
`$1___MINIFY-PROTECTED-SPACE___$2`
|
|
1680
|
-
);
|
|
1681
|
-
|
|
1682
|
-
// Remove specific single spaces between tags and whitespace within tags.
|
|
1683
|
-
html = html.replace(/ >/g, ">"); // <tag > -> <tag>
|
|
1684
|
-
html = html.replace(/ </g, "<"); // leading space before tag
|
|
1685
|
-
html = html.replace(/> /g, ">"); // trailing space after tag
|
|
1686
|
-
html = html.replace(/< /g, "<"); // < tag> -> <tag>
|
|
1687
|
-
html = html.replace(/<\s+\//g, '</'); // < /tag> -> </tag>
|
|
1688
|
-
html = html.replace(/<\/\s+/g, '</'); // </ tag> -> </tag>
|
|
1689
|
-
|
|
1690
|
-
// Unprotect space around inner tags
|
|
1691
|
-
html = html.replace(new RegExp('___MINIFY-PROTECTED-SPACE___', 'g'), ' ');
|
|
1692
|
-
|
|
1693
|
-
// Trim spaces around equals signs in attributes (run before value trim)
|
|
1694
|
-
// This handles `attr = "value"` -> `attr="value"`
|
|
1695
|
-
html = html.replace(/ = /g, "=");
|
|
1696
|
-
// Consider safer alternatives if needed (e.g., / = "/g, '="')
|
|
1697
|
-
|
|
1698
|
-
// Trim whitespace inside attribute values
|
|
1699
|
-
html = html.replace(
|
|
1700
|
-
/([a-zA-Z0-9_-]+)=(['"])(.*?)\2/g,
|
|
1701
|
-
(match, attr_name, quote, value) => {
|
|
1702
|
-
// value.trim() handles both leading/trailing spaces
|
|
1703
|
-
// and cases where the value is only whitespace (becomes empty string)
|
|
1704
|
-
const trimmed_value = value.trim();
|
|
1705
|
-
return `${attr_name}=${quote}${trimmed_value}${quote}`
|
|
1706
|
-
}
|
|
1707
|
-
);
|
|
1708
|
-
|
|
1709
|
-
// Final trim for the whole string
|
|
1710
|
-
html = html.trim();
|
|
1711
|
-
|
|
1712
|
-
/* Remove protective entities. */
|
|
1713
|
-
html = dentify(html);
|
|
1714
|
-
|
|
1715
|
-
/* Re-insert ignored elements. Skipped unless minify did the ignore. */
|
|
1716
|
-
if (reinsert_ignored) {
|
|
1717
|
-
html = reinsertIgnoredBlocks(html, ignore_map$1);
|
|
1718
|
-
}
|
|
1719
|
-
|
|
1720
|
-
return html
|
|
1721
|
-
};
|
|
1722
|
-
|
|
1723
|
-
/**
|
|
1724
|
-
* @type {{ line: Record<string,string>[] }}
|
|
1725
|
-
*/
|
|
1726
|
-
const convert = {
|
|
1727
|
-
line: []
|
|
1728
|
-
};
|
|
1729
|
-
|
|
1730
|
-
/**
|
|
1731
|
-
* @type {Map<any,any>}
|
|
1732
|
-
*/
|
|
1733
|
-
let ignore_map;
|
|
1734
|
-
|
|
1735
|
-
/**
|
|
1736
|
-
* Isolate tags, content, and comments.
|
|
1737
|
-
*
|
|
1738
|
-
* @param {string} html The HTML string to evaluate.
|
|
1739
|
-
* @example <div>Hello World!</div> =>
|
|
1740
|
-
* [#-# : 0 : <div> : #-#]
|
|
1741
|
-
* Hello World!
|
|
1742
|
-
* [#-# : 1 : </div> : #-#]
|
|
1743
|
-
*/
|
|
1744
|
-
const enqueue = (html) => {
|
|
1745
|
-
convert.line = [];
|
|
1746
|
-
let i = -1;
|
|
1747
|
-
/* Regex to find tags OR text content between tags. */
|
|
1748
|
-
const regex = /(<[^>]+>)|([^<]+)/g;
|
|
1749
|
-
|
|
1750
|
-
html.replace(regex, (match, c1, c2) => {
|
|
1751
|
-
if (c1) {
|
|
1752
|
-
convert.line.push({ type: "tag", value: match });
|
|
1753
|
-
} else if (c2 && c2.trim().length > 0) {
|
|
1754
|
-
/* It's text content (and not just whitespace). */
|
|
1755
|
-
convert.line.push({ type: "text", value: match });
|
|
1756
|
-
}
|
|
1757
|
-
|
|
1758
|
-
i++;
|
|
1759
|
-
return `\n[#-# : ${i} : ${match} : #-#]\n`
|
|
1760
|
-
});
|
|
1761
|
-
};
|
|
1762
|
-
|
|
1763
|
-
/**
|
|
1764
|
-
* Process enqueued content.
|
|
1765
|
-
*
|
|
1766
|
-
* @returns {string}
|
|
1767
|
-
*/
|
|
1768
|
-
const process = () => {
|
|
1769
|
-
const { config, constants } = getState();
|
|
1770
|
-
const step = " ".repeat(config.tab_size);
|
|
1771
|
-
const tag_wrap = config.tag_wrap;
|
|
1772
|
-
const content_wrap = config.content_wrap;
|
|
1773
|
-
const strict = config.strict;
|
|
1774
|
-
|
|
1775
|
-
/* Track current number of indentations needed. */
|
|
1776
|
-
let indents = '';
|
|
1777
|
-
|
|
1778
|
-
/** @type string[] */
|
|
1779
|
-
const output_lines = [];
|
|
1780
|
-
const tag_regex = /<[A-Za-z]+\b[^>]*(?:.|\n)*?\/?>/g; /* Is opening tag or void element. */
|
|
1781
|
-
const attribute_regex = /\s{1}[A-Za-z-]+(?:=".*?")?/g; /* Matches all tag/element attributes. */
|
|
1782
|
-
|
|
1783
|
-
/* Process lines and indent. */
|
|
1784
|
-
convert.line.forEach((source, index) => {
|
|
1785
|
-
let current_line_value = source.value;
|
|
1786
|
-
|
|
1787
|
-
const is_ignored_content =
|
|
1788
|
-
current_line_value.startsWith('___HTMLFY_SPECIAL_IGNORE_MARKER_');
|
|
1789
|
-
|
|
1790
|
-
let subtrahend = 0;
|
|
1791
|
-
const prev_line_data = convert.line[index - 1];
|
|
1792
|
-
const prev_line_value = prev_line_data?.value ?? ""; // Use empty string if no prev line
|
|
1793
|
-
|
|
1794
|
-
/**
|
|
1795
|
-
* Arbitratry character, to keep track of the string's length.
|
|
1796
|
-
*/
|
|
1797
|
-
indents += '0';
|
|
1798
|
-
|
|
1799
|
-
if (index === 0) subtrahend++;
|
|
1800
|
-
/* We're processing a closing tag. */
|
|
1801
|
-
if (current_line_value.trim().startsWith("</")) subtrahend++;
|
|
1802
|
-
/* prevLine is a doctype declaration. */
|
|
1803
|
-
if (prev_line_value.trim().startsWith("<!doctype")) subtrahend++;
|
|
1804
|
-
/* prevLine is a comment. */
|
|
1805
|
-
if (prev_line_value.trim().startsWith("<!--")) subtrahend++;
|
|
1806
|
-
/* prevLine is a void element. */
|
|
1807
|
-
if (
|
|
1808
|
-
prev_line_value.trim().endsWith("/>") // native self-closing
|
|
1809
|
-
||
|
|
1810
|
-
prev_line_value.trim().endsWith(constants.SELF_CLOSING_PLACEHOLDER) // synthetic self-closing
|
|
1811
|
-
) subtrahend++;
|
|
1812
|
-
/* prevLine is a closing tag. */
|
|
1813
|
-
if (prev_line_value.trim().startsWith("</")) subtrahend++;
|
|
1814
|
-
/* prevLine is text. */
|
|
1815
|
-
if (prev_line_data?.type === "text") subtrahend++;
|
|
1816
|
-
|
|
1817
|
-
/* Determine offset for line indentation. */
|
|
1818
|
-
const offset = Math.max(0, indents.length - subtrahend);
|
|
1819
|
-
/* Correct indent level for *this* line's content */
|
|
1820
|
-
const current_indent_level = offset; // Store the level for this line
|
|
1821
|
-
|
|
1822
|
-
indents = indents.substring(0, current_indent_level); // Adjust for *next* round
|
|
1823
|
-
|
|
1824
|
-
/**
|
|
1825
|
-
* Starts with a single punctuation character.
|
|
1826
|
-
* Add punctuation to end of previous line.
|
|
1827
|
-
*/
|
|
1828
|
-
if (source.type === 'text' && /^[!,;\.]/.test(current_line_value)) {
|
|
1829
|
-
if (current_line_value.length === 1) {
|
|
1830
|
-
output_lines[output_lines.length - 1] =
|
|
1831
|
-
output_lines.at(-1) + current_line_value;
|
|
1832
|
-
return
|
|
1833
|
-
} else {
|
|
1834
|
-
output_lines[output_lines.length - 1] =
|
|
1835
|
-
output_lines.at(-1) + current_line_value.charAt(0);
|
|
1836
|
-
current_line_value = current_line_value.slice(1).trim();
|
|
1837
|
-
|
|
1838
|
-
/* If nothing left after extracting punctuation, skip this line. */
|
|
1839
|
-
if (current_line_value.length === 0) return
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
|
|
1843
|
-
const padding = step.repeat(current_indent_level);
|
|
1844
|
-
|
|
1845
|
-
if (is_ignored_content) {
|
|
1846
|
-
/* Stop processing this line, as it's set to be ignored. */
|
|
1847
|
-
output_lines.push(current_line_value);
|
|
1848
|
-
} else {
|
|
1849
|
-
/* Remove comment. */
|
|
1850
|
-
if (strict && current_line_value.trim().startsWith("<!--"))
|
|
1851
|
-
return
|
|
1852
|
-
|
|
1853
|
-
let result = current_line_value;
|
|
1854
|
-
|
|
1855
|
-
/* Remove self-closing placeholder, if needed. */
|
|
1856
|
-
result = unsetSelfClosing(result);
|
|
1857
|
-
|
|
1858
|
-
if (
|
|
1859
|
-
source.type === 'text' &&
|
|
1860
|
-
content_wrap > 0 &&
|
|
1861
|
-
result.length >= content_wrap
|
|
1862
|
-
) {
|
|
1863
|
-
result = wordWrap(result, content_wrap, padding);
|
|
1864
|
-
}
|
|
1865
|
-
/* Wrap the attributes of open tags and void elements. */
|
|
1866
|
-
else if (
|
|
1867
|
-
tag_wrap > 0 &&
|
|
1868
|
-
result.length > tag_wrap &&
|
|
1869
|
-
tag_regex.test(result)
|
|
1870
|
-
) {
|
|
1871
|
-
tag_regex.lastIndex = 0; // Reset stateful regex
|
|
1872
|
-
attribute_regex.lastIndex = 0; // Reset stateful regex
|
|
1873
|
-
|
|
1874
|
-
const tag_parts = result.split(attribute_regex).filter(Boolean);
|
|
1875
|
-
|
|
1876
|
-
if (tag_parts.length >= 2) {
|
|
1877
|
-
const attributes = result.matchAll(attribute_regex);
|
|
1878
|
-
const inner_padding = padding + step;
|
|
1879
|
-
let wrapped_tag = padding + tag_parts[0] + "\n";
|
|
1880
|
-
|
|
1881
|
-
for (const a of attributes) {
|
|
1882
|
-
const attribute_string = a[0].trim();
|
|
1883
|
-
wrapped_tag += inner_padding + attribute_string + "\n";
|
|
1884
|
-
}
|
|
1885
|
-
|
|
1886
|
-
const tag_name_match = tag_parts[0].match(/<([A-Za-z_:-]+)/);
|
|
1887
|
-
const tag_name = tag_name_match ? tag_name_match[1] : "";
|
|
1888
|
-
const is_self_closing = tag_parts.at(-1)?.endsWith("/>") && VOID_ELEMENTS.includes(tag_name);
|
|
1889
|
-
const closing_part = tag_parts[1].trim();
|
|
1890
|
-
const closing_padding = padding + (strict && is_self_closing ? " " : "");
|
|
1891
|
-
|
|
1892
|
-
wrapped_tag += closing_padding + closing_part;
|
|
1893
|
-
|
|
1894
|
-
result = wrapped_tag; // Assign the fully wrapped string
|
|
1895
|
-
} else {
|
|
1896
|
-
result = padding + result;
|
|
1897
|
-
}
|
|
1898
|
-
} else {
|
|
1899
|
-
/* Apply simple indentation (if no wrapping occurred) */
|
|
1900
|
-
result = padding + result;
|
|
1901
|
-
}
|
|
1902
|
-
|
|
1903
|
-
/* Add the processed line (or lines if wordWrap creates them) to the output */
|
|
1904
|
-
output_lines.push(result);
|
|
1905
|
-
}
|
|
1906
|
-
});
|
|
1907
|
-
|
|
1908
|
-
/* Join all processed lines into the final HTML string */
|
|
1909
|
-
let final_html = output_lines.join("\n");
|
|
1910
|
-
|
|
1911
|
-
/* Preserve wrapped attributes. */
|
|
1912
|
-
if (tag_wrap > 0) final_html = protectAttributes(final_html);
|
|
1913
|
-
|
|
1914
|
-
/* Extra preserve wrapped content. */
|
|
1915
|
-
if (content_wrap > 0 && new RegExp(`/\\n[ ]*[^\\n]*${constants.CONTENT_IGNORE_PLACEHOLDER}[^\\n]*\\n/`).test(final_html))
|
|
1916
|
-
final_html = finalProtectContent(final_html);
|
|
1917
|
-
|
|
1918
|
-
/* Remove line returns, tabs, and consecutive spaces within html elements or their content. */
|
|
1919
|
-
final_html = final_html.replace(
|
|
1920
|
-
/<(?<Element>[^>\s]+)[^>]*>[^<]*?[^><\/\s][^<]*?<\/\k<Element>>|<script[^>]*>[\s]*<\/script>|<([\w:\._-]+)([^>]*)><\/\2>|<([\w:\._-]+)([^>]*)>[\s]+<\/\4>/g,
|
|
1921
|
-
match => {
|
|
1922
|
-
// Check if this contains placeholder
|
|
1923
|
-
if (match.includes(constants.SELF_CLOSING_PLACEHOLDER) || match.includes(constants.CONTENT_IGNORE_PLACEHOLDER)) {
|
|
1924
|
-
return match // Don't modify if it contains the placeholder
|
|
1925
|
-
}
|
|
1926
|
-
|
|
1927
|
-
return match.replace(/\n|\t|\s{2,}/g, '')
|
|
1928
|
-
}
|
|
1929
|
-
);
|
|
1930
|
-
|
|
1931
|
-
/* Revert wrapped content. */
|
|
1932
|
-
if (content_wrap > 0) final_html = unprotectContent(final_html);
|
|
1933
|
-
|
|
1934
|
-
/* Revert wrapped attributes. */
|
|
1935
|
-
if (tag_wrap > 0) final_html = unprotectAttributes(final_html);
|
|
1936
|
-
|
|
1937
|
-
/* Remove self-closing nature of void elements. */
|
|
1938
|
-
if (strict) final_html = final_html.replace(/\s\/>|\/>/g, '>');
|
|
1939
|
-
|
|
1940
|
-
/* Trim leading and/or trailing line returns. */
|
|
1941
|
-
if (final_html.startsWith("\n")) final_html = final_html.substring(1);
|
|
1942
|
-
if (final_html.endsWith("\n")) final_html = final_html.substring(0, final_html.length - 1);
|
|
1943
|
-
|
|
1944
|
-
return final_html
|
|
1945
|
-
};
|
|
1946
|
-
|
|
1947
|
-
/**
|
|
1948
|
-
* Format HTML with line returns and indentations.
|
|
1949
|
-
*
|
|
1950
|
-
* @param {string} html The HTML string to prettify.
|
|
1951
|
-
* @param {import('htmlfy').UserConfig} [config] A user configuration object.
|
|
1952
|
-
* @returns {string} A well-formed HTML string.
|
|
1953
|
-
*/
|
|
1954
|
-
const prettify = (html, config) => {
|
|
1955
|
-
let reinsert_ignored = false;
|
|
1956
|
-
const { checked_html, ignored } = getState();
|
|
1957
|
-
|
|
1958
|
-
/* Return content as-is if it does not contain any HTML elements. */
|
|
1959
|
-
if (!checked_html && !isHtml(html)) return html
|
|
1960
|
-
|
|
1961
|
-
/* Runs setState for config. */
|
|
1962
|
-
const validated_config = validateConfig(config || {});
|
|
1963
|
-
|
|
1964
|
-
const ignore = validated_config.ignore.length > 0;
|
|
1965
|
-
|
|
1966
|
-
/* Allows you to trimify before ignoring. */
|
|
1967
|
-
if (validated_config.trim.length > 0) html = trimify(html, validated_config.trim);
|
|
1968
|
-
|
|
1969
|
-
/* Extract ignored elements. */
|
|
1970
|
-
if (!ignored && ignore) {
|
|
1971
|
-
const { html_with_markers, extracted_map } = extractIgnoredBlocks(html);
|
|
1972
|
-
html = html_with_markers;
|
|
1973
|
-
ignore_map = extracted_map;
|
|
1974
|
-
reinsert_ignored = true;
|
|
1975
|
-
}
|
|
1976
|
-
|
|
1977
|
-
/* Preserve html text within attribute values. */
|
|
1978
|
-
html = setIgnoreAttribute(html);
|
|
1979
|
-
|
|
1980
|
-
/* Insert placeholder for void elements that aren't self-closing. */
|
|
1981
|
-
html = setSelfClosing(html);
|
|
1982
|
-
|
|
1983
|
-
html = minify(html);
|
|
1984
|
-
enqueue(html);
|
|
1985
|
-
html = process();
|
|
1986
|
-
|
|
1987
|
-
/* Revert html text within attribute values. */
|
|
1988
|
-
html = unsetIgnoreAttribute(html);
|
|
1989
|
-
|
|
1990
|
-
/* Re-insert ignored elements. */
|
|
1991
|
-
if (reinsert_ignored) {
|
|
1992
|
-
html = reinsertIgnoredBlocks(html, ignore_map);
|
|
1993
|
-
}
|
|
1994
|
-
|
|
1995
|
-
return html
|
|
1996
|
-
};
|
|
1997
|
-
|
|
1998
|
-
/**
|
|
1999
|
-
* Render attribute on the given element
|
|
2000
|
-
* @param element - targeted to render attribute
|
|
2001
|
-
* @param name - of the attribute
|
|
2002
|
-
* @param value - of the attribute
|
|
2003
|
-
*/
|
|
2004
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2005
|
-
const renderAttribute = (element, name, value) => {
|
|
2006
|
-
if ([null, undefined, '', false].includes(value) || ['innerHTML', 'style', ''].includes(name)) {
|
|
2007
|
-
return;
|
|
2008
|
-
}
|
|
2009
|
-
element.setAttribute(name, !['object', 'function'].includes(typeof value) ? value : `⚠️ Property must be set through a script or a framework-specific syntax.`);
|
|
2010
|
-
};
|
|
2011
|
-
/**
|
|
2012
|
-
* Render new element in parent element
|
|
2013
|
-
* @param parentNode - HTML element
|
|
2014
|
-
* @param tagName - of the new element
|
|
2015
|
-
* @param attributes - of the new element
|
|
2016
|
-
* @param children - of the new element
|
|
2017
|
-
* @param text - of the new element
|
|
2018
|
-
*/
|
|
2019
|
-
const renderElement = (parentNode, tagName, attributes, children, text) => {
|
|
2020
|
-
// render HTML
|
|
2021
|
-
if (tagName && typeof tagName === 'string') {
|
|
2022
|
-
const element = document.createElement(tagName);
|
|
2023
|
-
Object.keys(attributes || {}).forEach(attr => {
|
|
2024
|
-
renderAttribute(element, attr, attributes[attr]);
|
|
2025
|
-
});
|
|
2026
|
-
children?.forEach(child => {
|
|
2027
|
-
renderElement(element, child.$tag$, child.$attrs$, child.$children$, child.$text$);
|
|
2028
|
-
});
|
|
2029
|
-
if (attributes?.innerHTML)
|
|
2030
|
-
element.innerHTML = attributes.innerHTML;
|
|
2031
|
-
parentNode.appendChild(element);
|
|
2032
|
-
}
|
|
2033
|
-
// render text
|
|
2034
|
-
if (text) {
|
|
2035
|
-
parentNode.innerHTML = text;
|
|
2036
|
-
}
|
|
2037
|
-
};
|
|
2038
|
-
/**
|
|
2039
|
-
* Filter default argument on component argument to prevent them to be rendered
|
|
2040
|
-
* @param args - all possible args with custom values
|
|
2041
|
-
* @param defaultValues - component default args values
|
|
2042
|
-
* @param slots - slots
|
|
2043
|
-
* @returns filtres args
|
|
2044
|
-
* @example
|
|
2045
|
-
* ```ts
|
|
2046
|
-
* import { filterArgs } from '@mgdis/core-ui-helpers/dist/storybook';
|
|
2047
|
-
* const Template = (args: MgBadgeType): HTMLElement => <mg-badge {...filterArgs(args, { variant: 'info' }, ['actions'])}></mg-badge>;
|
|
2048
|
-
* ```
|
|
2049
|
-
*/
|
|
2050
|
-
const filterArgs = (args, defaultValues, slots = []) => {
|
|
2051
|
-
const filteredArgs = {};
|
|
2052
|
-
if (typeof args !== 'object') {
|
|
2053
|
-
throw new Error("filterArgs - args isn't an object.");
|
|
2054
|
-
}
|
|
2055
|
-
for (const k in args) {
|
|
2056
|
-
if (!slots.includes(k)) {
|
|
2057
|
-
const arg = args[k];
|
|
2058
|
-
// Change camelCase k to kebab-case
|
|
2059
|
-
const key = k.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
2060
|
-
if (!defaultValues || !Object.keys(defaultValues).includes(k) || defaultValues[k] !== arg) {
|
|
2061
|
-
filteredArgs[key] = arg;
|
|
2062
|
-
}
|
|
2063
|
-
}
|
|
2064
|
-
}
|
|
2065
|
-
return filteredArgs;
|
|
2066
|
-
};
|
|
2067
|
-
/**
|
|
2068
|
-
* Storybook stencil wrapper. Used to target element with `storybook-root` id and render virtual DOM inside.
|
|
2069
|
-
* @param storyFn - storybook render function
|
|
2070
|
-
* @param context - storybook context
|
|
2071
|
-
* @returns rendered element
|
|
2072
|
-
* @example
|
|
2073
|
-
* ```ts
|
|
2074
|
-
* // .storybook/preview.ts
|
|
2075
|
-
* import { stencilWrapper } from '@mgdis/core-ui-helpers/dist/storybook';
|
|
2076
|
-
* export const decorators: Preview['decorators'] = [stencilWrapper];
|
|
2077
|
-
* ```
|
|
2078
|
-
*/
|
|
2079
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2080
|
-
const stencilWrapper = (storyFn, context) => {
|
|
2081
|
-
const host = document.getElementById('storybook-root');
|
|
2082
|
-
if (host === null)
|
|
2083
|
-
return;
|
|
2084
|
-
// update local switcher based on context variable
|
|
2085
|
-
document.querySelector('[lang]')?.setAttribute('lang', context.globals?.locale || 'en');
|
|
2086
|
-
renderVdom({
|
|
2087
|
-
$cmpMeta$: {
|
|
2088
|
-
$flags$: 0,
|
|
2089
|
-
$tagName$: host.tagName,
|
|
2090
|
-
},
|
|
2091
|
-
$hostElement$: host,
|
|
2092
|
-
}, storyFn(context));
|
|
2093
|
-
return host.children[host.children.length - 1];
|
|
2094
|
-
};
|
|
2095
|
-
/**
|
|
2096
|
-
* Get story HTML from virtual DOM.
|
|
2097
|
-
* Mainly used to render, component code exemple in stories.
|
|
2098
|
-
* @param vitualNode - story virtual DOM
|
|
2099
|
-
* @returns stringified rendered HTML
|
|
2100
|
-
* @example
|
|
2101
|
-
* ```ts
|
|
2102
|
-
* // .storybook/preview.ts
|
|
2103
|
-
* import { getStoryHTML } from '@mgdis/core-ui-helpers/dist/storybook';
|
|
2104
|
-
*
|
|
2105
|
-
* export const parameters: Preview['parameters'] = {
|
|
2106
|
-
* docs: {
|
|
2107
|
-
* source: {
|
|
2108
|
-
* transform: (_, ctx) => getStoryHTML(ctx.originalStoryFn(ctx.args)),
|
|
2109
|
-
* }
|
|
2110
|
-
* },
|
|
2111
|
-
* };
|
|
2112
|
-
* ```
|
|
2113
|
-
*/
|
|
2114
|
-
const getStoryHTML = ({ $tag$, $attrs$, $children$, $text$ }) => {
|
|
2115
|
-
const host = document.createElement('div');
|
|
2116
|
-
renderElement(host, $tag$, $attrs$, $children$, $text$);
|
|
2117
|
-
return prettify(host.innerHTML, {
|
|
2118
|
-
tag_wrap: 40,
|
|
2119
|
-
content_wrap: 120,
|
|
2120
|
-
}).replace(/="true"/g, '');
|
|
2121
|
-
};
|
|
2122
|
-
/**
|
|
2123
|
-
* Retrieve Component Storybook URL from file path
|
|
2124
|
-
* @param storybookBaseUrl - Storybook Base URL
|
|
2125
|
-
* @param filePath - Component file path
|
|
2126
|
-
* @returns Component Storybook URL
|
|
2127
|
-
*/
|
|
2128
|
-
const getStorybookUrl = (storybookBaseUrl, filePath) => {
|
|
2129
|
-
if (!filePath) {
|
|
2130
|
-
return;
|
|
2131
|
-
}
|
|
2132
|
-
const split = filePath.split('/');
|
|
2133
|
-
return `${storybookBaseUrl}${split.slice(2, split.length - 1).join('-')}--docs`;
|
|
2134
|
-
};
|
|
2135
|
-
class StorybookPreview {
|
|
2136
|
-
/**
|
|
2137
|
-
* JsonDocs
|
|
2138
|
-
*/
|
|
2139
|
-
jsonDoc;
|
|
2140
|
-
constructor(jsonDoc) {
|
|
2141
|
-
this.jsonDoc = jsonDoc;
|
|
2142
|
-
}
|
|
2143
|
-
/**
|
|
2144
|
-
* Get component data from the jsonDoc
|
|
2145
|
-
* @param tagName - tag name we want to get the data from
|
|
2146
|
-
* @returns component data
|
|
2147
|
-
*/
|
|
2148
|
-
#getComponentData = (tagName) => {
|
|
2149
|
-
return this.jsonDoc.components.find(component => component.tag === tagName);
|
|
2150
|
-
};
|
|
2151
|
-
/**
|
|
2152
|
-
* Get the control for the given prop
|
|
2153
|
-
* Based on https://storybook.js.org/docs/api/arg-types#controltype
|
|
2154
|
-
* @param prop - prop to get control for
|
|
2155
|
-
* @returns control type and options if applicable
|
|
2156
|
-
*/
|
|
2157
|
-
#getPropControl = (prop) => {
|
|
2158
|
-
// Get types
|
|
2159
|
-
const types = prop.type
|
|
2160
|
-
.replace(/"([^"]+)"/g, '$1') // Remove quotes
|
|
2161
|
-
.replace(/\s/g, '') // Remove all whitespace for simplicity
|
|
2162
|
-
.replace(/\(.*?\)/g, match => match.replace(/\|/g, ' OR ')) // Replace '|' inside parentheses
|
|
2163
|
-
.split('|')
|
|
2164
|
-
.map(type => type.trim().replace(/ OR /g, '|')); // Revert ' OR ' back to '|'
|
|
2165
|
-
// Return control and options
|
|
2166
|
-
if (prop.type === 'string') {
|
|
2167
|
-
return { control: { type: 'text' } };
|
|
2168
|
-
}
|
|
2169
|
-
else if (prop.type === 'number') {
|
|
2170
|
-
return { control: { type: 'number' } };
|
|
2171
|
-
}
|
|
2172
|
-
else if (prop.type === 'boolean') {
|
|
2173
|
-
return { control: { type: 'boolean' } };
|
|
2174
|
-
}
|
|
2175
|
-
else if (prop.type.startsWith('{') && prop.type.endsWith('}')) {
|
|
2176
|
-
return { control: { type: 'object' } };
|
|
2177
|
-
}
|
|
2178
|
-
else if (types.length > 1) {
|
|
2179
|
-
// Manage case when multiple types are possible
|
|
2180
|
-
if (types.includes('string')) {
|
|
2181
|
-
return { control: { type: 'text' } };
|
|
2182
|
-
}
|
|
2183
|
-
else if (types.every(type => type?.includes('[]'))) {
|
|
2184
|
-
return { control: { type: 'object' } };
|
|
2185
|
-
}
|
|
2186
|
-
else {
|
|
2187
|
-
// Add the posibility to set undefined
|
|
2188
|
-
types.unshift(undefined);
|
|
2189
|
-
return { control: { type: 'select' }, options: types };
|
|
2190
|
-
}
|
|
2191
|
-
}
|
|
2192
|
-
else
|
|
2193
|
-
return { control: { type: 'object' } };
|
|
2194
|
-
};
|
|
2195
|
-
/**
|
|
2196
|
-
* Extract component arg types from the component data
|
|
2197
|
-
* @param tagName - tag name we want to extract the arg types from
|
|
2198
|
-
* @returns component arg types
|
|
2199
|
-
*/
|
|
2200
|
-
extractArgTypes = (tagName) => {
|
|
2201
|
-
const componentData = this.#getComponentData(tagName);
|
|
2202
|
-
// Extract props arg types
|
|
2203
|
-
const componentPropsArgTypes = componentData?.props.reduce((acc, prop) => {
|
|
2204
|
-
// Get Controls
|
|
2205
|
-
const { control, options } = this.#getPropControl(prop);
|
|
2206
|
-
// Set Component ArgTypes
|
|
2207
|
-
return {
|
|
2208
|
-
...acc,
|
|
2209
|
-
[prop.name]: {
|
|
2210
|
-
name: prop.attr || prop.name,
|
|
2211
|
-
description: prop.docs,
|
|
2212
|
-
type: { required: prop.required },
|
|
2213
|
-
table: {
|
|
2214
|
-
category: 'props',
|
|
2215
|
-
type: { summary: prop.type },
|
|
2216
|
-
defaultValue: { summary: prop.default },
|
|
2217
|
-
},
|
|
2218
|
-
control,
|
|
2219
|
-
options,
|
|
2220
|
-
},
|
|
2221
|
-
};
|
|
2222
|
-
}, {});
|
|
2223
|
-
// Extract events arg types
|
|
2224
|
-
const componentEventsArgTypes = componentData?.events.reduce((acc, event) => ({
|
|
2225
|
-
...acc,
|
|
2226
|
-
[event.event]: {
|
|
2227
|
-
name: event.event,
|
|
2228
|
-
description: event.docs,
|
|
2229
|
-
table: {
|
|
2230
|
-
category: 'events',
|
|
2231
|
-
type: { summary: event.detail },
|
|
2232
|
-
},
|
|
2233
|
-
},
|
|
2234
|
-
}), {});
|
|
2235
|
-
// Extracts Methods arg types
|
|
2236
|
-
const componentMethodsArgTypes = componentData?.methods.reduce((acc, method) => ({
|
|
2237
|
-
...acc,
|
|
2238
|
-
[method.name]: {
|
|
2239
|
-
name: method.name,
|
|
2240
|
-
description: method.docs,
|
|
2241
|
-
table: {
|
|
2242
|
-
category: 'methods',
|
|
2243
|
-
type: { summary: method.signature },
|
|
2244
|
-
},
|
|
2245
|
-
},
|
|
2246
|
-
}), {});
|
|
2247
|
-
// Extracts Slots arg types
|
|
2248
|
-
const componentSlotsArgTypes = componentData?.slots.reduce((acc, slot) => ({
|
|
2249
|
-
...acc,
|
|
2250
|
-
[slot.name]: {
|
|
2251
|
-
name: slot.name !== '' ? slot.name : 'default', // default slot are unnamed
|
|
2252
|
-
description: slot.docs,
|
|
2253
|
-
table: {
|
|
2254
|
-
category: 'slots',
|
|
2255
|
-
type: { summary: undefined },
|
|
2256
|
-
},
|
|
2257
|
-
},
|
|
2258
|
-
}), {});
|
|
2259
|
-
// Extracts CSS Properties arg types
|
|
2260
|
-
const componentCSSPropArgTypes = componentData?.styles.reduce((acc, style) => ({
|
|
2261
|
-
...acc,
|
|
2262
|
-
[style.name]: {
|
|
2263
|
-
name: style.name,
|
|
2264
|
-
description: style.docs,
|
|
2265
|
-
table: {
|
|
2266
|
-
category: 'custom properties',
|
|
2267
|
-
type: { summary: undefined },
|
|
2268
|
-
},
|
|
2269
|
-
},
|
|
2270
|
-
}), {});
|
|
2271
|
-
// Extract component dependencies
|
|
2272
|
-
const componentDependencies = componentData?.dependencies.reduce((acc, dependency) => {
|
|
2273
|
-
const dependencyData = this.#getComponentData(dependency);
|
|
2274
|
-
if (!dependencyData)
|
|
2275
|
-
return acc; // Prevents from adding internal dependency
|
|
2276
|
-
return {
|
|
2277
|
-
...acc,
|
|
2278
|
-
[dependency]: {
|
|
2279
|
-
name: dependency,
|
|
2280
|
-
description: `<a href="./?path=/docs/${getStorybookUrl('', dependencyData?.filePath)}">View Component Documentation</a>`,
|
|
2281
|
-
table: {
|
|
2282
|
-
category: 'depends on',
|
|
2283
|
-
type: { summary: undefined },
|
|
2284
|
-
},
|
|
2285
|
-
},
|
|
2286
|
-
};
|
|
2287
|
-
}, {});
|
|
2288
|
-
// Extract dependents components
|
|
2289
|
-
const componentDependents = componentData?.dependents.reduce((acc, dependent) => {
|
|
2290
|
-
const dependentData = this.#getComponentData(dependent);
|
|
2291
|
-
if (!dependentData)
|
|
2292
|
-
return acc; // Prevents from adding internal dependent
|
|
2293
|
-
return {
|
|
2294
|
-
...acc,
|
|
2295
|
-
[dependent]: {
|
|
2296
|
-
name: dependent,
|
|
2297
|
-
description: `<a href="./?path=/docs/${getStorybookUrl('', dependentData?.filePath)}">View Component Documentation</a>`,
|
|
2298
|
-
table: {
|
|
2299
|
-
category: 'used by',
|
|
2300
|
-
type: { summary: undefined },
|
|
2301
|
-
},
|
|
2302
|
-
},
|
|
2303
|
-
};
|
|
2304
|
-
}, {});
|
|
2305
|
-
return {
|
|
2306
|
-
...componentPropsArgTypes,
|
|
2307
|
-
...componentEventsArgTypes,
|
|
2308
|
-
...componentMethodsArgTypes,
|
|
2309
|
-
...componentSlotsArgTypes,
|
|
2310
|
-
...componentCSSPropArgTypes,
|
|
2311
|
-
...componentDependencies,
|
|
2312
|
-
...componentDependents,
|
|
2313
|
-
};
|
|
2314
|
-
};
|
|
2315
|
-
/**
|
|
2316
|
-
* Extract component description from the component data
|
|
2317
|
-
* @param tagName - tag name we want to extract the description from
|
|
2318
|
-
* @returns component description
|
|
2319
|
-
*/
|
|
2320
|
-
extractComponentDescription = (tagName) => {
|
|
2321
|
-
const componentData = this.#getComponentData(tagName);
|
|
2322
|
-
return componentData?.readme || componentData?.docs;
|
|
2323
|
-
};
|
|
2324
|
-
}
|
|
2325
|
-
|
|
2326
|
-
export { StorybookPreview, filterArgs, getStoryHTML, getStorybookUrl, stencilWrapper };
|
|
2327
|
-
//# sourceMappingURL=index.js.map
|