@oscarpalmer/abydon 0.12.0 → 0.14.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.
Files changed (48) hide show
  1. package/dist/abydon.full.js +1153 -0
  2. package/dist/constants.js +10 -0
  3. package/dist/fragment.js +60 -0
  4. package/dist/helpers/dom.js +28 -0
  5. package/dist/helpers/index.js +11 -0
  6. package/dist/index.js +6 -911
  7. package/dist/node/attribute/index.js +39 -0
  8. package/dist/node/attribute/value.js +56 -0
  9. package/dist/node/event.js +30 -0
  10. package/dist/node/index.js +52 -0
  11. package/dist/node/value.js +108 -0
  12. package/dist/parse.js +18 -0
  13. package/package.json +38 -27
  14. package/src/constants.ts +20 -0
  15. package/src/fragment.ts +63 -36
  16. package/src/helpers/dom.ts +5 -4
  17. package/src/helpers/index.ts +0 -9
  18. package/src/index.ts +13 -3
  19. package/src/models.ts +14 -11
  20. package/src/node/attribute/index.ts +28 -22
  21. package/src/node/attribute/value.ts +38 -42
  22. package/src/node/event.ts +12 -13
  23. package/src/node/index.ts +7 -7
  24. package/src/node/value.ts +169 -122
  25. package/src/{html.ts → parse.ts} +15 -17
  26. package/types/constants.d.ts +9 -0
  27. package/types/fragment.d.ts +3 -5
  28. package/types/helpers/index.d.ts +0 -3
  29. package/types/index.d.ts +4 -2
  30. package/types/models.d.ts +7 -9
  31. package/types/node/attribute/index.d.ts +2 -1
  32. package/types/node/attribute/value.d.ts +2 -1
  33. package/types/node/event.d.ts +1 -1
  34. package/types/node/value.d.ts +2 -2
  35. package/types/parse.d.ts +2 -0
  36. package/dist/fragment.mjs +0 -75
  37. package/dist/helpers/dom.mjs +0 -44
  38. package/dist/helpers/index.mjs +0 -25
  39. package/dist/html.mjs +0 -36
  40. package/dist/index.mjs +0 -6
  41. package/dist/node/attribute/index.mjs +0 -40
  42. package/dist/node/attribute/value.mjs +0 -89
  43. package/dist/node/event.mjs +0 -38
  44. package/dist/node/index.mjs +0 -60
  45. package/dist/node/value.mjs +0 -123
  46. package/types/html.d.ts +0 -4
  47. package/types/index.d.cts +0 -26
  48. /package/dist/{models.mjs → models.js} +0 -0
@@ -0,0 +1,10 @@
1
+ const ABORT_CONTROLLERS = /* @__PURE__ */ new WeakMap();
2
+ const ATTRIBUTE_CLASS_PREFIX_LENGTH = 6;
3
+ const EXPRESSION_ATTRIBUTE_CLASS = /^class\./;
4
+ const EXPRESSION_ATTRIBUTE_STYLE_FULL = /^style\.([\w-]+)(?:\.([\w-]+))?$/;
5
+ const EXPRESSION_ATTRIBUTE_STYLE_PREFIX = /^style\./;
6
+ const EXPRESSION_COMMENT_FULL = /^<!--abydon\.(\d+)-->$/;
7
+ const EXPRESSION_COMMENT_CONTENT = /^abydon\.(\d+)$/;
8
+ const EXPRESSION_EVENT_NAME = /^@([\w-]+)(?::([a-z:]+))?$/i;
9
+ const REASON_EVENT_REMOVED = "Event removed as element was removed from document by Abydon";
10
+ export { ABORT_CONTROLLERS, ATTRIBUTE_CLASS_PREFIX_LENGTH, EXPRESSION_ATTRIBUTE_CLASS, EXPRESSION_ATTRIBUTE_STYLE_FULL, EXPRESSION_ATTRIBUTE_STYLE_PREFIX, EXPRESSION_COMMENT_CONTENT, EXPRESSION_COMMENT_FULL, EXPRESSION_EVENT_NAME, REASON_EVENT_REMOVED };
@@ -0,0 +1,60 @@
1
+ import { parse } from "./parse.js";
2
+ import { removeNodes } from "./helpers/dom.js";
3
+ import { mapNodes } from "./node/index.js";
4
+ import { html } from "@oscarpalmer/toretto/html";
5
+ var Fragment = class {
6
+ #data;
7
+ get identifier() {
8
+ return this.#data.identifier;
9
+ }
10
+ constructor(strings, expressions) {
11
+ this.#data = {
12
+ expressions,
13
+ strings,
14
+ items: [],
15
+ mora: {
16
+ subscribers: /* @__PURE__ */ new Set(),
17
+ values: /* @__PURE__ */ new Set()
18
+ },
19
+ values: []
20
+ };
21
+ Object.defineProperty(this, "$fragment", { value: true });
22
+ }
23
+ appendTo(element) {
24
+ element.append(...this.get());
25
+ }
26
+ get() {
27
+ const data = this.#data;
28
+ if (data.items.length === 0) {
29
+ const templated = html(parse(data), { sanitizeBooleanAttributes: false });
30
+ data.items.splice(0, data.items.length, ...templated.map((node) => ({ nodes: [node] })));
31
+ mapNodes(data, data.items.flatMap((item) => item.fragments?.flatMap((fragment) => fragment.get()) ?? item.nodes));
32
+ }
33
+ return [...data.items.flatMap((item) => item.fragments?.flatMap((fragment) => fragment.get()) ?? item.nodes)];
34
+ }
35
+ identify(identifier) {
36
+ this.#data.identifier = identifier;
37
+ return this;
38
+ }
39
+ remove() {
40
+ removeFragment(this.#data);
41
+ }
42
+ };
43
+ function removeFragment(data) {
44
+ removeMora(data);
45
+ const { length } = data.items;
46
+ for (let index = 0; index < length; index += 1) {
47
+ const { fragments, nodes } = data.items[index];
48
+ const { length: length$1 } = fragments ?? [];
49
+ for (let index$1 = 0; index$1 < length$1; index$1 += 1) fragments?.[index$1]?.remove();
50
+ removeNodes(nodes);
51
+ }
52
+ data.items.splice(0, length);
53
+ }
54
+ function removeMora(data) {
55
+ const unsubscribers = [...data.mora.subscribers];
56
+ data.mora.subscribers.clear();
57
+ data.mora.values.clear();
58
+ for (const unsubscribe of unsubscribers) unsubscribe();
59
+ }
60
+ export { Fragment };
@@ -0,0 +1,28 @@
1
+ import { removeEvents } from "../node/event.js";
2
+ import { isFragment } from "./index.js";
3
+ import { getString } from "@oscarpalmer/atoms/string";
4
+ import { isChildNode, isHTMLOrSVGElement } from "@oscarpalmer/toretto/is";
5
+ function createNodes(value) {
6
+ if (isFragment(value)) return value.get();
7
+ if (isChildNode(value)) return [value];
8
+ return [new Text(getString(value))];
9
+ }
10
+ function removeNodes(nodes) {
11
+ sanitizeNodes(nodes);
12
+ const { length } = nodes;
13
+ for (let index = 0; index < length; index += 1) nodes[index].remove();
14
+ }
15
+ function replaceNodes(from, to) {
16
+ from[0]?.replaceWith(...to);
17
+ const { length } = from;
18
+ for (let index = 1; index < length; index += 1) from[index].remove();
19
+ }
20
+ function sanitizeNodes(nodes) {
21
+ const { length } = nodes;
22
+ for (let index = 0; index < length; index += 1) {
23
+ const node = nodes[index];
24
+ if (isHTMLOrSVGElement(node)) removeEvents(node);
25
+ if (node.hasChildNodes()) sanitizeNodes([...node.childNodes]);
26
+ }
27
+ }
28
+ export { createNodes, removeNodes, replaceNodes };
@@ -0,0 +1,11 @@
1
+ function compareArrays(first, second) {
2
+ const firstIsLarger = first.length > second.length;
3
+ const from = firstIsLarger ? first : second;
4
+ const to = firstIsLarger ? second : first;
5
+ if (!from.filter((key) => to.includes(key)).every((key, index) => to[index] === key)) return "dissimilar";
6
+ return firstIsLarger ? "removed" : "added";
7
+ }
8
+ function isFragment(value) {
9
+ return typeof value === "object" && value != null && "$fragment" in value && value.$fragment === true;
10
+ }
11
+ export { compareArrays, isFragment };