@oscarpalmer/abydon 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ // src/fragment.ts
2
+ import {createTemplate, getNodes} from "./helpers/dom";
3
+ import {parse} from "./html";
4
+ import {mapNodes} from "./node";
5
+
6
+ class Fragment {
7
+ data;
8
+ constructor(strings, expressions) {
9
+ this.data = {
10
+ expressions,
11
+ strings,
12
+ nodes: [],
13
+ values: []
14
+ };
15
+ }
16
+ appendTo(element) {
17
+ element.append(...this.get());
18
+ }
19
+ get() {
20
+ if (this.data.nodes.length === 0) {
21
+ const parsed = parse(this.data);
22
+ const templated = createTemplate(parsed);
23
+ this.data.nodes.splice(0, this.data.nodes.length, ...getNodes(templated));
24
+ mapNodes(this.data, this.data.nodes);
25
+ }
26
+ return [...this.data.nodes];
27
+ }
28
+ remove() {
29
+ const { length } = this.data.nodes;
30
+ for (let index = 0;index < length; index += 1) {
31
+ const node2 = this.data.nodes[index];
32
+ node2.parentNode?.removeChild(node2);
33
+ }
34
+ this.data.nodes.splice(0, length);
35
+ }
36
+ }
37
+ export {
38
+ Fragment
39
+ };
@@ -0,0 +1,46 @@
1
+ // src/helpers/dom.ts
2
+ import {getString} from "@oscarpalmer/atoms/string";
3
+ import {isFragment, isFragmentNode} from "./index";
4
+ function createNodes(value) {
5
+ if (isFragmentNode(value)) {
6
+ return [value];
7
+ }
8
+ if (isFragment(value)) {
9
+ return value.get();
10
+ }
11
+ return [new Text(getString(value))];
12
+ }
13
+ function createTemplate(html) {
14
+ const template = document.createElement("template");
15
+ template.innerHTML = html;
16
+ const cloned = template.content.cloneNode(true);
17
+ const scripts = [
18
+ ...cloned instanceof Element ? cloned.querySelectorAll("script") : []
19
+ ];
20
+ const { length } = scripts;
21
+ for (let index = 0;index < length; index += 1) {
22
+ scripts[index].remove();
23
+ }
24
+ cloned.normalize();
25
+ return cloned;
26
+ }
27
+ function getNodes(node) {
28
+ return /^documentfragment$/i.test(node.constructor.name) ? [...node.childNodes] : [node];
29
+ }
30
+ function replaceNodes(from, to) {
31
+ const { length } = from;
32
+ for (let index = 0;index < length; index += 1) {
33
+ const node = from[index];
34
+ if (index === 0) {
35
+ node.replaceWith(...to);
36
+ } else {
37
+ node.remove();
38
+ }
39
+ }
40
+ }
41
+ export {
42
+ replaceNodes,
43
+ getNodes,
44
+ createTemplate,
45
+ createNodes
46
+ };
@@ -0,0 +1,15 @@
1
+ // src/helpers/index.ts
2
+ function isFragment(value) {
3
+ return typeof value === "object" && value != null && "$fragment" in value && value.$fragment === true;
4
+ }
5
+ function isFragmentElement(value) {
6
+ return value instanceof HTMLElement || value instanceof SVGElement;
7
+ }
8
+ function isFragmentNode(value) {
9
+ return value instanceof Comment || value instanceof Element || value instanceof Text;
10
+ }
11
+ export {
12
+ isFragmentNode,
13
+ isFragmentElement,
14
+ isFragment
15
+ };
package/dist/html.mjs ADDED
@@ -0,0 +1,36 @@
1
+ // src/html.ts
2
+ import {isNullableOrWhitespace} from "@oscarpalmer/atoms/is";
3
+ import {Fragment} from "./fragment";
4
+ function handleExpression(data, prefix, expression) {
5
+ if (isNullableOrWhitespace(expression)) {
6
+ return prefix;
7
+ }
8
+ if (Array.isArray(expression)) {
9
+ const { length } = expression;
10
+ let expressions = "";
11
+ for (let index = 0;index < length; index += 1) {
12
+ expressions += handleExpression(data, "", expression[index]);
13
+ }
14
+ return `${prefix}${expressions}`;
15
+ }
16
+ if (typeof expression === "function" || typeof expression === "object") {
17
+ const index = data.values.push(expression) - 1;
18
+ return `${prefix}<!--abydon.${index}-->`;
19
+ }
20
+ return `${prefix}${expression}`;
21
+ }
22
+ function html(strings, ...values) {
23
+ return new Fragment(strings, values);
24
+ }
25
+ function parse(data) {
26
+ const { length } = data.strings;
27
+ let template = "";
28
+ for (let index = 0;index < length; index += 1) {
29
+ template += handleExpression(data, data.strings[index], data.expressions[index]);
30
+ }
31
+ return template;
32
+ }
33
+ export {
34
+ parse,
35
+ html
36
+ };