@oscarpalmer/abydon 0.7.0 → 0.9.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,49 @@
1
+ // src/fragment.ts
2
+ import {html as html2} from "@oscarpalmer/toretto/html";
3
+ import {parse} from "./html";
4
+ import {mapNodes} from "./node";
5
+
6
+ class Fragment {
7
+ $fragment = true;
8
+ data;
9
+ constructor(strings, expressions) {
10
+ this.data = {
11
+ expressions,
12
+ strings,
13
+ items: [],
14
+ values: []
15
+ };
16
+ }
17
+ appendTo(element) {
18
+ element.append(...this.get());
19
+ }
20
+ get() {
21
+ if (this.data.items.length === 0) {
22
+ const parsed = parse(this.data);
23
+ const templated = html2(parsed, {
24
+ sanitiseBooleanAttributes: false
25
+ });
26
+ this.data.items.splice(0, this.data.items.length, ...templated.map((node2) => ({
27
+ nodes: [node2]
28
+ })));
29
+ mapNodes(this.data, this.data.items.flatMap((item) => item.fragment?.get() ?? item.nodes));
30
+ }
31
+ return [
32
+ ...this.data.items.flatMap((item) => item.fragment?.get() ?? item.nodes)
33
+ ];
34
+ }
35
+ remove() {
36
+ const { length } = this.data.items;
37
+ for (let index = 0;index < length; index += 1) {
38
+ const item = this.data.items[index];
39
+ item.fragment?.remove();
40
+ for (const node2 of item.nodes) {
41
+ node2.remove();
42
+ }
43
+ }
44
+ this.data.items.splice(0, length);
45
+ }
46
+ }
47
+ export {
48
+ Fragment
49
+ };
@@ -0,0 +1,27 @@
1
+ // src/helpers/dom.ts
2
+ import {getString} from "@oscarpalmer/atoms/string";
3
+ import {isFragment, isProperNode} from "./index";
4
+ function createNodes(value) {
5
+ if (isFragment(value)) {
6
+ return value.get();
7
+ }
8
+ if (isProperNode(value)) {
9
+ return [value];
10
+ }
11
+ return [new Text(getString(value))];
12
+ }
13
+ function replaceNodes(from, to) {
14
+ const { length } = from;
15
+ for (let index = 0;index < length; index += 1) {
16
+ const node = from[index];
17
+ if (index === 0) {
18
+ node.replaceWith(...to);
19
+ } else {
20
+ node.remove();
21
+ }
22
+ }
23
+ }
24
+ export {
25
+ replaceNodes,
26
+ createNodes
27
+ };
@@ -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 isProperElement(value) {
6
+ return value instanceof HTMLElement || value instanceof SVGElement;
7
+ }
8
+ function isProperNode(value) {
9
+ return value instanceof CharacterData || value instanceof Element;
10
+ }
11
+ export {
12
+ isProperNode,
13
+ isProperElement,
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
+ };