@md-code/component-uniqueness 0.1.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/README.md +149 -0
- package/config.js +337 -0
- package/decision-maker.js +113 -0
- package/filter/drop-child-element.js +13 -0
- package/filter/drop-usages.js +10 -0
- package/index.js +178 -0
- package/main.js +433 -0
- package/matcher/a11y.js +65 -0
- package/matcher/behavior.js +27 -0
- package/matcher/framework/index.js +15 -0
- package/matcher/framework/react.js +25 -0
- package/matcher/index.js +36 -0
- package/matcher/name.js +115 -0
- package/matcher/structure.js +168 -0
- package/normalize/candidate.js +20 -0
- package/normalize/dom.js +165 -0
- package/normalize/framework.js +100 -0
- package/normalize/style.js +174 -0
- package/package.json +61 -0
- package/playgrounds/angular.js +11 -0
- package/playgrounds/framework-entrypoint.js +71 -0
- package/playgrounds/react.js +928 -0
- package/playgrounds/scss.js +7 -0
- package/playgrounds/style-entrypoint.js +78 -0
- package/playgrounds/styled-components.js +7 -0
- package/playgrounds/tailwind.js +186 -0
- package/playgrounds/vue.js +11 -0
- package/plugin.js +14 -0
- package/report.js +126 -0
- package/utils.js +92 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const react = require("./react.js");
|
|
4
|
+
|
|
5
|
+
const REGISTRY = {
|
|
6
|
+
react,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const getFrameworkMatcher = (framework) => {
|
|
10
|
+
if (!framework) return react;
|
|
11
|
+
const key = String(framework).toLowerCase();
|
|
12
|
+
return REGISTRY[key] || react;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = { getFrameworkMatcher };
|
|
@@ -0,0 +1,25 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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 props = new Set(candidate.framework.props);
|
|
11
|
+
const a11y = tokensOf(candidate, "a11y");
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
...canon,
|
|
15
|
+
framework: {
|
|
16
|
+
props: canon.framework.open ? [...props] : (canon.framework.props || []).filter((p) => props.has(p)),
|
|
17
|
+
events: (canon.framework.events || []).filter((e) => events.has(e)),
|
|
18
|
+
names: [],
|
|
19
|
+
},
|
|
20
|
+
a11y: canon.framework.open ? candidate.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
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
|
|
5
|
+
const FUZZY_NAME_MIN_LEN = 5;
|
|
6
|
+
const FUZZY_BASENAME_MIN_LEN = 4;
|
|
7
|
+
|
|
8
|
+
const kebabToPascal = (s) =>
|
|
9
|
+
s
|
|
10
|
+
.split(/[-_ ]+/)
|
|
11
|
+
.filter(Boolean)
|
|
12
|
+
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
13
|
+
.join("");
|
|
14
|
+
|
|
15
|
+
const basenameToPascal = (file) => kebabToPascal(path.basename(file, path.extname(file)));
|
|
16
|
+
|
|
17
|
+
const pascalWords = (s) => String(s || "").replace(/([a-z0-9])([A-Z])/g, "$1 $2").split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
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 fuzzyNameHit = (appName, canonNames) => {
|
|
22
|
+
const words = pascalWords(appName);
|
|
23
|
+
if (words.length === 0) return null;
|
|
24
|
+
let best = null;
|
|
25
|
+
for (const cn of canonNames) {
|
|
26
|
+
if (cn.length < FUZZY_NAME_MIN_LEN) continue;
|
|
27
|
+
const lc = cn.toLowerCase();
|
|
28
|
+
for (const w of words) {
|
|
29
|
+
const lw = w.toLowerCase();
|
|
30
|
+
if (lw === lc || (lw.length >= FUZZY_NAME_MIN_LEN && (lw.startsWith(lc) || lw.endsWith(lc)))) {
|
|
31
|
+
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return best;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const fuzzyBasenameHit = (appPath, canonNames) => {
|
|
40
|
+
const base = path.basename(appPath, path.extname(appPath));
|
|
41
|
+
const words = kebabWords(base).map((w) => w.toLowerCase());
|
|
42
|
+
if (words.length === 0) return null;
|
|
43
|
+
const fullKebab = words.join("-");
|
|
44
|
+
let best = null;
|
|
45
|
+
for (const cn of canonNames) {
|
|
46
|
+
if (cn.length < FUZZY_BASENAME_MIN_LEN) continue;
|
|
47
|
+
const cnWords = pascalWords(cn).map((w) => w.toLowerCase());
|
|
48
|
+
const cnKebab = cnWords.join("-");
|
|
49
|
+
if (fullKebab === cnKebab || fullKebab === singular(cnKebab) || fullKebab === cnKebab.replace(/s$/, "")) {
|
|
50
|
+
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const lc = cn.toLowerCase();
|
|
54
|
+
const lcs = singular(lc);
|
|
55
|
+
for (const w of words) {
|
|
56
|
+
if (w === lc || w === lcs || singular(w) === lc) {
|
|
57
|
+
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (best && best.name === cn) continue;
|
|
62
|
+
for (const w of words) {
|
|
63
|
+
if (w.length < 4) continue;
|
|
64
|
+
const ws = singular(w);
|
|
65
|
+
if (cnWords.some((cw) => cw === w || cw === ws || singular(cw) === ws)) {
|
|
66
|
+
if (!best || cn.length > best.name.length) best = { name: cn };
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return best;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const matchName = (candidate, canon, ctx) => {
|
|
75
|
+
const o = ctx || {};
|
|
76
|
+
const canonNames = o.canonNames || [canon.name];
|
|
77
|
+
|
|
78
|
+
const exactName = candidate.name === canon.name;
|
|
79
|
+
const exactBasename = !exactName && basenameToPascal(candidate.path) === canon.name;
|
|
80
|
+
|
|
81
|
+
let fuzzy = null;
|
|
82
|
+
if (!exactName && !exactBasename) {
|
|
83
|
+
fuzzy = fuzzyNameHit(candidate.name, [canon.name]) || fuzzyBasenameHit(candidate.path, [canon.name]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let score = 0;
|
|
87
|
+
let matchType = null;
|
|
88
|
+
if (exactName) {
|
|
89
|
+
score = 1;
|
|
90
|
+
matchType = "exact-name";
|
|
91
|
+
} else if (exactBasename) {
|
|
92
|
+
score = 0.9;
|
|
93
|
+
matchType = "exact-basename";
|
|
94
|
+
} else if (fuzzy) {
|
|
95
|
+
const byName = !!fuzzyNameHit(candidate.name, [canon.name]);
|
|
96
|
+
score = byName ? 0.7 : 0.6;
|
|
97
|
+
matchType = byName ? "fuzzy-name" : "fuzzy-basename";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const evidence = {
|
|
101
|
+
exactName,
|
|
102
|
+
exactBasename,
|
|
103
|
+
fuzzy: fuzzy ? fuzzy.name : null,
|
|
104
|
+
matchType,
|
|
105
|
+
};
|
|
106
|
+
return { score, evidence };
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
module.exports = {
|
|
110
|
+
matchName,
|
|
111
|
+
kebabToPascal,
|
|
112
|
+
basenameToPascal,
|
|
113
|
+
fuzzyNameHit,
|
|
114
|
+
fuzzyBasenameHit,
|
|
115
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { NON_SEMANTIC_TAGS } = require("../normalize/dom.js");
|
|
4
|
+
|
|
5
|
+
const SINGLE_NODE_CEILING = 0.7;
|
|
6
|
+
const FEW_NODES_CEILING = 0.85;
|
|
7
|
+
const CONFLICT_PENALTY = 0.6;
|
|
8
|
+
const CONFLICTED_RICH_CEILING = 0.8;
|
|
9
|
+
|
|
10
|
+
const pairsOf = (css) => new Set(Object.entries(css || {}).map(([prop, value]) => `${prop}:${value}`));
|
|
11
|
+
|
|
12
|
+
const overlap = (a, b) => [...a].filter((pair) => b.has(pair)).length;
|
|
13
|
+
|
|
14
|
+
const jaccard = (a, b) => {
|
|
15
|
+
const union = a.size + b.size - overlap(a, b);
|
|
16
|
+
|
|
17
|
+
return union === 0 ? 0 : overlap(a, b) / union;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const coverage = (a, b) => (a.size === 0 ? 0 : overlap(a, b) / a.size);
|
|
21
|
+
|
|
22
|
+
const nodeSimilarity = (a, b) => {
|
|
23
|
+
const tag = a.tag === b.tag ? 1 : 0;
|
|
24
|
+
const pa = pairsOf(a.css);
|
|
25
|
+
const pb = pairsOf(b.css);
|
|
26
|
+
if (pa.size === 0 && pb.size === 0) return tag;
|
|
27
|
+
|
|
28
|
+
return 0.6 * tag + 0.4 * jaccard(pa, pb);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const forestOf = (node) => (node.tag === "fragment" ? (node.children || []).flatMap(forestOf) : [node]);
|
|
32
|
+
|
|
33
|
+
const align = (xs, ys) => {
|
|
34
|
+
const cells = xs.map((x) => ys.map((y) => compare(x, y)));
|
|
35
|
+
const table = Array.from({ length: xs.length + 1 }, () => new Array(ys.length + 1).fill(0));
|
|
36
|
+
for (let i = 1; i <= xs.length; i += 1) {
|
|
37
|
+
for (let j = 1; j <= ys.length; j += 1) {
|
|
38
|
+
table[i][j] = Math.max(table[i - 1][j], table[i][j - 1], table[i - 1][j - 1] + cells[i - 1][j - 1].score);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const pairs = [];
|
|
42
|
+
let i = xs.length;
|
|
43
|
+
let j = ys.length;
|
|
44
|
+
while (i > 0 && j > 0) {
|
|
45
|
+
if (table[i][j] === table[i - 1][j]) i -= 1;
|
|
46
|
+
else if (table[i][j] === table[i][j - 1]) j -= 1;
|
|
47
|
+
else {
|
|
48
|
+
pairs.push(...cells[i - 1][j - 1].pairs);
|
|
49
|
+
i -= 1;
|
|
50
|
+
j -= 1;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { total: table[xs.length][ys.length], pairs };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const compare = (a, b) => {
|
|
58
|
+
const kidsA = (a.children || []).flatMap(forestOf);
|
|
59
|
+
const kidsB = (b.children || []).flatMap(forestOf);
|
|
60
|
+
const { total, pairs } = align(kidsA, kidsB);
|
|
61
|
+
|
|
62
|
+
return { score: (nodeSimilarity(a, b) + total) / (1 + Math.max(kidsA.length, kidsB.length)), pairs: [[a, b], ...pairs] };
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const compareForests = (a, b) => {
|
|
66
|
+
const xs = forestOf(a);
|
|
67
|
+
const ys = forestOf(b);
|
|
68
|
+
const longest = Math.max(xs.length, ys.length);
|
|
69
|
+
const { total, pairs } = align(xs, ys);
|
|
70
|
+
|
|
71
|
+
return { score: longest === 0 ? 0 : total / longest, pairs };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const cssStats = (pairs) => {
|
|
75
|
+
let comparable = 0;
|
|
76
|
+
let conflicts = 0;
|
|
77
|
+
let shared = 0;
|
|
78
|
+
let union = 0;
|
|
79
|
+
for (const [a, b] of pairs) {
|
|
80
|
+
for (const [prop, value] of Object.entries(a.css || {})) {
|
|
81
|
+
if (!Object.prototype.hasOwnProperty.call(b.css || {}, prop)) continue;
|
|
82
|
+
comparable += 1;
|
|
83
|
+
if (b.css[prop] !== value) conflicts += 1;
|
|
84
|
+
}
|
|
85
|
+
const pa = pairsOf(a.css);
|
|
86
|
+
const pb = pairsOf(b.css);
|
|
87
|
+
shared += overlap(pa, pb);
|
|
88
|
+
union += pa.size + pb.size - overlap(pa, pb);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { conflicts, ratio: comparable === 0 ? 0 : conflicts / comparable, agreement: union === 0 ? 0 : shared / union };
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const rootNodeScore = (cand, target) => {
|
|
95
|
+
const parts = [];
|
|
96
|
+
if (!NON_SEMANTIC_TAGS.has(cand.tag)) parts.push({ w: 0.6, v: cand.tag === target.tag ? 1 : 0 });
|
|
97
|
+
const candPairs = pairsOf(cand.css);
|
|
98
|
+
if (candPairs.size > 0) parts.push({ w: 0.4, v: coverage(candPairs, pairsOf(target.css)) });
|
|
99
|
+
const totalW = parts.reduce((sum, part) => sum + part.w, 0);
|
|
100
|
+
|
|
101
|
+
return totalW === 0 ? 0 : parts.reduce((sum, part) => sum + part.w * part.v, 0) / totalW;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const isInformative = (node) => !!node && (node.tag !== "fragment" || (node.children || []).some(isInformative));
|
|
105
|
+
|
|
106
|
+
const shapeOf = (node) => {
|
|
107
|
+
const tags = new Set();
|
|
108
|
+
let nodes = 0;
|
|
109
|
+
const visit = (n) => {
|
|
110
|
+
if (n.tag !== "fragment") {
|
|
111
|
+
tags.add(n.tag);
|
|
112
|
+
nodes += 1;
|
|
113
|
+
}
|
|
114
|
+
for (const child of n.children || []) visit(child);
|
|
115
|
+
};
|
|
116
|
+
visit(node);
|
|
117
|
+
|
|
118
|
+
return { tags, nodes };
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const componentNamesOf = (tree, out = new Set()) => {
|
|
122
|
+
if (!tree) return out;
|
|
123
|
+
for (const name of tree.via || []) out.add(name);
|
|
124
|
+
for (const child of tree.children || []) componentNamesOf(child, out);
|
|
125
|
+
|
|
126
|
+
return out;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const sizeOf = (candidate) => {
|
|
130
|
+
const { tags, nodes } = shapeOf(candidate.dom.tree);
|
|
131
|
+
const distinct = new Set([...tags, ...componentNamesOf(candidate.tree)]).size;
|
|
132
|
+
|
|
133
|
+
return { nodes, distinct };
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const ceilingOf = (nodes, distinct, stats) => {
|
|
137
|
+
if (nodes <= 1) return SINGLE_NODE_CEILING;
|
|
138
|
+
if (nodes === 2) return SINGLE_NODE_CEILING + (1 - SINGLE_NODE_CEILING) * stats.agreement;
|
|
139
|
+
if (distinct >= 3) return stats.conflicts > 0 ? CONFLICTED_RICH_CEILING : 1;
|
|
140
|
+
|
|
141
|
+
return stats.conflicts > 0 ? SINGLE_NODE_CEILING : FEW_NODES_CEILING;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const matchStructure = (candidate, canon) => {
|
|
145
|
+
const cand = candidate.dom && candidate.dom.tree;
|
|
146
|
+
const target = canon.dom && canon.dom.tree;
|
|
147
|
+
const mode = candidate.partial ? "root-node" : "tree";
|
|
148
|
+
const applicable = isInformative(cand) || isInformative(target);
|
|
149
|
+
let score = 0;
|
|
150
|
+
let detail = null;
|
|
151
|
+
if (applicable && cand && target) {
|
|
152
|
+
if (candidate.partial) {
|
|
153
|
+
score = rootNodeScore(cand, target);
|
|
154
|
+
} else {
|
|
155
|
+
const result = compareForests(cand, target);
|
|
156
|
+
const stats = cssStats(result.pairs);
|
|
157
|
+
const a = sizeOf(candidate);
|
|
158
|
+
const b = sizeOf(canon);
|
|
159
|
+
const ceiling = ceilingOf(Math.min(a.nodes, b.nodes), Math.min(a.distinct, b.distinct), stats);
|
|
160
|
+
score = Math.max(0, Math.min(result.score, ceiling) - CONFLICT_PENALTY * stats.ratio);
|
|
161
|
+
detail = { similarity: result.score, ceiling, conflicts: stats.conflicts, conflictRatio: stats.ratio, agreement: stats.agreement, nodes: Math.min(a.nodes, b.nodes), distinct: Math.min(a.distinct, b.distinct) };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return { score, applicable, evidence: { mode, ...(detail || {}) } };
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
module.exports = { matchStructure };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { normalizeDom } = require("./dom.js");
|
|
4
|
+
const { normalizeFramework } = require("./framework.js");
|
|
5
|
+
|
|
6
|
+
const buildCandidate = (comp, config) => ({
|
|
7
|
+
...comp,
|
|
8
|
+
dom: normalizeDom(comp.tree, config, comp.name),
|
|
9
|
+
framework: normalizeFramework(comp.tree, comp.name),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const buildElementCandidate = (element, config) => {
|
|
13
|
+
const framework = normalizeFramework(element.tree, element.name);
|
|
14
|
+
if (framework.events.length === 0 && element.a11y.length === 0) return null;
|
|
15
|
+
const dom = normalizeDom(element.tree, config, element.name);
|
|
16
|
+
|
|
17
|
+
return { ...element, partial: true, dom, framework: { ...framework, names: [] } };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = { buildCandidate, buildElementCandidate };
|
package/normalize/dom.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
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
|
+
|
|
130
|
+
const isPascalCase = (name) => /^[A-Z][A-Za-z0-9]*$/.test(name);
|
|
131
|
+
|
|
132
|
+
const isHtmlTag = (tag) => HTML_TAGS.has(String(tag || ""));
|
|
133
|
+
|
|
134
|
+
const styleObjectToCssText = (obj) => {
|
|
135
|
+
const parts = [];
|
|
136
|
+
for (const [k, v] of Object.entries(obj || {})) {
|
|
137
|
+
if (Array.isArray(v)) parts.push(`${k}: ${v.join("; ")}`);
|
|
138
|
+
else parts.push(`${k}: ${v}`);
|
|
139
|
+
}
|
|
140
|
+
return parts.join("; ");
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
const layoutOnly = (node) => ({
|
|
145
|
+
tag: node.tag,
|
|
146
|
+
css: Object.fromEntries(Object.entries(node.css || {}).filter(([prop]) => isLayoutProp(prop))),
|
|
147
|
+
children: (node.children || []).map(layoutOnly),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const normalizeDom = (tree, opts, label) => {
|
|
151
|
+
const rendered = render(tree, opts);
|
|
152
|
+
const domTree = rendered.available && rendered.tree ? layoutOnly(rendered.tree) : null;
|
|
153
|
+
|
|
154
|
+
return { tree: domTree, available: rendered.available };
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
module.exports = {
|
|
158
|
+
HTML_TAGS,
|
|
159
|
+
INTERACTIVE_TAGS,
|
|
160
|
+
NON_SEMANTIC_TAGS,
|
|
161
|
+
isPascalCase,
|
|
162
|
+
isHtmlTag,
|
|
163
|
+
styleObjectToCssText,
|
|
164
|
+
normalizeDom,
|
|
165
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { INTERACTIVE_TAGS } = require("./dom.js");
|
|
4
|
+
|
|
5
|
+
const EVENT_PREFIX = /^(on|ng|v-on:|@)/;
|
|
6
|
+
|
|
7
|
+
const isEventName = (name) => EVENT_PREFIX.test(String(name || ""));
|
|
8
|
+
|
|
9
|
+
const eventNameOf = (name) => {
|
|
10
|
+
const raw = String(name || "");
|
|
11
|
+
if (raw.startsWith("on")) return raw.slice(2).toLowerCase();
|
|
12
|
+
if (raw.startsWith("ng")) return raw.slice(2).toLowerCase();
|
|
13
|
+
if (raw.startsWith("v-on:")) return raw.slice(5).toLowerCase();
|
|
14
|
+
if (raw.startsWith("@")) return raw.slice(1).toLowerCase();
|
|
15
|
+
return raw.toLowerCase();
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const walk = (tree, visit) => {
|
|
19
|
+
if (!tree) return;
|
|
20
|
+
visit(tree);
|
|
21
|
+
for (const child of tree.children || []) walk(child, visit);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const propsOf = (tree) => {
|
|
25
|
+
const out = new Set();
|
|
26
|
+
walk(tree, (node) => {
|
|
27
|
+
for (const name of Object.keys(node.attrs || {})) {
|
|
28
|
+
if (isEventName(name)) continue;
|
|
29
|
+
out.add(name);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return [...out].sort();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const eventsOf = (tree) => {
|
|
36
|
+
const out = new Set();
|
|
37
|
+
walk(tree, (node) => {
|
|
38
|
+
if (INTERACTIVE_TAGS.has(node.tag)) out.add("click");
|
|
39
|
+
for (const name of Object.keys(node.attrs || {})) {
|
|
40
|
+
if (isEventName(name)) out.add(eventNameOf(name));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return [...out].sort();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const namesOf = (tree) => {
|
|
47
|
+
const out = [];
|
|
48
|
+
walk(tree, (node) => {
|
|
49
|
+
if (node.tag) out.push(node.tag);
|
|
50
|
+
});
|
|
51
|
+
return out;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const normalizeFramework = (tree, label) => {
|
|
55
|
+
const profile = {
|
|
56
|
+
props: propsOf(tree),
|
|
57
|
+
events: eventsOf(tree),
|
|
58
|
+
names: namesOf(tree),
|
|
59
|
+
open: !!(tree && tree.spread),
|
|
60
|
+
};
|
|
61
|
+
return profile;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const jaccard = (a, b) => {
|
|
65
|
+
const A = new Set(a || []);
|
|
66
|
+
const B = new Set(b || []);
|
|
67
|
+
if (A.size === 0 || B.size === 0) return 0;
|
|
68
|
+
let inter = 0;
|
|
69
|
+
for (const item of A) if (B.has(item)) inter += 1;
|
|
70
|
+
const union = A.size + B.size - inter;
|
|
71
|
+
return union === 0 ? 0 : inter / union;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const shared = (a, b) => {
|
|
75
|
+
const B = new Set(b || []);
|
|
76
|
+
return [...new Set(a || [])].filter((item) => B.has(item)).sort();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const frameworkSimilarity = (a, b, label) => {
|
|
80
|
+
const props = jaccard(a && a.props, b && b.props);
|
|
81
|
+
const events = jaccard(a && a.events, b && b.events);
|
|
82
|
+
const names = jaccard(a && a.names, b && b.names);
|
|
83
|
+
const parts = [];
|
|
84
|
+
if ((a && a.props.length) || (b && b.props.length)) parts.push({ w: 0.5, v: props });
|
|
85
|
+
if ((a && a.events.length) || (b && b.events.length)) parts.push({ w: 0.2, v: events });
|
|
86
|
+
if ((a && a.names.length) || (b && b.names.length)) parts.push({ w: 0.3, v: names });
|
|
87
|
+
const totalW = parts.reduce((sum, part) => sum + part.w, 0);
|
|
88
|
+
const score = totalW === 0 ? 0 : parts.reduce((sum, part) => sum + part.w * part.v, 0) / totalW;
|
|
89
|
+
return { score, props, events, names };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
module.exports = {
|
|
93
|
+
normalizeFramework,
|
|
94
|
+
frameworkSimilarity,
|
|
95
|
+
propsOf,
|
|
96
|
+
eventsOf,
|
|
97
|
+
namesOf,
|
|
98
|
+
isEventName,
|
|
99
|
+
eventNameOf,
|
|
100
|
+
};
|