responsivelint 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +214 -2
- package/agents/skills/responsivelint/SKILL.md +57 -0
- package/agents/skills/responsivelint-fix/SKILL.md +38 -0
- package/agents/skills/responsivelint-fix-controls/SKILL.md +35 -0
- package/agents/skills/responsivelint-fix-cramped-text/SKILL.md +47 -0
- package/agents/skills/responsivelint-fix-overflow/SKILL.md +33 -0
- package/agents/skills/responsivelint-fix-overlap/SKILL.md +33 -0
- package/bin/responsivelint.mjs +111 -0
- package/dist/core.iife.js +944 -0
- package/dist/responsivelint.js +1232 -0
- package/dist/responsivelint.min.js +25 -0
- package/docs/rules/clipped-text.md +21 -0
- package/docs/rules/disappears-in-band.md +21 -0
- package/docs/rules/hidden-control.md +21 -0
- package/docs/rules/page-overflow.md +21 -0
- package/docs/rules/squashed-text.md +21 -0
- package/docs/rules/text-overlap.md +21 -0
- package/docs/rules/width-outlier.md +21 -0
- package/llms.txt +49 -0
- package/package.json +43 -4
- package/src/browser/entry.js +337 -0
- package/src/cli/replay.js +86 -0
- package/src/cli/report.js +90 -0
- package/src/cli/run.js +241 -0
- package/src/cli/server.js +24 -0
- package/src/core/analyze.js +265 -0
- package/src/core/bar.js +78 -0
- package/src/core/dom.js +122 -0
- package/src/core/highlight.js +73 -0
- package/src/core/index.js +5 -0
- package/src/core/inject.js +5 -0
- package/src/core/measure.js +241 -0
- package/src/core/recipe.js +19 -0
- package/src/explore/page.js +63 -0
- package/src/icons/measure.js +106 -0
- package/src/index.js +5 -0
|
@@ -0,0 +1,1232 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
// src/core/dom.js
|
|
3
|
+
var SKIP_TAGS = /* @__PURE__ */ new Set(["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE", "TITLE", "OPTION", "HEAD", "META", "LINK"]);
|
|
4
|
+
var INTERACTIVE_SELECTOR = [
|
|
5
|
+
"button",
|
|
6
|
+
"a[href]",
|
|
7
|
+
"summary",
|
|
8
|
+
"select",
|
|
9
|
+
"textarea",
|
|
10
|
+
"input:not([type=hidden])",
|
|
11
|
+
"[role=button]",
|
|
12
|
+
"[role=tab]",
|
|
13
|
+
"[role=link]",
|
|
14
|
+
"[role=menuitem]",
|
|
15
|
+
"[role=switch]",
|
|
16
|
+
"[role=checkbox]",
|
|
17
|
+
"[role=option]",
|
|
18
|
+
'[tabindex]:not([tabindex="-1"])'
|
|
19
|
+
].join(",");
|
|
20
|
+
function elementKey(el) {
|
|
21
|
+
const parts = [];
|
|
22
|
+
for (let node = el; node && node.nodeType === 1 && node !== document.body && node !== document.documentElement; node = node.parentElement) {
|
|
23
|
+
if (node.id && /^[A-Za-z][\w-]*$/.test(node.id)) {
|
|
24
|
+
parts.unshift("#" + node.id);
|
|
25
|
+
return parts.join(">");
|
|
26
|
+
}
|
|
27
|
+
const parent = node.parentElement;
|
|
28
|
+
const index = parent ? Array.prototype.indexOf.call(parent.children, node) + 1 : 1;
|
|
29
|
+
parts.unshift(node.tagName.toLowerCase() + ":nth-child(" + index + ")");
|
|
30
|
+
}
|
|
31
|
+
parts.unshift("body");
|
|
32
|
+
return parts.join(">");
|
|
33
|
+
}
|
|
34
|
+
function ignoredRules(el) {
|
|
35
|
+
const out = /* @__PURE__ */ new Set();
|
|
36
|
+
for (let node = el; node && node.nodeType === 1; node = node.parentElement) {
|
|
37
|
+
const v = node.getAttribute && node.getAttribute("data-rl-ignore");
|
|
38
|
+
if (v != null) {
|
|
39
|
+
if (v === "" || v === "all") out.add("all");
|
|
40
|
+
else v.split(/[\s,]+/).forEach((r) => out.add(r));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
function createContext() {
|
|
46
|
+
const style = /* @__PURE__ */ new Map(), opacity = /* @__PURE__ */ new Map(), clip = /* @__PURE__ */ new Map(), fixed = /* @__PURE__ */ new Map();
|
|
47
|
+
const cs = (el) => {
|
|
48
|
+
let s = style.get(el);
|
|
49
|
+
if (!s) {
|
|
50
|
+
s = getComputedStyle(el);
|
|
51
|
+
style.set(el, s);
|
|
52
|
+
}
|
|
53
|
+
return s;
|
|
54
|
+
};
|
|
55
|
+
function effectiveOpacity(el) {
|
|
56
|
+
if (!el || el.nodeType !== 1) return 1;
|
|
57
|
+
if (opacity.has(el)) return opacity.get(el);
|
|
58
|
+
const v = parseFloat(cs(el).opacity || "1") * effectiveOpacity(el.parentElement);
|
|
59
|
+
opacity.set(el, v);
|
|
60
|
+
return v;
|
|
61
|
+
}
|
|
62
|
+
function isPinned(el) {
|
|
63
|
+
if (!el || el.nodeType !== 1) return false;
|
|
64
|
+
if (fixed.has(el)) return fixed.get(el);
|
|
65
|
+
const p = cs(el).position;
|
|
66
|
+
const v = p === "fixed" || p === "sticky" || isPinned(el.parentElement);
|
|
67
|
+
fixed.set(el, v);
|
|
68
|
+
return v;
|
|
69
|
+
}
|
|
70
|
+
function clipRect(el) {
|
|
71
|
+
if (!el || el.nodeType !== 1 || el === document.documentElement) return { left: -1e9, top: -1e9, right: 1e9, bottom: 1e9 };
|
|
72
|
+
if (clip.has(el)) return clip.get(el);
|
|
73
|
+
const parent = clipRect(el.parentElement);
|
|
74
|
+
const s = cs(el);
|
|
75
|
+
let out = parent;
|
|
76
|
+
if (el !== document.body && (s.overflowX !== "visible" || s.overflowY !== "visible")) {
|
|
77
|
+
const r = el.getBoundingClientRect();
|
|
78
|
+
const box = { left: r.left + scrollX + el.clientLeft, top: r.top + scrollY + el.clientTop };
|
|
79
|
+
box.right = box.left + el.clientWidth;
|
|
80
|
+
box.bottom = box.top + el.clientHeight;
|
|
81
|
+
out = {
|
|
82
|
+
left: s.overflowX === "visible" ? parent.left : Math.max(parent.left, box.left),
|
|
83
|
+
right: s.overflowX === "visible" ? parent.right : Math.min(parent.right, box.right),
|
|
84
|
+
top: s.overflowY === "visible" ? parent.top : Math.max(parent.top, box.top),
|
|
85
|
+
bottom: s.overflowY === "visible" ? parent.bottom : Math.min(parent.bottom, box.bottom)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (s.position === "fixed") out = { left: -1e9, top: -1e9, right: 1e9, bottom: 1e9 };
|
|
89
|
+
clip.set(el, out);
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
function hidden(el) {
|
|
93
|
+
for (let node = el; node && node.nodeType === 1; node = node.parentElement) {
|
|
94
|
+
if (node.getAttribute("aria-hidden") === "true" || node.hasAttribute("inert") || node.hidden) return true;
|
|
95
|
+
}
|
|
96
|
+
const s = cs(el);
|
|
97
|
+
return s.visibility !== "visible" || s.display === "none" || s.contentVisibility === "hidden";
|
|
98
|
+
}
|
|
99
|
+
return { cs, effectiveOpacity, isPinned, clipRect, hidden };
|
|
100
|
+
}
|
|
101
|
+
var intersect = (a, b) => ({ left: Math.max(a.left, b.left), top: Math.max(a.top, b.top), right: Math.min(a.right, b.right), bottom: Math.min(a.bottom, b.bottom) });
|
|
102
|
+
var area = (r) => Math.max(0, r.right - r.left) * Math.max(0, r.bottom - r.top);
|
|
103
|
+
function ownTextRects(el, ctx) {
|
|
104
|
+
const rects = [];
|
|
105
|
+
let text = "";
|
|
106
|
+
const clipTo = ctx.clipRect(el);
|
|
107
|
+
for (const node of el.childNodes) {
|
|
108
|
+
if (node.nodeType !== 3 || !node.textContent.trim()) continue;
|
|
109
|
+
text += node.textContent;
|
|
110
|
+
const range = document.createRange();
|
|
111
|
+
range.selectNodeContents(node);
|
|
112
|
+
for (const r of range.getClientRects()) {
|
|
113
|
+
if (r.width < 1 || r.height < 1) continue;
|
|
114
|
+
const doc = { left: r.left + scrollX, top: r.top + scrollY, right: r.right + scrollX, bottom: r.bottom + scrollY };
|
|
115
|
+
const cut = intersect(doc, clipTo);
|
|
116
|
+
if (area(cut) > 0) rects.push(cut);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { text: text.replace(/\s+/g, " ").trim(), rects };
|
|
120
|
+
}
|
|
121
|
+
function countLines(rects) {
|
|
122
|
+
const centres = rects.map((r) => (r.top + r.bottom) / 2).sort((a, b) => a - b);
|
|
123
|
+
let lines = 0, last = -1e9;
|
|
124
|
+
for (const c of centres) {
|
|
125
|
+
if (c - last > 3) {
|
|
126
|
+
lines++;
|
|
127
|
+
last = c;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return lines;
|
|
131
|
+
}
|
|
132
|
+
function label(el, text) {
|
|
133
|
+
const name = el.getAttribute("aria-label") || text || el.getAttribute("title") || el.getAttribute("alt") || "";
|
|
134
|
+
return name.replace(/\s+/g, " ").trim().slice(0, 60);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/core/measure.js
|
|
138
|
+
var MAX_TEXT = 4e3;
|
|
139
|
+
var MAX_MEASURES = 2500;
|
|
140
|
+
function detectEnvironment() {
|
|
141
|
+
const w = globalThis;
|
|
142
|
+
const frameworks = [];
|
|
143
|
+
if (w.__THREE__) frameworks.push("three.js r" + w.__THREE__);
|
|
144
|
+
if (w.AFRAME) frameworks.push("A-Frame " + (w.AFRAME.version || ""));
|
|
145
|
+
if (w.BABYLON) frameworks.push("Babylon.js " + (w.BABYLON.Engine?.Version || ""));
|
|
146
|
+
if (w.pc && w.pc.version) frameworks.push("PlayCanvas " + w.pc.version);
|
|
147
|
+
if ([...document.querySelectorAll("canvas")].some((c) => c.__r3f || c.parentElement?.__r3f)) frameworks.push("React Three Fiber");
|
|
148
|
+
const contexts = w.__rlContexts || {};
|
|
149
|
+
const canvases = document.querySelectorAll("canvas").length;
|
|
150
|
+
return {
|
|
151
|
+
frameworks: frameworks.map((f) => f.trim()),
|
|
152
|
+
webgl: (contexts.webgl || 0) + (contexts.webgl2 || 0),
|
|
153
|
+
webgpu: contexts.webgpu || 0,
|
|
154
|
+
canvases,
|
|
155
|
+
xr: !!w.__rlXR || !!navigator.xr && frameworks.length > 0,
|
|
156
|
+
is3D: frameworks.length > 0 || (contexts.webgl || 0) + (contexts.webgl2 || 0) + (contexts.webgpu || 0) > 0
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function checkNow(options = {}) {
|
|
160
|
+
let reveal = null;
|
|
161
|
+
if (options.revealSkipped) {
|
|
162
|
+
reveal = document.createElement("style");
|
|
163
|
+
reveal.textContent = "*{content-visibility:visible!important}";
|
|
164
|
+
document.head.append(reveal);
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
return measureAll(options);
|
|
168
|
+
} finally {
|
|
169
|
+
reveal?.remove();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function measureAll(options) {
|
|
173
|
+
const ctx = createContext();
|
|
174
|
+
const viewport = { width: innerWidth, height: innerHeight };
|
|
175
|
+
const findings = [];
|
|
176
|
+
const add = (f) => findings.push(f);
|
|
177
|
+
const texts = [];
|
|
178
|
+
const measures = [];
|
|
179
|
+
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
|
|
180
|
+
for (let el = walker.currentNode; el; el = walker.nextNode()) {
|
|
181
|
+
if (texts.length >= MAX_TEXT) break;
|
|
182
|
+
if (!(el instanceof HTMLElement) || SKIP_TAGS.has(el.tagName)) continue;
|
|
183
|
+
if (el.closest("[data-rl-overlay]")) continue;
|
|
184
|
+
const { text, rects } = ownTextRects(el, ctx);
|
|
185
|
+
if (!text || !rects.length || ctx.hidden(el) || ctx.effectiveOpacity(el) < 0.05) continue;
|
|
186
|
+
if (!options.revealSkipped && el.checkVisibility && !el.checkVisibility({ contentVisibilityAuto: true })) continue;
|
|
187
|
+
const box = el.getBoundingClientRect();
|
|
188
|
+
if (box.width <= 2 || box.height <= 2) continue;
|
|
189
|
+
const ignore = ignoredRules(el);
|
|
190
|
+
texts.push({ el, key: elementKey(el), text, rects, ignore, pinned: ctx.isPinned(el) });
|
|
191
|
+
}
|
|
192
|
+
const boxes = [];
|
|
193
|
+
texts.forEach((t, i) => t.rects.forEach((r) => boxes.push({ i, r })));
|
|
194
|
+
boxes.sort((a, b) => a.r.top - b.r.top);
|
|
195
|
+
const reported = /* @__PURE__ */ new Set();
|
|
196
|
+
const overlaps = [];
|
|
197
|
+
let start = 0;
|
|
198
|
+
for (let a = 0; a < boxes.length; a++) {
|
|
199
|
+
const A = boxes[a];
|
|
200
|
+
while (start < a && boxes[start].r.bottom <= A.r.top) start++;
|
|
201
|
+
for (let b = start; b < a; b++) {
|
|
202
|
+
const B = boxes[b];
|
|
203
|
+
if (A.i === B.i || B.r.bottom <= A.r.top) continue;
|
|
204
|
+
const ta = texts[A.i], tb = texts[B.i];
|
|
205
|
+
if (ta.pinned !== tb.pinned) continue;
|
|
206
|
+
if (ta.el.contains(tb.el) || tb.el.contains(ta.el)) continue;
|
|
207
|
+
if (ta.ignore.has("all") || ta.ignore.has("text-overlap") || tb.ignore.has("all") || tb.ignore.has("text-overlap")) continue;
|
|
208
|
+
const cut = intersect(A.r, B.r);
|
|
209
|
+
const smaller = Math.min(area(A.r), area(B.r));
|
|
210
|
+
const minH = Math.min(A.r.bottom - A.r.top, B.r.bottom - B.r.top);
|
|
211
|
+
if (area(cut) < 0.25 * smaller || cut.bottom - cut.top < 0.4 * minH) continue;
|
|
212
|
+
const pair = A.i < B.i ? A.i + ":" + B.i : B.i + ":" + A.i;
|
|
213
|
+
if (reported.has(pair)) continue;
|
|
214
|
+
reported.add(pair);
|
|
215
|
+
overlaps.push({ ta, tb, cut, share: Math.round(100 * area(cut) / smaller) });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const o of confirmOverlaps(overlaps)) {
|
|
219
|
+
const { ta, tb, cut, share, top } = o;
|
|
220
|
+
add({
|
|
221
|
+
rule: "text-overlap",
|
|
222
|
+
keys: [ta.key, tb.key].sort(),
|
|
223
|
+
elements: [{ key: ta.key, text: ta.text.slice(0, 60) }, { key: tb.key, text: tb.text.slice(0, 60) }],
|
|
224
|
+
rect: cut,
|
|
225
|
+
onTop: top,
|
|
226
|
+
measured: { overlap: share + "% of the smaller line", ...top ? { onTop: top.summary } : {} }
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
for (const t of texts) {
|
|
230
|
+
const words = t.text.split(/\s+/).filter(Boolean).length;
|
|
231
|
+
const lines = countLines(t.rects);
|
|
232
|
+
if (measures.length < MAX_MEASURES && t.text.length >= 6) {
|
|
233
|
+
const left = Math.min(...t.rects.map((r) => r.left)), right = Math.max(...t.rects.map((r) => r.right));
|
|
234
|
+
const top = Math.min(...t.rects.map((r) => r.top)), bottom = Math.max(...t.rects.map((r) => r.bottom));
|
|
235
|
+
measures.push({ key: t.key, kind: "text", text: t.text.slice(0, 60), words, lines, w: Math.round(right - left), rect: { left, top, right, bottom } });
|
|
236
|
+
}
|
|
237
|
+
if (t.ignore.has("all") || t.ignore.has("squashed-text")) continue;
|
|
238
|
+
if (words >= 3 && lines >= 3 && words / lines <= 1.25 && t.text.length >= 15) {
|
|
239
|
+
const left = Math.min(...t.rects.map((r) => r.left)), right = Math.max(...t.rects.map((r) => r.right));
|
|
240
|
+
const top = Math.min(...t.rects.map((r) => r.top)), bottom = Math.max(...t.rects.map((r) => r.bottom));
|
|
241
|
+
add({
|
|
242
|
+
rule: "squashed-text",
|
|
243
|
+
keys: [t.key],
|
|
244
|
+
elements: [{ key: t.key, text: t.text.slice(0, 60) }],
|
|
245
|
+
rect: { left, top, right, bottom },
|
|
246
|
+
measured: { width: Math.round(right - left), lines, words }
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const seenClip = /* @__PURE__ */ new Set();
|
|
251
|
+
for (const t of texts) {
|
|
252
|
+
for (let el = t.el; el && el !== document.body; el = el.parentElement) {
|
|
253
|
+
const s = ctx.cs(el);
|
|
254
|
+
const clipsX = s.overflowX === "hidden" || s.overflowX === "clip";
|
|
255
|
+
const clipsY = s.overflowY === "hidden" || s.overflowY === "clip";
|
|
256
|
+
if (!clipsX && !clipsY) continue;
|
|
257
|
+
if (seenClip.has(el)) break;
|
|
258
|
+
seenClip.add(el);
|
|
259
|
+
if (el.clientWidth < 2 || el.clientHeight < 2 || ctx.hidden(el)) break;
|
|
260
|
+
if (s.webkitLineClamp && s.webkitLineClamp !== "none") break;
|
|
261
|
+
const over = clipsX && el.scrollWidth > el.clientWidth + 2 || clipsY && el.scrollHeight > el.clientHeight + 2;
|
|
262
|
+
if (!over) break;
|
|
263
|
+
const r = el.getBoundingClientRect();
|
|
264
|
+
const edge = { left: r.left + scrollX, top: r.top + scrollY, right: r.left + scrollX + el.clientWidth, bottom: r.top + scrollY + el.clientHeight };
|
|
265
|
+
const ig = ignoredRules(el);
|
|
266
|
+
if (!ig.has("all") && !ig.has("clipped-text")) {
|
|
267
|
+
add({
|
|
268
|
+
rule: "clipped-text",
|
|
269
|
+
keys: [elementKey(el)],
|
|
270
|
+
elements: [{ key: elementKey(el), text: label(el, t.text) }],
|
|
271
|
+
rect: edge,
|
|
272
|
+
ellipsis: s.textOverflow === "ellipsis",
|
|
273
|
+
measured: { contentWidth: el.scrollWidth, boxWidth: el.clientWidth, contentHeight: el.scrollHeight, boxHeight: el.clientHeight }
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const docWidth = document.documentElement.scrollWidth;
|
|
280
|
+
if (docWidth > viewport.width + 1 && !ignoredRules(document.body).has("page-overflow")) {
|
|
281
|
+
const culprits = [];
|
|
282
|
+
const all = document.body.querySelectorAll("*");
|
|
283
|
+
for (const el of all) {
|
|
284
|
+
if (culprits.length >= 5) break;
|
|
285
|
+
if (!(el instanceof HTMLElement) || SKIP_TAGS.has(el.tagName) || ctx.hidden(el)) continue;
|
|
286
|
+
const r = el.getBoundingClientRect();
|
|
287
|
+
if (r.right + scrollX <= viewport.width + 1 || r.width === 0) continue;
|
|
288
|
+
const c = ctx.clipRect(el.parentElement);
|
|
289
|
+
if (c.right <= viewport.width + 1) continue;
|
|
290
|
+
const parent = el.parentElement.getBoundingClientRect();
|
|
291
|
+
if (parent.right + scrollX > viewport.width + 1 && el.parentElement !== document.body) continue;
|
|
292
|
+
culprits.push({ key: elementKey(el), text: label(el, (el.textContent || "").trim()) || el.tagName.toLowerCase(), right: Math.round(r.right + scrollX) });
|
|
293
|
+
}
|
|
294
|
+
add({
|
|
295
|
+
rule: "page-overflow",
|
|
296
|
+
keys: culprits.map((c) => c.key).sort().slice(0, 1),
|
|
297
|
+
elements: culprits,
|
|
298
|
+
rect: null,
|
|
299
|
+
measured: { pageWidth: docWidth, viewport: viewport.width, by: docWidth - viewport.width }
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
for (const el of document.body.querySelectorAll(INTERACTIVE_SELECTOR)) {
|
|
303
|
+
if (!(el instanceof HTMLElement) || el.closest("[data-rl-overlay]")) continue;
|
|
304
|
+
const s = ctx.cs(el);
|
|
305
|
+
if (s.display === "none" || el.disabled) continue;
|
|
306
|
+
if (el.closest("[aria-hidden=true],[inert],[hidden]")) continue;
|
|
307
|
+
const r = el.getBoundingClientRect();
|
|
308
|
+
if (r.width < 4 || r.height < 4) continue;
|
|
309
|
+
const box = { left: r.left + scrollX, top: r.top + scrollY, right: r.right + scrollX, bottom: r.bottom + scrollY };
|
|
310
|
+
if (area(intersect(box, ctx.clipRect(el.parentElement))) < 4) continue;
|
|
311
|
+
const op = ctx.effectiveOpacity(el);
|
|
312
|
+
const invisible = op < 0.1 && s.visibility === "visible" && s.pointerEvents !== "none";
|
|
313
|
+
if (measures.length < MAX_MEASURES + 500) measures.push({ key: elementKey(el), kind: "control", text: label(el, (el.textContent || "").trim()), visible: !invisible, rect: box });
|
|
314
|
+
if (!invisible) continue;
|
|
315
|
+
if (el.matches("input[type=checkbox],input[type=radio],input[type=file],input[type=range]") && (el.labels?.length || el.closest("label"))) continue;
|
|
316
|
+
const ig = ignoredRules(el);
|
|
317
|
+
if (ig.has("all") || ig.has("hidden-control")) continue;
|
|
318
|
+
add({
|
|
319
|
+
rule: "hidden-control",
|
|
320
|
+
keys: [elementKey(el)],
|
|
321
|
+
elements: [{ key: elementKey(el), text: label(el, (el.textContent || "").trim()) || el.tagName.toLowerCase() }],
|
|
322
|
+
rect: box,
|
|
323
|
+
measured: { opacity: +op.toFixed(2) }
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
return { width: viewport.width, height: viewport.height, env: detectEnvironment(), findings, measures };
|
|
327
|
+
}
|
|
328
|
+
function confirmOverlaps(list) {
|
|
329
|
+
if (!list.length) return [];
|
|
330
|
+
const allow = document.createElement("style");
|
|
331
|
+
allow.textContent = "*{pointer-events:auto!important}";
|
|
332
|
+
document.head.append(allow);
|
|
333
|
+
const sx = scrollX, sy = scrollY;
|
|
334
|
+
const out = [];
|
|
335
|
+
try {
|
|
336
|
+
for (const o of list) {
|
|
337
|
+
const cx = (o.cut.left + o.cut.right) / 2, cy = (o.cut.top + o.cut.bottom) / 2;
|
|
338
|
+
const pinned = o.ta.pinned && o.tb.pinned;
|
|
339
|
+
let vx = cx - scrollX, vy = cy - scrollY;
|
|
340
|
+
if (!pinned && (vx < 0 || vy < 0 || vx >= innerWidth || vy >= innerHeight)) {
|
|
341
|
+
window.scrollTo({ left: Math.max(0, cx - innerWidth / 2), top: Math.max(0, cy - innerHeight / 2), behavior: "instant" });
|
|
342
|
+
vx = cx - scrollX;
|
|
343
|
+
vy = cy - scrollY;
|
|
344
|
+
}
|
|
345
|
+
if (pinned) {
|
|
346
|
+
vx = cx - sx;
|
|
347
|
+
vy = cy - sy;
|
|
348
|
+
}
|
|
349
|
+
const stack = document.elementsFromPoint(vx, vy);
|
|
350
|
+
const at = (el) => stack.findIndex((s) => s === el || el.contains(s));
|
|
351
|
+
const ia = at(o.ta.el), ib = at(o.tb.el);
|
|
352
|
+
if (ia < 0 && ib < 0) continue;
|
|
353
|
+
const topEl = ia < 0 ? o.tb.el : ib < 0 ? o.ta.el : ia <= ib ? o.ta.el : o.tb.el;
|
|
354
|
+
out.push({ ...o, top: describeLayer(topEl) });
|
|
355
|
+
}
|
|
356
|
+
} finally {
|
|
357
|
+
allow.remove();
|
|
358
|
+
if (scrollX !== sx || scrollY !== sy) window.scrollTo({ left: sx, top: sy, behavior: "instant" });
|
|
359
|
+
}
|
|
360
|
+
return out;
|
|
361
|
+
}
|
|
362
|
+
function describeLayer(el) {
|
|
363
|
+
let layer = el;
|
|
364
|
+
for (let n = el; n && n !== document.body; n = n.parentElement) {
|
|
365
|
+
const s2 = getComputedStyle(n);
|
|
366
|
+
if (s2.position !== "static" || s2.zIndex !== "auto" || s2.transform !== "none") {
|
|
367
|
+
layer = n;
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const s = getComputedStyle(layer);
|
|
372
|
+
const parts = [s.position !== "static" ? "position: " + s.position : "", s.zIndex !== "auto" ? "z-index: " + s.zIndex : "", s.transform !== "none" ? "transformed" : ""].filter(Boolean);
|
|
373
|
+
const text = (el.textContent || "").replace(/\s+/g, " ").trim().slice(0, 40);
|
|
374
|
+
return { key: elementKey(layer), text, position: s.position, zIndex: s.zIndex, summary: `"${text}"` + (parts.length ? " (" + parts.join(", ") + ")" : " (normal flow)") };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/core/analyze.js
|
|
378
|
+
var REPO = "https://github.com/prajwalsouza/ResponsiveLint/blob/main/";
|
|
379
|
+
var FIX_SKILLS = {
|
|
380
|
+
"responsivelint-fix-overlap": { title: "Fix text drawn over other text", rules: ["text-overlap"] },
|
|
381
|
+
"responsivelint-fix-cramped-text": { title: "Fix text that is squeezed or cut off", rules: ["squashed-text", "clipped-text", "width-outlier"] },
|
|
382
|
+
"responsivelint-fix-overflow": { title: "Fix a page that scrolls sideways", rules: ["page-overflow"] },
|
|
383
|
+
"responsivelint-fix-controls": { title: "Fix controls that are invisible or go missing", rules: ["hidden-control", "disappears-in-band"] }
|
|
384
|
+
};
|
|
385
|
+
var skillFor = (rule) => {
|
|
386
|
+
const name = Object.keys(FIX_SKILLS).find((k) => FIX_SKILLS[k].rules.includes(rule)) || "responsivelint-fix";
|
|
387
|
+
const file = `agents/skills/${name}/SKILL.md`;
|
|
388
|
+
return { name, file, url: REPO + file };
|
|
389
|
+
};
|
|
390
|
+
var RULES = {
|
|
391
|
+
"text-overlap": {
|
|
392
|
+
pack: "text",
|
|
393
|
+
severity: "error",
|
|
394
|
+
confidence: "high",
|
|
395
|
+
title: "Text covers other text",
|
|
396
|
+
why: "Two pieces of visible text are drawn in the same place, so neither can be read.",
|
|
397
|
+
tries: [
|
|
398
|
+
"An element with position: absolute or fixed sits on text that wraps at this width: place it with flex or grid instead, or move it off the text. Changing z-index only swaps which text is hidden.",
|
|
399
|
+
"A box has a fixed height, so its text spills out when it wraps: remove height, or use min-height.",
|
|
400
|
+
"A negative margin or a transform pulls one element onto another: remove it at these widths.",
|
|
401
|
+
"A toast, tooltip or badge is placed on a toolbar: place it above or beside the toolbar."
|
|
402
|
+
]
|
|
403
|
+
},
|
|
404
|
+
"squashed-text": {
|
|
405
|
+
pack: "text",
|
|
406
|
+
severity: "warning",
|
|
407
|
+
confidence: "high",
|
|
408
|
+
title: "Text squeezed to about one word per line",
|
|
409
|
+
why: "The text has become so narrow that almost every word sits on its own line.",
|
|
410
|
+
tries: [
|
|
411
|
+
"A neighbour in the same row does not wrap (white-space: nowrap, flex-shrink: 0, an auto grid column full of buttons): let it wrap with flex-wrap: wrap, or put it on its own line with grid-template-columns: 1fr.",
|
|
412
|
+
"The row keeps two columns at a width where one fits better: stack them below this width with a media or container query.",
|
|
413
|
+
"Nothing stops the text from shrinking: give it min-width: 12em (or similar)."
|
|
414
|
+
]
|
|
415
|
+
},
|
|
416
|
+
"clipped-text": {
|
|
417
|
+
pack: "text",
|
|
418
|
+
severity: "warning",
|
|
419
|
+
confidence: "medium",
|
|
420
|
+
title: "Text cut off by its box",
|
|
421
|
+
why: "A box with overflow hidden is smaller than the text inside it, so part of the text is not shown.",
|
|
422
|
+
tries: [
|
|
423
|
+
"The box has a fixed width or height: remove it, or use min-width or min-height, so the box grows with its text.",
|
|
424
|
+
"The text is not allowed to wrap (white-space: nowrap): allow wrapping.",
|
|
425
|
+
"The cut is intended: add text-overflow: ellipsis and a title with the full text, so the finding becomes a note."
|
|
426
|
+
]
|
|
427
|
+
},
|
|
428
|
+
"page-overflow": {
|
|
429
|
+
pack: "overflow",
|
|
430
|
+
severity: "error",
|
|
431
|
+
confidence: "high",
|
|
432
|
+
title: "The page scrolls sideways",
|
|
433
|
+
why: "Something is wider than the screen, so the whole page can be dragged sideways.",
|
|
434
|
+
tries: [
|
|
435
|
+
"The named element has a fixed width in pixels: use max-width: 100%, or a width in % or fr.",
|
|
436
|
+
"A row of controls or a long word or URL does not wrap: add flex-wrap: wrap, or overflow-wrap: anywhere for long words.",
|
|
437
|
+
"A wide table, code block or image: wrap it in a box with overflow-x: auto, or give images max-width: 100%.",
|
|
438
|
+
"Do not hide the problem with overflow-x: hidden on body or html; it cuts content off instead."
|
|
439
|
+
]
|
|
440
|
+
},
|
|
441
|
+
"hidden-control": {
|
|
442
|
+
pack: "visibility",
|
|
443
|
+
severity: "warning",
|
|
444
|
+
confidence: "medium",
|
|
445
|
+
title: "A control takes space but cannot be seen",
|
|
446
|
+
why: "A button, link or tab is laid out, clickable and focusable, but fully transparent in its resting state, so people do not know it is there.",
|
|
447
|
+
tries: [
|
|
448
|
+
"It appears only on hover (opacity: 0 until :hover): show it at rest, perhaps quieter, because touch screens have no hover.",
|
|
449
|
+
"It should stay hidden until something happens: hide it properly with display: none, visibility: hidden or the hidden attribute until then, so it leaves the tab order too.",
|
|
450
|
+
"Keyboard users cannot find it: also show it on :focus-visible and :focus-within."
|
|
451
|
+
]
|
|
452
|
+
},
|
|
453
|
+
"width-outlier": {
|
|
454
|
+
pack: "sweep",
|
|
455
|
+
severity: "warning",
|
|
456
|
+
confidence: "high",
|
|
457
|
+
title: "Breaks only in a narrow band of widths",
|
|
458
|
+
why: "This element looks normal at the widths on both sides, but changes sharply inside this band, which is how layout bugs between breakpoints show up.",
|
|
459
|
+
tries: [
|
|
460
|
+
"One part of the layout switches at a breakpoint before another does (a sidebar appears before the content stacks): line the two media queries up, or switch the content to a container query.",
|
|
461
|
+
"A neighbour stops wrapping inside the band and takes the width: let it wrap, or stack it below this width.",
|
|
462
|
+
"A fixed size takes over inside the band: replace it with min(), max() or clamp()."
|
|
463
|
+
]
|
|
464
|
+
},
|
|
465
|
+
"disappears-in-band": {
|
|
466
|
+
pack: "sweep",
|
|
467
|
+
severity: "warning",
|
|
468
|
+
confidence: "medium",
|
|
469
|
+
title: "A control disappears in a narrow band of widths",
|
|
470
|
+
why: "The control is visible at the widths on both sides but not inside this band.",
|
|
471
|
+
tries: [
|
|
472
|
+
"Two media queries hand over at different widths (one hides the desktop control, the other shows the mobile one a few pixels later): use the same width for both, or one query.",
|
|
473
|
+
"max-width and min-width both use the same number, so one pixel shows neither: use max-width: 767px with min-width: 768px, or a range query.",
|
|
474
|
+
"The control is pushed out of its row and clipped: let the row wrap."
|
|
475
|
+
]
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
for (const r of Object.values(RULES)) r.fix = r.tries[0];
|
|
479
|
+
function bands(widths, allWidths) {
|
|
480
|
+
const sorted = [...new Set(widths)].sort((a, b) => a - b);
|
|
481
|
+
const index = new Map(allWidths.map((w, i) => [w, i]));
|
|
482
|
+
const out = [];
|
|
483
|
+
for (const w of sorted) {
|
|
484
|
+
const last = out[out.length - 1];
|
|
485
|
+
if (last && index.get(w) === index.get(last[1]) + 1) last[1] = w;
|
|
486
|
+
else out.push([w, w]);
|
|
487
|
+
}
|
|
488
|
+
return out;
|
|
489
|
+
}
|
|
490
|
+
var median = (xs) => {
|
|
491
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
492
|
+
return s.length ? s[Math.floor(s.length / 2)] : 0;
|
|
493
|
+
};
|
|
494
|
+
function sweepOutliers(series, allWidths, state) {
|
|
495
|
+
const findings = [];
|
|
496
|
+
for (const [key, byWidth] of series) {
|
|
497
|
+
const pts = allWidths.map((w) => byWidth.get(w));
|
|
498
|
+
if (pts.filter(Boolean).length < 5) continue;
|
|
499
|
+
const kind = pts.find(Boolean).kind;
|
|
500
|
+
const odd = new Array(pts.length).fill(false);
|
|
501
|
+
for (let i2 = 0; i2 < pts.length; i2++) {
|
|
502
|
+
const p = pts[i2];
|
|
503
|
+
const near = [];
|
|
504
|
+
for (let d = 1; d <= 3; d++) {
|
|
505
|
+
if (pts[i2 - d]) near.push(pts[i2 - d]);
|
|
506
|
+
if (pts[i2 + d]) near.push(pts[i2 + d]);
|
|
507
|
+
}
|
|
508
|
+
if (near.length < 3) continue;
|
|
509
|
+
if (kind === "text") {
|
|
510
|
+
if (!p) continue;
|
|
511
|
+
const ml = median(near.map((n) => n.lines)), mw = median(near.map((n) => n.words / Math.max(1, n.lines)));
|
|
512
|
+
const wpl = p.words / Math.max(1, p.lines);
|
|
513
|
+
odd[i2] = p.words >= 3 && p.lines >= Math.max(3, 2 * ml, ml + 2) && wpl <= 0.5 * mw && wpl <= 2.5;
|
|
514
|
+
} else if (kind === "control") {
|
|
515
|
+
const visibleNear = near.filter((n) => n.visible).length;
|
|
516
|
+
odd[i2] = (!p || !p.visible) && visibleNear >= near.length - 1;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
let i = 0;
|
|
520
|
+
while (i < pts.length) {
|
|
521
|
+
if (!odd[i]) {
|
|
522
|
+
i++;
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
let j = i;
|
|
526
|
+
while (j + 1 < pts.length && odd[j + 1]) j++;
|
|
527
|
+
const before = pts.slice(0, i).some((p, k) => p && !odd[k]);
|
|
528
|
+
const after = pts.slice(j + 1).some((p, k) => p && !odd[j + 1 + k]);
|
|
529
|
+
const narrow = j - i + 1 <= Math.max(1, Math.floor(0.35 * pts.length));
|
|
530
|
+
if (before && after && narrow) {
|
|
531
|
+
const bad = pts[i] || pts.slice(i, j + 1).find(Boolean);
|
|
532
|
+
const good = pts.slice(0, i).reverse().find(Boolean);
|
|
533
|
+
const rule = kind === "text" ? "width-outlier" : "disappears-in-band";
|
|
534
|
+
findings.push({
|
|
535
|
+
rule,
|
|
536
|
+
keys: [key],
|
|
537
|
+
state,
|
|
538
|
+
widths: allWidths.slice(i, j + 1),
|
|
539
|
+
elements: [{ key, text: (bad || good)?.text || "" }],
|
|
540
|
+
rect: bad?.rect || good?.rect,
|
|
541
|
+
goodWidth: allWidths[allWidths.indexOf(allWidths[i]) - 1],
|
|
542
|
+
measured: kind === "text" && bad ? { lines: bad.lines, wordsPerLine: +(bad.words / bad.lines).toFixed(1) } : { visible: false },
|
|
543
|
+
expected: kind === "text" && good ? { lines: good.lines, wordsPerLine: +(good.words / Math.max(1, good.lines)).toFixed(1) } : { visible: true }
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
i = j + 1;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return findings;
|
|
550
|
+
}
|
|
551
|
+
function analyze(snapshots, options = {}) {
|
|
552
|
+
const allWidths = [...new Set(snapshots.map((s) => s.width))].sort((a, b) => a - b);
|
|
553
|
+
const raw = [];
|
|
554
|
+
for (const s of snapshots) for (const f of s.findings) raw.push({ ...f, state: s.state, widths: [s.width] });
|
|
555
|
+
const states = /* @__PURE__ */ new Map();
|
|
556
|
+
for (const s of snapshots) {
|
|
557
|
+
if (!states.has(s.state.id)) states.set(s.state.id, { state: s.state, snaps: [] });
|
|
558
|
+
states.get(s.state.id).snaps.push(s);
|
|
559
|
+
}
|
|
560
|
+
for (const { state, snaps } of states.values()) {
|
|
561
|
+
if (snaps.length < 5) continue;
|
|
562
|
+
const series = /* @__PURE__ */ new Map();
|
|
563
|
+
for (const s of snaps) for (const m of s.measures) {
|
|
564
|
+
if (!series.has(m.key + "|" + m.kind)) series.set(m.key + "|" + m.kind, /* @__PURE__ */ new Map());
|
|
565
|
+
series.get(m.key + "|" + m.kind).set(s.width, m);
|
|
566
|
+
}
|
|
567
|
+
const keyed = new Map([...series].map(([k, v]) => [k.split("|")[0], v]));
|
|
568
|
+
raw.push(...sweepOutliers(keyed, snaps.map((s) => s.width).sort((a, b) => a - b), state));
|
|
569
|
+
}
|
|
570
|
+
const groups = /* @__PURE__ */ new Map();
|
|
571
|
+
for (const f of raw) {
|
|
572
|
+
const id = f.rule + "|" + f.keys.join("+");
|
|
573
|
+
if (!groups.has(id)) groups.set(id, { ...f, widths: [], states: [], instances: [] });
|
|
574
|
+
const g = groups.get(id);
|
|
575
|
+
g.widths.push(...f.widths);
|
|
576
|
+
if (!g.states.some((s) => s.id === f.state.id)) g.states.push(f.state);
|
|
577
|
+
g.instances.push({ state: f.state.id, width: f.widths[0], rect: f.rect, measured: f.measured });
|
|
578
|
+
}
|
|
579
|
+
mergeOverlaps(groups);
|
|
580
|
+
const order = { error: 0, warning: 1, info: 2 };
|
|
581
|
+
const conf = { high: 0, medium: 1, low: 2 };
|
|
582
|
+
const out = [];
|
|
583
|
+
for (const [id, g] of groups) {
|
|
584
|
+
const meta = RULES[g.rule];
|
|
585
|
+
if (options.only && !options.only.includes(meta.pack) && !options.only.includes(g.rule)) continue;
|
|
586
|
+
if (options.skip && (options.skip.includes(meta.pack) || options.skip.includes(g.rule))) continue;
|
|
587
|
+
let severity = meta.severity, confidence = meta.confidence;
|
|
588
|
+
if (g.rule === "clipped-text" && g.ellipsis) {
|
|
589
|
+
severity = "info";
|
|
590
|
+
}
|
|
591
|
+
const ws = [...new Set(g.widths)].sort((a, b) => a - b);
|
|
592
|
+
const tries = likelyTries(g, meta);
|
|
593
|
+
out.push({
|
|
594
|
+
id: fingerprint(id),
|
|
595
|
+
rule: g.rule,
|
|
596
|
+
pack: meta.pack,
|
|
597
|
+
severity,
|
|
598
|
+
confidence,
|
|
599
|
+
title: meta.title,
|
|
600
|
+
elements: g.elements,
|
|
601
|
+
widths: ws,
|
|
602
|
+
bands: bands(ws, allWidths),
|
|
603
|
+
everyWidth: ws.length === allWidths.length,
|
|
604
|
+
states: g.states.map((s) => s.label),
|
|
605
|
+
measured: g.measured,
|
|
606
|
+
expected: g.expected,
|
|
607
|
+
why: meta.why,
|
|
608
|
+
fix: tries[0],
|
|
609
|
+
tries,
|
|
610
|
+
guide: REPO + "docs/rules/" + g.rule + ".md",
|
|
611
|
+
skill: skillFor(g.rule),
|
|
612
|
+
suppress: `data-rl-ignore="${g.rule}"`,
|
|
613
|
+
instances: g.instances.slice(0, 50),
|
|
614
|
+
goodWidth: g.goodWidth
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
if (options.minConfidence) out.splice(0, out.length, ...out.filter((f) => conf[f.confidence] <= conf[options.minConfidence]));
|
|
618
|
+
out.sort((a, b) => order[a.severity] - order[b.severity] || conf[a.confidence] - conf[b.confidence] || b.widths.length - a.widths.length);
|
|
619
|
+
return { widths: allWidths, findings: out };
|
|
620
|
+
}
|
|
621
|
+
function likelyTries(g, meta) {
|
|
622
|
+
const t = [...meta.tries];
|
|
623
|
+
const first = (i, text) => {
|
|
624
|
+
const [x] = t.splice(i, 1);
|
|
625
|
+
t.unshift(text || x);
|
|
626
|
+
};
|
|
627
|
+
const m = g.measured || {};
|
|
628
|
+
if (g.rule === "text-overlap") {
|
|
629
|
+
const top = g.onTop;
|
|
630
|
+
if (top && ["absolute", "fixed", "sticky"].includes(top.position)) {
|
|
631
|
+
first(0, `The text on top, ${top.summary}, is taken out of the normal layout: place it with flex or grid instead, or move it so it does not sit on the text. Changing z-index only swaps which text is hidden. (${top.key})`);
|
|
632
|
+
} else if (top && top.position === "static") first(1);
|
|
633
|
+
} else if (g.rule === "clipped-text") {
|
|
634
|
+
if (g.ellipsis) first(2, "The cut is marked with an ellipsis, so it looks intended: make sure a title or tooltip gives the full text.");
|
|
635
|
+
else if (m.contentWidth > m.boxWidth && !(m.contentHeight > m.boxHeight)) first(1);
|
|
636
|
+
}
|
|
637
|
+
return t;
|
|
638
|
+
}
|
|
639
|
+
function mergeOverlaps(groups) {
|
|
640
|
+
const parent = (k) => k.includes(">") ? k.slice(0, k.lastIndexOf(">")) : k;
|
|
641
|
+
const byRoot = /* @__PURE__ */ new Map();
|
|
642
|
+
for (const [id, g] of groups) {
|
|
643
|
+
if (g.rule !== "text-overlap") continue;
|
|
644
|
+
const [a, b] = g.keys;
|
|
645
|
+
for (const [shared, other] of [[a, b], [b, a]]) {
|
|
646
|
+
const root = shared + "~" + parent(other);
|
|
647
|
+
if (!byRoot.has(root)) byRoot.set(root, []);
|
|
648
|
+
byRoot.get(root).push({ id, shared, other });
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
const done = /* @__PURE__ */ new Set();
|
|
652
|
+
for (const [root, list] of [...byRoot].sort((x, y) => y[1].length - x[1].length)) {
|
|
653
|
+
const live = list.filter((x) => !done.has(x.id) && groups.has(x.id));
|
|
654
|
+
if (live.length < 2) continue;
|
|
655
|
+
const first = groups.get(live[0].id);
|
|
656
|
+
const shared = live[0].shared, container = parent(live[0].other);
|
|
657
|
+
const sharedEl = first.elements.find((e) => e.key === shared);
|
|
658
|
+
const others = live.map((x) => groups.get(x.id).elements.find((e) => e.key === x.other)?.text).filter(Boolean);
|
|
659
|
+
const merged = {
|
|
660
|
+
...first,
|
|
661
|
+
keys: [shared, container],
|
|
662
|
+
widths: [],
|
|
663
|
+
states: [],
|
|
664
|
+
instances: [],
|
|
665
|
+
elements: [sharedEl, { key: container, text: others.join(" \xB7 ").slice(0, 60) }]
|
|
666
|
+
};
|
|
667
|
+
for (const x of live) {
|
|
668
|
+
const g = groups.get(x.id);
|
|
669
|
+
merged.widths.push(...g.widths);
|
|
670
|
+
for (const st of g.states) if (!merged.states.some((s) => s.id === st.id)) merged.states.push(st);
|
|
671
|
+
merged.instances.push(...g.instances);
|
|
672
|
+
groups.delete(x.id);
|
|
673
|
+
done.add(x.id);
|
|
674
|
+
}
|
|
675
|
+
groups.set("text-overlap|" + shared + "+" + container, merged);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
function fingerprint(s) {
|
|
679
|
+
let h = 2166136261;
|
|
680
|
+
for (let i = 0; i < s.length; i++) {
|
|
681
|
+
h ^= s.charCodeAt(i);
|
|
682
|
+
h = Math.imul(h, 16777619);
|
|
683
|
+
}
|
|
684
|
+
return (h >>> 0).toString(36);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// src/core/highlight.js
|
|
688
|
+
var TOKENS = {
|
|
689
|
+
error: "hsla(358, 75%, 59%, 1)",
|
|
690
|
+
warning: "hsla(38, 92%, 50%, 1)",
|
|
691
|
+
info: "hsla(220, 9%, 46%, 1)",
|
|
692
|
+
labelText: "hsla(0, 0%, 100%, 1)",
|
|
693
|
+
halo: "hsla(0, 0%, 100%, 0.85)"
|
|
694
|
+
};
|
|
695
|
+
var findingLabel = (finding, width) => `${finding.title} \xB7 ${width} px`;
|
|
696
|
+
function highlight(doc, finding, { width, rect: fallback } = {}) {
|
|
697
|
+
const win = doc.defaultView;
|
|
698
|
+
clearHighlight(doc);
|
|
699
|
+
const els = (finding.elements || []).map((e) => {
|
|
700
|
+
try {
|
|
701
|
+
return doc.querySelector(e.key);
|
|
702
|
+
} catch {
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
const main = els[0];
|
|
707
|
+
if (main) {
|
|
708
|
+
const b = main.getBoundingClientRect();
|
|
709
|
+
const left = b.left < 0 || b.right > win.innerWidth ? Math.max(0, win.scrollX + b.left - 16) : win.scrollX;
|
|
710
|
+
win.scrollTo(left, Math.max(0, win.scrollY + b.top - Math.max(48, (win.innerHeight - b.height) / 3)));
|
|
711
|
+
} else if (fallback) {
|
|
712
|
+
win.scrollTo(0, Math.max(0, fallback.top - win.innerHeight / 3));
|
|
713
|
+
}
|
|
714
|
+
const pageRect = (el) => {
|
|
715
|
+
const b = el.getBoundingClientRect();
|
|
716
|
+
return { left: b.left + win.scrollX, top: b.top + win.scrollY, right: b.right + win.scrollX, bottom: b.bottom + win.scrollY };
|
|
717
|
+
};
|
|
718
|
+
const r = main ? pageRect(main) : fallback;
|
|
719
|
+
if (!r) return { found: false, rect: null, remove: () => {
|
|
720
|
+
} };
|
|
721
|
+
const color = TOKENS[finding.severity] || TOKENS.error;
|
|
722
|
+
const host = doc.createElement("div");
|
|
723
|
+
host.setAttribute("data-rl-overlay", "highlight");
|
|
724
|
+
host.style.cssText = "all:initial;position:absolute;left:0;top:0;width:0;height:0;z-index:2147483646;pointer-events:none";
|
|
725
|
+
const root = host.attachShadow({ mode: "open" });
|
|
726
|
+
const box = (q, dashed) => {
|
|
727
|
+
const d = doc.createElement("div");
|
|
728
|
+
d.style.cssText = `position:absolute;box-sizing:border-box;left:${q.left - 3}px;top:${q.top - 3}px;width:${q.right - q.left + 6}px;height:${q.bottom - q.top + 6}px;border:2px ${dashed ? "dashed" : "solid"} ${color};border-radius:4px;box-shadow:0 0 0 2px ${TOKENS.halo}`;
|
|
729
|
+
d.setAttribute("part", dashed ? "other" : "outline");
|
|
730
|
+
root.append(d);
|
|
731
|
+
};
|
|
732
|
+
box(r, false);
|
|
733
|
+
if (els[1] && els[1] !== main) box(pageRect(els[1]), true);
|
|
734
|
+
const above = r.top - win.scrollY > 56 || r.top > 56;
|
|
735
|
+
const x = Math.min(Math.max(r.left + 12, 12), win.scrollX + win.innerWidth - 24);
|
|
736
|
+
const arrow = doc.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
737
|
+
arrow.setAttribute("width", "16");
|
|
738
|
+
arrow.setAttribute("height", "10");
|
|
739
|
+
arrow.setAttribute("viewBox", "0 0 16 10");
|
|
740
|
+
arrow.setAttribute("part", "pointer");
|
|
741
|
+
arrow.style.cssText = `position:absolute;left:${x}px;top:${above ? r.top - 15 : r.bottom + 5}px;overflow:visible;${above ? "" : "transform:rotate(180deg)"}`;
|
|
742
|
+
arrow.innerHTML = `<path d="M0 0 L16 0 L8 10 Z" fill="${color}"/>`;
|
|
743
|
+
root.append(arrow);
|
|
744
|
+
const label2 = doc.createElement("div");
|
|
745
|
+
label2.setAttribute("part", "label");
|
|
746
|
+
label2.textContent = findingLabel(finding, width ?? win.innerWidth);
|
|
747
|
+
label2.style.cssText = `position:absolute;left:${Math.max(4, x - 8)}px;top:${above ? r.top - 15 : r.bottom + 15}px;transform:${above ? "translateY(-100%)" : "none"};max-width:${Math.max(160, win.innerWidth - 24)}px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;background:${color};color:${TOKENS.labelText};font:600 12px/1.4 system-ui,-apple-system,sans-serif;padding:3px 8px;border-radius:5px`;
|
|
748
|
+
root.append(label2);
|
|
749
|
+
doc.body.append(host);
|
|
750
|
+
return { found: !!main, rect: r, remove: () => host.remove() };
|
|
751
|
+
}
|
|
752
|
+
function clearHighlight(doc) {
|
|
753
|
+
for (const el of doc.querySelectorAll('[data-rl-overlay="highlight"]')) el.remove();
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// src/core/bar.js
|
|
757
|
+
var T = {
|
|
758
|
+
bg: "hsla(222, 18%, 13%, 0.96)",
|
|
759
|
+
fg: "hsla(0, 0%, 100%, 0.95)",
|
|
760
|
+
muted: "hsla(220, 12%, 72%, 1)",
|
|
761
|
+
line: "hsla(0, 0%, 100%, 0.14)",
|
|
762
|
+
hover: "hsla(0, 0%, 100%, 0.08)",
|
|
763
|
+
link: "hsla(212, 92%, 72%, 1)",
|
|
764
|
+
shadow: "hsla(0, 0%, 0%, 0.28)"
|
|
765
|
+
};
|
|
766
|
+
var CSS = `
|
|
767
|
+
:host { all: initial; }
|
|
768
|
+
.bar { position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); z-index: 2147483647; box-sizing: border-box;
|
|
769
|
+
width: min(560px, calc(100vw - 16px)); background: ${T.bg}; color: ${T.fg}; border-radius: 12px; padding: 10px 12px;
|
|
770
|
+
box-shadow: 0 8px 28px ${T.shadow}; font: 13px/1.45 system-ui, -apple-system, sans-serif; }
|
|
771
|
+
.nav { display: flex; align-items: center; gap: 6px; }
|
|
772
|
+
button { all: unset; cursor: pointer; padding: 3px 10px; border-radius: 7px; border: 1px solid ${T.line}; color: ${T.fg}; font: inherit; }
|
|
773
|
+
button:hover:not([disabled]) { background: ${T.hover}; }
|
|
774
|
+
button[disabled] { opacity: .4; cursor: default; }
|
|
775
|
+
button.close { border-color: transparent; margin-left: auto; padding: 3px 8px; font-size: 16px; line-height: 1; }
|
|
776
|
+
.count { color: ${T.muted}; min-width: 56px; text-align: center; font-variant-numeric: tabular-nums; }
|
|
777
|
+
.title { margin-top: 8px; font-weight: 600; display: flex; gap: 8px; align-items: baseline; }
|
|
778
|
+
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; transform: translateY(-1px); }
|
|
779
|
+
.where, .note, .skill { color: ${T.muted}; margin-top: 2px; }
|
|
780
|
+
.try { margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
|
|
781
|
+
a { color: ${T.link}; text-decoration: none; }
|
|
782
|
+
a:hover { text-decoration: none; opacity: .85; }
|
|
783
|
+
[hidden] { display: none !important; }
|
|
784
|
+
`;
|
|
785
|
+
var fmtWhere = (f) => (f.bands || []).map(([a, b]) => a === b ? `${a}` : `${a}\u2013${b}`).join(", ") + " px";
|
|
786
|
+
function createBar(doc, { onPrev, onNext, onClose } = {}) {
|
|
787
|
+
const host = doc.createElement("div");
|
|
788
|
+
host.setAttribute("data-rl-overlay", "bar");
|
|
789
|
+
const root = host.attachShadow({ mode: "open" });
|
|
790
|
+
root.innerHTML = `<style>${CSS}</style><div class="bar" role="dialog" aria-label="ResponsiveLint replay">
|
|
791
|
+
<div class="nav"><button class="prev" type="button">\u2039 Previous</button><span class="count"></span><button class="next" type="button">Next \u203A</button>
|
|
792
|
+
<button class="close" type="button" aria-label="Close">\xD7</button></div>
|
|
793
|
+
<div class="title"><span class="dot"></span><span class="text"></span></div>
|
|
794
|
+
<div class="where"></div><div class="try"></div><div class="skill"></div><div class="note" hidden></div></div>`;
|
|
795
|
+
const $ = (s) => root.querySelector(s);
|
|
796
|
+
$(".prev").onclick = () => onPrev?.();
|
|
797
|
+
$(".next").onclick = () => onNext?.();
|
|
798
|
+
$(".close").onclick = () => onClose?.();
|
|
799
|
+
const key = (e) => {
|
|
800
|
+
if (e.target?.closest?.("input,textarea,select,[contenteditable]")) return;
|
|
801
|
+
if (e.key === "ArrowRight") onNext?.();
|
|
802
|
+
else if (e.key === "ArrowLeft") onPrev?.();
|
|
803
|
+
else if (e.key === "Escape") onClose?.();
|
|
804
|
+
else return;
|
|
805
|
+
e.preventDefault();
|
|
806
|
+
};
|
|
807
|
+
doc.addEventListener("keydown", key, true);
|
|
808
|
+
(doc.body || doc.documentElement).append(host);
|
|
809
|
+
return {
|
|
810
|
+
host,
|
|
811
|
+
update({ finding: f, index, total, width, note, busy }) {
|
|
812
|
+
$(".count").textContent = `${index + 1} of ${total}`;
|
|
813
|
+
$(".prev").disabled = busy || index <= 0;
|
|
814
|
+
$(".next").disabled = busy || index >= total - 1;
|
|
815
|
+
$(".dot").style.background = TOKENS[f.severity] || TOKENS.error;
|
|
816
|
+
$(".text").textContent = f.title;
|
|
817
|
+
const states = (f.states || []).join(" \xB7 ");
|
|
818
|
+
$(".where").textContent = `${fmtWhere(f)}${states ? " \xB7 " + states : ""}${width ? ` \xB7 shown at ${width} px` : ""}`;
|
|
819
|
+
$(".try").textContent = f.tries?.[0] ? "Try: " + f.tries[0] : "";
|
|
820
|
+
const sk = $(".skill");
|
|
821
|
+
sk.textContent = "";
|
|
822
|
+
if (f.skill?.url) {
|
|
823
|
+
const a = doc.createElement("a");
|
|
824
|
+
a.href = f.skill.url;
|
|
825
|
+
a.target = "_blank";
|
|
826
|
+
a.rel = "noopener";
|
|
827
|
+
a.textContent = f.skill.name;
|
|
828
|
+
sk.append("Fix skill: ", a);
|
|
829
|
+
}
|
|
830
|
+
$(".note").hidden = !(note || busy);
|
|
831
|
+
$(".note").textContent = busy ? "Loading from the start\u2026" : note || "";
|
|
832
|
+
},
|
|
833
|
+
remove() {
|
|
834
|
+
doc.removeEventListener("keydown", key, true);
|
|
835
|
+
host.remove();
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
// src/core/recipe.js
|
|
841
|
+
var START_WIDTH = 1280;
|
|
842
|
+
function recipe(f, states, { url, route, storage, height }) {
|
|
843
|
+
const inst = f.instances[0];
|
|
844
|
+
const state = states.find((s) => s.id === inst?.state) || states[0];
|
|
845
|
+
const seen = f.instances.filter((i) => i.state === inst?.state).map((i) => i.width);
|
|
846
|
+
const band = f.bands.find(([a, b]) => seen.some((w) => w >= a && w <= b)) || f.bands[0];
|
|
847
|
+
const mid = (band[0] + band[1]) / 2;
|
|
848
|
+
const inBand = seen.filter((w) => w >= band[0] && w <= band[1]);
|
|
849
|
+
const width = (inBand.length ? inBand : seen.length ? seen : [Math.round(mid)]).reduce((best, w) => Math.abs(w - mid) < Math.abs(best - mid) ? w : best);
|
|
850
|
+
return { url, route, storage, width, height, startWidth: START_WIDTH, state: state.id, label: state.label, actions: state.actions || [] };
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// src/browser/entry.js
|
|
854
|
+
var FRAME = "responsivelint-frame";
|
|
855
|
+
var script = document.currentScript;
|
|
856
|
+
var opts = script ? script.dataset : {};
|
|
857
|
+
if (window.name === FRAME) {
|
|
858
|
+
window.__ResponsiveLintFrame = { checkNow };
|
|
859
|
+
} else {
|
|
860
|
+
const local = /^(localhost|127\.0\.0\.1|\[::1\]|.+\.localhost|.+\.test)$/.test(location.hostname) || location.protocol === "file:";
|
|
861
|
+
const api2 = window.ResponsiveLint = createApi();
|
|
862
|
+
if (local || "anywhere" in opts) {
|
|
863
|
+
const start = async () => {
|
|
864
|
+
api2.check();
|
|
865
|
+
const replaying = opts.ui === "replay";
|
|
866
|
+
if ("ui" in opts && !replaying) api2.ui(true);
|
|
867
|
+
if ("sweep" in opts && (!replaying || !opts.report)) await api2.sweep();
|
|
868
|
+
if (replaying) api2.replay();
|
|
869
|
+
};
|
|
870
|
+
if (document.readyState === "complete") setTimeout(start, 50);
|
|
871
|
+
else addEventListener("load", () => setTimeout(start, 50));
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
var TIMING = { wait: 300, clickWait: 300, clickTimeout: 3e3, pace: 0 };
|
|
875
|
+
var NETWORK_CAP = 5e3;
|
|
876
|
+
var NETWORK_IDLE = 250;
|
|
877
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
878
|
+
var frames = (win, n = 2) => new Promise((r) => {
|
|
879
|
+
const step = (k) => k ? win.requestAnimationFrame(() => step(k - 1)) : r();
|
|
880
|
+
step(n);
|
|
881
|
+
});
|
|
882
|
+
function timing(o = {}) {
|
|
883
|
+
const t = { ...TIMING };
|
|
884
|
+
for (const k of Object.keys(TIMING)) {
|
|
885
|
+
const v = o[k] ?? opts[k];
|
|
886
|
+
if (v != null && v !== "" && Number.isFinite(+v) && +v >= 0) t[k] = +v;
|
|
887
|
+
}
|
|
888
|
+
return t;
|
|
889
|
+
}
|
|
890
|
+
async function settle(win, extra = 0) {
|
|
891
|
+
try {
|
|
892
|
+
await win.document.fonts?.ready;
|
|
893
|
+
await frames(win);
|
|
894
|
+
const anims = (win.document.getAnimations?.() || []).filter((a) => a.playState === "running" && a.effect?.getTiming?.().iterations !== Infinity);
|
|
895
|
+
await Promise.race([Promise.all(anims.map((a) => a.finished.catch(() => {
|
|
896
|
+
}))), sleep(600)]);
|
|
897
|
+
} catch {
|
|
898
|
+
}
|
|
899
|
+
if (extra) await sleep(extra);
|
|
900
|
+
}
|
|
901
|
+
function trackRequests(win) {
|
|
902
|
+
let open = 0, last = 0;
|
|
903
|
+
const bump = (d) => {
|
|
904
|
+
open = Math.max(0, open + d);
|
|
905
|
+
last = Date.now();
|
|
906
|
+
};
|
|
907
|
+
const read = (m) => String(m || "GET").toUpperCase();
|
|
908
|
+
const safe = (m) => ["GET", "HEAD", "OPTIONS"].includes(read(m));
|
|
909
|
+
try {
|
|
910
|
+
const f = win.fetch;
|
|
911
|
+
if (f) win.fetch = function(input, init) {
|
|
912
|
+
const method = init?.method || input && typeof input === "object" && input.method;
|
|
913
|
+
if (!safe(method)) return Promise.reject(new TypeError("ResponsiveLint replay blocks " + read(method) + " requests"));
|
|
914
|
+
bump(1);
|
|
915
|
+
return f.call(this, input, init).finally(() => bump(-1));
|
|
916
|
+
};
|
|
917
|
+
const X = win.XMLHttpRequest?.prototype;
|
|
918
|
+
if (X) {
|
|
919
|
+
const xopen = X.open, send = X.send;
|
|
920
|
+
X.open = function(method, ...rest) {
|
|
921
|
+
this.__rlMethod = method;
|
|
922
|
+
return xopen.call(this, method, ...rest);
|
|
923
|
+
};
|
|
924
|
+
X.send = function(...a) {
|
|
925
|
+
if (!safe(this.__rlMethod)) {
|
|
926
|
+
this.abort();
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
bump(1);
|
|
930
|
+
this.addEventListener("loadend", () => bump(-1), { once: true });
|
|
931
|
+
return send.apply(this, a);
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
} catch {
|
|
935
|
+
}
|
|
936
|
+
return async () => {
|
|
937
|
+
const end = Date.now() + NETWORK_CAP;
|
|
938
|
+
while (Date.now() < end) {
|
|
939
|
+
if (!open && Date.now() - last >= NETWORK_IDLE) return;
|
|
940
|
+
await sleep(40);
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
async function waitFor(doc, selector, timeout) {
|
|
945
|
+
const end = Date.now() + timeout;
|
|
946
|
+
for (; ; ) {
|
|
947
|
+
const el = doc.querySelector(selector);
|
|
948
|
+
if (el) {
|
|
949
|
+
const r = el.getBoundingClientRect();
|
|
950
|
+
if (r.width && r.height && doc.defaultView.getComputedStyle(el).visibility !== "hidden") return el;
|
|
951
|
+
}
|
|
952
|
+
if (Date.now() > end) throw new Error(`no visible ${selector} after ${timeout} ms`);
|
|
953
|
+
await sleep(50);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
function createApi() {
|
|
957
|
+
const logged = /* @__PURE__ */ new Set();
|
|
958
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
959
|
+
let current = [], sweepFindings = [], uiOn = false, timer = 0;
|
|
960
|
+
const emit = (f) => listeners.forEach((fn) => {
|
|
961
|
+
try {
|
|
962
|
+
fn(f);
|
|
963
|
+
} catch {
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
function report(findings) {
|
|
967
|
+
for (const f of findings) {
|
|
968
|
+
if (logged.has(f.id)) continue;
|
|
969
|
+
logged.add(f.id);
|
|
970
|
+
emit(f);
|
|
971
|
+
const color = f.severity === "error" ? "#e5484d" : f.severity === "warning" ? "#d08a12" : "#888";
|
|
972
|
+
console.groupCollapsed(`%cResponsiveLint%c ${f.severity === "error" ? "\u2716" : "\u25B2"} ${f.rule}: ${f.title}`, "font-weight:700", `color:${color}`);
|
|
973
|
+
for (const e of f.elements) console.log(e.text ? `\u201C${e.text}\u201D` : e.key, document.querySelector(e.key) || e.key);
|
|
974
|
+
console.log("where:", f.bands.map(([a, b]) => a === b ? a : `${a}\u2013${b}`).join(", ") + " px", f.states.join(" \xB7 "));
|
|
975
|
+
if (f.measured) console.log("measured:", f.measured, f.expected ? ["neighbours:", f.expected] : "");
|
|
976
|
+
console.log("why:", f.why);
|
|
977
|
+
console.log("try:\n" + f.tries.map((t, i) => `${i + 1}. ${t}`).join("\n"));
|
|
978
|
+
console.log("skill:", f.skill.name, f.skill.url);
|
|
979
|
+
console.log("silence:", f.suppress);
|
|
980
|
+
console.groupEnd();
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
function check() {
|
|
984
|
+
const snap = checkNow();
|
|
985
|
+
const env = detectEnvironment();
|
|
986
|
+
current = analyze([{ state: { id: "live", label: "live page" }, width: snap.width, findings: snap.findings, measures: [] }]).findings;
|
|
987
|
+
report(current);
|
|
988
|
+
if (env.is3D && !logged.has("3d")) {
|
|
989
|
+
logged.add("3d");
|
|
990
|
+
console.info("ResponsiveLint: 3D detected (" + (env.frameworks.join(", ") || "WebGL") + "). The 3D rule pack is not implemented yet.");
|
|
991
|
+
}
|
|
992
|
+
if (uiOn) draw();
|
|
993
|
+
return current;
|
|
994
|
+
}
|
|
995
|
+
const recheck = () => {
|
|
996
|
+
clearTimeout(timer);
|
|
997
|
+
timer = setTimeout(check, 500);
|
|
998
|
+
};
|
|
999
|
+
addEventListener("resize", recheck);
|
|
1000
|
+
addEventListener("click", recheck, true);
|
|
1001
|
+
new MutationObserver((m) => {
|
|
1002
|
+
if (m.some((r) => !(r.target instanceof Element && r.target.closest("[data-rl-overlay]")))) recheck();
|
|
1003
|
+
}).observe(document.documentElement, { subtree: true, childList: true, attributes: true, attributeFilter: ["class", "style", "hidden", "aria-expanded", "open"] });
|
|
1004
|
+
async function sweep({ from = 320, to = 1920, step = 48 } = {}) {
|
|
1005
|
+
const frame = document.createElement("iframe");
|
|
1006
|
+
frame.name = FRAME;
|
|
1007
|
+
frame.src = location.href;
|
|
1008
|
+
frame.setAttribute("data-rl-overlay", "");
|
|
1009
|
+
frame.style.cssText = `position:fixed;left:-20000px;top:0;height:${innerHeight}px;width:1280px;border:0;visibility:hidden`;
|
|
1010
|
+
document.body.append(frame);
|
|
1011
|
+
try {
|
|
1012
|
+
await new Promise((r, x) => {
|
|
1013
|
+
frame.onload = r;
|
|
1014
|
+
setTimeout(() => x(new Error("timeout")), 2e4);
|
|
1015
|
+
});
|
|
1016
|
+
const win = frame.contentWindow;
|
|
1017
|
+
if (!win.__ResponsiveLintFrame) throw new Error("the page does not load this script in a frame");
|
|
1018
|
+
await settle(win, timing().wait);
|
|
1019
|
+
const snaps = [];
|
|
1020
|
+
for (let w = from; w <= to; w += step) {
|
|
1021
|
+
frame.style.width = w + "px";
|
|
1022
|
+
await new Promise((r) => win.requestAnimationFrame(() => win.requestAnimationFrame(() => setTimeout(r, 120))));
|
|
1023
|
+
const s = win.__ResponsiveLintFrame.checkNow();
|
|
1024
|
+
snaps.push({ state: { id: "sweep", label: "width sweep" }, width: w, findings: s.findings, measures: s.measures });
|
|
1025
|
+
}
|
|
1026
|
+
sweepFindings = analyze(snaps).findings;
|
|
1027
|
+
report(sweepFindings);
|
|
1028
|
+
if (uiOn) draw();
|
|
1029
|
+
return sweepFindings;
|
|
1030
|
+
} catch (e) {
|
|
1031
|
+
console.info("ResponsiveLint: the width sweep could not run (" + e.message + "). Use the command line, npx responsivelint, for a full sweep.");
|
|
1032
|
+
return [];
|
|
1033
|
+
} finally {
|
|
1034
|
+
frame.remove();
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
let layer = null;
|
|
1038
|
+
function draw() {
|
|
1039
|
+
layer?.remove();
|
|
1040
|
+
if (!uiOn) return;
|
|
1041
|
+
layer = document.createElement("div");
|
|
1042
|
+
layer.setAttribute("data-rl-overlay", "");
|
|
1043
|
+
layer.style.cssText = "position:absolute;left:0;top:0;width:0;height:0;z-index:2147483646;pointer-events:none";
|
|
1044
|
+
const all = [...current, ...sweepFindings.filter((f) => !current.some((c) => c.id === f.id))];
|
|
1045
|
+
for (const f of current) {
|
|
1046
|
+
for (const inst of f.instances) {
|
|
1047
|
+
const r = inst.rect;
|
|
1048
|
+
if (!r) continue;
|
|
1049
|
+
const box = document.createElement("div");
|
|
1050
|
+
box.style.cssText = `position:absolute;left:${r.left}px;top:${r.top}px;width:${r.right - r.left}px;height:${r.bottom - r.top}px;outline:2px solid ${f.severity === "error" ? "#e5484d" : "#d08a12"};outline-offset:1px;border-radius:2px`;
|
|
1051
|
+
box.title = f.title;
|
|
1052
|
+
layer.append(box);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
const panel = document.createElement("div");
|
|
1056
|
+
panel.style.cssText = "position:fixed;right:12px;bottom:12px;max-width:360px;max-height:50vh;overflow:auto;pointer-events:auto;background:hsla(0,0%,100%,.97);color:#1d1f24;border-radius:10px;box-shadow:0 6px 24px hsla(0,0%,0%,.18);font:12px/1.45 system-ui,sans-serif;padding:10px 12px";
|
|
1057
|
+
panel.innerHTML = `<div style="display:flex;align-items:center;gap:8px;margin-bottom:6px"><b style="margin-right:auto">ResponsiveLint \xB7 ${all.length} finding${all.length === 1 ? "" : "s"}</b>${all.length ? '<button type="button" data-act="replay" style="border:1px solid hsla(220,15%,85%,1);border-radius:6px;background:none;cursor:pointer;font:inherit;padding:1px 8px">Replay</button>' : ""}<button type="button" data-act="close" style="border:0;background:none;cursor:pointer;font:inherit">\xD7</button></div>`;
|
|
1058
|
+
panel.querySelector("[data-act=close]").onclick = () => api.ui(false);
|
|
1059
|
+
const rb = panel.querySelector("[data-act=replay]");
|
|
1060
|
+
if (rb) rb.onclick = () => api.replay(all);
|
|
1061
|
+
for (const f of all) {
|
|
1062
|
+
const row = document.createElement("div");
|
|
1063
|
+
row.style.cssText = "padding:5px 0;border-top:1px solid hsla(220,15%,90%,1);cursor:pointer";
|
|
1064
|
+
const where = f.bands.map(([a, b]) => a === b ? a : `${a}\u2013${b}`).join(", ") + " px";
|
|
1065
|
+
const esc = (t) => String(t).replace(/[&<>"]/g, (ch) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[ch]);
|
|
1066
|
+
row.innerHTML = `<span style="color:${f.severity === "error" ? "#e5484d" : "#b37400"}">${f.severity === "error" ? "\u2716" : "\u25B2"}</span> ${esc(f.title)}<br><span style="color:#6b7280">${esc((f.elements[0]?.text || f.elements[0]?.key || "").slice(0, 50))} \xB7 ${where}</span><br><span style="color:#374151">Try: ${esc(f.tries[0])}</span> <a href="${esc(f.skill.url)}" target="_blank" rel="noopener" style="color:hsla(220,70%,45%,1);text-decoration:none">${esc(f.skill.name)}</a>`;
|
|
1067
|
+
row.onclick = () => document.querySelector(f.elements[0]?.key)?.scrollIntoView({ block: "center", behavior: "smooth" });
|
|
1068
|
+
panel.append(row);
|
|
1069
|
+
}
|
|
1070
|
+
layer.append(panel);
|
|
1071
|
+
if (session) layer.style.display = "none";
|
|
1072
|
+
document.body.append(layer);
|
|
1073
|
+
}
|
|
1074
|
+
let session = null;
|
|
1075
|
+
async function loadReport(url) {
|
|
1076
|
+
const res = await fetch(url, { cache: "no-store" });
|
|
1077
|
+
if (!res.ok) throw new Error(`${url}: ${res.status}`);
|
|
1078
|
+
const r = await res.json();
|
|
1079
|
+
return r.findings || [];
|
|
1080
|
+
}
|
|
1081
|
+
async function replay(list, o = {}) {
|
|
1082
|
+
if (!list) {
|
|
1083
|
+
try {
|
|
1084
|
+
list = opts.report ? await loadReport(opts.report) : [...current, ...sweepFindings.filter((f) => !current.some((c) => c.id === f.id))];
|
|
1085
|
+
} catch (e) {
|
|
1086
|
+
console.info("ResponsiveLint: could not load the report (" + e.message + ").");
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
if (!list.length) {
|
|
1091
|
+
console.info("ResponsiveLint: nothing to replay.");
|
|
1092
|
+
return null;
|
|
1093
|
+
}
|
|
1094
|
+
session?.close();
|
|
1095
|
+
if (layer) layer.style.display = "none";
|
|
1096
|
+
const t = timing(o);
|
|
1097
|
+
const steps = list.map((f) => ({ finding: f, recipe: f.replay || recipe(f, [{ id: f.instances?.[0]?.state || "live", label: "live page", actions: [] }], { url: location.href, route: null, storage: {}, height: innerHeight }) }));
|
|
1098
|
+
const saved = /* @__PURE__ */ new Map();
|
|
1099
|
+
const store = (k, v) => {
|
|
1100
|
+
try {
|
|
1101
|
+
if (!saved.has(k)) saved.set(k, localStorage.getItem(k));
|
|
1102
|
+
localStorage.setItem(k, v);
|
|
1103
|
+
} catch {
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
let index = -1, frame = null, token = 0, last = 0, closed = false;
|
|
1107
|
+
const paced = async () => {
|
|
1108
|
+
const gap = last + t.pace - Date.now();
|
|
1109
|
+
if (gap > 0) await sleep(gap);
|
|
1110
|
+
last = Date.now();
|
|
1111
|
+
};
|
|
1112
|
+
const shade = document.createElement("div");
|
|
1113
|
+
shade.setAttribute("data-rl-overlay", "replay");
|
|
1114
|
+
shade.style.cssText = "position:fixed;inset:0;z-index:2147483645;background:hsla(222,18%,10%,0.55)";
|
|
1115
|
+
const stage = document.createElement("div");
|
|
1116
|
+
stage.style.cssText = "position:absolute;left:0;top:0;transform-origin:0 0;box-shadow:0 8px 32px hsla(0,0%,0%,0.35);background:hsla(0,0%,100%,1)";
|
|
1117
|
+
shade.append(stage);
|
|
1118
|
+
document.body.append(shade);
|
|
1119
|
+
const bar = createBar(document, { onPrev: () => go(index - 1), onNext: () => go(index + 1), onClose: () => close() });
|
|
1120
|
+
const BAR_SPACE = 204, GAP = 12;
|
|
1121
|
+
const fit = (w, h) => {
|
|
1122
|
+
const s = Math.min(1, (innerWidth - 2 * GAP) / w, (innerHeight - BAR_SPACE - GAP) / h);
|
|
1123
|
+
frame.style.width = w + "px";
|
|
1124
|
+
frame.style.height = h + "px";
|
|
1125
|
+
stage.style.width = w + "px";
|
|
1126
|
+
stage.style.height = h + "px";
|
|
1127
|
+
stage.style.transform = `translate(${Math.round((innerWidth - w * s) / 2)}px, ${GAP}px) scale(${s})`;
|
|
1128
|
+
stage.dataset.scale = s.toFixed(3);
|
|
1129
|
+
};
|
|
1130
|
+
function url(r) {
|
|
1131
|
+
const u = new URL(r.url, location.href);
|
|
1132
|
+
return u.origin === location.origin ? u.href : location.origin + u.pathname + u.search + u.hash;
|
|
1133
|
+
}
|
|
1134
|
+
async function go(i) {
|
|
1135
|
+
if (closed || i < 0 || i >= steps.length) return null;
|
|
1136
|
+
const my = ++token;
|
|
1137
|
+
index = i;
|
|
1138
|
+
const { finding: f, recipe: r } = steps[i];
|
|
1139
|
+
const h = r.height || innerHeight;
|
|
1140
|
+
bar.update({ finding: f, index: i, total: steps.length, width: r.width, busy: true });
|
|
1141
|
+
frame?.remove();
|
|
1142
|
+
for (const [k, v] of Object.entries(r.storage || {})) store(k, v);
|
|
1143
|
+
await paced();
|
|
1144
|
+
if (my !== token) return null;
|
|
1145
|
+
frame = document.createElement("iframe");
|
|
1146
|
+
frame.name = FRAME;
|
|
1147
|
+
frame.setAttribute("data-rl-overlay", "replay-frame");
|
|
1148
|
+
frame.title = "ResponsiveLint replay";
|
|
1149
|
+
frame.style.cssText = "display:block;border:0;background:hsla(0,0%,100%,1)";
|
|
1150
|
+
stage.append(frame);
|
|
1151
|
+
fit(r.startWidth || START_WIDTH, h);
|
|
1152
|
+
let note = "";
|
|
1153
|
+
try {
|
|
1154
|
+
await new Promise((ok, fail) => {
|
|
1155
|
+
frame.onload = ok;
|
|
1156
|
+
setTimeout(() => fail(new Error("the page took over 20 s to load")), 2e4);
|
|
1157
|
+
frame.src = url(r);
|
|
1158
|
+
});
|
|
1159
|
+
if (my !== token) return null;
|
|
1160
|
+
const win = frame.contentWindow, doc = frame.contentDocument;
|
|
1161
|
+
if (!doc) throw new Error("the frame is not on the same site");
|
|
1162
|
+
const quiet = trackRequests(win);
|
|
1163
|
+
await settle(win, t.wait);
|
|
1164
|
+
for (const a of r.actions || []) {
|
|
1165
|
+
const el = await waitFor(doc, a.click, t.clickTimeout);
|
|
1166
|
+
if (my !== token) return null;
|
|
1167
|
+
await paced();
|
|
1168
|
+
el.click();
|
|
1169
|
+
await quiet();
|
|
1170
|
+
await settle(win, 150);
|
|
1171
|
+
if (t.clickWait) await sleep(t.clickWait);
|
|
1172
|
+
}
|
|
1173
|
+
fit(r.width, h);
|
|
1174
|
+
await settle(win);
|
|
1175
|
+
if (my !== token) return null;
|
|
1176
|
+
const inst = (f.instances || []).find((x) => x.width === r.width && x.state === r.state && x.rect);
|
|
1177
|
+
const shown = highlight(doc, f, { width: r.width, rect: inst?.rect });
|
|
1178
|
+
if (!shown.found) note = "The element is not on the page at this width.";
|
|
1179
|
+
if (!(r.actions || []).length && !f.replay) note = note || "Shown in the starting state: clicks made on the live page are not recorded.";
|
|
1180
|
+
} catch (e) {
|
|
1181
|
+
note = "Could not repeat the steps: " + e.message;
|
|
1182
|
+
}
|
|
1183
|
+
if (my !== token) return null;
|
|
1184
|
+
bar.update({ finding: f, index: i, total: steps.length, width: r.width, note });
|
|
1185
|
+
shade.dataset.step = String(i);
|
|
1186
|
+
return { index: i, note, frame };
|
|
1187
|
+
}
|
|
1188
|
+
function close() {
|
|
1189
|
+
if (closed) return;
|
|
1190
|
+
closed = true;
|
|
1191
|
+
token++;
|
|
1192
|
+
bar.remove();
|
|
1193
|
+
shade.remove();
|
|
1194
|
+
for (const [k, v] of saved) try {
|
|
1195
|
+
v == null ? localStorage.removeItem(k) : localStorage.setItem(k, v);
|
|
1196
|
+
} catch {
|
|
1197
|
+
}
|
|
1198
|
+
if (session === api_) {
|
|
1199
|
+
session = null;
|
|
1200
|
+
draw();
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
const api_ = { go, close, get index() {
|
|
1204
|
+
return index;
|
|
1205
|
+
}, get frame() {
|
|
1206
|
+
return frame;
|
|
1207
|
+
}, steps: steps.map((s) => s.recipe), done: null };
|
|
1208
|
+
session = api_;
|
|
1209
|
+
api_.done = go(o.start || 0);
|
|
1210
|
+
await api_.done;
|
|
1211
|
+
return api_;
|
|
1212
|
+
}
|
|
1213
|
+
return {
|
|
1214
|
+
check,
|
|
1215
|
+
sweep,
|
|
1216
|
+
replay,
|
|
1217
|
+
ui(on = true) {
|
|
1218
|
+
uiOn = !!on;
|
|
1219
|
+
draw();
|
|
1220
|
+
return uiOn;
|
|
1221
|
+
},
|
|
1222
|
+
on(event, fn) {
|
|
1223
|
+
if (event === "finding") listeners.add(fn);
|
|
1224
|
+
return () => listeners.delete(fn);
|
|
1225
|
+
},
|
|
1226
|
+
get findings() {
|
|
1227
|
+
return [...current, ...sweepFindings];
|
|
1228
|
+
},
|
|
1229
|
+
rules: RULES
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
})();
|