@oscarpalmer/abydon 0.8.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.
package/dist/fragment.mjs CHANGED
@@ -1,15 +1,16 @@
1
1
  // src/fragment.ts
2
- import {createTemplate, getNodes} from "./helpers/dom";
2
+ import {html as html2} from "@oscarpalmer/toretto/html";
3
3
  import {parse} from "./html";
4
4
  import {mapNodes} from "./node";
5
5
 
6
6
  class Fragment {
7
+ $fragment = true;
7
8
  data;
8
9
  constructor(strings, expressions) {
9
10
  this.data = {
10
11
  expressions,
11
12
  strings,
12
- nodes: [],
13
+ items: [],
13
14
  values: []
14
15
  };
15
16
  }
@@ -17,21 +18,30 @@ class Fragment {
17
18
  element.append(...this.get());
18
19
  }
19
20
  get() {
20
- if (this.data.nodes.length === 0) {
21
+ if (this.data.items.length === 0) {
21
22
  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);
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));
25
30
  }
26
- return [...this.data.nodes];
31
+ return [
32
+ ...this.data.items.flatMap((item) => item.fragment?.get() ?? item.nodes)
33
+ ];
27
34
  }
28
35
  remove() {
29
- const { length } = this.data.nodes;
36
+ const { length } = this.data.items;
30
37
  for (let index = 0;index < length; index += 1) {
31
- const node2 = this.data.nodes[index];
32
- node2.parentNode?.removeChild(node2);
38
+ const item = this.data.items[index];
39
+ item.fragment?.remove();
40
+ for (const node2 of item.nodes) {
41
+ node2.remove();
42
+ }
33
43
  }
34
- this.data.nodes.splice(0, length);
44
+ this.data.items.splice(0, length);
35
45
  }
36
46
  }
37
47
  export {
@@ -1,31 +1,14 @@
1
1
  // src/helpers/dom.ts
2
2
  import {getString} from "@oscarpalmer/atoms/string";
3
- import {isFragment, isFragmentNode} from "./index";
3
+ import {isFragment, isProperNode} from "./index";
4
4
  function createNodes(value) {
5
- if (isFragmentNode(value)) {
6
- return [value];
7
- }
8
5
  if (isFragment(value)) {
9
6
  return value.get();
10
7
  }
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();
8
+ if (isProperNode(value)) {
9
+ return [value];
23
10
  }
24
- cloned.normalize();
25
- return cloned;
26
- }
27
- function getNodes(node) {
28
- return /^documentfragment$/i.test(node.constructor.name) ? [...node.childNodes] : [node];
11
+ return [new Text(getString(value))];
29
12
  }
30
13
  function replaceNodes(from, to) {
31
14
  const { length } = from;
@@ -40,7 +23,5 @@ function replaceNodes(from, to) {
40
23
  }
41
24
  export {
42
25
  replaceNodes,
43
- getNodes,
44
- createTemplate,
45
26
  createNodes
46
27
  };
@@ -2,14 +2,14 @@
2
2
  function isFragment(value) {
3
3
  return typeof value === "object" && value != null && "$fragment" in value && value.$fragment === true;
4
4
  }
5
- function isFragmentElement(value) {
5
+ function isProperElement(value) {
6
6
  return value instanceof HTMLElement || value instanceof SVGElement;
7
7
  }
8
- function isFragmentNode(value) {
9
- return value instanceof Comment || value instanceof Element || value instanceof Text;
8
+ function isProperNode(value) {
9
+ return value instanceof CharacterData || value instanceof Element;
10
10
  }
11
11
  export {
12
- isFragmentNode,
13
- isFragmentElement,
12
+ isProperNode,
13
+ isProperElement,
14
14
  isFragment
15
15
  };