@mizchi/vlmkit-core 0.11.1 → 0.22.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,41 @@
1
+ import { FindingSeverity, RuleView } from "./contract.mjs";
2
+ import { TieredRow } from "./rule-tier.mjs";
3
+
4
+ //#region src/plugin/rule-prose.d.ts
5
+ /** The shape all seven reports' rows already have. */
6
+ interface IssueLike {
7
+ kind: string;
8
+ severity: FindingSeverity;
9
+ }
10
+ interface TieredIssues<T> {
11
+ /** Rows whose rule is still on, each carrying the severity to print it at. */
12
+ shown: TieredRow<T>[];
13
+ /**
14
+ * The gate's own `status:` word, computed from what survives.
15
+ *
16
+ * `ok` when nothing survives — including when the only reason nothing survives is that the
17
+ * rows were turned off. That is the honest word for it: the project said these do not count,
18
+ * and `note` is printed alongside so the screen still says how many were dropped.
19
+ */
20
+ status: "ok" | "info" | "warn" | "suspect";
21
+ /** `hiddenByRuleNote`'s line, or undefined when the settings dropped nothing. */
22
+ note?: string;
23
+ }
24
+ /**
25
+ * Tier a report's `issues[]` and derive the status word.
26
+ *
27
+ * Callers keep their own icons and wording — the palettes and the glyphs (`x` vs `✗`) differ
28
+ * per gate and are not worth unifying — but they must read `tier` from the returned rows rather
29
+ * than `severity` from their own, which is the point of routing through here.
30
+ */
31
+ declare function tierIssues<T extends IssueLike>(issues: readonly T[], rules?: RuleView): TieredIssues<T>;
32
+ /**
33
+ * ` [kind re-tuned to warn]`, or `""` when the row prints at the severity it was emitted at.
34
+ *
35
+ * Without it a demoted row is indistinguishable from one the gate itself graded that way, and
36
+ * the reader has no way to tell a measurement from a policy. The suffix names the rule id
37
+ * because that is the string they would edit to change it back.
38
+ */
39
+ declare function retuneNote<T extends IssueLike>(entry: TieredRow<T>): string;
40
+ //#endregion
41
+ export { IssueLike, TieredIssues, retuneNote, tierIssues };
@@ -0,0 +1,34 @@
1
+ import { applyRuleTiers, hiddenByRuleNote } from "./rule-tier.mjs";
2
+ //#region src/plugin/rule-prose.ts
3
+ /**
4
+ * Tier a report's `issues[]` and derive the status word.
5
+ *
6
+ * Callers keep their own icons and wording — the palettes and the glyphs (`x` vs `✗`) differ
7
+ * per gate and are not worth unifying — but they must read `tier` from the returned rows rather
8
+ * than `severity` from their own, which is the point of routing through here.
9
+ */
10
+ function tierIssues(issues, rules) {
11
+ const { shown, hiddenByRule } = applyRuleTiers(issues, (issue) => ({
12
+ rule: issue.kind,
13
+ emitted: issue.severity
14
+ }), rules);
15
+ const status = shown.some((s) => s.tier === "suspect") ? "suspect" : shown.some((s) => s.tier === "warn") ? "warn" : shown.length > 0 ? "info" : "ok";
16
+ const note = hiddenByRuleNote(hiddenByRule);
17
+ return {
18
+ shown,
19
+ status,
20
+ ...note ? { note } : {}
21
+ };
22
+ }
23
+ /**
24
+ * ` [kind re-tuned to warn]`, or `""` when the row prints at the severity it was emitted at.
25
+ *
26
+ * Without it a demoted row is indistinguishable from one the gate itself graded that way, and
27
+ * the reader has no way to tell a measurement from a policy. The suffix names the rule id
28
+ * because that is the string they would edit to change it back.
29
+ */
30
+ function retuneNote(entry) {
31
+ return entry.tier === entry.row.severity ? "" : ` [${entry.row.kind} re-tuned to ${entry.tier}]`;
32
+ }
33
+ //#endregion
34
+ export { retuneNote, tierIssues };
@@ -0,0 +1,50 @@
1
+ //#region src/stable-selector.d.ts
2
+ /**
3
+ * The one in-page selector generator, as a script fragment.
4
+ *
5
+ * Six gates needed "name this element so a reader can find it again", and six
6
+ * copies of this function existed. Three of them had lost the recursive call:
7
+ *
8
+ * const parent = el.parentElement;
9
+ * if (!parent) return el.tagName.toLowerCase();
10
+ * const siblings = Array.from(parent.children).filter((s) => s.tagName === el.tagName);
11
+ * return el.tagName.toLowerCase() + ":nth-of-type(" + (siblings.indexOf(el) + 1) + ")";
12
+ *
13
+ * which returns `p:nth-of-type(1)` — the first `<p>` of *every* parent on the page.
14
+ * `check animation` on a page with two animated first-children reported three
15
+ * findings on two different elements, all three carrying the identical selector
16
+ * `div:nth-of-type(1)`, which matched both. The report named nothing.
17
+ *
18
+ * The `if (!parent) return` line is what gives the accident away: without recursion a
19
+ * root element returning `html` rather than `html:nth-of-type(1)` is a distinction
20
+ * with no purpose. It is a base case, and the recursive step it guarded was dropped.
21
+ *
22
+ * ## The contract
23
+ *
24
+ * The returned selector matches **exactly one** element in the document it was
25
+ * generated from. That is the whole point — it is what makes a finding actionable,
26
+ * what lets two findings be told apart, and what `--allow "<selector>;<reason>"`
27
+ * matches against. `packages/vlmkit-markup/src/selector-uniqueness.test.ts` asserts
28
+ * it against real gate output rather than against this source, so a seventh copy
29
+ * appearing elsewhere is still caught.
30
+ *
31
+ * Preference order: `#id` → `tag.class` when unique → recursive
32
+ * `<parent-path> > tag:nth-of-type(n)`.
33
+ *
34
+ * ## Why a string
35
+ *
36
+ * These run inside `page.evaluate`, and most callers assemble their collector as a
37
+ * template literal, so the shared form has to be text. Interpolate it at the top of
38
+ * the script:
39
+ *
40
+ * ```ts
41
+ * const COLLECT = `(() => { ${STABLE_SELECTOR_JS} … })()`;
42
+ * ```
43
+ *
44
+ * `motion-detect.ts` is the exception: it passes a real typed arrow to
45
+ * `page.evaluate`, so it carries its own copy on purpose and the uniqueness test is
46
+ * what holds the two in agreement.
47
+ */
48
+ declare const STABLE_SELECTOR_JS = "\n function stableSelector(el) {\n if (!el || !el.tagName) return \"(no target)\";\n const id = el.getAttribute && el.getAttribute(\"id\");\n if (id) return \"#\" + CSS.escape(id);\n const classes = el.classList ? Array.from(el.classList).slice(0, 3) : [];\n if (classes.length > 0) {\n const selector = el.tagName.toLowerCase() + classes.map((c) => \".\" + CSS.escape(c)).join(\"\");\n if (document.querySelectorAll(selector).length === 1) return selector;\n }\n const parent = el.parentElement;\n if (!parent) return el.tagName.toLowerCase();\n const siblings = Array.from(parent.children).filter((item) => item.tagName === el.tagName);\n return stableSelector(parent) + \" > \" + el.tagName.toLowerCase() + \":nth-of-type(\" + (siblings.indexOf(el) + 1) + \")\";\n }\n";
49
+ //#endregion
50
+ export { STABLE_SELECTOR_JS };
@@ -0,0 +1,65 @@
1
+ //#region src/stable-selector.ts
2
+ /**
3
+ * The one in-page selector generator, as a script fragment.
4
+ *
5
+ * Six gates needed "name this element so a reader can find it again", and six
6
+ * copies of this function existed. Three of them had lost the recursive call:
7
+ *
8
+ * const parent = el.parentElement;
9
+ * if (!parent) return el.tagName.toLowerCase();
10
+ * const siblings = Array.from(parent.children).filter((s) => s.tagName === el.tagName);
11
+ * return el.tagName.toLowerCase() + ":nth-of-type(" + (siblings.indexOf(el) + 1) + ")";
12
+ *
13
+ * which returns `p:nth-of-type(1)` — the first `<p>` of *every* parent on the page.
14
+ * `check animation` on a page with two animated first-children reported three
15
+ * findings on two different elements, all three carrying the identical selector
16
+ * `div:nth-of-type(1)`, which matched both. The report named nothing.
17
+ *
18
+ * The `if (!parent) return` line is what gives the accident away: without recursion a
19
+ * root element returning `html` rather than `html:nth-of-type(1)` is a distinction
20
+ * with no purpose. It is a base case, and the recursive step it guarded was dropped.
21
+ *
22
+ * ## The contract
23
+ *
24
+ * The returned selector matches **exactly one** element in the document it was
25
+ * generated from. That is the whole point — it is what makes a finding actionable,
26
+ * what lets two findings be told apart, and what `--allow "<selector>;<reason>"`
27
+ * matches against. `packages/vlmkit-markup/src/selector-uniqueness.test.ts` asserts
28
+ * it against real gate output rather than against this source, so a seventh copy
29
+ * appearing elsewhere is still caught.
30
+ *
31
+ * Preference order: `#id` → `tag.class` when unique → recursive
32
+ * `<parent-path> > tag:nth-of-type(n)`.
33
+ *
34
+ * ## Why a string
35
+ *
36
+ * These run inside `page.evaluate`, and most callers assemble their collector as a
37
+ * template literal, so the shared form has to be text. Interpolate it at the top of
38
+ * the script:
39
+ *
40
+ * ```ts
41
+ * const COLLECT = `(() => { ${STABLE_SELECTOR_JS} … })()`;
42
+ * ```
43
+ *
44
+ * `motion-detect.ts` is the exception: it passes a real typed arrow to
45
+ * `page.evaluate`, so it carries its own copy on purpose and the uniqueness test is
46
+ * what holds the two in agreement.
47
+ */
48
+ const STABLE_SELECTOR_JS = `
49
+ function stableSelector(el) {
50
+ if (!el || !el.tagName) return "(no target)";
51
+ const id = el.getAttribute && el.getAttribute("id");
52
+ if (id) return "#" + CSS.escape(id);
53
+ const classes = el.classList ? Array.from(el.classList).slice(0, 3) : [];
54
+ if (classes.length > 0) {
55
+ const selector = el.tagName.toLowerCase() + classes.map((c) => "." + CSS.escape(c)).join("");
56
+ if (document.querySelectorAll(selector).length === 1) return selector;
57
+ }
58
+ const parent = el.parentElement;
59
+ if (!parent) return el.tagName.toLowerCase();
60
+ const siblings = Array.from(parent.children).filter((item) => item.tagName === el.tagName);
61
+ return stableSelector(parent) + " > " + el.tagName.toLowerCase() + ":nth-of-type(" + (siblings.indexOf(el) + 1) + ")";
62
+ }
63
+ `;
64
+ //#endregion
65
+ export { STABLE_SELECTOR_JS };
@@ -15,6 +15,6 @@
15
15
  * every `package.json` and that the CHANGELOG has a section for it, so the literal cannot drift
16
16
  * from the release either.
17
17
  */
18
- declare const VLMKIT_VERSION = "0.11.1";
18
+ declare const VLMKIT_VERSION = "0.22.0";
19
19
  //#endregion
20
20
  export { VLMKIT_VERSION };
package/dist/version.mjs CHANGED
@@ -15,6 +15,6 @@
15
15
  * every `package.json` and that the CHANGELOG has a section for it, so the literal cannot drift
16
16
  * from the release either.
17
17
  */
18
- const VLMKIT_VERSION = "0.11.1";
18
+ const VLMKIT_VERSION = "0.22.0";
19
19
  //#endregion
20
20
  export { VLMKIT_VERSION };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mizchi/vlmkit-core",
3
- "version": "0.11.1",
3
+ "version": "0.22.0",
4
4
  "description": "VRT diff engine + shared types — pure image / DOM / a11y / visual comparison primitives.",
5
5
  "license": "MIT",
6
6
  "author": "mizchi",