@unframework.dev/testing 0.0.0-stage → 0.2.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 (133) hide show
  1. package/README.md +536 -2
  2. package/dist/browser/axe.js +34 -0
  3. package/dist/browser/behaviour.js +59 -0
  4. package/dist/browser/capabilities.js +58 -0
  5. package/dist/browser/clock.d.ts +17 -0
  6. package/dist/browser/clock.js +69 -0
  7. package/dist/browser/console.d.ts +27 -0
  8. package/dist/browser/console.js +142 -0
  9. package/dist/browser/determinism.js +108 -0
  10. package/dist/browser/element-timeout.js +28 -0
  11. package/dist/browser/events.js +68 -0
  12. package/dist/browser/geometry.js +135 -0
  13. package/dist/browser/keys.js +62 -0
  14. package/dist/browser/matchers.d.ts +39 -0
  15. package/dist/browser/matchers.js +78 -0
  16. package/dist/browser/navigation.js +38 -0
  17. package/dist/browser/parity.d.ts +10 -0
  18. package/dist/browser/parity.js +93 -0
  19. package/dist/browser/spec.d.ts +25 -0
  20. package/dist/browser/spec.js +38 -0
  21. package/dist/browser/target.d.ts +16 -0
  22. package/dist/browser/target.js +27 -0
  23. package/dist/browser/text-runs.js +68 -0
  24. package/dist/browser/thrown.js +71 -0
  25. package/dist/browser/trace.d.ts +21 -0
  26. package/dist/browser/trace.js +73 -0
  27. package/dist/browser/user.d.ts +45 -0
  28. package/dist/browser/user.js +184 -0
  29. package/dist/browser/view.d.ts +114 -0
  30. package/dist/browser/view.js +282 -0
  31. package/dist/browser/visual.d.ts +20 -0
  32. package/dist/browser/visual.js +36 -0
  33. package/dist/cli.d.ts +23 -0
  34. package/dist/cli.js +90 -0
  35. package/dist/commands.d.ts +107 -0
  36. package/dist/config/broadcast.js +29 -0
  37. package/dist/config/cases.d.ts +28 -0
  38. package/dist/config/cases.js +94 -0
  39. package/dist/config/index.d.ts +82 -0
  40. package/dist/config/index.js +295 -0
  41. package/dist/config/project-vite.d.ts +40 -0
  42. package/dist/config/project-vite.js +152 -0
  43. package/dist/diff.d.ts +14 -0
  44. package/dist/diff.js +92 -0
  45. package/dist/dom/serialize.js +236 -0
  46. package/dist/errors.d.ts +13 -0
  47. package/dist/errors.js +38 -0
  48. package/dist/harness.d.ts +189 -0
  49. package/dist/harness.js +53 -0
  50. package/dist/index.d.ts +16 -0
  51. package/dist/index.js +12 -0
  52. package/dist/internal.d.ts +8 -0
  53. package/dist/internal.js +6 -0
  54. package/dist/layers.d.ts +183 -0
  55. package/dist/layers.js +241 -0
  56. package/dist/node/browser.d.ts +33 -0
  57. package/dist/node/browser.js +64 -0
  58. package/dist/node/commands.d.ts +137 -0
  59. package/dist/node/commands.js +139 -0
  60. package/dist/node/context.d.ts +28 -0
  61. package/dist/node/context.js +55 -0
  62. package/dist/node/events.d.ts +25 -0
  63. package/dist/node/events.js +50 -0
  64. package/dist/node/finished.d.ts +12 -0
  65. package/dist/node/finished.js +28 -0
  66. package/dist/node/index.d.ts +21 -0
  67. package/dist/node/index.js +20 -0
  68. package/dist/node/matrix.d.ts +219 -0
  69. package/dist/node/matrix.js +309 -0
  70. package/dist/node/mode.d.ts +103 -0
  71. package/dist/node/mode.js +204 -0
  72. package/dist/node/names.d.ts +4 -0
  73. package/dist/node/names.js +5 -0
  74. package/dist/node/policy.d.ts +44 -0
  75. package/dist/node/policy.js +140 -0
  76. package/dist/node/reporter.d.ts +101 -0
  77. package/dist/node/reporter.js +251 -0
  78. package/dist/node/summary.d.ts +99 -0
  79. package/dist/node/summary.js +352 -0
  80. package/dist/node/thrown.js +78 -0
  81. package/dist/node/viewport.d.ts +7 -0
  82. package/dist/node/viewport.js +8 -0
  83. package/dist/node/visual.d.ts +85 -0
  84. package/dist/node/visual.js +373 -0
  85. package/dist/node_modules/.pnpm/playwright-core@1.63.0/node_modules/playwright-core/types/protocol.d.ts +23930 -0
  86. package/dist/node_modules/.pnpm/playwright-core@1.63.0/node_modules/playwright-core/types/structs.d.ts +22 -0
  87. package/dist/node_modules/.pnpm/playwright-core@1.63.0/node_modules/playwright-core/types/types.d.ts +22221 -0
  88. package/dist/normalize/display.js +309 -0
  89. package/dist/normalize/index.d.ts +34 -0
  90. package/dist/normalize/index.js +38 -0
  91. package/dist/normalize/pipeline.d.ts +19 -0
  92. package/dist/normalize/pipeline.js +45 -0
  93. package/dist/normalize/print.js +103 -0
  94. package/dist/normalize/rules/angular-hosts.js +53 -0
  95. package/dist/normalize/rules/aria-hosts.js +48 -0
  96. package/dist/normalize/rules/comments.js +16 -0
  97. package/dist/normalize/rules/control-values.js +65 -0
  98. package/dist/normalize/rules/engine-aliases.js +34 -0
  99. package/dist/normalize/rules/framework-attributes.js +85 -0
  100. package/dist/normalize/rules/generated-ids.js +64 -0
  101. package/dist/normalize/rules/ordering.js +39 -0
  102. package/dist/normalize/rules/qwik-styles.js +21 -0
  103. package/dist/normalize/rules/qwik-templates.js +20 -0
  104. package/dist/normalize/rules/values.js +125 -0
  105. package/dist/normalize/rules/whitespace.js +381 -0
  106. package/dist/normalize/style.js +121 -0
  107. package/dist/normalize/targets.d.ts +8 -0
  108. package/dist/normalize/targets.js +17 -0
  109. package/dist/normalize/tree.d.ts +1 -0
  110. package/dist/normalize/tree.js +151 -0
  111. package/dist/setup.d.ts +1 -0
  112. package/dist/setup.js +149 -0
  113. package/dist/ssr/index.d.ts +10 -0
  114. package/dist/ssr/index.js +103 -0
  115. package/dist/ssr/spec.d.ts +1 -0
  116. package/dist/ssr/spec.js +7 -0
  117. package/dist/targets/angular.d.ts +1 -0
  118. package/dist/targets/angular.js +5 -0
  119. package/dist/targets/astro.d.ts +1 -0
  120. package/dist/targets/astro.js +5 -0
  121. package/dist/targets/qwik.d.ts +1 -0
  122. package/dist/targets/qwik.js +5 -0
  123. package/dist/targets/react.d.ts +1 -0
  124. package/dist/targets/react.js +5 -0
  125. package/dist/targets/solid.d.ts +1 -0
  126. package/dist/targets/solid.js +5 -0
  127. package/dist/targets/svelte.d.ts +1 -0
  128. package/dist/targets/svelte.js +5 -0
  129. package/dist/targets/vue.d.ts +1 -0
  130. package/dist/targets/vue.js +5 -0
  131. package/dist/visual-types.d.ts +92 -0
  132. package/dist/visual-types.js +24 -0
  133. package/package.json +146 -3
@@ -0,0 +1,16 @@
1
+ import { childrenOf, isElement, replaceChildren } from "../tree.js";
2
+ //#region src/normalize/rules/comments.ts
3
+ /**
4
+ * Rule 1: removes every comment. Comments are never rendered, and every framework uses them as
5
+ * anchors in its own way: Vue `<!--v-if-->` and `<!--[-->…<!--]-->`, Svelte `<!--[-->…<!--]-->`
6
+ * and `<!---->`, Solid `<!--$-->`/`<!--/-->`, React `<!-- -->` between adjacent texts, Angular
7
+ * `<!--container-->` and `<!--nghm-->`, Qwik `<!--qv …-->`. The text on either side of a removed
8
+ * comment is left as it is, so it joins up exactly as it renders.
9
+ */
10
+ function removeComments(root) {
11
+ const children = childrenOf(root);
12
+ if (children.some((node) => node.nodeName === "#comment")) replaceChildren(root, children.filter((node) => node.nodeName !== "#comment"));
13
+ for (const node of childrenOf(root)) if (isElement(node)) removeComments(node);
14
+ }
15
+ //#endregion
16
+ export { removeComments };
@@ -0,0 +1,65 @@
1
+ import { HTML_NAMESPACE, attributeName, childrenOf, forEachElement, getAttribute, isText, replaceChildren } from "../tree.js";
2
+ //#region src/normalize/rules/control-values.ts
3
+ /**
4
+ * Rule 4d: removes the markup of a form control's initial state where its live state, a `uf:*`
5
+ * pseudo-attribute, says the same (ADR-0058):
6
+ * - an `<input>`'s `value` attribute that equals its `uf:value`;
7
+ * - an `<input>`'s `checked` attribute where its `uf:checked` is `"true"`;
8
+ * - an `<option>`'s `selected` attribute where its `uf:selected` is `"true"`;
9
+ * - a `<textarea>`'s text that equals its `uf:value`.
10
+ *
11
+ * Vue's `v-model` sets only the property, where react-dom keeps a controlled input's attribute
12
+ * in step with its value and writes `defaultChecked` and `defaultValue` as markup, and Astro,
13
+ * which runs no client code, writes the initial state as markup alone: the state compares in
14
+ * `uf:*` on every target, so the markup only repeats it. Markup that differs (a static
15
+ * `value="x"` after the person typed `y`, a `checked` box the person cleared) stays, and so does
16
+ * the markup of an element without the `uf:*` state (a server render). It holds for every
17
+ * target, the reference included: a static field's attribute, which React writes as
18
+ * `defaultValue` and Vue as the attribute, must still compare before anyone acts.
19
+ */
20
+ function removeSettledValues(root) {
21
+ forEachElement(root, (element) => {
22
+ if (element.namespaceURI !== HTML_NAMESPACE) return;
23
+ switch (element.tagName) {
24
+ case "input": {
25
+ const state = getAttribute(element, "uf:value");
26
+ if (state !== void 0 && getAttribute(element, "value") === state) removeAttribute(element, "value");
27
+ if (getAttribute(element, "uf:checked") === "true") removeAttribute(element, "checked");
28
+ return;
29
+ }
30
+ case "option":
31
+ if (getAttribute(element, "uf:selected") === "true") removeAttribute(element, "selected");
32
+ return;
33
+ case "textarea": {
34
+ const state = getAttribute(element, "uf:value");
35
+ const children = childrenOf(element);
36
+ const text = children.every(isText) ? children.map((child) => child.value).join("") : void 0;
37
+ if (state !== void 0 && children.length && text === state) replaceChildren(element, []);
38
+ return;
39
+ }
40
+ default: return;
41
+ }
42
+ });
43
+ }
44
+ /**
45
+ * Rule 4e: on React only, removes a `checked` or `selected` attribute wherever the element's
46
+ * `uf:checked` or `uf:selected` state is there, whatever that state says (ADR-0060). react-dom
47
+ * writes a controlled box's or option's first state as its attribute and never updates it, so
48
+ * the attribute of a box the person cleared shows a state that is gone; the live state compares
49
+ * in `uf:*`, which a real difference (a box still checked) still changes. Rule 4d keeps such an
50
+ * attribute on every other target, where only the markup the source wrote puts it there. An
51
+ * element without the `uf:*` state (a server render) keeps its attribute on React too.
52
+ */
53
+ function removeReactDefaultStates(root, target) {
54
+ if (target !== "react") return;
55
+ forEachElement(root, (element) => {
56
+ if (element.namespaceURI !== HTML_NAMESPACE) return;
57
+ if (element.tagName === "input" && getAttribute(element, "uf:checked") !== void 0) removeAttribute(element, "checked");
58
+ else if (element.tagName === "option" && getAttribute(element, "uf:selected") !== void 0) removeAttribute(element, "selected");
59
+ });
60
+ }
61
+ function removeAttribute(element, name) {
62
+ element.attrs = element.attrs.filter((attribute) => attributeName(attribute) !== name);
63
+ }
64
+ //#endregion
65
+ export { removeReactDefaultStates, removeSettledValues };
@@ -0,0 +1,34 @@
1
+ import { parseStyle, printStyle } from "../style.js";
2
+ import { attributeName, forEachElement } from "../tree.js";
3
+ //#region src/normalize/rules/engine-aliases.ts
4
+ /**
5
+ * The properties an engine serialises under its own alias, by engine: each alias and the name
6
+ * Chromium, which writes every committed expectation, serialises. Only an alias that names the
7
+ * same property is listed, so a value still compares as it is.
8
+ */
9
+ const ENGINE_ALIASES = { firefox: { "-moz-box-orient": "-webkit-box-orient" } };
10
+ /**
11
+ * Rule 3b: renames, in every `style` attribute, the properties the engine the DOM was read in
12
+ * serialises under its own alias (ADR-0081, amending ADR-0031). The engine renders the source's
13
+ * property (L8 checks it), but its `cssText` spells it another way, so a Firefox DOM never
14
+ * matched an expectation Chromium wrote. Only the named engine's aliases are renamed, and only
15
+ * the property: a value that differs still differs, and so does every other `-moz-*` property.
16
+ * It runs before rule 4a, which sorts the declarations by their names.
17
+ */
18
+ function renameEngineAliases(root, engine) {
19
+ const aliases = engine === void 0 ? void 0 : ENGINE_ALIASES[engine];
20
+ if (!aliases) return;
21
+ forEachElement(root, (element) => {
22
+ for (const attribute of element.attrs) {
23
+ if (attributeName(attribute) !== "style") continue;
24
+ const declarations = parseStyle(attribute.value);
25
+ if (!declarations.some((declaration) => Object.hasOwn(aliases, declaration.property))) continue;
26
+ attribute.value = printStyle(declarations.map((declaration) => Object.hasOwn(aliases, declaration.property) ? {
27
+ ...declaration,
28
+ property: aliases[declaration.property]
29
+ } : declaration));
30
+ }
31
+ });
32
+ }
33
+ //#endregion
34
+ export { renameEngineAliases };
@@ -0,0 +1,85 @@
1
+ import { attributeName, forEachElement } from "../tree.js";
2
+ //#region src/normalize/rules/framework-attributes.ts
3
+ /**
4
+ * Attributes a framework adds on its own, which no source asks for, by the target whose
5
+ * framework adds them. Authored attributes, the compiler's own scope attributes (`data-uf-*`)
6
+ * and the serialiser's form state (`uf:*`) are not in the list and always stay.
7
+ */
8
+ const FRAMEWORK_ATTRIBUTES = [
9
+ {
10
+ framework: "angular",
11
+ pattern: /^_nghost-/
12
+ },
13
+ {
14
+ framework: "angular",
15
+ pattern: /^_ngcontent-/
16
+ },
17
+ {
18
+ framework: "angular",
19
+ pattern: /^ng-reflect-/
20
+ },
21
+ {
22
+ framework: "angular",
23
+ pattern: /^ng-version$/
24
+ },
25
+ {
26
+ framework: "angular",
27
+ pattern: /^ng-server-context$/
28
+ },
29
+ {
30
+ framework: "angular",
31
+ pattern: /^ngh$/
32
+ },
33
+ {
34
+ framework: "qwik",
35
+ pattern: /^q:/
36
+ },
37
+ {
38
+ framework: "qwik",
39
+ pattern: /^q-[a-z]p?:/
40
+ },
41
+ {
42
+ framework: "qwik",
43
+ pattern: /^:$/
44
+ },
45
+ {
46
+ framework: "qwik",
47
+ pattern: /^(?:preventdefault|stoppropagation|capture):/
48
+ },
49
+ {
50
+ framework: "qwik",
51
+ pattern: /^on(?:-document|-window)?:/
52
+ },
53
+ {
54
+ framework: "solid",
55
+ pattern: /^data-hk$/
56
+ },
57
+ {
58
+ framework: "astro",
59
+ pattern: /^data-astro-cid-/
60
+ },
61
+ {
62
+ framework: "astro",
63
+ pattern: /^data-astro-source-/
64
+ }
65
+ ];
66
+ /**
67
+ * Whether `target`'s framework adds the attribute on its own (see `FRAMEWORK_ATTRIBUTES`). The
68
+ * same name in another target's output is not noise: it is an attribute that target rendered.
69
+ */
70
+ function isFrameworkAttribute(name, target) {
71
+ return FRAMEWORK_ATTRIBUTES.some(({ framework, pattern }) => framework === target && pattern.test(name));
72
+ }
73
+ /**
74
+ * Rule 2: removes every attribute the target's framework added (see `FRAMEWORK_ATTRIBUTES`).
75
+ * Without a target no framework is known, so nothing is noise and every attribute stays: the
76
+ * reference's output is never normalised by another framework's rules.
77
+ */
78
+ function removeFrameworkAttributes(root, target) {
79
+ if (target === void 0) return;
80
+ forEachElement(root, (element) => {
81
+ element.attrs = element.attrs.filter((attribute) => !isFrameworkAttribute(attributeName(attribute), target));
82
+ });
83
+ }
84
+ //#endregion
85
+ export { FRAMEWORK_ATTRIBUTES, isFrameworkAttribute, removeFrameworkAttributes };
@@ -0,0 +1,64 @@
1
+ import { attributeName, childrenOf, isElement, isText } from "../tree.js";
2
+ import { GENERATED_ID_PREFIX } from "@unframework.dev/ir";
3
+ //#region src/normalize/rules/generated-ids.ts
4
+ /**
5
+ * Generated ids are recognised by where they come from, not by their shape: they carry the
6
+ * prefix only the compiler writes (`GENERATED_ID_PREFIX`), in front of each framework's id on
7
+ * every target (`"uf-id-" + useId()`, ADR-0049), and the analyzer rejects an authored id or id
8
+ * reference that starts with it (UF3005). The frameworks' own formats are deliberately not
9
+ * recognised: Qwik's (`B2t0`) is any short word, Svelte's (`s1`, `c2`) and Solid's (`cl-1`,
10
+ * `00`) are ordinary authored ids, and naming them generated on one target renamed authored ids
11
+ * there alone, while the reference kept them.
12
+ *
13
+ * A generated id stands wherever it appears in a value or a text: the prefix, then the
14
+ * characters the frameworks' ids are made of (Vue's `v-0`, React's `_R_2_` and `_r_1_`,
15
+ * Svelte's `s1`, Solid's `cl-1`, Qwik's `B2t0`, Angular's and Astro's `name-0`), not preceded by
16
+ * one of them. So an id ends where an id character stops (`uf-id-v-0.` is the id and a full
17
+ * stop, as are `#uf-id-v-0` and `url(#uf-id-v-0)` around it), and an author's suffix
18
+ * (`${id}-${index}`) makes another id, numbered on its own: two ids a follower makes equal and
19
+ * the reference does not still read apart. An id with another character (a future format) is
20
+ * not renamed whole, and fails parity loudly.
21
+ */
22
+ const GENERATED_ID = new RegExp(`(?<![\\w-])${GENERATED_ID_PREFIX}[\\w-]+`, "g");
23
+ /**
24
+ * Renames the generated ids in a text (an attribute value, a text node, an ARIA snapshot, a
25
+ * payload's string) to `uf-id-1`, `uf-id-2`… in order of first appearance, continuing the
26
+ * renaming `names` holds and adding to it.
27
+ */
28
+ function renameGeneratedIds(text, names) {
29
+ return text.replace(GENERATED_ID, (id) => {
30
+ let name = names.get(id);
31
+ if (name === void 0) {
32
+ name = `${GENERATED_ID_PREFIX}${names.size + 1}`;
33
+ names.set(id, name);
34
+ }
35
+ return name;
36
+ });
37
+ }
38
+ /** ASCII whitespace, which separates the tokens of a `class` attribute. */
39
+ const ASCII_WHITESPACE = /[\t\n\f\r ]+/;
40
+ /** Compares by UTF-16 code units, as rule 4c sorts a `class`'s tokens. */
41
+ const byCodeUnits = (a, b) => a < b ? -1 : a > b ? 1 : 0;
42
+ /**
43
+ * Rule 7: renames every generated id (see `GENERATED_ID`) to `uf-id-1`, `uf-id-2`… in order of
44
+ * first appearance, the same way wherever it appears (ADR-0049): in every attribute value (`id`,
45
+ * the idref attributes, a `#id` URL, `url(#id)`, a radio group's `name`, a `data-*` value) and
46
+ * in every text node, elements in document order, each element's attributes (in the order
47
+ * `sortAttributes` leaves) before its children. References stay consistent and a broken
48
+ * association still shows. A `class`'s tokens, which carry no order, are sorted again once
49
+ * renamed. Every other id is authored and stays exactly as it is. Returns the renaming, from
50
+ * each original id to its new name (`names`, when given, is filled in): a trace renames the ids
51
+ * a step's ARIA tree and emitted payloads carry with it, as its DOM's.
52
+ */
53
+ function canonicalizeGeneratedIds(root, names = /* @__PURE__ */ new Map()) {
54
+ for (const node of childrenOf(root)) if (isElement(node)) {
55
+ for (const attribute of node.attrs) {
56
+ const value = renameGeneratedIds(attribute.value, names);
57
+ attribute.value = attributeName(attribute) === "class" && value !== attribute.value ? value.split(ASCII_WHITESPACE).toSorted(byCodeUnits).join(" ") : value;
58
+ }
59
+ canonicalizeGeneratedIds(node, names);
60
+ } else if (isText(node)) node.value = renameGeneratedIds(node.value, names);
61
+ return names;
62
+ }
63
+ //#endregion
64
+ export { canonicalizeGeneratedIds, renameGeneratedIds };
@@ -0,0 +1,39 @@
1
+ import { attributeName, forEachElement } from "../tree.js";
2
+ //#region src/normalize/rules/ordering.ts
3
+ /** Compares by UTF-16 code units, so the order never depends on a locale. */
4
+ const byCodeUnits = (a, b) => a < b ? -1 : a > b ? 1 : 0;
5
+ /**
6
+ * Rule 5: sorts every element's attributes by name. Attribute order carries no meaning, and
7
+ * each framework writes them in its own order (React and Solid follow props, Qwik puts `:`
8
+ * first, Vue moves `class` and `style`). It runs before ids are numbered (rule 7), so the
9
+ * numbering never depends on a framework's attribute order.
10
+ */
11
+ function sortAttributes(root) {
12
+ forEachElement(root, (element) => {
13
+ element.attrs = element.attrs.toSorted((a, b) => byCodeUnits(attributeName(a), attributeName(b)));
14
+ });
15
+ }
16
+ /** ASCII whitespace, which separates the tokens of a `class` attribute. */
17
+ const ASCII_WHITESPACE = /[\t\n\f\r ]+/;
18
+ /**
19
+ * Rule 4c: sorts the tokens of every `class` attribute and joins them with one space. Token
20
+ * order and spacing carry no meaning. Duplicate tokens are kept: a doubled class usually means
21
+ * a merge went wrong. A `class` with no token goes (ADR-0044, amending ADR-0031): frameworks
22
+ * disagree even with themselves on writing an empty one (Vue's server writes `class=""` where
23
+ * its client writes none, Svelte's client the other way round, and Chromium keeps
24
+ * `class=""` once the last token is toggled off), and it applies no class: only a `[class]`
25
+ * selector tells the two apart.
26
+ */
27
+ function canonicalizeClasses(root) {
28
+ forEachElement(root, (element) => {
29
+ element.attrs = element.attrs.flatMap((attribute) => {
30
+ if (attributeName(attribute) !== "class") return [attribute];
31
+ const tokens = attribute.value.split(ASCII_WHITESPACE).filter((token) => token !== "");
32
+ if (!tokens.length) return [];
33
+ attribute.value = tokens.toSorted(byCodeUnits).join(" ");
34
+ return [attribute];
35
+ });
36
+ });
37
+ }
38
+ //#endregion
39
+ export { canonicalizeClasses, sortAttributes };
@@ -0,0 +1,21 @@
1
+ import { childrenOf, getAttribute, isElement, replaceChildren } from "../tree.js";
2
+ //#region src/normalize/rules/qwik-styles.ts
3
+ /**
4
+ * Rule 2c: removes every `<style q:style>` element, with its content, from Qwik's output
5
+ * (ADR-0063). A component's `useStyles$` sheet is written where the component renders by Qwik's
6
+ * server render of a fragment, and in `<head>` by its client render, so the server HTML has an
7
+ * element the client's markup does not. Only a `style` carrying Qwik's `q:style` goes: it runs
8
+ * before rule 2 removes that attribute, and a `<style>` the source writes stays.
9
+ */
10
+ function removeQwikStyles(root, target) {
11
+ if (target !== "qwik") return;
12
+ remove(root);
13
+ }
14
+ function remove(root) {
15
+ const children = childrenOf(root);
16
+ const kept = children.filter((node) => !isElement(node) || node.tagName !== "style" || getAttribute(node, "q:style") === void 0);
17
+ if (kept.length !== children.length) replaceChildren(root, kept);
18
+ for (const node of kept) if (isElement(node)) remove(node);
19
+ }
20
+ //#endregion
21
+ export { removeQwikStyles };
@@ -0,0 +1,20 @@
1
+ import { childrenOf, isElement, replaceChildren } from "../tree.js";
2
+ //#region src/normalize/rules/qwik-templates.ts
3
+ /**
4
+ * Rule 2b: removes every `q:template` element, with its content, from Qwik's output (ADR-0058).
5
+ * Qwik's server render keeps a slot's unclaimed fallback in one, hidden and `aria-hidden`, for
6
+ * resumption (`addUnclaimedProjection`); it renders nowhere, and the client render does not have
7
+ * it. Only Qwik writes it: the same element in another target's output stays.
8
+ */
9
+ function removeQwikTemplates(root, target) {
10
+ if (target !== "qwik") return;
11
+ remove(root);
12
+ }
13
+ function remove(root) {
14
+ const children = childrenOf(root);
15
+ const kept = children.filter((node) => !isElement(node) || node.tagName !== "q:template");
16
+ if (kept.length !== children.length) replaceChildren(root, kept);
17
+ for (const node of kept) if (isElement(node)) remove(node);
18
+ }
19
+ //#endregion
20
+ export { removeQwikTemplates };
@@ -0,0 +1,125 @@
1
+ import { parseStyle, printStyle, sortDeclarations } from "../style.js";
2
+ import { HTML_NAMESPACE, attributeName, forEachElement } from "../tree.js";
3
+ //#region src/normalize/rules/values.ts
4
+ /**
5
+ * Rule 4a: rewrites every `style` attribute in the CSSOM's format (`a: b; c: d;`): comments and
6
+ * spacing go and property names are lowercased (see `style.ts`). Then (ADR-0044, amending
7
+ * ADR-0031), as frameworks disagree on these even with themselves:
8
+ *
9
+ * - a declaration with an empty value goes: the CSSOM ignores it, and Vue's server renders a
10
+ * bound empty or nullish value as `color:;` where its client sets nothing;
11
+ * - the declarations are sorted by property name, unless the order of two of them decides what
12
+ * renders (the same property twice, a shorthand and its longhand, a flow-relative longhand and
13
+ * a physical one): then they keep their order (`sortDeclarations`). A client appends a
14
+ * declaration it sets again after removing it, and Angular applies its static `style` first;
15
+ * - a `style` with no declaration left goes, as a style that declares nothing is no style:
16
+ * Vue's server writes `style=""` where its client writes none.
17
+ *
18
+ * An overridden declaration is kept, in its place. Only the browser knows whether the later one
19
+ * is valid: if it is not, the earlier one is what renders (`color: red; color: nonsense` is red),
20
+ * so dropping it would equate a fallback with its loss. The live DOM's `style.cssText` has
21
+ * already been through the CSSOM, so the DOM path needs no such guess.
22
+ */
23
+ function canonicalizeStyles(root) {
24
+ forEachElement(root, (element) => {
25
+ element.attrs = element.attrs.flatMap((attribute) => {
26
+ if (attributeName(attribute) !== "style") return [attribute];
27
+ const declarations = parseStyle(attribute.value).filter((declaration) => declaration.value !== "");
28
+ if (!declarations.length) return [];
29
+ attribute.value = printStyle(sortDeclarations(declarations));
30
+ return [attribute];
31
+ });
32
+ });
33
+ }
34
+ /** Boolean attributes every HTML element can have. */
35
+ const GLOBAL_BOOLEAN_ATTRIBUTES = [
36
+ "autofocus",
37
+ "hidden",
38
+ "inert",
39
+ "itemscope"
40
+ ];
41
+ /** Boolean attributes per HTML element, from the HTML standard's attribute index. */
42
+ const BOOLEAN_ATTRIBUTES = {
43
+ audio: [
44
+ "autoplay",
45
+ "controls",
46
+ "loop",
47
+ "muted"
48
+ ],
49
+ button: ["disabled", "formnovalidate"],
50
+ details: ["open"],
51
+ dialog: ["open"],
52
+ fieldset: ["disabled"],
53
+ form: ["novalidate"],
54
+ iframe: ["allowfullscreen"],
55
+ img: ["ismap"],
56
+ input: [
57
+ "alpha",
58
+ "checked",
59
+ "disabled",
60
+ "formnovalidate",
61
+ "multiple",
62
+ "readonly",
63
+ "required"
64
+ ],
65
+ link: ["disabled"],
66
+ ol: ["reversed"],
67
+ optgroup: ["disabled"],
68
+ option: ["disabled", "selected"],
69
+ script: [
70
+ "async",
71
+ "defer",
72
+ "nomodule"
73
+ ],
74
+ select: [
75
+ "disabled",
76
+ "multiple",
77
+ "required"
78
+ ],
79
+ template: [
80
+ "shadowrootclonable",
81
+ "shadowrootcustomelementregistry",
82
+ "shadowrootdelegatesfocus",
83
+ "shadowrootserializable"
84
+ ],
85
+ textarea: [
86
+ "disabled",
87
+ "readonly",
88
+ "required"
89
+ ],
90
+ track: ["default"],
91
+ video: [
92
+ "autoplay",
93
+ "controls",
94
+ "loop",
95
+ "muted",
96
+ "playsinline"
97
+ ]
98
+ };
99
+ /**
100
+ * Rule 4b: writes every boolean attribute of an HTML element as `name=""`. The standard allows
101
+ * the empty string or the attribute's own name, and frameworks pick either (`disabled`,
102
+ * `disabled=""`, `disabled="disabled"`). Any other value (`disabled="false"`) is invalid HTML
103
+ * that still switches the state on, so it is kept for the reader to see. `hidden="until-found"`
104
+ * is a state of its own and stays too.
105
+ *
106
+ * Qwik's client is the one exception (ADR-0031: noise is per target): Qwik 2.0 beta writes a
107
+ * boolean attribute that is on as `name="true"`, a constant one through `setAttribute(name,
108
+ * true)` and a bound one too, but for the few whose DOM property has the same lower-case name,
109
+ * where its server writes `name=""`. The IR only ever turns a boolean attribute on with `true`,
110
+ * so on Qwik `"true"` is that, and only Qwik's `"true"` is rewritten.
111
+ */
112
+ function canonicalizeBooleanAttributes(root, target) {
113
+ forEachElement(root, (element) => {
114
+ if (element.namespaceURI !== HTML_NAMESPACE) return;
115
+ const own = BOOLEAN_ATTRIBUTES[element.tagName] ?? [];
116
+ for (const attribute of element.attrs) {
117
+ const name = attributeName(attribute);
118
+ if (!GLOBAL_BOOLEAN_ATTRIBUTES.includes(name) && !own.includes(name)) continue;
119
+ const value = attribute.value.toLowerCase();
120
+ if (value === name || target === "qwik" && value === "true") attribute.value = "";
121
+ }
122
+ });
123
+ }
124
+ //#endregion
125
+ export { canonicalizeBooleanAttributes, canonicalizeStyles };