@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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +202 -0
- package/README.md +21 -3
- package/dist/config.d.ts +50 -0
- package/dist/config.js +402 -0
- package/dist/constants.d.ts +55 -0
- package/dist/constants.js +86 -0
- package/dist/decision-maker.d.ts +39 -0
- package/dist/decision-maker.js +119 -0
- package/dist/filter/child-element.d.ts +3 -0
- package/dist/filter/child-element.js +17 -0
- package/dist/filter/missing-component.d.ts +10 -0
- package/dist/filter/missing-component.js +96 -0
- package/dist/filter/no-structure.d.ts +3 -0
- package/dist/filter/no-structure.js +16 -0
- package/dist/filter/usages.d.ts +3 -0
- package/dist/filter/usages.js +14 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +244 -0
- package/dist/lib-registry.d.ts +4 -0
- package/dist/lib-registry.js +376 -0
- package/dist/logger.d.ts +7 -0
- package/dist/logger.js +70 -0
- package/dist/main.d.ts +25 -0
- package/dist/main.js +666 -0
- package/dist/matcher/a11y.d.ts +19 -0
- package/dist/matcher/a11y.js +311 -0
- package/dist/matcher/behavior.d.ts +10 -0
- package/dist/matcher/behavior.js +32 -0
- package/dist/matcher/css-matching.d.ts +18 -0
- package/dist/matcher/css-matching.js +159 -0
- package/dist/matcher/framework/index.d.ts +2 -0
- package/dist/matcher/framework/index.js +37 -0
- package/dist/matcher/framework/react.d.ts +12 -0
- package/dist/matcher/framework/react.js +70 -0
- package/dist/matcher/index.d.ts +64 -0
- package/dist/matcher/index.js +38 -0
- package/dist/matcher/name.d.ts +15 -0
- package/dist/matcher/name.js +154 -0
- package/dist/matcher/structure.d.ts +22 -0
- package/dist/matcher/structure.js +329 -0
- package/dist/matcher/styles.d.ts +20 -0
- package/dist/matcher/styles.js +49 -0
- package/dist/normalize/candidate.d.ts +3 -0
- package/dist/normalize/candidate.js +21 -0
- package/dist/normalize/dom.d.ts +22 -0
- package/dist/normalize/dom.js +180 -0
- package/dist/normalize/external-html.d.ts +5 -0
- package/dist/normalize/external-html.js +40 -0
- package/dist/normalize/framework.d.ts +20 -0
- package/dist/normalize/framework.js +130 -0
- package/dist/normalize/resolver.d.ts +7 -0
- package/dist/normalize/resolver.js +28 -0
- package/dist/normalize/style-resolver.d.ts +7 -0
- package/dist/normalize/style-resolver.js +28 -0
- package/dist/normalize/style.d.ts +15 -0
- package/dist/normalize/style.js +174 -0
- package/dist/normalize/words.d.ts +3 -0
- package/dist/normalize/words.js +40 -0
- package/dist/package.json +88 -0
- package/dist/playgrounds/angular.d.ts +7 -0
- package/dist/playgrounds/angular.js +9 -0
- package/dist/playgrounds/framework-entrypoint.d.ts +10 -0
- package/dist/playgrounds/framework-entrypoint.js +102 -0
- package/dist/playgrounds/framework.d.ts +10 -0
- package/dist/playgrounds/framework.js +102 -0
- package/dist/playgrounds/react.d.ts +14 -0
- package/dist/playgrounds/react.js +1288 -0
- package/dist/playgrounds/scss.d.ts +6 -0
- package/dist/playgrounds/scss.js +9 -0
- package/dist/playgrounds/style-entrypoint.d.ts +13 -0
- package/dist/playgrounds/style-entrypoint.js +82 -0
- package/dist/playgrounds/style.d.ts +13 -0
- package/dist/playgrounds/style.js +82 -0
- package/dist/playgrounds/styled-components.d.ts +7 -0
- package/dist/playgrounds/styled-components.js +190 -0
- package/dist/playgrounds/tailwind.d.ts +15 -0
- package/dist/playgrounds/tailwind.js +256 -0
- package/dist/playgrounds/vue.d.ts +7 -0
- package/dist/playgrounds/vue.js +9 -0
- package/dist/report/asks.d.ts +27 -0
- package/dist/report/asks.js +28 -0
- package/dist/report/cli-report.d.ts +7 -0
- package/dist/report/cli-report.js +187 -0
- package/dist/report/codes/index.d.ts +37 -0
- package/dist/report/codes/index.js +31 -0
- package/dist/report/errorTypes/index.d.ts +92 -0
- package/dist/report/errorTypes/index.js +186 -0
- package/dist/report/evidence.d.ts +2 -0
- package/dist/report/evidence.js +8 -0
- package/dist/report/explain.d.ts +27 -0
- package/dist/report/explain.js +76 -0
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +24 -0
- package/dist/report/matchers/a11y/model.d.ts +20 -0
- package/dist/report/matchers/a11y/model.js +14 -0
- package/dist/report/matchers/a11y/text.d.ts +2 -0
- package/dist/report/matchers/a11y/text.js +22 -0
- package/dist/report/matchers/behavior/model.d.ts +20 -0
- package/dist/report/matchers/behavior/model.js +12 -0
- package/dist/report/matchers/behavior/text.d.ts +2 -0
- package/dist/report/matchers/behavior/text.js +22 -0
- package/dist/report/matchers/framework/model.d.ts +10 -0
- package/dist/report/matchers/framework/model.js +39 -0
- package/dist/report/matchers/framework/text.d.ts +2 -0
- package/dist/report/matchers/framework/text.js +26 -0
- package/dist/report/matchers/index.d.ts +5 -0
- package/dist/report/matchers/index.js +97 -0
- package/dist/report/matchers/name/model.d.ts +20 -0
- package/dist/report/matchers/name/model.js +30 -0
- package/dist/report/matchers/name/text.d.ts +2 -0
- package/dist/report/matchers/name/text.js +17 -0
- package/dist/report/matchers/set-model.d.ts +31 -0
- package/dist/report/matchers/set-model.js +42 -0
- package/dist/report/matchers/structure/model.d.ts +10 -0
- package/dist/report/matchers/structure/model.js +58 -0
- package/dist/report/matchers/structure/text.d.ts +2 -0
- package/dist/report/matchers/structure/text.js +40 -0
- package/dist/report/matchers/styles/model.d.ts +11 -0
- package/dist/report/matchers/styles/model.js +36 -0
- package/dist/report/matchers/styles/text.d.ts +2 -0
- package/dist/report/matchers/styles/text.js +40 -0
- package/dist/report/user-report/build/bundle.d.ts +3 -0
- package/dist/report/user-report/build/bundle.js +185 -0
- package/dist/report/user-report/build/bundle.prebuilt.js +955 -0
- package/dist/report/user-report/data/collect.d.ts +68 -0
- package/dist/report/user-report/data/collect.js +150 -0
- package/dist/report/user-report/data/folders.d.ts +3 -0
- package/dist/report/user-report/data/folders.js +73 -0
- package/dist/report/user-report/data/groups.d.ts +13 -0
- package/dist/report/user-report/data/groups.js +76 -0
- package/dist/report/user-report/data/links.d.ts +39 -0
- package/dist/report/user-report/data/links.js +222 -0
- package/dist/report/user-report/data/snapshot.d.ts +9 -0
- package/dist/report/user-report/data/snapshot.js +180 -0
- package/dist/report/user-report/data/source.d.ts +13 -0
- package/dist/report/user-report/data/source.js +58 -0
- package/dist/report/user-report/data/word.d.ts +3 -0
- package/dist/report/user-report/data/word.js +32 -0
- package/dist/report/user-report/html/shell.d.ts +2 -0
- package/dist/report/user-report/html/shell.js +27 -0
- package/dist/report/user-report/index.d.ts +7 -0
- package/dist/report/user-report/index.js +47 -0
- package/dist/report/vocabulary/evidence.d.ts +48 -0
- package/dist/report/vocabulary/evidence.js +66 -0
- package/dist/report/vocabulary/format.d.ts +4 -0
- package/dist/report/vocabulary/format.js +20 -0
- package/dist/report/vocabulary/labels.d.ts +134 -0
- package/dist/report/vocabulary/labels.js +87 -0
- package/dist/report/vocabulary/phrases.d.ts +20 -0
- package/dist/report/vocabulary/phrases.js +34 -0
- package/dist/self-catalog.d.ts +4 -0
- package/dist/self-catalog.js +179 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +125 -0
- package/package.json +86 -71
- package/config.js +0 -372
- package/decision-maker.js +0 -109
- package/filter/drop-child-element.js +0 -14
- package/filter/drop-no-structure.js +0 -13
- package/filter/drop-usages.js +0 -13
- package/filter/missing-component.js +0 -100
- package/index.js +0 -184
- package/logger.js +0 -44
- package/main.js +0 -584
- package/matcher/a11y.js +0 -65
- package/matcher/behavior.js +0 -31
- package/matcher/framework/index.js +0 -15
- package/matcher/framework/react.js +0 -25
- package/matcher/index.js +0 -36
- package/matcher/name.js +0 -132
- package/matcher/structure.js +0 -242
- package/normalize/candidate.js +0 -22
- package/normalize/dom.js +0 -187
- package/normalize/framework.js +0 -110
- package/normalize/style-resolver.js +0 -24
- package/normalize/style.js +0 -175
- package/normalize/words.js +0 -40
- package/playgrounds/angular.js +0 -11
- package/playgrounds/framework-entrypoint.js +0 -90
- package/playgrounds/react.js +0 -998
- package/playgrounds/scss.js +0 -7
- package/playgrounds/style-entrypoint.js +0 -78
- package/playgrounds/styled-components.js +0 -138
- package/playgrounds/tailwind.js +0 -186
- package/playgrounds/vue.js +0 -11
- package/plugin.js +0 -3
- package/report.js +0 -162
- package/utils.js +0 -92
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { frameworkSimilarity } = require("../../normalize/framework.js");
|
|
4
|
-
|
|
5
|
-
const matchFramework = (candidate, canon) => {
|
|
6
|
-
const cand = candidate.framework || { props: [], events: [], names: [] };
|
|
7
|
-
const canonFw = canon.framework || { props: [], events: [], names: [] };
|
|
8
|
-
const sim = frameworkSimilarity(cand, canonFw, `${candidate.name} vs ${canon.name}`);
|
|
9
|
-
return {
|
|
10
|
-
score: sim.score,
|
|
11
|
-
evidence: {
|
|
12
|
-
propsSimilarity: sim.props,
|
|
13
|
-
eventsSimilarity: sim.events,
|
|
14
|
-
namesSimilarity: sim.names,
|
|
15
|
-
candidateProps: cand.props,
|
|
16
|
-
canonicalProps: canonFw.props,
|
|
17
|
-
candidateEvents: cand.events,
|
|
18
|
-
canonicalEvents: canonFw.events,
|
|
19
|
-
candidateNames: cand.names,
|
|
20
|
-
canonicalNames: canonFw.names,
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
module.exports = { matchFramework };
|
package/matcher/index.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { matchName } = require("./name");
|
|
4
|
-
const { matchStructure } = require("./structure");
|
|
5
|
-
const { matchBehavior } = require("./behavior");
|
|
6
|
-
const { matchA11y, tokensOf } = require("./a11y");
|
|
7
|
-
|
|
8
|
-
const restrictTo = (candidate, canon) => {
|
|
9
|
-
const events = new Set(candidate.framework.events);
|
|
10
|
-
const a11y = tokensOf(candidate, "a11y");
|
|
11
|
-
const openProps = canon.framework.open ? [] : canon.framework.props || [];
|
|
12
|
-
|
|
13
|
-
return {
|
|
14
|
-
...canon,
|
|
15
|
-
framework: {
|
|
16
|
-
props: openProps,
|
|
17
|
-
events: (canon.framework.events || []).filter((e) => events.has(e)),
|
|
18
|
-
names: [],
|
|
19
|
-
},
|
|
20
|
-
a11y: (canon.a11y || []).filter((t) => a11y.has(String(t).toLowerCase())),
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const matchSignals = (candidate, canon, { canonNames, frameworkMatcher }) => {
|
|
25
|
-
const target = candidate.partial ? restrictTo(candidate, canon) : canon;
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
name: matchName(candidate, target, { canonNames }),
|
|
29
|
-
structure: matchStructure(candidate, target),
|
|
30
|
-
behavior: matchBehavior(candidate, target),
|
|
31
|
-
a11y: matchA11y(candidate, target),
|
|
32
|
-
framework: frameworkMatcher.matchFramework(candidate, target),
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
module.exports = { matchSignals };
|
package/matcher/name.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
const { pascalWords, GENERIC_WORDS } = require("../normalize/words.js");
|
|
5
|
-
|
|
6
|
-
const FUZZY_NAME_MIN_LEN = 5;
|
|
7
|
-
const FUZZY_BASENAME_MIN_LEN = 4;
|
|
8
|
-
|
|
9
|
-
const kebabToPascal = (s) =>
|
|
10
|
-
s
|
|
11
|
-
.split(/[-_ ]+/)
|
|
12
|
-
.filter(Boolean)
|
|
13
|
-
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
14
|
-
.join("");
|
|
15
|
-
|
|
16
|
-
const basenameToPascal = (file) => kebabToPascal(path.basename(file, path.extname(file)));
|
|
17
|
-
|
|
18
|
-
const kebabWords = (s) => String(s || "").split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
19
|
-
const singular = (w) => (w.length > 3 && w.endsWith("s") && !w.endsWith("ss") ? w.slice(0, -1) : w);
|
|
20
|
-
|
|
21
|
-
const wordSim = (a, b) => {
|
|
22
|
-
const wa = new Set(pascalWords(a).map((w) => singular(w.toLowerCase())).filter((w) => w.length >= 3 && !GENERIC_WORDS.has(w)));
|
|
23
|
-
const wb = new Set(pascalWords(b).map((w) => singular(w.toLowerCase())).filter((w) => w.length >= 3 && !GENERIC_WORDS.has(w)));
|
|
24
|
-
if (wa.size === 0 || wb.size === 0) return 0;
|
|
25
|
-
let inter = 0;
|
|
26
|
-
for (const w of wa) if (wb.has(w)) inter += 1;
|
|
27
|
-
const union = wa.size + wb.size - inter;
|
|
28
|
-
|
|
29
|
-
return union === 0 ? 0 : inter / union;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const fuzzyNameHit = (appName, canonNames) => {
|
|
33
|
-
const words = pascalWords(appName);
|
|
34
|
-
if (words.length === 0) return null;
|
|
35
|
-
let best = null;
|
|
36
|
-
for (const cn of canonNames) {
|
|
37
|
-
if (cn.length < FUZZY_NAME_MIN_LEN) continue;
|
|
38
|
-
const lc = cn.toLowerCase();
|
|
39
|
-
for (const w of words) {
|
|
40
|
-
const lw = w.toLowerCase();
|
|
41
|
-
if (lw === lc || (lw.length >= FUZZY_NAME_MIN_LEN && (lw.startsWith(lc) || lw.endsWith(lc)))) {
|
|
42
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return best;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const fuzzyBasenameHit = (appPath, canonNames) => {
|
|
51
|
-
const base = path.basename(appPath, path.extname(appPath));
|
|
52
|
-
const words = kebabWords(base).map((w) => w.toLowerCase());
|
|
53
|
-
if (words.length === 0) return null;
|
|
54
|
-
const fullKebab = words.join("-");
|
|
55
|
-
let best = null;
|
|
56
|
-
for (const cn of canonNames) {
|
|
57
|
-
if (cn.length < FUZZY_BASENAME_MIN_LEN) continue;
|
|
58
|
-
const cnWords = pascalWords(cn).map((w) => w.toLowerCase());
|
|
59
|
-
const cnKebab = cnWords.join("-");
|
|
60
|
-
if (fullKebab === cnKebab || fullKebab === singular(cnKebab) || fullKebab === cnKebab.replace(/s$/, "")) {
|
|
61
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
const lc = cn.toLowerCase();
|
|
65
|
-
const lcs = singular(lc);
|
|
66
|
-
for (const w of words) {
|
|
67
|
-
if (w === lc || w === lcs || singular(w) === lc) {
|
|
68
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (best && best.name === cn) continue;
|
|
73
|
-
for (const w of words) {
|
|
74
|
-
if (w.length < 4) continue;
|
|
75
|
-
const ws = singular(w);
|
|
76
|
-
if (cnWords.some((cw) => cw === w || cw === ws || singular(cw) === ws)) {
|
|
77
|
-
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return best;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const matchName = (candidate, canon, ctx) => {
|
|
86
|
-
const o = ctx || {};
|
|
87
|
-
const canonNames = o.canonNames || [canon.name];
|
|
88
|
-
|
|
89
|
-
const exactName = candidate.name === canon.name;
|
|
90
|
-
const exactBasename = !exactName && basenameToPascal(candidate.path) === canon.name;
|
|
91
|
-
|
|
92
|
-
let fuzzy = null;
|
|
93
|
-
if (!exactName && !exactBasename) {
|
|
94
|
-
fuzzy = fuzzyNameHit(candidate.name, [canon.name]) || fuzzyBasenameHit(candidate.path, [canon.name]);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const word = wordSim(candidate.name, canon.name);
|
|
98
|
-
|
|
99
|
-
let score = 0;
|
|
100
|
-
let matchType = null;
|
|
101
|
-
if (exactName) {
|
|
102
|
-
score = 1;
|
|
103
|
-
matchType = "exact-name";
|
|
104
|
-
} else if (exactBasename) {
|
|
105
|
-
score = 0.9;
|
|
106
|
-
matchType = "exact-basename";
|
|
107
|
-
} else if (fuzzy) {
|
|
108
|
-
const byName = !!fuzzyNameHit(candidate.name, [canon.name]);
|
|
109
|
-
score = (byName ? 0.7 : 0.6) * (0.5 + 0.5 * word);
|
|
110
|
-
matchType = byName ? "fuzzy-name" : "fuzzy-basename";
|
|
111
|
-
} else if (word > 0) {
|
|
112
|
-
score = 0.6 * word;
|
|
113
|
-
matchType = "word-overlap";
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const evidence = {
|
|
117
|
-
exactName,
|
|
118
|
-
exactBasename,
|
|
119
|
-
fuzzy: fuzzy ? fuzzy.name : null,
|
|
120
|
-
wordSim: Math.round(word * 100) / 100,
|
|
121
|
-
matchType,
|
|
122
|
-
};
|
|
123
|
-
return { score, evidence };
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
module.exports = {
|
|
127
|
-
matchName,
|
|
128
|
-
kebabToPascal,
|
|
129
|
-
basenameToPascal,
|
|
130
|
-
fuzzyNameHit,
|
|
131
|
-
fuzzyBasenameHit,
|
|
132
|
-
};
|
package/matcher/structure.js
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { isPascalCase, tagAffinity, GENERIC_CONTAINER_TAGS } = require("../normalize/dom.js");
|
|
4
|
-
|
|
5
|
-
const CONFLICT_PENALTY = 0.6;
|
|
6
|
-
const NEAR_IDENTICAL_CEILING = 0.98;
|
|
7
|
-
|
|
8
|
-
const isTextNode = (node) => node.tag === "children" || node.tag === "text";
|
|
9
|
-
const isIconNode = (node) => isPascalCase(node.tag);
|
|
10
|
-
|
|
11
|
-
const nodeCount = (tree) => {
|
|
12
|
-
if (!tree) return 0;
|
|
13
|
-
const own = tree.tag === "fragment" || tree.tag === "children" ? 0 : 1;
|
|
14
|
-
|
|
15
|
-
return own + (tree.children || []).reduce((sum, child) => sum + nodeCount(child), 0);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const pairsOf = (css) => new Set(Object.entries(css || {}).map(([prop, value]) => `${prop}:${value}`));
|
|
19
|
-
|
|
20
|
-
const overlap = (a, b) => [...a].filter((pair) => b.has(pair)).length;
|
|
21
|
-
|
|
22
|
-
const jaccard = (a, b) => {
|
|
23
|
-
const union = a.size + b.size - overlap(a, b);
|
|
24
|
-
|
|
25
|
-
return union === 0 ? 0 : overlap(a, b) / union;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const textSim = (a, b) => {
|
|
29
|
-
const ta = String(a.text || "").toLowerCase().trim();
|
|
30
|
-
const tb = String(b.text || "").toLowerCase().trim();
|
|
31
|
-
if (!ta && !tb) return 1;
|
|
32
|
-
if (!ta || !tb) return 0;
|
|
33
|
-
|
|
34
|
-
return ta === tb ? 1 : 0;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const tagMatch = (a, b) => {
|
|
38
|
-
if (a.interactive && b.interactive) return 1;
|
|
39
|
-
const affinity = tagAffinity(a.tag, b.tag);
|
|
40
|
-
if (affinity > 0) return affinity;
|
|
41
|
-
|
|
42
|
-
return 0;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const nodeSimilarity = (a, b) => {
|
|
46
|
-
if (isTextNode(a) && isTextNode(b)) return textSim(a, b);
|
|
47
|
-
if (isIconNode(a) && isIconNode(b)) return a.tag === b.tag ? 1 : 0.1;
|
|
48
|
-
const tag = tagMatch(a, b);
|
|
49
|
-
const pa = pairsOf(a.css);
|
|
50
|
-
const pb = pairsOf(b.css);
|
|
51
|
-
let score = pa.size === 0 && pb.size === 0 ? tag : 0.6 * tag + 0.4 * jaccard(pa, pb);
|
|
52
|
-
const ta = String(a.text || "").trim();
|
|
53
|
-
const tb = String(b.text || "").trim();
|
|
54
|
-
if (ta || tb) score = 0.7 * score + 0.3 * textSim(a, b);
|
|
55
|
-
|
|
56
|
-
return score;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const chainList = (node) => {
|
|
60
|
-
const list = [];
|
|
61
|
-
const walk = (n, parentIdx) => {
|
|
62
|
-
if (!n) return;
|
|
63
|
-
if (n.tag === "fragment" || n.tag === "children") {
|
|
64
|
-
for (const child of n.children || []) walk(child, parentIdx);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (n.tag === "text") return;
|
|
68
|
-
const idx = list.length;
|
|
69
|
-
list.push({ node: n, parent: parentIdx });
|
|
70
|
-
for (const child of n.children || []) walk(child, idx);
|
|
71
|
-
};
|
|
72
|
-
walk(node, -1);
|
|
73
|
-
|
|
74
|
-
return list;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const bestChain = (a, b) => {
|
|
78
|
-
const xs = chainList(a);
|
|
79
|
-
const ys = chainList(b);
|
|
80
|
-
const refSize = Math.max(xs.length, ys.length);
|
|
81
|
-
if (xs.length === 0 || ys.length === 0) return { score: 0, quality: 0, chainSize: 0, refSize, pairs: [] };
|
|
82
|
-
const dp = xs.map(() => new Array(ys.length).fill(0));
|
|
83
|
-
const len = xs.map(() => new Array(ys.length).fill(0));
|
|
84
|
-
let best = { score: 0, quality: 0, chainSize: 0, i: -1, j: -1 };
|
|
85
|
-
for (let i = 0; i < xs.length; i += 1) {
|
|
86
|
-
for (let j = 0; j < ys.length; j += 1) {
|
|
87
|
-
const sim = nodeSimilarity(xs[i].node, ys[j].node);
|
|
88
|
-
let value = sim;
|
|
89
|
-
let length = 1;
|
|
90
|
-
const pi = xs[i].parent;
|
|
91
|
-
const pj = ys[j].parent;
|
|
92
|
-
if (pi >= 0 && pj >= 0 && dp[pi][pj] > 0) {
|
|
93
|
-
value += dp[pi][pj];
|
|
94
|
-
length += len[pi][pj];
|
|
95
|
-
}
|
|
96
|
-
dp[i][j] = value;
|
|
97
|
-
len[i][j] = length;
|
|
98
|
-
const quality = value / length;
|
|
99
|
-
const score = quality * (length / refSize);
|
|
100
|
-
if (score > best.score) best = { score, quality, chainSize: length, i, j };
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
const pairs = [];
|
|
104
|
-
if (best.i >= 0) {
|
|
105
|
-
let i = best.i;
|
|
106
|
-
let j = best.j;
|
|
107
|
-
while (i >= 0 && j >= 0) {
|
|
108
|
-
pairs.unshift([xs[i].node, ys[j].node]);
|
|
109
|
-
i = xs[i].parent;
|
|
110
|
-
j = ys[j].parent;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return { score: best.score, quality: best.quality, chainSize: best.chainSize, refSize, pairs };
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const cssStats = (pairs) => {
|
|
118
|
-
let comparable = 0;
|
|
119
|
-
let conflicts = 0;
|
|
120
|
-
let shared = 0;
|
|
121
|
-
let union = 0;
|
|
122
|
-
for (const [a, b] of pairs) {
|
|
123
|
-
for (const [prop, value] of Object.entries(a.css || {})) {
|
|
124
|
-
if (!Object.prototype.hasOwnProperty.call(b.css || {}, prop)) continue;
|
|
125
|
-
comparable += 1;
|
|
126
|
-
if (b.css[prop] !== value) conflicts += 1;
|
|
127
|
-
}
|
|
128
|
-
const pa = pairsOf(a.css);
|
|
129
|
-
const pb = pairsOf(b.css);
|
|
130
|
-
shared += overlap(pa, pb);
|
|
131
|
-
union += pa.size + pb.size - overlap(pa, pb);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return { conflicts, ratio: comparable === 0 ? 0 : conflicts / comparable, agreement: union === 0 ? 0 : shared / union };
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const isInformative = (node) => !!node && (node.tag !== "fragment" || (node.children || []).some(isInformative));
|
|
138
|
-
|
|
139
|
-
const classTokensDiffer = (a, b) => {
|
|
140
|
-
const A = new Set(a || []);
|
|
141
|
-
const B = new Set(b || []);
|
|
142
|
-
if (A.size === 0 && B.size === 0) return false;
|
|
143
|
-
for (const token of A) if (!B.has(token)) return true;
|
|
144
|
-
for (const token of B) if (!A.has(token)) return true;
|
|
145
|
-
|
|
146
|
-
return false;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const treeSimilarity = (a, b) => {
|
|
150
|
-
const result = bestChain(a, b);
|
|
151
|
-
return { score: result.score, quality: result.quality, detail: { similarity: result.score, quality: result.quality, chainSize: result.chainSize, refSize: result.refSize, pairs: result.pairs.length } };
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
const firstReal = (node) => {
|
|
155
|
-
if (!node) return null;
|
|
156
|
-
if (node.tag === "fragment" || node.tag === "children" || node.tag === "text") {
|
|
157
|
-
for (const child of node.children || []) {
|
|
158
|
-
const found = firstReal(child);
|
|
159
|
-
if (found) return found;
|
|
160
|
-
}
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
return node;
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const rootSimilarity = (a, b) => {
|
|
167
|
-
const ra = firstReal(a);
|
|
168
|
-
const rb = firstReal(b);
|
|
169
|
-
if (!ra || !rb) return { score: 0, detail: { rootSim: 0 } };
|
|
170
|
-
|
|
171
|
-
return { score: nodeSimilarity(ra, rb), detail: { rootSim: nodeSimilarity(ra, rb) } };
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const MIN_CROSS_TAG_CLASS_OVERLAP = 3;
|
|
175
|
-
|
|
176
|
-
const rootClassOverlap = (candidate, canon) => {
|
|
177
|
-
const rt = firstReal(candidate.tree);
|
|
178
|
-
if (!rt) return 0;
|
|
179
|
-
const partToks = new Set(String(rt.className || "").split(/\s+/).filter(Boolean));
|
|
180
|
-
const canonToks = new Set(canon.rawClasses || []);
|
|
181
|
-
let n = 0;
|
|
182
|
-
for (const t of partToks) if (canonToks.has(t)) n += 1;
|
|
183
|
-
|
|
184
|
-
return n;
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
const matchStructure = (candidate, canon) => {
|
|
188
|
-
const cand = candidate.dom && candidate.dom.tree;
|
|
189
|
-
const target = canon.dom && canon.dom.tree;
|
|
190
|
-
const mode = candidate.partial ? "partial" : "tree";
|
|
191
|
-
const applicable = isInformative(cand) || isInformative(target);
|
|
192
|
-
let score = 0;
|
|
193
|
-
let detail = null;
|
|
194
|
-
if (applicable && cand && target) {
|
|
195
|
-
if (candidate.partial) {
|
|
196
|
-
const singleton = nodeCount(target) === 1;
|
|
197
|
-
const partNodes = nodeCount(cand);
|
|
198
|
-
const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
|
|
199
|
-
if (singleton) {
|
|
200
|
-
const ra = firstReal(cand);
|
|
201
|
-
const rb = firstReal(target);
|
|
202
|
-
const partTag = ra ? ra.tag : "";
|
|
203
|
-
const canonTag = rb ? rb.tag : "";
|
|
204
|
-
const crossTag = GENERIC_CONTAINER_TAGS.has(partTag) && GENERIC_CONTAINER_TAGS.has(canonTag) && partTag !== canonTag;
|
|
205
|
-
if (crossTag) {
|
|
206
|
-
const overlap = rootClassOverlap(candidate, canon);
|
|
207
|
-
if (overlap < MIN_CROSS_TAG_CLASS_OVERLAP) {
|
|
208
|
-
score = 0;
|
|
209
|
-
detail = { singleton, crossTag, overlap, rejected: true };
|
|
210
|
-
} else {
|
|
211
|
-
score = Math.min(0.9, 0.5 + 0.05 * overlap);
|
|
212
|
-
detail = { singleton, crossTag, overlap };
|
|
213
|
-
}
|
|
214
|
-
} else if (partNodes > 4) {
|
|
215
|
-
score = 0;
|
|
216
|
-
detail = { singleton, partNodes, leafVsWrapper: true };
|
|
217
|
-
} else {
|
|
218
|
-
const { score: q, detail: d } = rootSimilarity(cand, target);
|
|
219
|
-
score = Math.min(q, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
|
|
220
|
-
detail = { ...d, singleton, classesDiffer };
|
|
221
|
-
}
|
|
222
|
-
} else {
|
|
223
|
-
const { quality, detail: d } = treeSimilarity(cand, target);
|
|
224
|
-
const sizeFactor = Math.min(1, nodeCount(target) / nodeCount(cand));
|
|
225
|
-
const base = quality * sizeFactor;
|
|
226
|
-
score = Math.min(base, classesDiffer ? NEAR_IDENTICAL_CEILING : 1);
|
|
227
|
-
detail = { ...d, singleton, sizeFactor, classesDiffer };
|
|
228
|
-
}
|
|
229
|
-
} else {
|
|
230
|
-
const result = bestChain(cand, target);
|
|
231
|
-
const stats = cssStats(result.pairs);
|
|
232
|
-
const classesDiffer = classTokensDiffer(candidate.rawClasses, canon.rawClasses);
|
|
233
|
-
const ceiling = classesDiffer ? NEAR_IDENTICAL_CEILING : 1;
|
|
234
|
-
score = Math.max(0, Math.min(result.score, ceiling) - CONFLICT_PENALTY * stats.ratio);
|
|
235
|
-
detail = { similarity: result.score, quality: result.quality, chainSize: result.chainSize, refSize: result.refSize, ceiling, classesDiffer, conflicts: stats.conflicts, conflictRatio: stats.ratio, agreement: stats.agreement };
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return { score, applicable, evidence: { mode, ...(detail || {}) } };
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
module.exports = { matchStructure };
|
package/normalize/candidate.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { normalizeDom } = require("./dom.js");
|
|
4
|
-
const { normalizeFramework } = require("./framework.js");
|
|
5
|
-
const { rawClassTokens } = require("../playgrounds/framework-entrypoint.js");
|
|
6
|
-
|
|
7
|
-
const buildCandidate = (comp, config) => ({
|
|
8
|
-
...comp,
|
|
9
|
-
dom: normalizeDom(comp.tree, config, comp.name),
|
|
10
|
-
framework: normalizeFramework(comp.rawTree || comp.tree, comp.name),
|
|
11
|
-
rawClasses: [...rawClassTokens(comp.rawTree || comp.tree)],
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
const buildElementCandidate = (element, config) => {
|
|
15
|
-
const framework = normalizeFramework(element.tree, element.name);
|
|
16
|
-
if (framework.events.length === 0 && element.a11y.length === 0) return null;
|
|
17
|
-
const dom = normalizeDom(element.tree, config, element.name);
|
|
18
|
-
|
|
19
|
-
return { ...element, partial: true, dom, framework: { ...framework, names: [] }, rawClasses: [...rawClassTokens(element.tree)] };
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
module.exports = { buildCandidate, buildElementCandidate };
|
package/normalize/dom.js
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { render } = require("../playgrounds/framework-entrypoint.js");
|
|
4
|
-
const { isLayoutProp } = require("./style.js");
|
|
5
|
-
|
|
6
|
-
const HTML_TAGS = new Set([
|
|
7
|
-
"html",
|
|
8
|
-
"head",
|
|
9
|
-
"body",
|
|
10
|
-
"div",
|
|
11
|
-
"span",
|
|
12
|
-
"a",
|
|
13
|
-
"p",
|
|
14
|
-
"main",
|
|
15
|
-
"section",
|
|
16
|
-
"header",
|
|
17
|
-
"footer",
|
|
18
|
-
"nav",
|
|
19
|
-
"aside",
|
|
20
|
-
"article",
|
|
21
|
-
"ul",
|
|
22
|
-
"ol",
|
|
23
|
-
"li",
|
|
24
|
-
"table",
|
|
25
|
-
"thead",
|
|
26
|
-
"tbody",
|
|
27
|
-
"tr",
|
|
28
|
-
"td",
|
|
29
|
-
"th",
|
|
30
|
-
"form",
|
|
31
|
-
"label",
|
|
32
|
-
"input",
|
|
33
|
-
"button",
|
|
34
|
-
"select",
|
|
35
|
-
"textarea",
|
|
36
|
-
"img",
|
|
37
|
-
"figure",
|
|
38
|
-
"figcaption",
|
|
39
|
-
"svg",
|
|
40
|
-
"path",
|
|
41
|
-
"circle",
|
|
42
|
-
"rect",
|
|
43
|
-
"g",
|
|
44
|
-
"pre",
|
|
45
|
-
"code",
|
|
46
|
-
"strong",
|
|
47
|
-
"em",
|
|
48
|
-
"small",
|
|
49
|
-
"h1",
|
|
50
|
-
"h2",
|
|
51
|
-
"h3",
|
|
52
|
-
"h4",
|
|
53
|
-
"h5",
|
|
54
|
-
"h6",
|
|
55
|
-
"hr",
|
|
56
|
-
"br",
|
|
57
|
-
"i",
|
|
58
|
-
"b",
|
|
59
|
-
"u",
|
|
60
|
-
"s",
|
|
61
|
-
"sub",
|
|
62
|
-
"sup",
|
|
63
|
-
"blockquote",
|
|
64
|
-
"details",
|
|
65
|
-
"summary",
|
|
66
|
-
"dialog",
|
|
67
|
-
"canvas",
|
|
68
|
-
"video",
|
|
69
|
-
"audio",
|
|
70
|
-
"source",
|
|
71
|
-
"track",
|
|
72
|
-
"map",
|
|
73
|
-
"area",
|
|
74
|
-
"dl",
|
|
75
|
-
"dt",
|
|
76
|
-
"dd",
|
|
77
|
-
"fieldset",
|
|
78
|
-
"legend",
|
|
79
|
-
"data",
|
|
80
|
-
"output",
|
|
81
|
-
"progress",
|
|
82
|
-
"meter",
|
|
83
|
-
"time",
|
|
84
|
-
"mark",
|
|
85
|
-
"abbr",
|
|
86
|
-
"ruby",
|
|
87
|
-
"rt",
|
|
88
|
-
"rp",
|
|
89
|
-
"wbr",
|
|
90
|
-
"bdi",
|
|
91
|
-
"bdo",
|
|
92
|
-
"template",
|
|
93
|
-
"slot",
|
|
94
|
-
"picture",
|
|
95
|
-
"base",
|
|
96
|
-
"link",
|
|
97
|
-
"meta",
|
|
98
|
-
"style",
|
|
99
|
-
"title",
|
|
100
|
-
"script",
|
|
101
|
-
"noscript",
|
|
102
|
-
"address",
|
|
103
|
-
"hgroup",
|
|
104
|
-
"search",
|
|
105
|
-
"menu",
|
|
106
|
-
"kbd",
|
|
107
|
-
"samp",
|
|
108
|
-
"var",
|
|
109
|
-
"dfn",
|
|
110
|
-
"cite",
|
|
111
|
-
"q",
|
|
112
|
-
"del",
|
|
113
|
-
"ins",
|
|
114
|
-
"caption",
|
|
115
|
-
"col",
|
|
116
|
-
"colgroup",
|
|
117
|
-
"tfoot",
|
|
118
|
-
"optgroup",
|
|
119
|
-
"option",
|
|
120
|
-
"datalist",
|
|
121
|
-
"iframe",
|
|
122
|
-
"embed",
|
|
123
|
-
"object",
|
|
124
|
-
"math",
|
|
125
|
-
]);
|
|
126
|
-
|
|
127
|
-
const INTERACTIVE_TAGS = new Set(["a", "area", "audio", "button", "details", "embed", "iframe", "input", "label", "optgroup", "option", "select", "summary", "textarea", "video"]);
|
|
128
|
-
const NON_SEMANTIC_TAGS = new Set(["div", "span"]);
|
|
129
|
-
const SEMANTIC_CONTAINER_TAGS = new Set(["section", "header", "footer", "article", "aside", "nav"]);
|
|
130
|
-
const GENERIC_CONTAINER_TAGS = new Set([...NON_SEMANTIC_TAGS, ...SEMANTIC_CONTAINER_TAGS]);
|
|
131
|
-
const TAG_SYNONYMS = new Map([
|
|
132
|
-
["b", "strong"],
|
|
133
|
-
["strong", "b"],
|
|
134
|
-
["i", "em"],
|
|
135
|
-
["em", "i"],
|
|
136
|
-
]);
|
|
137
|
-
|
|
138
|
-
const tagAffinity = (a, b) => {
|
|
139
|
-
const ta = String(a || "").toLowerCase();
|
|
140
|
-
const tb = String(b || "").toLowerCase();
|
|
141
|
-
if (ta === tb) return 1;
|
|
142
|
-
if (TAG_SYNONYMS.get(ta) === tb) return 1;
|
|
143
|
-
if (GENERIC_CONTAINER_TAGS.has(ta) && GENERIC_CONTAINER_TAGS.has(tb)) return 0.75;
|
|
144
|
-
return 0;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
const isPascalCase = (name) => /^[A-Z][A-Za-z0-9]*$/.test(name);
|
|
148
|
-
|
|
149
|
-
const isHtmlTag = (tag) => HTML_TAGS.has(String(tag || ""));
|
|
150
|
-
|
|
151
|
-
const styleObjectToCssText = (obj) => {
|
|
152
|
-
const parts = [];
|
|
153
|
-
for (const [k, v] of Object.entries(obj || {})) {
|
|
154
|
-
if (Array.isArray(v)) parts.push(`${k}: ${v.join("; ")}`);
|
|
155
|
-
else parts.push(`${k}: ${v}`);
|
|
156
|
-
}
|
|
157
|
-
return parts.join("; ");
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const layoutOnly = (node) => ({
|
|
162
|
-
tag: node.tag,
|
|
163
|
-
text: node.text || undefined,
|
|
164
|
-
interactive: !!node.interactive,
|
|
165
|
-
css: Object.fromEntries(Object.entries(node.css || {}).filter(([prop]) => isLayoutProp(prop))),
|
|
166
|
-
children: (node.children || []).map(layoutOnly),
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const normalizeDom = (tree, opts, label) => {
|
|
170
|
-
const rendered = render(tree, opts);
|
|
171
|
-
const domTree = rendered.available && rendered.tree ? layoutOnly(rendered.tree) : null;
|
|
172
|
-
|
|
173
|
-
return { tree: domTree, available: rendered.available };
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
module.exports = {
|
|
177
|
-
HTML_TAGS,
|
|
178
|
-
INTERACTIVE_TAGS,
|
|
179
|
-
NON_SEMANTIC_TAGS,
|
|
180
|
-
SEMANTIC_CONTAINER_TAGS,
|
|
181
|
-
GENERIC_CONTAINER_TAGS,
|
|
182
|
-
tagAffinity,
|
|
183
|
-
isPascalCase,
|
|
184
|
-
isHtmlTag,
|
|
185
|
-
styleObjectToCssText,
|
|
186
|
-
normalizeDom,
|
|
187
|
-
};
|