@md-code/component-uniqueness 0.1.4 → 0.3.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 (189) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +202 -0
  3. package/README.md +21 -3
  4. package/dist/config.d.ts +50 -0
  5. package/dist/config.js +402 -0
  6. package/dist/constants.d.ts +55 -0
  7. package/dist/constants.js +86 -0
  8. package/dist/decision-maker.d.ts +39 -0
  9. package/dist/decision-maker.js +119 -0
  10. package/dist/filter/child-element.d.ts +3 -0
  11. package/dist/filter/child-element.js +17 -0
  12. package/dist/filter/missing-component.d.ts +10 -0
  13. package/dist/filter/missing-component.js +96 -0
  14. package/dist/filter/no-structure.d.ts +3 -0
  15. package/dist/filter/no-structure.js +16 -0
  16. package/dist/filter/usages.d.ts +3 -0
  17. package/dist/filter/usages.js +14 -0
  18. package/dist/index.d.ts +105 -0
  19. package/dist/index.js +244 -0
  20. package/dist/lib-registry.d.ts +4 -0
  21. package/dist/lib-registry.js +376 -0
  22. package/dist/logger.d.ts +7 -0
  23. package/dist/logger.js +70 -0
  24. package/dist/main.d.ts +25 -0
  25. package/dist/main.js +666 -0
  26. package/dist/matcher/a11y.d.ts +19 -0
  27. package/dist/matcher/a11y.js +311 -0
  28. package/dist/matcher/behavior.d.ts +10 -0
  29. package/dist/matcher/behavior.js +32 -0
  30. package/dist/matcher/css-matching.d.ts +18 -0
  31. package/dist/matcher/css-matching.js +159 -0
  32. package/dist/matcher/framework/index.d.ts +2 -0
  33. package/dist/matcher/framework/index.js +37 -0
  34. package/dist/matcher/framework/react.d.ts +12 -0
  35. package/dist/matcher/framework/react.js +70 -0
  36. package/dist/matcher/index.d.ts +64 -0
  37. package/dist/matcher/index.js +38 -0
  38. package/dist/matcher/name.d.ts +15 -0
  39. package/dist/matcher/name.js +154 -0
  40. package/dist/matcher/structure.d.ts +22 -0
  41. package/dist/matcher/structure.js +329 -0
  42. package/dist/matcher/styles.d.ts +20 -0
  43. package/dist/matcher/styles.js +49 -0
  44. package/dist/normalize/candidate.d.ts +3 -0
  45. package/dist/normalize/candidate.js +21 -0
  46. package/dist/normalize/dom.d.ts +22 -0
  47. package/dist/normalize/dom.js +180 -0
  48. package/dist/normalize/external-html.d.ts +5 -0
  49. package/dist/normalize/external-html.js +40 -0
  50. package/dist/normalize/framework.d.ts +20 -0
  51. package/dist/normalize/framework.js +130 -0
  52. package/dist/normalize/resolver.d.ts +7 -0
  53. package/dist/normalize/resolver.js +28 -0
  54. package/dist/normalize/style-resolver.d.ts +7 -0
  55. package/dist/normalize/style-resolver.js +28 -0
  56. package/dist/normalize/style.d.ts +15 -0
  57. package/dist/normalize/style.js +174 -0
  58. package/dist/normalize/words.d.ts +3 -0
  59. package/dist/normalize/words.js +40 -0
  60. package/dist/package.json +88 -0
  61. package/dist/playgrounds/angular.d.ts +7 -0
  62. package/dist/playgrounds/angular.js +9 -0
  63. package/dist/playgrounds/framework-entrypoint.d.ts +10 -0
  64. package/dist/playgrounds/framework-entrypoint.js +102 -0
  65. package/dist/playgrounds/framework.d.ts +10 -0
  66. package/dist/playgrounds/framework.js +102 -0
  67. package/dist/playgrounds/react.d.ts +14 -0
  68. package/dist/playgrounds/react.js +1288 -0
  69. package/dist/playgrounds/scss.d.ts +6 -0
  70. package/dist/playgrounds/scss.js +9 -0
  71. package/dist/playgrounds/style-entrypoint.d.ts +13 -0
  72. package/dist/playgrounds/style-entrypoint.js +82 -0
  73. package/dist/playgrounds/style.d.ts +13 -0
  74. package/dist/playgrounds/style.js +82 -0
  75. package/dist/playgrounds/styled-components.d.ts +7 -0
  76. package/dist/playgrounds/styled-components.js +190 -0
  77. package/dist/playgrounds/tailwind.d.ts +15 -0
  78. package/dist/playgrounds/tailwind.js +256 -0
  79. package/dist/playgrounds/vue.d.ts +7 -0
  80. package/dist/playgrounds/vue.js +9 -0
  81. package/dist/report/asks.d.ts +27 -0
  82. package/dist/report/asks.js +28 -0
  83. package/dist/report/cli-report.d.ts +7 -0
  84. package/dist/report/cli-report.js +187 -0
  85. package/dist/report/codes/index.d.ts +37 -0
  86. package/dist/report/codes/index.js +31 -0
  87. package/dist/report/errorTypes/index.d.ts +92 -0
  88. package/dist/report/errorTypes/index.js +186 -0
  89. package/dist/report/evidence.d.ts +2 -0
  90. package/dist/report/evidence.js +8 -0
  91. package/dist/report/explain.d.ts +27 -0
  92. package/dist/report/explain.js +76 -0
  93. package/dist/report/index.d.ts +2 -0
  94. package/dist/report/index.js +24 -0
  95. package/dist/report/matchers/a11y/model.d.ts +20 -0
  96. package/dist/report/matchers/a11y/model.js +14 -0
  97. package/dist/report/matchers/a11y/text.d.ts +2 -0
  98. package/dist/report/matchers/a11y/text.js +22 -0
  99. package/dist/report/matchers/behavior/model.d.ts +20 -0
  100. package/dist/report/matchers/behavior/model.js +12 -0
  101. package/dist/report/matchers/behavior/text.d.ts +2 -0
  102. package/dist/report/matchers/behavior/text.js +22 -0
  103. package/dist/report/matchers/framework/model.d.ts +10 -0
  104. package/dist/report/matchers/framework/model.js +39 -0
  105. package/dist/report/matchers/framework/text.d.ts +2 -0
  106. package/dist/report/matchers/framework/text.js +26 -0
  107. package/dist/report/matchers/index.d.ts +5 -0
  108. package/dist/report/matchers/index.js +97 -0
  109. package/dist/report/matchers/name/model.d.ts +20 -0
  110. package/dist/report/matchers/name/model.js +30 -0
  111. package/dist/report/matchers/name/text.d.ts +2 -0
  112. package/dist/report/matchers/name/text.js +17 -0
  113. package/dist/report/matchers/set-model.d.ts +31 -0
  114. package/dist/report/matchers/set-model.js +42 -0
  115. package/dist/report/matchers/structure/model.d.ts +10 -0
  116. package/dist/report/matchers/structure/model.js +58 -0
  117. package/dist/report/matchers/structure/text.d.ts +2 -0
  118. package/dist/report/matchers/structure/text.js +40 -0
  119. package/dist/report/matchers/styles/model.d.ts +11 -0
  120. package/dist/report/matchers/styles/model.js +36 -0
  121. package/dist/report/matchers/styles/text.d.ts +2 -0
  122. package/dist/report/matchers/styles/text.js +40 -0
  123. package/dist/report/user-report/build/bundle.d.ts +3 -0
  124. package/dist/report/user-report/build/bundle.js +185 -0
  125. package/dist/report/user-report/build/bundle.prebuilt.js +955 -0
  126. package/dist/report/user-report/data/collect.d.ts +68 -0
  127. package/dist/report/user-report/data/collect.js +150 -0
  128. package/dist/report/user-report/data/folders.d.ts +3 -0
  129. package/dist/report/user-report/data/folders.js +73 -0
  130. package/dist/report/user-report/data/groups.d.ts +13 -0
  131. package/dist/report/user-report/data/groups.js +76 -0
  132. package/dist/report/user-report/data/links.d.ts +39 -0
  133. package/dist/report/user-report/data/links.js +222 -0
  134. package/dist/report/user-report/data/snapshot.d.ts +9 -0
  135. package/dist/report/user-report/data/snapshot.js +180 -0
  136. package/dist/report/user-report/data/source.d.ts +13 -0
  137. package/dist/report/user-report/data/source.js +58 -0
  138. package/dist/report/user-report/data/word.d.ts +3 -0
  139. package/dist/report/user-report/data/word.js +32 -0
  140. package/dist/report/user-report/html/shell.d.ts +2 -0
  141. package/dist/report/user-report/html/shell.js +27 -0
  142. package/dist/report/user-report/index.d.ts +7 -0
  143. package/dist/report/user-report/index.js +47 -0
  144. package/dist/report/vocabulary/evidence.d.ts +48 -0
  145. package/dist/report/vocabulary/evidence.js +66 -0
  146. package/dist/report/vocabulary/format.d.ts +4 -0
  147. package/dist/report/vocabulary/format.js +20 -0
  148. package/dist/report/vocabulary/labels.d.ts +134 -0
  149. package/dist/report/vocabulary/labels.js +87 -0
  150. package/dist/report/vocabulary/phrases.d.ts +20 -0
  151. package/dist/report/vocabulary/phrases.js +34 -0
  152. package/dist/self-catalog.d.ts +4 -0
  153. package/dist/self-catalog.js +179 -0
  154. package/dist/utils.d.ts +6 -0
  155. package/dist/utils.js +125 -0
  156. package/package.json +86 -71
  157. package/config.js +0 -372
  158. package/decision-maker.js +0 -109
  159. package/filter/drop-child-element.js +0 -14
  160. package/filter/drop-no-structure.js +0 -13
  161. package/filter/drop-usages.js +0 -13
  162. package/filter/missing-component.js +0 -100
  163. package/index.js +0 -184
  164. package/logger.js +0 -44
  165. package/main.js +0 -584
  166. package/matcher/a11y.js +0 -65
  167. package/matcher/behavior.js +0 -31
  168. package/matcher/framework/index.js +0 -15
  169. package/matcher/framework/react.js +0 -25
  170. package/matcher/index.js +0 -36
  171. package/matcher/name.js +0 -132
  172. package/matcher/structure.js +0 -242
  173. package/normalize/candidate.js +0 -22
  174. package/normalize/dom.js +0 -187
  175. package/normalize/framework.js +0 -110
  176. package/normalize/style-resolver.js +0 -24
  177. package/normalize/style.js +0 -175
  178. package/normalize/words.js +0 -40
  179. package/playgrounds/angular.js +0 -11
  180. package/playgrounds/framework-entrypoint.js +0 -90
  181. package/playgrounds/react.js +0 -998
  182. package/playgrounds/scss.js +0 -7
  183. package/playgrounds/style-entrypoint.js +0 -78
  184. package/playgrounds/styled-components.js +0 -138
  185. package/playgrounds/tailwind.js +0 -186
  186. package/playgrounds/vue.js +0 -11
  187. package/plugin.js +0 -3
  188. package/report.js +0 -162
  189. package/utils.js +0 -92
@@ -0,0 +1,64 @@
1
+ declare const matchSignals: (candidate: any, canon: any, { canonNames, frameworkMatcher }: {
2
+ canonNames: any;
3
+ frameworkMatcher: any;
4
+ }) => {
5
+ name: {
6
+ score: number;
7
+ evidence: {
8
+ exactName: boolean;
9
+ exactBasename: boolean;
10
+ fuzzy: any;
11
+ wordSim: number;
12
+ matchType: any;
13
+ };
14
+ };
15
+ structure: {
16
+ score: number;
17
+ applicable: any;
18
+ evidence: any;
19
+ };
20
+ styles: {
21
+ score: number;
22
+ applicable: boolean;
23
+ evidence: {
24
+ pairs: number;
25
+ properties?: undefined;
26
+ agreeing?: undefined;
27
+ details?: undefined;
28
+ };
29
+ } | {
30
+ score: number;
31
+ applicable: boolean;
32
+ evidence: {
33
+ pairs: number;
34
+ properties: number;
35
+ agreeing: number;
36
+ details: any[];
37
+ };
38
+ };
39
+ behavior: {
40
+ score: number;
41
+ evidence: {
42
+ candidateEvents: unknown[];
43
+ canonicalEvents: unknown[];
44
+ sharedEvents: unknown[];
45
+ smallSetDiscount: number;
46
+ };
47
+ };
48
+ a11y: {
49
+ score: number;
50
+ evidence: {
51
+ candidateAnnouncements?: string[];
52
+ canonicalAnnouncements?: string[];
53
+ candidateA11y: unknown[];
54
+ canonicalA11y: unknown[];
55
+ candidateTestId: unknown[];
56
+ canonicalTestId: unknown[];
57
+ a11ySimilarity: any;
58
+ testIdSimilarity: number;
59
+ form: any;
60
+ };
61
+ };
62
+ framework: any;
63
+ };
64
+ export { matchSignals };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchSignals = void 0;
4
+ const name_1 = require("./name");
5
+ const structure_1 = require("./structure");
6
+ const styles_1 = require("./styles");
7
+ const behavior_1 = require("./behavior");
8
+ const a11y_1 = require("./a11y");
9
+ const restrictTo = (candidate, canon) => {
10
+ const events = new Set(candidate.framework.events);
11
+ const a11y = (0, a11y_1.tokensOf)(candidate, "a11y");
12
+ const openProps = canon.framework.open ? [] : canon.framework.props || [];
13
+ // An OPEN canonical (spread passthrough) imposes no component usages either:
14
+ // the part's own usages are not evidence about the canonical's framework.
15
+ const usages = canon.framework.open ? [] : canon.framework.usages || [];
16
+ return {
17
+ ...canon,
18
+ framework: {
19
+ props: openProps,
20
+ events: (canon.framework.events || []).filter((e) => events.has(e)),
21
+ names: [],
22
+ usages,
23
+ },
24
+ a11y: (canon.a11y || []).filter((t) => a11y.has(String(t).toLowerCase())),
25
+ };
26
+ };
27
+ const matchSignals = (candidate, canon, { canonNames, frameworkMatcher }) => {
28
+ const target = candidate.partial ? restrictTo(candidate, canon) : canon;
29
+ return {
30
+ name: (0, name_1.matchName)(candidate, target, { canonNames }),
31
+ structure: (0, structure_1.matchStructure)(candidate, target),
32
+ styles: (0, styles_1.matchStyles)(candidate, target),
33
+ behavior: (0, behavior_1.matchBehavior)(candidate, target),
34
+ a11y: (0, a11y_1.matchA11y)(candidate, target),
35
+ framework: frameworkMatcher.matchFramework(candidate, target),
36
+ };
37
+ };
38
+ exports.matchSignals = matchSignals;
@@ -0,0 +1,15 @@
1
+ declare const kebabToPascal: (s: any) => any;
2
+ declare const basenameToPascal: (file: any) => any;
3
+ declare const fuzzyNameHit: (appName: any, canonNames: any) => any;
4
+ declare const fuzzyBasenameHit: (appPath: any, canonNames: any) => any;
5
+ declare const matchName: (candidate: any, canon: any, ctx: any) => {
6
+ score: number;
7
+ evidence: {
8
+ exactName: boolean;
9
+ exactBasename: boolean;
10
+ fuzzy: any;
11
+ wordSim: number;
12
+ matchType: any;
13
+ };
14
+ };
15
+ export { matchName, kebabToPascal, basenameToPascal, fuzzyNameHit, fuzzyBasenameHit, };
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.fuzzyBasenameHit = exports.fuzzyNameHit = exports.basenameToPascal = exports.kebabToPascal = exports.matchName = void 0;
27
+ const path = __importStar(require("node:path"));
28
+ const words_js_1 = require("../normalize/words.js");
29
+ const FUZZY_NAME_MIN_LEN = 5;
30
+ const FUZZY_BASENAME_MIN_LEN = 4;
31
+ const kebabToPascal = (s) => s
32
+ .split(/[-_ ]+/)
33
+ .filter(Boolean)
34
+ .map((w) => w[0].toUpperCase() + w.slice(1))
35
+ .join("");
36
+ exports.kebabToPascal = kebabToPascal;
37
+ const basenameToPascal = (file) => kebabToPascal(path.basename(file, path.extname(file)));
38
+ exports.basenameToPascal = basenameToPascal;
39
+ const kebabWords = (s) => String(s || "").split(/[^A-Za-z0-9]+/).filter(Boolean);
40
+ const singular = (w) => (w.length > 3 && w.endsWith("s") && !w.endsWith("ss") ? w.slice(0, -1) : w);
41
+ const wordSim = (a, b) => {
42
+ const wa = new Set((0, words_js_1.pascalWords)(a).map((w) => singular(w.toLowerCase())).filter((w) => w.length >= 3 && !words_js_1.GENERIC_WORDS.has(w)));
43
+ const wb = new Set((0, words_js_1.pascalWords)(b).map((w) => singular(w.toLowerCase())).filter((w) => w.length >= 3 && !words_js_1.GENERIC_WORDS.has(w)));
44
+ if (wa.size === 0 || wb.size === 0)
45
+ return 0;
46
+ let inter = 0;
47
+ for (const w of wa)
48
+ if (wb.has(w))
49
+ inter += 1;
50
+ const union = wa.size + wb.size - inter;
51
+ return union === 0 ? 0 : inter / union;
52
+ };
53
+ const fuzzyNameHit = (appName, canonNames) => {
54
+ const words = (0, words_js_1.pascalWords)(appName);
55
+ if (words.length === 0)
56
+ return null;
57
+ let best = null;
58
+ for (const cn of canonNames) {
59
+ if (cn.length < FUZZY_NAME_MIN_LEN)
60
+ continue;
61
+ const lc = cn.toLowerCase();
62
+ for (const w of words) {
63
+ const lw = w.toLowerCase();
64
+ if (lw === lc || (lw.length >= FUZZY_NAME_MIN_LEN && (lw.startsWith(lc) || lw.endsWith(lc)))) {
65
+ if (!best || cn.length > best.name.length)
66
+ best = { name: cn };
67
+ break;
68
+ }
69
+ }
70
+ }
71
+ return best;
72
+ };
73
+ exports.fuzzyNameHit = fuzzyNameHit;
74
+ const fuzzyBasenameHit = (appPath, canonNames) => {
75
+ const base = path.basename(appPath, path.extname(appPath));
76
+ const words = kebabWords(base).map((w) => w.toLowerCase());
77
+ if (words.length === 0)
78
+ return null;
79
+ const fullKebab = words.join("-");
80
+ let best = null;
81
+ for (const cn of canonNames) {
82
+ if (cn.length < FUZZY_BASENAME_MIN_LEN)
83
+ continue;
84
+ const cnWords = (0, words_js_1.pascalWords)(cn).map((w) => w.toLowerCase());
85
+ const cnKebab = cnWords.join("-");
86
+ if (fullKebab === cnKebab || fullKebab === singular(cnKebab) || fullKebab === cnKebab.replace(/s$/, "")) {
87
+ if (!best || cn.length > best.name.length)
88
+ best = { name: cn };
89
+ continue;
90
+ }
91
+ const lc = cn.toLowerCase();
92
+ const lcs = singular(lc);
93
+ for (const w of words) {
94
+ if (w === lc || w === lcs || singular(w) === lc) {
95
+ if (!best || cn.length > best.name.length)
96
+ best = { name: cn };
97
+ break;
98
+ }
99
+ }
100
+ if (best && best.name === cn)
101
+ continue;
102
+ for (const w of words) {
103
+ if (w.length < 4)
104
+ continue;
105
+ const ws = singular(w);
106
+ if (cnWords.some((cw) => cw === w || cw === ws || singular(cw) === ws)) {
107
+ if (!best || cn.length > best.name.length)
108
+ best = { name: cn };
109
+ break;
110
+ }
111
+ }
112
+ }
113
+ return best;
114
+ };
115
+ exports.fuzzyBasenameHit = fuzzyBasenameHit;
116
+ const matchName = (candidate, canon, ctx) => {
117
+ const o = ctx || {};
118
+ const canonNames = o.canonNames || [canon.name];
119
+ const exactName = candidate.name === canon.name;
120
+ const exactBasename = !exactName && basenameToPascal(candidate.path) === canon.name;
121
+ let fuzzy = null;
122
+ if (!exactName && !exactBasename) {
123
+ fuzzy = fuzzyNameHit(candidate.name, [canon.name]) || fuzzyBasenameHit(candidate.path, [canon.name]);
124
+ }
125
+ const word = wordSim(candidate.name, canon.name);
126
+ let score = 0;
127
+ let matchType = null;
128
+ if (exactName) {
129
+ score = 1;
130
+ matchType = "exact-name";
131
+ }
132
+ else if (exactBasename) {
133
+ score = 0.9;
134
+ matchType = "exact-basename";
135
+ }
136
+ else if (fuzzy) {
137
+ const byName = !!fuzzyNameHit(candidate.name, [canon.name]);
138
+ score = (byName ? 0.7 : 0.6) * (0.5 + 0.5 * word);
139
+ matchType = byName ? "fuzzy-name" : "fuzzy-basename";
140
+ }
141
+ else if (word > 0) {
142
+ score = 0.6 * word;
143
+ matchType = "word-overlap";
144
+ }
145
+ const evidence = {
146
+ exactName,
147
+ exactBasename,
148
+ fuzzy: fuzzy ? fuzzy.name : null,
149
+ wordSim: Math.round(word * 100) / 100,
150
+ matchType,
151
+ };
152
+ return { score, evidence };
153
+ };
154
+ exports.matchName = matchName;
@@ -0,0 +1,22 @@
1
+ declare const nodeSimilarity: (a: any, b: any) => number;
2
+ declare const chainList: (node: any) => any[];
3
+ declare const bestChain: (a: any, b: any) => {
4
+ score: number;
5
+ quality: number;
6
+ chainSize: number;
7
+ refSize: number;
8
+ pairs: any[];
9
+ };
10
+ declare const alignTrees: (a: any, b: any) => {
11
+ score: number;
12
+ quality: number;
13
+ matched: number;
14
+ refSize: number;
15
+ pairs: any[];
16
+ };
17
+ declare const matchStructure: (candidate: any, canon: any) => {
18
+ score: number;
19
+ applicable: any;
20
+ evidence: any;
21
+ };
22
+ export { matchStructure, bestChain, alignTrees, chainList, nodeSimilarity };
@@ -0,0 +1,329 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nodeSimilarity = exports.chainList = exports.alignTrees = exports.bestChain = exports.matchStructure = void 0;
4
+ const dom_js_1 = require("../normalize/dom.js");
5
+ const CONFLICT_PENALTY = 0.6;
6
+ const NEAR_IDENTICAL_CEILING = 0.98;
7
+ // A partial duplicate is a *faithful copy* of a region of the canonical: every
8
+ // matched node must closely resemble its counterpart (tag + styling). A short
9
+ // chain of generic containers that only shares tag names (e.g. a wireframe
10
+ // div>span>strong matching a dynamic list's div>span) has one weakly-matched
11
+ // node whose similarity is well below this floor, so the chain is not a real
12
+ // copy and the structure signal is zeroed out. We check the *minimum* per-node
13
+ // similarity, not the average: the average can be inflated by a single strong
14
+ // node (e.g. a matching font-size) while the rest of the chain is generic.
15
+ const MIN_PARTIAL_NODE_SIM = 0.8;
16
+ const isTextNode = (node) => node.tag === "children" || node.tag === "text";
17
+ const isIconNode = (node) => (0, dom_js_1.isPascalCase)(node.tag);
18
+ const nodeCount = (tree) => {
19
+ if (!tree)
20
+ return 0;
21
+ const own = tree.tag === "fragment" || tree.tag === "children" ? 0 : 1;
22
+ return own + (tree.children || []).reduce((sum, child) => sum + nodeCount(child), 0);
23
+ };
24
+ const pairsOf = (css) => new Set(Object.entries(css || {}).map(([prop, value]) => `${prop}:${value}`));
25
+ const overlap = (a, b) => [...a].filter((pair) => b.has(pair)).length;
26
+ const jaccard = (a, b) => {
27
+ const union = a.size + b.size - overlap(a, b);
28
+ return union === 0 ? 0 : overlap(a, b) / union;
29
+ };
30
+ const textSim = (a, b) => {
31
+ const ta = String(a.text || "").toLowerCase().trim();
32
+ const tb = String(b.text || "").toLowerCase().trim();
33
+ if (!ta && !tb)
34
+ return 1;
35
+ if (!ta || !tb)
36
+ return 0;
37
+ return ta === tb ? 1 : 0;
38
+ };
39
+ const tagMatch = (a, b) => {
40
+ if (a.interactive && b.interactive)
41
+ return 1;
42
+ const affinity = (0, dom_js_1.tagAffinity)(a.tag, b.tag);
43
+ if (affinity > 0)
44
+ return affinity;
45
+ return 0;
46
+ };
47
+ const nodeSimilarity = (a, b) => {
48
+ if (isTextNode(a) && isTextNode(b))
49
+ return textSim(a, b);
50
+ if (isIconNode(a) && isIconNode(b))
51
+ return a.tag === b.tag ? 1 : 0.1;
52
+ const tag = tagMatch(a, b);
53
+ const pa = pairsOf(a.css);
54
+ const pb = pairsOf(b.css);
55
+ let score = pa.size === 0 && pb.size === 0 ? tag : 0.6 * tag + 0.4 * jaccard(pa, pb);
56
+ const ta = String(a.text || "").trim();
57
+ const tb = String(b.text || "").trim();
58
+ if (ta || tb)
59
+ score = 0.7 * score + 0.3 * textSim(a, b);
60
+ return score;
61
+ };
62
+ exports.nodeSimilarity = nodeSimilarity;
63
+ const chainList = (node) => {
64
+ const list = [];
65
+ const walk = (n, parentIdx) => {
66
+ if (!n)
67
+ return;
68
+ if (n.tag === "fragment" || n.tag === "children") {
69
+ for (const child of n.children || [])
70
+ walk(child, parentIdx);
71
+ return;
72
+ }
73
+ if (n.tag === "text")
74
+ return;
75
+ const idx = list.length;
76
+ list.push({ node: n, parent: parentIdx });
77
+ for (const child of n.children || [])
78
+ walk(child, idx);
79
+ };
80
+ walk(node, -1);
81
+ return list;
82
+ };
83
+ exports.chainList = chainList;
84
+ const bestChain = (a, b) => {
85
+ const xs = chainList(a);
86
+ const ys = chainList(b);
87
+ const refSize = Math.max(xs.length, ys.length);
88
+ if (xs.length === 0 || ys.length === 0)
89
+ return { score: 0, quality: 0, chainSize: 0, refSize, pairs: [] };
90
+ const dp = xs.map(() => new Array(ys.length).fill(0));
91
+ const len = xs.map(() => new Array(ys.length).fill(0));
92
+ let best = { score: 0, quality: 0, chainSize: 0, i: -1, j: -1 };
93
+ for (let i = 0; i < xs.length; i += 1) {
94
+ for (let j = 0; j < ys.length; j += 1) {
95
+ const sim = nodeSimilarity(xs[i].node, ys[j].node);
96
+ let value = sim;
97
+ let length = 1;
98
+ const pi = xs[i].parent;
99
+ const pj = ys[j].parent;
100
+ if (pi >= 0 && pj >= 0 && dp[pi][pj] > 0) {
101
+ value += dp[pi][pj];
102
+ length += len[pi][pj];
103
+ }
104
+ dp[i][j] = value;
105
+ len[i][j] = length;
106
+ const quality = value / length;
107
+ const score = quality * (length / refSize);
108
+ if (score > best.score)
109
+ best = { score, quality, chainSize: length, i, j };
110
+ }
111
+ }
112
+ const pairs = [];
113
+ if (best.i >= 0) {
114
+ let i = best.i;
115
+ let j = best.j;
116
+ while (i >= 0 && j >= 0) {
117
+ pairs.unshift([xs[i].node, ys[j].node]);
118
+ i = xs[i].parent;
119
+ j = ys[j].parent;
120
+ }
121
+ }
122
+ return { score: best.score, quality: best.quality, chainSize: best.chainSize, refSize, pairs };
123
+ };
124
+ exports.bestChain = bestChain;
125
+ const cssStats = (pairs) => {
126
+ let comparable = 0;
127
+ let conflicts = 0;
128
+ let shared = 0;
129
+ let union = 0;
130
+ for (const [a, b] of pairs) {
131
+ for (const [prop, value] of Object.entries(a.css || {})) {
132
+ if (!Object.prototype.hasOwnProperty.call(b.css || {}, prop))
133
+ continue;
134
+ comparable += 1;
135
+ if (b.css[prop] !== value)
136
+ conflicts += 1;
137
+ }
138
+ const pa = pairsOf(a.css);
139
+ const pb = pairsOf(b.css);
140
+ shared += overlap(pa, pb);
141
+ union += pa.size + pb.size - overlap(pa, pb);
142
+ }
143
+ return { conflicts, ratio: comparable === 0 ? 0 : conflicts / comparable, agreement: union === 0 ? 0 : shared / union };
144
+ };
145
+ // ── FULL-TREE ALIGNMENT ─────────────────────────────────────────────────────
146
+ //
147
+ // A component is a TREE, not a chain: a page has a header AND a main AND a
148
+ // footer, each with its own subtree. The best single root-to-leaf chain covers
149
+ // only one branch of such a tree, so two wide pages that render the same HTML
150
+ // scored near zero. The full comparison aligns BOTH flattened node lists in
151
+ // document order (LCS-style DP over nodeSimilarity): every branch can match its
152
+ // counterpart, unmatched nodes lower the score instead of disappearing, and the
153
+ // order keeps a header from matching a footer.
154
+ const MIN_ALIGN_SIM = 0.5;
155
+ const alignTrees = (a, b) => {
156
+ const xs = chainList(a);
157
+ const ys = chainList(b);
158
+ const refSize = Math.max(xs.length, ys.length);
159
+ if (xs.length === 0 || ys.length === 0)
160
+ return { score: 0, quality: 0, matched: 0, refSize, pairs: [] };
161
+ const n = xs.length;
162
+ const m = ys.length;
163
+ const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
164
+ for (let i = n - 1; i >= 0; i -= 1) {
165
+ for (let j = m - 1; j >= 0; j -= 1) {
166
+ const s = nodeSimilarity(xs[i].node, ys[j].node);
167
+ const take = s >= MIN_ALIGN_SIM ? s + dp[i + 1][j + 1] : -1;
168
+ dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1], take);
169
+ }
170
+ }
171
+ const pairs = [];
172
+ let i = 0;
173
+ let j = 0;
174
+ while (i < n && j < m) {
175
+ const s = nodeSimilarity(xs[i].node, ys[j].node);
176
+ if (s >= MIN_ALIGN_SIM && Math.abs(dp[i][j] - (s + dp[i + 1][j + 1])) < 1e-9) {
177
+ pairs.push([xs[i].node, ys[j].node]);
178
+ i += 1;
179
+ j += 1;
180
+ }
181
+ else if (dp[i + 1][j] >= dp[i][j + 1]) {
182
+ i += 1;
183
+ }
184
+ else {
185
+ j += 1;
186
+ }
187
+ }
188
+ const total = dp[0][0];
189
+ // A structural correspondence must cover MOST of the larger tree. A few
190
+ // coincidentally similar nodes (a shared nav row, a generic container) are
191
+ // not a copied structure: below half coverage the signal is zero, so shared
192
+ // styles/behavior alone cannot carry an unrelated pair into a verdict.
193
+ const coverage = pairs.length / refSize;
194
+ const score = coverage >= 0.5 ? total / refSize : 0;
195
+ return { score, quality: pairs.length > 0 ? total / pairs.length : 0, matched: pairs.length, refSize, pairs };
196
+ };
197
+ exports.alignTrees = alignTrees;
198
+ const isInformative = (node) => !!node && (node.tag !== "fragment" || (node.children || []).some(isInformative));
199
+ const classTokensDiffer = (a, b) => {
200
+ const A = new Set(a || []);
201
+ const B = new Set(b || []);
202
+ if (A.size === 0 && B.size === 0)
203
+ return false;
204
+ for (const token of A)
205
+ if (!B.has(token))
206
+ return true;
207
+ for (const token of B)
208
+ if (!A.has(token))
209
+ return true;
210
+ return false;
211
+ };
212
+ const treeSimilarity = (a, b) => {
213
+ const result = bestChain(a, b);
214
+ // A genuine partial duplicate is a *faithful copy*: every matched node closely
215
+ // resembles its counterpart. The average `quality` alone is not enough — a weak
216
+ // generic container (div>div>strong) can ride along with one strong node and push
217
+ // the average up while the chain as a whole is not a real copy. The minimum
218
+ // per-node similarity exposes that weak link.
219
+ let minNodeSim = 1;
220
+ for (const [x, y] of result.pairs) {
221
+ const sim = nodeSimilarity(x, y);
222
+ if (sim < minNodeSim)
223
+ minNodeSim = sim;
224
+ }
225
+ return { score: result.score, quality: result.quality, minNodeSim, detail: { similarity: result.score, quality: result.quality, minNodeSim, chainSize: result.chainSize, refSize: result.refSize, pairs: result.pairs.length } };
226
+ };
227
+ const firstReal = (node) => {
228
+ if (!node)
229
+ return null;
230
+ if (node.tag === "fragment" || node.tag === "children" || node.tag === "text") {
231
+ for (const child of node.children || []) {
232
+ const found = firstReal(child);
233
+ if (found)
234
+ return found;
235
+ }
236
+ return null;
237
+ }
238
+ return node;
239
+ };
240
+ const rootSimilarity = (a, b) => {
241
+ const ra = firstReal(a);
242
+ const rb = firstReal(b);
243
+ if (!ra || !rb)
244
+ return { score: 0, detail: { rootSim: 0 } };
245
+ return { score: nodeSimilarity(ra, rb), detail: { rootSim: nodeSimilarity(ra, rb) } };
246
+ };
247
+ const MIN_CROSS_TAG_CLASS_OVERLAP = 3;
248
+ const rootClassOverlap = (candidate, canon) => {
249
+ const rt = firstReal(candidate.tree);
250
+ if (!rt)
251
+ return 0;
252
+ const partToks = new Set(String(rt.className || "").split(/\s+/).filter(Boolean));
253
+ const canonToks = new Set(canon.rawClasses || []);
254
+ let n = 0;
255
+ for (const t of partToks)
256
+ if (canonToks.has(t))
257
+ n += 1;
258
+ return n;
259
+ };
260
+ const matchStructure = (candidate, canon) => {
261
+ const cand = candidate.dom && candidate.dom.tree;
262
+ const target = canon.dom && canon.dom.tree;
263
+ const mode = candidate.partial ? "partial" : "tree";
264
+ const applicable = isInformative(cand) || isInformative(target);
265
+ let score = 0;
266
+ let detail = null;
267
+ if (applicable && cand && target) {
268
+ if (candidate.partial) {
269
+ const singleton = nodeCount(target) === 1;
270
+ const partNodes = nodeCount(cand);
271
+ const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
272
+ if (singleton) {
273
+ const ra = firstReal(cand);
274
+ const rb = firstReal(target);
275
+ const partTag = ra ? ra.tag : "";
276
+ const canonTag = rb ? rb.tag : "";
277
+ const crossTag = dom_js_1.GENERIC_CONTAINER_TAGS.has(partTag) && dom_js_1.GENERIC_CONTAINER_TAGS.has(canonTag) && partTag !== canonTag;
278
+ if (crossTag) {
279
+ const overlap = rootClassOverlap(candidate, canon);
280
+ if (overlap < MIN_CROSS_TAG_CLASS_OVERLAP) {
281
+ score = 0;
282
+ detail = { singleton, crossTag, overlap, rejected: true };
283
+ }
284
+ else {
285
+ score = Math.min(0.9, 0.5 + 0.05 * overlap);
286
+ detail = { singleton, crossTag, overlap };
287
+ }
288
+ }
289
+ else if (partNodes > 4) {
290
+ score = 0;
291
+ detail = { singleton, partNodes, leafVsWrapper: true };
292
+ }
293
+ else {
294
+ const { score: q, detail: d } = rootSimilarity(cand, target);
295
+ score = Math.min(q, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
296
+ detail = { ...d, singleton, classesDiffer };
297
+ }
298
+ }
299
+ else {
300
+ const { quality, minNodeSim, detail: d } = treeSimilarity(cand, target);
301
+ const sizeFactor = Math.min(1, nodeCount(target) / nodeCount(cand));
302
+ const base = quality * sizeFactor;
303
+ if (minNodeSim < MIN_PARTIAL_NODE_SIM) {
304
+ score = 0;
305
+ detail = { ...d, singleton, sizeFactor, classesDiffer, rejected: true, reason: "node-sim-below-floor" };
306
+ }
307
+ else {
308
+ score = Math.min(base, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
309
+ detail = { ...d, singleton, sizeFactor, classesDiffer };
310
+ }
311
+ }
312
+ }
313
+ else {
314
+ // Full-tree alignment: every branch of both trees can match, not just
315
+ // the single best root-to-leaf chain (a wide page is many branches).
316
+ const result = alignTrees(cand, target);
317
+ const stats = cssStats(result.pairs);
318
+ const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
319
+ const ceiling = classesDiffer ? NEAR_IDENTICAL_CEILING : 1;
320
+ score = Math.max(0, Math.min(result.score, ceiling) - CONFLICT_PENALTY * stats.ratio);
321
+ // WHICH nodes correspond: the aligned tag pairs with their source lines,
322
+ // so the report can name the intersection instead of a bare count.
323
+ const nodes = result.pairs.map(([x, y]) => ({ left: x.tag, right: y.tag, leftLine: x.line ?? null, rightLine: y.line ?? null }));
324
+ detail = { similarity: result.score, quality: result.quality, chainSize: result.matched, refSize: result.refSize, ceiling, classesDiffer, conflicts: stats.conflicts, conflictRatio: stats.ratio, agreement: stats.agreement, nodes };
325
+ }
326
+ }
327
+ return { score, applicable, evidence: { mode, ...(detail || {}) } };
328
+ };
329
+ exports.matchStructure = matchStructure;
@@ -0,0 +1,20 @@
1
+ declare const matchStyles: (candidate: any, canon: any) => {
2
+ score: number;
3
+ applicable: boolean;
4
+ evidence: {
5
+ pairs: number;
6
+ properties?: undefined;
7
+ agreeing?: undefined;
8
+ details?: undefined;
9
+ };
10
+ } | {
11
+ score: number;
12
+ applicable: boolean;
13
+ evidence: {
14
+ pairs: number;
15
+ properties: number;
16
+ agreeing: number;
17
+ details: any[];
18
+ };
19
+ };
20
+ export { matchStyles };