@botiverse/porthole 0.9.0 → 0.12.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/dist/annotate/types.d.ts +21 -7
- package/dist/annotate/validate.d.ts +18 -3
- package/dist/annotate/validate.js +29 -5
- package/dist/bridge/bridge.js +1 -51
- package/dist/bridge/unseen.d.ts +18 -0
- package/dist/bridge/unseen.js +71 -0
- package/dist/check/files.d.ts +2 -0
- package/dist/check/files.js +67 -50
- package/dist/check/lint.d.ts +8 -1
- package/dist/check/lint.js +18 -0
- package/dist/check/markup.d.ts +7 -0
- package/dist/check/markup.js +49 -0
- package/dist/check/page-source.js +1 -1
- package/dist/check/scroll.d.ts +100 -0
- package/dist/check/scroll.js +1429 -0
- package/dist/conformance/behavior.js +107 -68
- package/dist/conformance/clipboard.js +16 -14
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/limits.js +117 -64
- package/dist/conformance/probes.js +66 -49
- package/dist/conformance/runtime.js +6 -0
- package/dist/conformance/served.js +66 -63
- package/dist/guide/guide.js +1 -1
- package/dist/guide/runtime.js +2 -2
- package/dist/host/create-host.js +46 -4
- package/dist/index.d.ts +1 -1
- package/dist/platform/types.d.ts +4 -2
- package/dist/react/frame.d.ts +2 -1
- package/dist/runtime/requests.d.ts +5 -0
- package/dist/runtime/requests.js +16 -1
- package/dist/runtime/types.generated.js +1 -1
- package/dist/shell/annotate.d.ts +6 -3
- package/dist/shell/annotate.js +474 -104
- package/dist/shell/use.js +9 -1
- package/docs/ADOPTING.md +99 -1
- package/docs/ARCHITECTURE.md +2 -1
- package/docs/DECISIONS.md +764 -14
- package/docs/DESIGN.md +192 -42
- package/docs/THREAT_MODEL.md +109 -27
- package/package.json +1 -1
- package/src/annotate/types.ts +22 -7
- package/src/annotate/validate.ts +32 -6
- package/src/bridge/bridge.ts +1 -60
- package/src/bridge/unseen.ts +82 -0
- package/src/check/files.ts +68 -54
- package/src/check/lint.ts +24 -0
- package/src/check/markup.ts +53 -0
- package/src/check/scroll.ts +1757 -0
- package/src/conformance/behavior.ts +221 -69
- package/src/conformance/clipboard.ts +44 -14
- package/src/conformance/limits.ts +169 -76
- package/src/conformance/probes.ts +97 -49
- package/src/conformance/runtime.ts +9 -0
- package/src/conformance/served.ts +112 -63
- package/src/guide/guide.ts +1 -0
- package/src/guide/runtime.ts +2 -2
- package/src/host/create-host.ts +50 -4
- package/src/index.ts +1 -0
- package/src/platform/types.ts +4 -2
- package/src/react/frame.tsx +2 -1
- package/src/runtime/requests.ts +23 -1
- package/src/runtime/types.generated.ts +1 -1
- package/src/shell/annotate.ts +474 -104
- package/src/shell/use.ts +9 -1
- package/types/runtime/v1/comments.d.ts +14 -4
|
@@ -0,0 +1,1757 @@
|
|
|
1
|
+
import { attributesOf } from "./markup.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether a page's own styles hold it to one screen (P106), read from its text: no layout, a
|
|
5
|
+
* cascade of the few properties that matter over the three elements that hold a document (html,
|
|
6
|
+
* body, and the body's one element of content, `#root` in an app's build). An app shell's styles
|
|
7
|
+
* (`html, body { height: 100%; overflow: hidden }`, `#root { position: fixed; height: 100dvh }`)
|
|
8
|
+
* stop a long page at its first screen in a frame: the person's wheel finds nothing to scroll.
|
|
9
|
+
*
|
|
10
|
+
* What counts, per element, by its cascaded values:
|
|
11
|
+
* - html with overflow-y hidden or clip (the viewport's own: nothing scrolls);
|
|
12
|
+
* - body with overflow-y hidden or clip, where html's overflow is visible (body's then becomes the
|
|
13
|
+
* viewport's) or body is one screen tall;
|
|
14
|
+
* - the root with overflow-y hidden or clip and one screen tall;
|
|
15
|
+
* - any of them position: fixed and one screen tall (a fixed box's content scrolls nothing);
|
|
16
|
+
* and nothing in the page's markup below body that scrolls itself: no element there with style=""
|
|
17
|
+
* or a Tailwind utility giving it overflow-y auto or scroll, or matched by a rule the check reads
|
|
18
|
+
* that does (a rule whose selector it can't read, or that matches nothing in the markup, doesn't
|
|
19
|
+
* count; nor does the root, by its style="", classes or rules: its own values say whether it
|
|
20
|
+
* scrolls the page, one screen tall or, in a one-screen body, in a height the check can't read
|
|
21
|
+
* (under a flex or grid body, a height of another shape), or grows with it). Then the page may
|
|
22
|
+
* scroll in that box, and
|
|
23
|
+
* the check can't tell which box holds what. A page its script draws (body holds an empty root at
|
|
24
|
+
* most) can't be matched: it is warned, worded as a page that looks held, as a box its script
|
|
25
|
+
* draws may scroll it. One screen tall: height or max-height (block-size, max-block-size) 100vh
|
|
26
|
+
* (dvh, svh, lvh) or calc(100vh - 4rem) (less one positive length), or 100% or calc(100% - 4rem)
|
|
27
|
+
* of an element that is (html's 100% is the frame's), or position fixed or absolute with both top
|
|
28
|
+
* and bottom set (`inset: 0`).
|
|
29
|
+
*
|
|
30
|
+
* What it reads: the page's `<style>` elements, the stylesheets it links that the caller finds
|
|
31
|
+
* (a version's own files) and what they `@import`, in document order; style="" on the three; the
|
|
32
|
+
* class rules that match their classes; and, where the page loads Tailwind from its CDN, the few
|
|
33
|
+
* Tailwind utilities that hold a page (h-screen, overflow-hidden, fixed, inset-0…). Specificity,
|
|
34
|
+
* order, !important and cascade layers decide between rules, so a later override is seen.
|
|
35
|
+
* What it doesn't: rules under @media (but `all` and `screen`), @supports or @container, selectors
|
|
36
|
+
* beyond type, id, class, `*`, `:root`, `:only-child`, `:first-child` and the descendant and child
|
|
37
|
+
* combinators (`:is()`, `:where()`, `:not()`, attributes, siblings and a compound of more than
|
|
38
|
+
* `MOST_IN_COMPOUND` ids and classes match nothing), nested rules, values through var() or calc()
|
|
39
|
+
* (but the two above), a stylesheet linked or imported a second time (read where it first comes),
|
|
40
|
+
* the rules of a layer named more than `LAYER_PARTS` deep (its first parts are still declared),
|
|
41
|
+
* and what scripts do. Each of those misses a lock, or an override (then the warning is wrong); it
|
|
42
|
+
* never refuses anything.
|
|
43
|
+
*
|
|
44
|
+
* Bounded: each stylesheet is read once per page, as it streams (nothing parsed is kept past the
|
|
45
|
+
* rule it is in, a rule's selectors one at a time), at most `SHEETS` of them and `READ` bytes in
|
|
46
|
+
* all, its own `<style>` included; past that, the rest isn't read. At most `MOST_LAYERS` layers a
|
|
47
|
+
* page, each named in at most `LONGEST_LAYER` characters: a new one past either isn't read. Each
|
|
48
|
+
* element keeps, per property, only each layer's winner so far, and a compound is matched against
|
|
49
|
+
* an element's classes (a set) in bounded work, so time and memory are linear in what it reads. A
|
|
50
|
+
* version's pages (`lintFiles`: the entry and at most 7 more) share one `scrollBudget()`:
|
|
51
|
+
* `VERSION_READ` bytes of every sheet read for any of them, its own `<style>` included, and
|
|
52
|
+
* `VERSION_STEPS` match steps; a page that would spend more than is left isn't checked, nor are the
|
|
53
|
+
* pages after it. What it echoes to the agent is only values of a known shape, and selectors of
|
|
54
|
+
* plain characters and the root's plain id, each at most `ECHOED` characters with its !important (a
|
|
55
|
+
* selector or id that isn't: the element's own name, `#root`, `body > div`, or `body > *` for a tag
|
|
56
|
+
* HTML doesn't have; the fix is then !important where the name has less specificity than the
|
|
57
|
+
* selector).
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/** A stylesheet the page links, as the caller finds it from `from` (a path), or null. */
|
|
61
|
+
export type LinkedSheet = (
|
|
62
|
+
href: string,
|
|
63
|
+
from: string,
|
|
64
|
+
) => { readonly path: string; readonly text: string } | null;
|
|
65
|
+
|
|
66
|
+
export interface ScrollLock {
|
|
67
|
+
/** What to change, for the agent. */
|
|
68
|
+
readonly message: string;
|
|
69
|
+
/** The file the rule that holds it is in (the page's own `path`, or a stylesheet's). */
|
|
70
|
+
readonly path?: string;
|
|
71
|
+
/** Its line (1-based). */
|
|
72
|
+
readonly line: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** How much of one stylesheet is read: its first 2 MiB. */
|
|
76
|
+
const SCANNED = 2 * 1024 * 1024;
|
|
77
|
+
/** How deep `@import` and `@layer` are followed. */
|
|
78
|
+
const DEEPEST = 8;
|
|
79
|
+
/** The most layers a page's sheets name: past that, a new layer's rules aren't read. */
|
|
80
|
+
const MOST_LAYERS = 1_024;
|
|
81
|
+
/**
|
|
82
|
+
* The longest layer name, with the layers it is in (`a.b.c`): a longer one isn't declared, nor
|
|
83
|
+
* its rules or the layers in it read (each would copy its name).
|
|
84
|
+
*/
|
|
85
|
+
const LONGEST_LAYER = 256;
|
|
86
|
+
/** The most parts of a layer's name (`a.b.c`, with the layers it is in): deeper isn't read. */
|
|
87
|
+
const LAYER_PARTS = 16;
|
|
88
|
+
/** The most stylesheets read for one page, each once. */
|
|
89
|
+
export const SHEETS = 32;
|
|
90
|
+
/** The most bytes of stylesheets read for one page, in all. */
|
|
91
|
+
export const READ = 4 * 1024 * 1024;
|
|
92
|
+
/**
|
|
93
|
+
* A version's pages share one budget (`lintFiles`, which checks the entry and at most 7 more):
|
|
94
|
+
* every byte of stylesheet read for any of them, its own `<style>` included (a sheet two pages
|
|
95
|
+
* link is read for each), and every step matching scroll rules against markup. A page that would
|
|
96
|
+
* spend more than is left isn't checked, nor are the pages after it.
|
|
97
|
+
*/
|
|
98
|
+
export const VERSION_READ = 8 * 1024 * 1024;
|
|
99
|
+
export const VERSION_STEPS = 2_000_000;
|
|
100
|
+
|
|
101
|
+
/** What a version's pages have left to spend on this rule, and whether one has run out. */
|
|
102
|
+
export interface ScrollBudget {
|
|
103
|
+
bytes: number;
|
|
104
|
+
steps: number;
|
|
105
|
+
spent: boolean;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A version's budget, for one `lintFiles`. */
|
|
109
|
+
export const scrollBudget = (): ScrollBudget => ({
|
|
110
|
+
bytes: VERSION_READ,
|
|
111
|
+
steps: VERSION_STEPS,
|
|
112
|
+
spent: false,
|
|
113
|
+
});
|
|
114
|
+
/** The most elements below body, and how deep, that scroll rules are matched against. */
|
|
115
|
+
const MOST_ELEMENTS = 2_000;
|
|
116
|
+
const DEEPEST_ELEMENT = 32;
|
|
117
|
+
/** The most ids and classes in one compound selector: one with more matches nothing here. */
|
|
118
|
+
const MOST_IN_COMPOUND = 32;
|
|
119
|
+
/** The most scroll rules' selectors kept, and steps spent matching them. */
|
|
120
|
+
const MOST_SCROLL_SELECTORS = 256;
|
|
121
|
+
const MATCH_STEPS = 1_000_000;
|
|
122
|
+
/** The longest declaration or selector told back to the agent. */
|
|
123
|
+
const ECHOED = 60;
|
|
124
|
+
|
|
125
|
+
interface Source {
|
|
126
|
+
readonly path: string | undefined;
|
|
127
|
+
readonly text: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One declaration's value for one property of one element, and what put it there. */
|
|
131
|
+
interface Value {
|
|
132
|
+
readonly value: string;
|
|
133
|
+
/** As the page wrote it, for the agent: `overflow: hidden`, or a utility class's name. */
|
|
134
|
+
readonly written: string;
|
|
135
|
+
/** Where it was said: a rule (with its selector), style="", or a utility class. */
|
|
136
|
+
readonly kind: "rule" | "inline" | "utility";
|
|
137
|
+
readonly selector: string;
|
|
138
|
+
readonly important: boolean;
|
|
139
|
+
readonly layer: string | null;
|
|
140
|
+
readonly inline: boolean;
|
|
141
|
+
readonly specificity: readonly number[];
|
|
142
|
+
readonly order: number;
|
|
143
|
+
readonly source: Source;
|
|
144
|
+
readonly at: number;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** An element as its tag says: what a selector is matched against. */
|
|
148
|
+
interface Element {
|
|
149
|
+
readonly tag: string;
|
|
150
|
+
readonly id: string;
|
|
151
|
+
readonly classes: ReadonlySet<string>;
|
|
152
|
+
readonly onlyChild: boolean;
|
|
153
|
+
readonly firstChild: boolean;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** An element of the document's top (html, body, the root), with its cascade. */
|
|
157
|
+
interface Node extends Element {
|
|
158
|
+
readonly label: string;
|
|
159
|
+
readonly style: { readonly text: string; readonly at: number } | null;
|
|
160
|
+
readonly classAt: number;
|
|
161
|
+
/**
|
|
162
|
+
* Each longhand's winner so far within each layer (and !important, style=""), by specificity and
|
|
163
|
+
* order: the layers' order is known once all is read, and the cascade picks among these then.
|
|
164
|
+
*/
|
|
165
|
+
readonly candidates: Map<string, Map<string, Value>>;
|
|
166
|
+
readonly values: Map<string, Value>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface Tag {
|
|
170
|
+
readonly name: string;
|
|
171
|
+
readonly attributes: readonly { name: string; value: string; at: number }[];
|
|
172
|
+
/** Where the tag's attributes start in the page. */
|
|
173
|
+
readonly at: number;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const VOID = new Set([
|
|
177
|
+
"area",
|
|
178
|
+
"base",
|
|
179
|
+
"br",
|
|
180
|
+
"col",
|
|
181
|
+
"embed",
|
|
182
|
+
"hr",
|
|
183
|
+
"img",
|
|
184
|
+
"input",
|
|
185
|
+
"link",
|
|
186
|
+
"meta",
|
|
187
|
+
"param",
|
|
188
|
+
"source",
|
|
189
|
+
"track",
|
|
190
|
+
"wbr",
|
|
191
|
+
]);
|
|
192
|
+
/** Elements whose text isn't markup (or never shown): read past, to their end tag. */
|
|
193
|
+
const RAW = new Set([
|
|
194
|
+
"script",
|
|
195
|
+
"style",
|
|
196
|
+
"textarea",
|
|
197
|
+
"title",
|
|
198
|
+
"noscript",
|
|
199
|
+
"template",
|
|
200
|
+
"iframe",
|
|
201
|
+
"xmp",
|
|
202
|
+
"noembed",
|
|
203
|
+
"noframes",
|
|
204
|
+
]);
|
|
205
|
+
/** HTML's own elements: a tag's name the agent may be told (another is page text). */
|
|
206
|
+
const HTML_TAGS = new Set(
|
|
207
|
+
(
|
|
208
|
+
"a abbr address area article aside audio b bdi bdo blockquote br button canvas center cite " +
|
|
209
|
+
"code data datalist del details dfn dialog div dl em embed fieldset figure font footer form " +
|
|
210
|
+
"h1 h2 h3 h4 h5 h6 header hgroup hr i iframe img input ins kbd label main map mark math menu " +
|
|
211
|
+
"meter nav object ol output p picture pre progress q s samp search section select slot small " +
|
|
212
|
+
"span strong sub sup svg table textarea time u ul var video"
|
|
213
|
+
).split(" "),
|
|
214
|
+
);
|
|
215
|
+
/** A body's children that hold no content. */
|
|
216
|
+
const NOT_CONTENT = new Set(["script", "style", "link", "meta", "noscript", "template", "base"]);
|
|
217
|
+
|
|
218
|
+
const TAG_AT = /<(\/?)([a-z][a-z0-9-]{0,30})\b([^<>]{0,8192})>/iuy;
|
|
219
|
+
const closers = new Map<string, RegExp>();
|
|
220
|
+
const closer = (name: string): RegExp => {
|
|
221
|
+
let found = closers.get(name);
|
|
222
|
+
if (found === undefined) {
|
|
223
|
+
found = new RegExp(`</${name}\\s*>`, "giu");
|
|
224
|
+
closers.set(name, found);
|
|
225
|
+
}
|
|
226
|
+
return found;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const attribute = (tag: Tag | null, name: string) =>
|
|
230
|
+
tag?.attributes.find((each) => each.name === name);
|
|
231
|
+
|
|
232
|
+
/** Where a stylesheet comes from, in document order. */
|
|
233
|
+
type SheetRef =
|
|
234
|
+
| { readonly kind: "style"; readonly start: number; readonly end: number; readonly tag: Tag }
|
|
235
|
+
| { readonly kind: "link"; readonly tag: Tag };
|
|
236
|
+
|
|
237
|
+
/** What a page's markup says of its top: html's and body's tags, body's children, its sheets. */
|
|
238
|
+
function scanPage(page: string): {
|
|
239
|
+
html: Tag | null;
|
|
240
|
+
body: Tag | null;
|
|
241
|
+
children: Tag[];
|
|
242
|
+
childCount: number;
|
|
243
|
+
content: Tag[];
|
|
244
|
+
contentCount: number;
|
|
245
|
+
sheets: SheetRef[];
|
|
246
|
+
tailwind: boolean;
|
|
247
|
+
styleScrolls: boolean;
|
|
248
|
+
classScrolls: boolean;
|
|
249
|
+
/** Each element of content below body, as its ancestors below body and itself (bounded). */
|
|
250
|
+
below: Element[][];
|
|
251
|
+
/** How many elements of content are below body, at any depth. */
|
|
252
|
+
contentBelow: number;
|
|
253
|
+
scripted: boolean;
|
|
254
|
+
texted: boolean;
|
|
255
|
+
} {
|
|
256
|
+
let html: Tag | null = null;
|
|
257
|
+
let body: Tag | null = null;
|
|
258
|
+
const children: Tag[] = [];
|
|
259
|
+
let childCount = 0;
|
|
260
|
+
const content: Tag[] = [];
|
|
261
|
+
let contentCount = 0;
|
|
262
|
+
const sheets: SheetRef[] = [];
|
|
263
|
+
let tailwind = false;
|
|
264
|
+
/** Whether body's children, and the elements below them, scroll themselves by style or class. */
|
|
265
|
+
const top = { style: false, classes: false };
|
|
266
|
+
const inner = { style: false, classes: false };
|
|
267
|
+
const below: Element[][] = [];
|
|
268
|
+
let contentBelow = 0;
|
|
269
|
+
let scripted = false;
|
|
270
|
+
/** Body has text of its own (outside scripts and styles). */
|
|
271
|
+
let texted = false;
|
|
272
|
+
/** The open elements below body, each with how many children it has had. */
|
|
273
|
+
const open: { element: Element; children: number }[] = [];
|
|
274
|
+
const bodyFrame = { children: 0 };
|
|
275
|
+
let opened: Element | null = null;
|
|
276
|
+
let inBody = false;
|
|
277
|
+
let depth = 0;
|
|
278
|
+
let at = page.indexOf("<");
|
|
279
|
+
while (at >= 0 && at < page.length) {
|
|
280
|
+
if (page.startsWith("<!--", at)) {
|
|
281
|
+
const end = page.indexOf("-->", at + 4);
|
|
282
|
+
at = end < 0 ? page.length : page.indexOf("<", end + 3);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
TAG_AT.lastIndex = at;
|
|
286
|
+
const match = TAG_AT.exec(page);
|
|
287
|
+
if (match === null) {
|
|
288
|
+
at = page.indexOf("<", at + 1);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const closing = match[1] === "/";
|
|
292
|
+
const name = (match[2] ?? "").toLowerCase();
|
|
293
|
+
const attributesAt = at + 1 + (match[1] ?? "").length + name.length;
|
|
294
|
+
let next = at + match[0].length;
|
|
295
|
+
if (closing) {
|
|
296
|
+
if (inBody) {
|
|
297
|
+
if (depth === 0 && (name === "body" || name === "html")) {
|
|
298
|
+
inBody = false;
|
|
299
|
+
} else if (depth > 0) {
|
|
300
|
+
depth -= 1;
|
|
301
|
+
open.pop();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
at = page.indexOf("<", next);
|
|
305
|
+
texted ||= inBody && hasText(page, next, at);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const tag: Tag = { name, attributes: attributesOf(match[3] ?? ""), at: attributesAt };
|
|
309
|
+
if (name === "html" && html === null) {
|
|
310
|
+
html = tag;
|
|
311
|
+
} else if (name === "body" && body === null) {
|
|
312
|
+
body = tag;
|
|
313
|
+
inBody = true;
|
|
314
|
+
depth = 0;
|
|
315
|
+
} else if (inBody) {
|
|
316
|
+
// An element below body that scrolls itself, as its markup says. Body's own children apart:
|
|
317
|
+
// where one is the root, its values are in the cascade, and they decide.
|
|
318
|
+
const own = depth === 0 ? top : inner;
|
|
319
|
+
own.style ||= declaresScroll(attribute(tag, "style")?.value ?? "");
|
|
320
|
+
own.classes ||= SCROLL_UTILITY.test(attribute(tag, "class")?.value ?? "");
|
|
321
|
+
const parent = open.at(-1) ?? bodyFrame;
|
|
322
|
+
const element: Element = {
|
|
323
|
+
tag: name,
|
|
324
|
+
id: attribute(tag, "id")?.value ?? "",
|
|
325
|
+
classes: classesOf(attribute(tag, "class")?.value ?? ""),
|
|
326
|
+
onlyChild: false,
|
|
327
|
+
firstChild: parent.children === 0,
|
|
328
|
+
};
|
|
329
|
+
parent.children += 1;
|
|
330
|
+
opened = element;
|
|
331
|
+
if (!NOT_CONTENT.has(name)) {
|
|
332
|
+
contentBelow += 1;
|
|
333
|
+
if (below.length < MOST_ELEMENTS && open.length < DEEPEST_ELEMENT) {
|
|
334
|
+
below.push([...open.map((each) => each.element), element]);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (name === "script") {
|
|
339
|
+
scripted = true;
|
|
340
|
+
}
|
|
341
|
+
if (inBody && depth === 0 && name !== "body") {
|
|
342
|
+
childCount += 1;
|
|
343
|
+
if (children.length < 2) {
|
|
344
|
+
children.push(tag);
|
|
345
|
+
}
|
|
346
|
+
if (!NOT_CONTENT.has(name)) {
|
|
347
|
+
contentCount += 1;
|
|
348
|
+
if (content.length < 2) {
|
|
349
|
+
content.push(tag);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (name === "link") {
|
|
354
|
+
const href = attribute(tag, "href")?.value ?? "";
|
|
355
|
+
if (/tailwindcss/iu.test(href)) {
|
|
356
|
+
tailwind = true;
|
|
357
|
+
}
|
|
358
|
+
sheets.push({ kind: "link", tag });
|
|
359
|
+
} else if (name === "script" && /tailwindcss/iu.test(attribute(tag, "src")?.value ?? "")) {
|
|
360
|
+
tailwind = true;
|
|
361
|
+
}
|
|
362
|
+
if (RAW.has(name)) {
|
|
363
|
+
const end = closer(name);
|
|
364
|
+
end.lastIndex = next;
|
|
365
|
+
const found = end.exec(page);
|
|
366
|
+
if (name === "style") {
|
|
367
|
+
sheets.push({
|
|
368
|
+
kind: "style",
|
|
369
|
+
start: next,
|
|
370
|
+
end: found === null ? page.length : found.index,
|
|
371
|
+
tag,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
next = found === null ? page.length : found.index + found[0].length;
|
|
375
|
+
} else if (inBody && name !== "body" && !VOID.has(name)) {
|
|
376
|
+
depth += 1;
|
|
377
|
+
if (opened !== null) {
|
|
378
|
+
open.push({ element: opened, children: 0 });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
opened = null;
|
|
382
|
+
at = page.indexOf("<", next);
|
|
383
|
+
texted ||= inBody && hasText(page, next, at);
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
html,
|
|
387
|
+
body,
|
|
388
|
+
children,
|
|
389
|
+
childCount,
|
|
390
|
+
content,
|
|
391
|
+
contentCount,
|
|
392
|
+
sheets,
|
|
393
|
+
tailwind,
|
|
394
|
+
// Body's children count where there is no root (it has two of content, or none).
|
|
395
|
+
styleScrolls: inner.style || (contentCount !== 1 && top.style),
|
|
396
|
+
classScrolls: inner.classes || (contentCount !== 1 && top.classes),
|
|
397
|
+
below,
|
|
398
|
+
contentBelow,
|
|
399
|
+
scripted,
|
|
400
|
+
texted,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** A class attribute's names. */
|
|
405
|
+
const classesOf = (value: string): ReadonlySet<string> =>
|
|
406
|
+
new Set(value.split(/\s+/u).filter((each) => each !== ""));
|
|
407
|
+
|
|
408
|
+
/** Whether `page` has text other than spaces from `from` to `to` (the end where `to` is -1). */
|
|
409
|
+
const hasText = (page: string, from: number, to: number): boolean =>
|
|
410
|
+
/\S/u.test(page.slice(from, to < 0 ? page.length : to));
|
|
411
|
+
|
|
412
|
+
/** Tailwind's utilities that make a box scroll itself. */
|
|
413
|
+
const SCROLL_UTILITY = /(?:^|\s)overflow-(?:y-)?(?:auto|scroll)(?:\s|$)/u;
|
|
414
|
+
|
|
415
|
+
/** Whether declarations (a block's, or style="") give overflow-y auto or scroll. */
|
|
416
|
+
function declaresScroll(text: string): boolean {
|
|
417
|
+
for (const match of text.matchAll(
|
|
418
|
+
/(?:^|[\s;{])overflow(-y)?\s{0,64}:\s{0,64}([^;{}!]{0,256})/giu,
|
|
419
|
+
)) {
|
|
420
|
+
const parts = (match[2] ?? "").trim().toLowerCase().split(/\s+/u);
|
|
421
|
+
const y = match[1] === undefined ? (parts[1] ?? parts[0]) : parts[0];
|
|
422
|
+
if (y !== undefined && SCROLLS.has(y)) {
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A text with its comments blanked (same length, same lines), strings kept. */
|
|
430
|
+
function withoutComments(text: string): string {
|
|
431
|
+
let out = "";
|
|
432
|
+
let from = 0;
|
|
433
|
+
let at = 0;
|
|
434
|
+
while (at < text.length) {
|
|
435
|
+
const code = text[at];
|
|
436
|
+
if (code === '"' || code === "'") {
|
|
437
|
+
at = stringEnd(text, at, text.length);
|
|
438
|
+
} else if (code === "/" && text[at + 1] === "*") {
|
|
439
|
+
const close = text.indexOf("*/", at + 2);
|
|
440
|
+
const end = close < 0 ? text.length : close + 2;
|
|
441
|
+
out += text.slice(from, at) + text.slice(at, end).replace(/[^\n]/gu, " ");
|
|
442
|
+
from = end;
|
|
443
|
+
at = end;
|
|
444
|
+
} else {
|
|
445
|
+
at += 1;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return out + text.slice(from);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** Past the string that starts at `at` (its quote): its end quote, a newline, or `to`. */
|
|
452
|
+
function stringEnd(text: string, at: number, to: number): number {
|
|
453
|
+
const quote = text[at];
|
|
454
|
+
let index = at + 1;
|
|
455
|
+
while (index < to) {
|
|
456
|
+
const code = text[index];
|
|
457
|
+
if (code === "\\") {
|
|
458
|
+
index += 2;
|
|
459
|
+
} else if (code === quote || code === "\n") {
|
|
460
|
+
return index + 1;
|
|
461
|
+
} else {
|
|
462
|
+
index += 1;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return to;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Past the block whose `{` is at `at`: after its `}`, or `to`. */
|
|
469
|
+
function blockEnd(text: string, at: number, to: number): number {
|
|
470
|
+
let depth = 0;
|
|
471
|
+
let index = at;
|
|
472
|
+
while (index < to) {
|
|
473
|
+
const code = text[index];
|
|
474
|
+
if (code === '"' || code === "'") {
|
|
475
|
+
index = stringEnd(text, index, to);
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (code === "{") {
|
|
479
|
+
depth += 1;
|
|
480
|
+
} else if (code === "}") {
|
|
481
|
+
depth -= 1;
|
|
482
|
+
if (depth === 0) {
|
|
483
|
+
return index + 1;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
index += 1;
|
|
487
|
+
}
|
|
488
|
+
return to;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** The next rule or statement from `at`: its prelude, and its block's bounds if it has one. */
|
|
492
|
+
function nextItem(
|
|
493
|
+
text: string,
|
|
494
|
+
at: number,
|
|
495
|
+
to: number,
|
|
496
|
+
): { prelude: string; block: readonly [number, number] | null; end: number } | null {
|
|
497
|
+
let start = at;
|
|
498
|
+
while (start < to && /\s/u.test(text[start] ?? "")) {
|
|
499
|
+
start += 1;
|
|
500
|
+
}
|
|
501
|
+
if (start >= to) {
|
|
502
|
+
return null;
|
|
503
|
+
}
|
|
504
|
+
let parens = 0;
|
|
505
|
+
let index = start;
|
|
506
|
+
while (index < to) {
|
|
507
|
+
const code = text[index];
|
|
508
|
+
if (code === '"' || code === "'") {
|
|
509
|
+
index = stringEnd(text, index, to);
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
if (code === "(") {
|
|
513
|
+
parens += 1;
|
|
514
|
+
} else if (code === ")") {
|
|
515
|
+
parens = Math.max(0, parens - 1);
|
|
516
|
+
} else if (parens === 0 && code === "{") {
|
|
517
|
+
const end = blockEnd(text, index, to);
|
|
518
|
+
return {
|
|
519
|
+
prelude: text.slice(start, index).trim(),
|
|
520
|
+
block: [index + 1, Math.max(index + 1, end - 1)],
|
|
521
|
+
end,
|
|
522
|
+
};
|
|
523
|
+
} else if (parens === 0 && (code === ";" || code === "}")) {
|
|
524
|
+
return { prelude: text.slice(start, index).trim(), block: null, end: index + 1 };
|
|
525
|
+
}
|
|
526
|
+
index += 1;
|
|
527
|
+
}
|
|
528
|
+
return { prelude: text.slice(start, to).trim(), block: null, end: to };
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Each item of a list at its top-level commas (not inside parentheses or strings), in turn. */
|
|
532
|
+
function* listOf(text: string): Generator<string> {
|
|
533
|
+
let parens = 0;
|
|
534
|
+
let from = 0;
|
|
535
|
+
for (let index = 0; index <= text.length; index += 1) {
|
|
536
|
+
const code = text[index];
|
|
537
|
+
if (code === '"' || code === "'") {
|
|
538
|
+
index = stringEnd(text, index, text.length) - 1;
|
|
539
|
+
} else if (code === "(") {
|
|
540
|
+
parens += 1;
|
|
541
|
+
} else if (code === ")") {
|
|
542
|
+
parens = Math.max(0, parens - 1);
|
|
543
|
+
} else if ((code === "," && parens === 0) || index === text.length) {
|
|
544
|
+
const item = text.slice(from, index).trim();
|
|
545
|
+
if (item !== "") {
|
|
546
|
+
yield item;
|
|
547
|
+
}
|
|
548
|
+
from = index + 1;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
interface Compound {
|
|
554
|
+
readonly tag: string | null;
|
|
555
|
+
readonly ids: readonly string[];
|
|
556
|
+
readonly classes: readonly string[];
|
|
557
|
+
readonly root: boolean;
|
|
558
|
+
readonly onlyChild: boolean;
|
|
559
|
+
readonly firstChild: boolean;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
interface Selector {
|
|
563
|
+
readonly compounds: readonly Compound[];
|
|
564
|
+
/** Between compound k and k + 1. */
|
|
565
|
+
readonly combinators: readonly (" " | ">")[];
|
|
566
|
+
readonly specificity: readonly [number, number, number];
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/** An identifier at `at`, its escapes read; null where there is none. */
|
|
570
|
+
function identAt(text: string, at: number): { value: string; end: number } | null {
|
|
571
|
+
let value = "";
|
|
572
|
+
let index = at;
|
|
573
|
+
while (index < text.length) {
|
|
574
|
+
const code = text[index] ?? "";
|
|
575
|
+
if (code === "\\") {
|
|
576
|
+
const hex = /^[0-9a-f]{1,6}\s?/iu.exec(text.slice(index + 1, index + 8));
|
|
577
|
+
if (hex !== null) {
|
|
578
|
+
value += String.fromCodePoint(Number.parseInt(hex[0].trim(), 16) || 0xfffd);
|
|
579
|
+
index += 1 + hex[0].length;
|
|
580
|
+
} else if (index + 1 < text.length) {
|
|
581
|
+
value += text[index + 1];
|
|
582
|
+
index += 2;
|
|
583
|
+
} else {
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
} else if (/[\w-]/u.test(code) || code.charCodeAt(0) > 0x7f) {
|
|
587
|
+
value += code;
|
|
588
|
+
index += 1;
|
|
589
|
+
} else {
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return value === "" ? null : { value, end: index };
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** A selector this check can match, or null (it then matches nothing here). */
|
|
597
|
+
function parseSelector(text: string): Selector | null {
|
|
598
|
+
const compounds: Compound[] = [];
|
|
599
|
+
const combinators: (" " | ">")[] = [];
|
|
600
|
+
let ids = 0;
|
|
601
|
+
let classLike = 0;
|
|
602
|
+
let tags = 0;
|
|
603
|
+
let index = 0;
|
|
604
|
+
let pending: " " | ">" | null = null;
|
|
605
|
+
while (index < text.length) {
|
|
606
|
+
const code = text[index] ?? "";
|
|
607
|
+
if (/\s/u.test(code)) {
|
|
608
|
+
pending ??= " ";
|
|
609
|
+
index += 1;
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if (code === ">") {
|
|
613
|
+
pending = ">";
|
|
614
|
+
index += 1;
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
if (compounds.length > 0) {
|
|
618
|
+
if (pending === null) {
|
|
619
|
+
return null;
|
|
620
|
+
}
|
|
621
|
+
combinators.push(pending);
|
|
622
|
+
} else if (pending === ">") {
|
|
623
|
+
return null;
|
|
624
|
+
}
|
|
625
|
+
pending = null;
|
|
626
|
+
let tag: string | null = null;
|
|
627
|
+
const compoundIds: string[] = [];
|
|
628
|
+
const compoundClasses: string[] = [];
|
|
629
|
+
let root = false;
|
|
630
|
+
let onlyChild = false;
|
|
631
|
+
let firstChild = false;
|
|
632
|
+
if (text[index] === "*") {
|
|
633
|
+
index += 1;
|
|
634
|
+
} else {
|
|
635
|
+
const name = identAt(text, index);
|
|
636
|
+
if (name !== null) {
|
|
637
|
+
tag = name.value.toLowerCase();
|
|
638
|
+
tags += 1;
|
|
639
|
+
index = name.end;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
let read = tag !== null || text[index - 1] === "*";
|
|
643
|
+
while (index < text.length) {
|
|
644
|
+
const mark = text[index];
|
|
645
|
+
if (mark === "#" || mark === ".") {
|
|
646
|
+
const name = identAt(text, index + 1);
|
|
647
|
+
if (name === null) {
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
650
|
+
if (compoundIds.length + compoundClasses.length >= MOST_IN_COMPOUND) {
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
if (mark === "#") {
|
|
654
|
+
compoundIds.push(name.value);
|
|
655
|
+
ids += 1;
|
|
656
|
+
} else {
|
|
657
|
+
compoundClasses.push(name.value);
|
|
658
|
+
classLike += 1;
|
|
659
|
+
}
|
|
660
|
+
index = name.end;
|
|
661
|
+
} else if (mark === ":") {
|
|
662
|
+
if (text[index + 1] === ":") {
|
|
663
|
+
return null;
|
|
664
|
+
}
|
|
665
|
+
const name = identAt(text, index + 1);
|
|
666
|
+
const pseudo = name?.value.toLowerCase();
|
|
667
|
+
if (name === null || text[name.end] === "(") {
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
if (pseudo === "root") {
|
|
671
|
+
root = true;
|
|
672
|
+
} else if (pseudo === "only-child") {
|
|
673
|
+
onlyChild = true;
|
|
674
|
+
} else if (pseudo === "first-child") {
|
|
675
|
+
firstChild = true;
|
|
676
|
+
} else {
|
|
677
|
+
return null;
|
|
678
|
+
}
|
|
679
|
+
classLike += 1;
|
|
680
|
+
index = name.end;
|
|
681
|
+
} else {
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
read = true;
|
|
685
|
+
}
|
|
686
|
+
if (!read) {
|
|
687
|
+
return null;
|
|
688
|
+
}
|
|
689
|
+
compounds.push({
|
|
690
|
+
tag,
|
|
691
|
+
ids: compoundIds,
|
|
692
|
+
classes: compoundClasses,
|
|
693
|
+
root,
|
|
694
|
+
onlyChild,
|
|
695
|
+
firstChild,
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
if (compounds.length === 0 || pending === ">") {
|
|
699
|
+
return null;
|
|
700
|
+
}
|
|
701
|
+
return { compounds, combinators, specificity: [ids, classLike, tags] };
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const compoundMatches = (compound: Compound, node: Element, index: number): boolean =>
|
|
705
|
+
(compound.tag === null || compound.tag === node.tag) &&
|
|
706
|
+
compound.ids.every((id) => id === node.id) &&
|
|
707
|
+
compound.classes.every((name) => node.classes.has(name)) &&
|
|
708
|
+
(!compound.root || index === 0) &&
|
|
709
|
+
(!compound.onlyChild || node.onlyChild) &&
|
|
710
|
+
(!compound.firstChild || node.firstChild);
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Whether `selector` matches `chain[index]` (each element's parent is the one before it). Each
|
|
714
|
+
* (compound, element) pair is tried once; `budget.steps` bounds the work across calls (none left:
|
|
715
|
+
* no match).
|
|
716
|
+
*/
|
|
717
|
+
function matches(
|
|
718
|
+
selector: Selector,
|
|
719
|
+
chain: readonly Element[],
|
|
720
|
+
index: number,
|
|
721
|
+
budget: { steps: number } = { steps: Infinity },
|
|
722
|
+
): boolean {
|
|
723
|
+
// One compound (`div`, `.list`): the element alone, no walk (a step all the same).
|
|
724
|
+
if (selector.compounds.length === 1) {
|
|
725
|
+
budget.steps -= 1;
|
|
726
|
+
const only = selector.compounds[0];
|
|
727
|
+
const node = chain[index];
|
|
728
|
+
return (
|
|
729
|
+
budget.steps > 0 &&
|
|
730
|
+
only !== undefined &&
|
|
731
|
+
node !== undefined &&
|
|
732
|
+
compoundMatches(only, node, index)
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
const tried = new Map<number, boolean>();
|
|
736
|
+
const at = (k: number, i: number): boolean => {
|
|
737
|
+
const key = k * (chain.length + 1) + i;
|
|
738
|
+
const known = tried.get(key);
|
|
739
|
+
if (known !== undefined) {
|
|
740
|
+
return known;
|
|
741
|
+
}
|
|
742
|
+
budget.steps -= 1;
|
|
743
|
+
const result = budget.steps > 0 && pair(k, i);
|
|
744
|
+
tried.set(key, result);
|
|
745
|
+
return result;
|
|
746
|
+
};
|
|
747
|
+
const pair = (k: number, i: number): boolean => {
|
|
748
|
+
const compound = selector.compounds[k];
|
|
749
|
+
const node = chain[i];
|
|
750
|
+
if (compound === undefined || node === undefined || !compoundMatches(compound, node, i)) {
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
if (k === 0) {
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
if (selector.combinators[k - 1] === ">") {
|
|
757
|
+
return at(k - 1, i - 1);
|
|
758
|
+
}
|
|
759
|
+
for (let j = i - 1; j >= 0; j -= 1) {
|
|
760
|
+
if (at(k - 1, j)) {
|
|
761
|
+
return true;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
return false;
|
|
765
|
+
};
|
|
766
|
+
return at(selector.compounds.length - 1, index);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** The longhands this check reads, from the properties that set them. */
|
|
770
|
+
function longhands(property: string, value: string): [string, string][] {
|
|
771
|
+
const parts = value.split(/\s+/u).filter((each) => each !== "");
|
|
772
|
+
switch (property) {
|
|
773
|
+
case "overflow":
|
|
774
|
+
return [
|
|
775
|
+
["overflow-x", parts[0] ?? ""],
|
|
776
|
+
["overflow-y", parts[1] ?? parts[0] ?? ""],
|
|
777
|
+
];
|
|
778
|
+
case "inset":
|
|
779
|
+
return [
|
|
780
|
+
["top", parts[0] ?? ""],
|
|
781
|
+
["bottom", parts[2] ?? parts[0] ?? ""],
|
|
782
|
+
];
|
|
783
|
+
case "inset-block":
|
|
784
|
+
return [
|
|
785
|
+
["top", parts[0] ?? ""],
|
|
786
|
+
["bottom", parts[1] ?? parts[0] ?? ""],
|
|
787
|
+
];
|
|
788
|
+
case "block-size":
|
|
789
|
+
return [["height", value]];
|
|
790
|
+
case "max-block-size":
|
|
791
|
+
return [["max-height", value]];
|
|
792
|
+
case "overflow-x":
|
|
793
|
+
case "overflow-y":
|
|
794
|
+
case "height":
|
|
795
|
+
case "max-height":
|
|
796
|
+
case "display":
|
|
797
|
+
case "position":
|
|
798
|
+
case "top":
|
|
799
|
+
case "bottom":
|
|
800
|
+
return [[property, value]];
|
|
801
|
+
default:
|
|
802
|
+
return [];
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/** Tailwind's utilities that hold a page, as the CSS they are. */
|
|
807
|
+
const UTILITIES: Readonly<Record<string, readonly [string, string]>> = {
|
|
808
|
+
"h-screen": ["height", "100vh"],
|
|
809
|
+
"h-dvh": ["height", "100dvh"],
|
|
810
|
+
"h-svh": ["height", "100svh"],
|
|
811
|
+
"h-lvh": ["height", "100lvh"],
|
|
812
|
+
"h-full": ["height", "100%"],
|
|
813
|
+
"h-auto": ["height", "auto"],
|
|
814
|
+
"max-h-screen": ["max-height", "100vh"],
|
|
815
|
+
"max-h-dvh": ["max-height", "100dvh"],
|
|
816
|
+
"max-h-svh": ["max-height", "100svh"],
|
|
817
|
+
"max-h-full": ["max-height", "100%"],
|
|
818
|
+
"overflow-hidden": ["overflow", "hidden"],
|
|
819
|
+
"overflow-clip": ["overflow", "clip"],
|
|
820
|
+
"overflow-auto": ["overflow", "auto"],
|
|
821
|
+
"overflow-scroll": ["overflow", "scroll"],
|
|
822
|
+
"overflow-visible": ["overflow", "visible"],
|
|
823
|
+
"overflow-y-hidden": ["overflow-y", "hidden"],
|
|
824
|
+
"overflow-y-clip": ["overflow-y", "clip"],
|
|
825
|
+
"overflow-y-auto": ["overflow-y", "auto"],
|
|
826
|
+
"overflow-y-scroll": ["overflow-y", "scroll"],
|
|
827
|
+
"overflow-y-visible": ["overflow-y", "visible"],
|
|
828
|
+
fixed: ["position", "fixed"],
|
|
829
|
+
absolute: ["position", "absolute"],
|
|
830
|
+
relative: ["position", "relative"],
|
|
831
|
+
static: ["position", "static"],
|
|
832
|
+
sticky: ["position", "sticky"],
|
|
833
|
+
"inset-0": ["inset", "0"],
|
|
834
|
+
"inset-y-0": ["inset-block", "0"],
|
|
835
|
+
"top-0": ["top", "0"],
|
|
836
|
+
"bottom-0": ["bottom", "0"],
|
|
837
|
+
flex: ["display", "flex"],
|
|
838
|
+
"inline-flex": ["display", "inline-flex"],
|
|
839
|
+
grid: ["display", "grid"],
|
|
840
|
+
"inline-grid": ["display", "inline-grid"],
|
|
841
|
+
block: ["display", "block"],
|
|
842
|
+
};
|
|
843
|
+
/** Tailwind's arbitrary heights: `h-[calc(100vh-4rem)]` (`calc(100vh - 4rem)`), `max-h-[80vh]`. */
|
|
844
|
+
const ARBITRARY_HEIGHT = /^(max-)?h-\[([^\]]{1,64})\]$/u;
|
|
845
|
+
|
|
846
|
+
const compare = (a: readonly number[], b: readonly number[]): number => {
|
|
847
|
+
for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
|
|
848
|
+
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
849
|
+
if (difference !== 0) {
|
|
850
|
+
return difference;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
return 0;
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
/** An element a rule's selector matches, and how specifically (its most specific one). */
|
|
857
|
+
interface Target {
|
|
858
|
+
readonly node: Node;
|
|
859
|
+
readonly specificity: readonly number[];
|
|
860
|
+
readonly selector: string;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* A layer's name cut to its first `LAYER_PARTS` parts where it has more (its rules then aren't
|
|
865
|
+
* read, but its parents are declared where it comes, as a browser does); null where it doesn't.
|
|
866
|
+
*/
|
|
867
|
+
const tooDeep = (name: string): string | null => {
|
|
868
|
+
let parts = 1;
|
|
869
|
+
for (let dot = name.indexOf(".", 1); dot > 0; dot = name.indexOf(".", dot + 1)) {
|
|
870
|
+
parts += 1;
|
|
871
|
+
if (parts > LAYER_PARTS) {
|
|
872
|
+
return name.slice(0, dot);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return null;
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
/** A stylesheet found for the page, once: its path and text. */
|
|
879
|
+
type Found = { readonly path: string; readonly text: string } | null;
|
|
880
|
+
|
|
881
|
+
/** The cascade over the chain: each declaration that matches, then the one that wins. */
|
|
882
|
+
class Cascade {
|
|
883
|
+
readonly chain: readonly Node[];
|
|
884
|
+
/** Layer names, in the order they were first declared or opened (`a`, `a.b`). */
|
|
885
|
+
readonly layers: string[] = [];
|
|
886
|
+
readonly known = new Set<string>();
|
|
887
|
+
/** The selectors of rules that give something below body overflow-y auto or scroll. */
|
|
888
|
+
readonly scrollSelectors: Selector[] = [];
|
|
889
|
+
order = 0;
|
|
890
|
+
anonymous = 0;
|
|
891
|
+
/** The stylesheets read for this page, by path: each once, within the budget. */
|
|
892
|
+
readonly read = new Set<string>();
|
|
893
|
+
bytes = 0;
|
|
894
|
+
readonly find: (href: string, from: string) => Found;
|
|
895
|
+
readonly budget: ScrollBudget;
|
|
896
|
+
|
|
897
|
+
constructor(
|
|
898
|
+
chain: readonly Node[],
|
|
899
|
+
find: (href: string, from: string) => Found,
|
|
900
|
+
budget: ScrollBudget,
|
|
901
|
+
) {
|
|
902
|
+
this.chain = chain;
|
|
903
|
+
this.find = find;
|
|
904
|
+
this.budget = budget;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/** Spends `bytes` of the version's budget; false (and spent: no page more) past what's left. */
|
|
908
|
+
spend(bytes: number): boolean {
|
|
909
|
+
if (this.budget.spent || bytes > this.budget.bytes) {
|
|
910
|
+
this.budget.spent = true;
|
|
911
|
+
return false;
|
|
912
|
+
}
|
|
913
|
+
this.budget.bytes -= bytes;
|
|
914
|
+
return true;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/** A layer, declared (its parents first): its place is where it first comes. */
|
|
918
|
+
declare(name: string): boolean {
|
|
919
|
+
if (this.known.has(name)) {
|
|
920
|
+
return true;
|
|
921
|
+
}
|
|
922
|
+
const add = (layer: string): boolean => {
|
|
923
|
+
if (this.known.has(layer)) {
|
|
924
|
+
return true;
|
|
925
|
+
}
|
|
926
|
+
// Past MOST_LAYERS a page's layers aren't kept: the new one's rules aren't read.
|
|
927
|
+
if (this.known.size >= MOST_LAYERS) {
|
|
928
|
+
return false;
|
|
929
|
+
}
|
|
930
|
+
this.known.add(layer);
|
|
931
|
+
this.layers.push(layer);
|
|
932
|
+
return true;
|
|
933
|
+
};
|
|
934
|
+
for (let dot = name.indexOf(".", 1); dot > 0; dot = name.indexOf(".", dot + 1)) {
|
|
935
|
+
if (!add(name.slice(0, dot))) {
|
|
936
|
+
return false;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return add(name);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Each layer's rank, low to high: a layer's sub-layers before its own rules, layers in the order
|
|
944
|
+
* they were first declared, unlayered rules (the document's own) above them all.
|
|
945
|
+
*/
|
|
946
|
+
ranks(): Map<string | null, number> {
|
|
947
|
+
const children = new Map<string | null, string[]>();
|
|
948
|
+
for (const name of this.layers) {
|
|
949
|
+
const dot = name.lastIndexOf(".");
|
|
950
|
+
const parent = dot > 0 ? name.slice(0, dot) : null;
|
|
951
|
+
const siblings = children.get(parent);
|
|
952
|
+
if (siblings === undefined) {
|
|
953
|
+
children.set(parent, [name]);
|
|
954
|
+
} else {
|
|
955
|
+
siblings.push(name);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
const out = new Map<string | null, number>();
|
|
959
|
+
const visit = (name: string | null) => {
|
|
960
|
+
for (const child of children.get(name) ?? []) {
|
|
961
|
+
visit(child);
|
|
962
|
+
}
|
|
963
|
+
out.set(name, out.size);
|
|
964
|
+
};
|
|
965
|
+
visit(null);
|
|
966
|
+
return out;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Whether the page's own `<style>` of `size` may be read: within `READ` for the page (else it
|
|
971
|
+
* isn't, as a linked sheet past it isn't), and within the version's budget (else nothing more
|
|
972
|
+
* is, and `spent` says so).
|
|
973
|
+
*/
|
|
974
|
+
inline(size: number): boolean {
|
|
975
|
+
if (this.bytes + size > READ || !this.spend(size)) {
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
this.bytes += size;
|
|
979
|
+
return true;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/** Whether a stylesheet may still be read for this page (once, within the budget). */
|
|
983
|
+
take(found: Found): found is { path: string; text: string } {
|
|
984
|
+
if (found === null || this.read.has(found.path) || this.read.size >= SHEETS) {
|
|
985
|
+
return false;
|
|
986
|
+
}
|
|
987
|
+
const size = Math.min(found.text.length, SCANNED);
|
|
988
|
+
if (this.bytes + size > READ || !this.spend(size)) {
|
|
989
|
+
return false;
|
|
990
|
+
}
|
|
991
|
+
this.read.add(found.path);
|
|
992
|
+
this.bytes += size;
|
|
993
|
+
return true;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* A declaration for `node`, kept where it wins so far within its layer (and !important,
|
|
998
|
+
* style=""): there, specificity and then order decide, as the layer's rank is the same for all.
|
|
999
|
+
*/
|
|
1000
|
+
set(
|
|
1001
|
+
node: Node,
|
|
1002
|
+
property: string,
|
|
1003
|
+
value: string,
|
|
1004
|
+
rank: {
|
|
1005
|
+
important: boolean;
|
|
1006
|
+
layer: string | null;
|
|
1007
|
+
inline: boolean;
|
|
1008
|
+
specificity: readonly number[];
|
|
1009
|
+
},
|
|
1010
|
+
where: {
|
|
1011
|
+
source: Source;
|
|
1012
|
+
at: number;
|
|
1013
|
+
selector: string;
|
|
1014
|
+
written?: string;
|
|
1015
|
+
kind?: Value["kind"];
|
|
1016
|
+
},
|
|
1017
|
+
) {
|
|
1018
|
+
if (rank.layer !== null) {
|
|
1019
|
+
this.declare(rank.layer);
|
|
1020
|
+
}
|
|
1021
|
+
this.order += 1;
|
|
1022
|
+
for (const [longhand, longValue] of longhands(property, value)) {
|
|
1023
|
+
const each: Value = {
|
|
1024
|
+
value: longValue,
|
|
1025
|
+
written: where.written ?? echoed(property, value, rank.important),
|
|
1026
|
+
kind: where.kind ?? (rank.inline ? "inline" : "rule"),
|
|
1027
|
+
selector: where.selector,
|
|
1028
|
+
important: rank.important,
|
|
1029
|
+
layer: rank.layer,
|
|
1030
|
+
inline: rank.inline,
|
|
1031
|
+
specificity: rank.specificity,
|
|
1032
|
+
order: this.order,
|
|
1033
|
+
source: where.source,
|
|
1034
|
+
at: where.at,
|
|
1035
|
+
};
|
|
1036
|
+
let kept = node.candidates.get(longhand);
|
|
1037
|
+
if (kept === undefined) {
|
|
1038
|
+
kept = new Map();
|
|
1039
|
+
node.candidates.set(longhand, kept);
|
|
1040
|
+
}
|
|
1041
|
+
const layer = each.layer === null ? "" : `@${each.layer}`;
|
|
1042
|
+
const key = `${each.important ? "!" : ""}${each.inline ? "=" : ""}${layer}`;
|
|
1043
|
+
const best = kept.get(key);
|
|
1044
|
+
// Later, as specific or more: it wins within its layer.
|
|
1045
|
+
if (best === undefined || compare(each.specificity, best.specificity) >= 0) {
|
|
1046
|
+
kept.set(key, each);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/** Picks each longhand's winner, once every rule is read and the layers' order is known. */
|
|
1052
|
+
resolve() {
|
|
1053
|
+
const ranks = this.ranks();
|
|
1054
|
+
const top = ranks.get(null) ?? 0;
|
|
1055
|
+
const priority = (each: Value): number[] => {
|
|
1056
|
+
const rank = ranks.get(each.layer) ?? top;
|
|
1057
|
+
// Normal: later layers win, unlayered most. !important: reversed. style="" above both.
|
|
1058
|
+
const score = each.inline ? (each.important ? 1 : top + 1) : each.important ? -rank : rank;
|
|
1059
|
+
return [each.important ? 1 : 0, score, ...each.specificity, each.order];
|
|
1060
|
+
};
|
|
1061
|
+
for (const node of this.chain) {
|
|
1062
|
+
for (const [property, candidates] of node.candidates) {
|
|
1063
|
+
let best: Value | undefined;
|
|
1064
|
+
let bestPriority: number[] = [];
|
|
1065
|
+
for (const each of candidates.values()) {
|
|
1066
|
+
const mine = priority(each);
|
|
1067
|
+
if (best === undefined || compare(mine, bestPriority) > 0) {
|
|
1068
|
+
best = each;
|
|
1069
|
+
bestPriority = mine;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
if (best !== undefined) {
|
|
1073
|
+
node.values.set(property, best);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/** The declarations of a block (`from`..`to` of `text`), for the nodes given. */
|
|
1080
|
+
declarations(
|
|
1081
|
+
text: string,
|
|
1082
|
+
from: number,
|
|
1083
|
+
to: number,
|
|
1084
|
+
targets: readonly Target[],
|
|
1085
|
+
rank: { layer: string | null; inline: boolean },
|
|
1086
|
+
source: Source,
|
|
1087
|
+
base: number,
|
|
1088
|
+
) {
|
|
1089
|
+
let start = from;
|
|
1090
|
+
let index = from;
|
|
1091
|
+
let parens = 0;
|
|
1092
|
+
const take = (end: number) => {
|
|
1093
|
+
const segment = text.slice(start, end);
|
|
1094
|
+
const colon = segment.indexOf(":");
|
|
1095
|
+
if (colon > 0) {
|
|
1096
|
+
const property = segment.slice(0, colon).trim().toLowerCase();
|
|
1097
|
+
let value = segment
|
|
1098
|
+
.slice(colon + 1)
|
|
1099
|
+
.trim()
|
|
1100
|
+
.toLowerCase()
|
|
1101
|
+
.replace(/\s+/gu, " ");
|
|
1102
|
+
const important = /!\s*important$/u.test(value);
|
|
1103
|
+
if (important) {
|
|
1104
|
+
value = value.replace(/\s*!\s*important$/u, "");
|
|
1105
|
+
}
|
|
1106
|
+
if (!property.startsWith("--") && longhands(property, value).length > 0) {
|
|
1107
|
+
const offset = start + (segment.length - segment.trimStart().length);
|
|
1108
|
+
for (const target of targets) {
|
|
1109
|
+
this.set(
|
|
1110
|
+
target.node,
|
|
1111
|
+
property,
|
|
1112
|
+
value,
|
|
1113
|
+
{
|
|
1114
|
+
important,
|
|
1115
|
+
layer: rank.layer,
|
|
1116
|
+
inline: rank.inline,
|
|
1117
|
+
specificity: target.specificity,
|
|
1118
|
+
},
|
|
1119
|
+
{ source, at: base + offset, selector: target.selector },
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1125
|
+
while (index < to) {
|
|
1126
|
+
const code = text[index];
|
|
1127
|
+
if (code === '"' || code === "'") {
|
|
1128
|
+
index = stringEnd(text, index, to);
|
|
1129
|
+
continue;
|
|
1130
|
+
}
|
|
1131
|
+
if (code === "(") {
|
|
1132
|
+
parens += 1;
|
|
1133
|
+
} else if (code === ")") {
|
|
1134
|
+
parens = Math.max(0, parens - 1);
|
|
1135
|
+
} else if (parens === 0 && code === ";") {
|
|
1136
|
+
take(index);
|
|
1137
|
+
start = index + 1;
|
|
1138
|
+
} else if (parens === 0 && code === "{") {
|
|
1139
|
+
// A nested rule: not read (its prelude is dropped with it).
|
|
1140
|
+
index = blockEnd(text, index, to);
|
|
1141
|
+
start = index;
|
|
1142
|
+
continue;
|
|
1143
|
+
}
|
|
1144
|
+
index += 1;
|
|
1145
|
+
}
|
|
1146
|
+
take(to);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/** A stylesheet the page links or a sheet imports, if it may still be read. */
|
|
1150
|
+
file(found: Found, layer: string | null) {
|
|
1151
|
+
if (!this.take(found)) {
|
|
1152
|
+
return;
|
|
1153
|
+
}
|
|
1154
|
+
const text = found.text.slice(0, SCANNED);
|
|
1155
|
+
this.sheet(withoutComments(text), 0, text.length, {
|
|
1156
|
+
source: { path: found.path, text },
|
|
1157
|
+
base: 0,
|
|
1158
|
+
layer,
|
|
1159
|
+
// A file starts again at the top: it is read once, within the budget, however it's reached.
|
|
1160
|
+
depth: 0,
|
|
1161
|
+
from: found.path,
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/** A stylesheet's rules (`text` is comment-free; offsets are `base` + its own). */
|
|
1166
|
+
sheet(
|
|
1167
|
+
text: string,
|
|
1168
|
+
from: number,
|
|
1169
|
+
to: number,
|
|
1170
|
+
context: {
|
|
1171
|
+
source: Source;
|
|
1172
|
+
base: number;
|
|
1173
|
+
layer: string | null;
|
|
1174
|
+
depth: number;
|
|
1175
|
+
/** Where its imports are found from: its own path, or the page's. */
|
|
1176
|
+
from: string;
|
|
1177
|
+
},
|
|
1178
|
+
) {
|
|
1179
|
+
if (context.depth > DEEPEST) {
|
|
1180
|
+
return;
|
|
1181
|
+
}
|
|
1182
|
+
let ruled = false;
|
|
1183
|
+
let at = from;
|
|
1184
|
+
for (let item = nextItem(text, at, to); item !== null; item = nextItem(text, at, to)) {
|
|
1185
|
+
at = item.end;
|
|
1186
|
+
const { prelude, block } = item;
|
|
1187
|
+
const within = (name: string) => (context.layer === null ? name : `${context.layer}.${name}`);
|
|
1188
|
+
if (prelude.startsWith("@")) {
|
|
1189
|
+
const name = /^@([\w-]+)/u.exec(prelude)?.[1]?.toLowerCase() ?? "";
|
|
1190
|
+
const rest = prelude.slice(name.length + 1).trim();
|
|
1191
|
+
if (name === "import" && block === null && !ruled) {
|
|
1192
|
+
this.import(rest, context, within);
|
|
1193
|
+
} else if (name === "layer") {
|
|
1194
|
+
if (block === null) {
|
|
1195
|
+
for (const each of listOf(rest)) {
|
|
1196
|
+
const layer = within(each);
|
|
1197
|
+
if (layer.length <= LONGEST_LAYER) {
|
|
1198
|
+
this.declare(tooDeep(layer) ?? layer);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
} else {
|
|
1202
|
+
this.anonymous += 1;
|
|
1203
|
+
const first = listOf(rest).next();
|
|
1204
|
+
const layer = within(first.done === true ? `\u0000${this.anonymous}` : first.value);
|
|
1205
|
+
if (layer.length > LONGEST_LAYER) {
|
|
1206
|
+
continue;
|
|
1207
|
+
}
|
|
1208
|
+
const cut = tooDeep(layer);
|
|
1209
|
+
if (cut !== null) {
|
|
1210
|
+
this.declare(cut);
|
|
1211
|
+
continue;
|
|
1212
|
+
}
|
|
1213
|
+
// Its place is where its block opens, whatever it holds; past MOST_LAYERS, not read.
|
|
1214
|
+
if (!this.declare(layer)) {
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
this.sheet(text, block[0], block[1], { ...context, layer, depth: context.depth + 1 });
|
|
1218
|
+
}
|
|
1219
|
+
} else if (
|
|
1220
|
+
name === "media" &&
|
|
1221
|
+
block !== null &&
|
|
1222
|
+
/^(?:only\s+)?(?:all|screen)$/iu.test(rest)
|
|
1223
|
+
) {
|
|
1224
|
+
this.sheet(text, block[0], block[1], { ...context, depth: context.depth + 1 });
|
|
1225
|
+
}
|
|
1226
|
+
continue;
|
|
1227
|
+
}
|
|
1228
|
+
ruled = true;
|
|
1229
|
+
if (block === null) {
|
|
1230
|
+
continue;
|
|
1231
|
+
}
|
|
1232
|
+
const targets: Target[] = [];
|
|
1233
|
+
// A scroll rule's selectors are kept as they come, while there is room for them.
|
|
1234
|
+
const scrolls =
|
|
1235
|
+
this.scrollSelectors.length < MOST_SCROLL_SELECTORS &&
|
|
1236
|
+
declaresScroll(text.slice(block[0], block[1]));
|
|
1237
|
+
for (const each of listOf(prelude)) {
|
|
1238
|
+
const selector = parseSelector(each);
|
|
1239
|
+
let top = false;
|
|
1240
|
+
if (selector !== null) {
|
|
1241
|
+
this.chain.forEach((node, index) => {
|
|
1242
|
+
if (!matches(selector, this.chain, index)) {
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
top ||= index < 2;
|
|
1246
|
+
const found = targets.find((target) => target.node === node);
|
|
1247
|
+
if (found === undefined) {
|
|
1248
|
+
targets.push({ node, specificity: selector.specificity, selector: each });
|
|
1249
|
+
} else if (compare(selector.specificity, found.specificity) > 0) {
|
|
1250
|
+
targets.splice(targets.indexOf(found), 1, {
|
|
1251
|
+
node,
|
|
1252
|
+
specificity: selector.specificity,
|
|
1253
|
+
selector: each,
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
1258
|
+
// A selector for anything but html or body may name an element below body; one the
|
|
1259
|
+
// check can't read names nothing it can see.
|
|
1260
|
+
if (
|
|
1261
|
+
scrolls &&
|
|
1262
|
+
selector !== null &&
|
|
1263
|
+
!top &&
|
|
1264
|
+
this.scrollSelectors.length < MOST_SCROLL_SELECTORS
|
|
1265
|
+
) {
|
|
1266
|
+
this.scrollSelectors.push(selector);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
if (targets.length > 0) {
|
|
1270
|
+
this.declarations(
|
|
1271
|
+
text,
|
|
1272
|
+
block[0],
|
|
1273
|
+
block[1],
|
|
1274
|
+
targets,
|
|
1275
|
+
{ layer: context.layer, inline: false },
|
|
1276
|
+
context.source,
|
|
1277
|
+
context.base,
|
|
1278
|
+
);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
import(rest: string, context: Parameters<Cascade["sheet"]>[3], within: (name: string) => string) {
|
|
1284
|
+
const parsed = /^(?:url\(\s*(["']?)([^"'()\s]+)\1\s*\)|(["'])([^"']+)\3)\s*([\s\S]*)$/iu.exec(
|
|
1285
|
+
rest,
|
|
1286
|
+
);
|
|
1287
|
+
if (parsed === null) {
|
|
1288
|
+
return;
|
|
1289
|
+
}
|
|
1290
|
+
const href = parsed[2] ?? parsed[4] ?? "";
|
|
1291
|
+
let tail = parsed[5]?.trim() ?? "";
|
|
1292
|
+
let layer = context.layer;
|
|
1293
|
+
const named = /^layer\(\s*([\w.-]+)\s*\)\s*/iu.exec(tail);
|
|
1294
|
+
if (named !== null) {
|
|
1295
|
+
layer = within(named[1] ?? "");
|
|
1296
|
+
tail = tail.slice(named[0].length);
|
|
1297
|
+
} else if (/^layer\b\s*/iu.test(tail)) {
|
|
1298
|
+
this.anonymous += 1;
|
|
1299
|
+
layer = within(`\u0000${this.anonymous}`);
|
|
1300
|
+
tail = tail.replace(/^layer\b\s*/iu, "");
|
|
1301
|
+
}
|
|
1302
|
+
if (tail !== "" && !/^(?:only\s+)?(?:all|screen)$/iu.test(tail)) {
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
if (layer !== null) {
|
|
1306
|
+
if (layer.length > LONGEST_LAYER) {
|
|
1307
|
+
return;
|
|
1308
|
+
}
|
|
1309
|
+
const cut = tooDeep(layer);
|
|
1310
|
+
if (!this.declare(cut ?? layer) || cut !== null) {
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
this.file(this.find(href, context.from), layer);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
/** A value of a shape the check knows, as the agent may be told it; else its property alone. */
|
|
1319
|
+
const KNOWN =
|
|
1320
|
+
/^(?:(?:-?(?:\d{1,6}(?:\.\d{1,4})?|\.\d{1,4})(?:px|%|d?vh|svh|lvh|vw|em|rem)?|auto|hidden|clip|visible|scroll|overlay|fixed|absolute|static|relative|sticky|none)(?: |$)){1,4}$/u;
|
|
1321
|
+
const echoed = (property: string, value: string, important: boolean): string => {
|
|
1322
|
+
const said = `${property}: ${KNOWN.test(value) ? value : "…"}`;
|
|
1323
|
+
const flag = important ? " !important" : "";
|
|
1324
|
+
const room = ECHOED - flag.length;
|
|
1325
|
+
return `${said.length > room ? `${said.slice(0, room - 1)}…` : said}${flag}`;
|
|
1326
|
+
};
|
|
1327
|
+
/** An element's own name's specificity: `#root`, `body > div`, `body > *`, `html`, `body`. */
|
|
1328
|
+
const specificityOf = (label: string): readonly number[] =>
|
|
1329
|
+
label.startsWith("#")
|
|
1330
|
+
? [1, 0, 0]
|
|
1331
|
+
: label.startsWith("body > ") && !label.endsWith("*")
|
|
1332
|
+
? [0, 0, 2]
|
|
1333
|
+
: [0, 0, 1];
|
|
1334
|
+
/** A selector the agent may be told: plain characters, short. */
|
|
1335
|
+
const PLAIN_SELECTOR = new RegExp(`^[\\w#.:>* ()-]{1,${ECHOED}}$`, "u");
|
|
1336
|
+
|
|
1337
|
+
/** One positive length taken from a whole: `calc(100vh - 4rem)`, `calc(100% - 56px)`. */
|
|
1338
|
+
const LESS = String.raw` - (?:\d{1,6}(?:\.\d{1,4})?|\.\d{1,4})(?:px|rem|em|%|[dsl]?vh)\s?\)`;
|
|
1339
|
+
const SCREEN = new RegExp(String.raw`^(?:100[dsl]?vh|calc\(\s?100[dsl]?vh${LESS})$`, "u");
|
|
1340
|
+
/** 100% of the parent, less something: one screen where the parent is. */
|
|
1341
|
+
const PARENT = new RegExp(String.raw`^(?:100%|calc\(\s?100%${LESS})$`, "u");
|
|
1342
|
+
/** Heights that are the content's (the box grows with what it holds). */
|
|
1343
|
+
const CONTENT_HEIGHT = /^(?:auto|none|fit-content|min-content|max-content)$/u;
|
|
1344
|
+
/** A display that lays its children out by flex or grid: their heights are its to give. */
|
|
1345
|
+
const FLEX_OR_GRID = /^(?:(?:block|inline) )?(?:inline-)?(?:flex|grid)$/u;
|
|
1346
|
+
const CLIPS = new Set(["hidden", "clip"]);
|
|
1347
|
+
const SCROLLS = new Set(["auto", "scroll", "overlay"]);
|
|
1348
|
+
const RESET = new Set(["initial", "unset", "revert", "revert-layer"]);
|
|
1349
|
+
|
|
1350
|
+
const lineOf = (text: string, index: number): number => {
|
|
1351
|
+
let line = 1;
|
|
1352
|
+
for (let at = text.indexOf("\n"); at >= 0 && at < index; at = text.indexOf("\n", at + 1)) {
|
|
1353
|
+
line += 1;
|
|
1354
|
+
}
|
|
1355
|
+
return line;
|
|
1356
|
+
};
|
|
1357
|
+
|
|
1358
|
+
/** What holds one element: the values that do, its line, and the fix's parts. */
|
|
1359
|
+
interface Hold {
|
|
1360
|
+
readonly node: Node;
|
|
1361
|
+
readonly shown: readonly Value[];
|
|
1362
|
+
readonly at: Value;
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
/**
|
|
1366
|
+
* Whether `page`'s own styles hold it to one screen, and what to tell the agent; null where they
|
|
1367
|
+
* don't (or where it can't tell). `sheet` finds what the page links (a version's own files).
|
|
1368
|
+
*/
|
|
1369
|
+
export function scrollLock(
|
|
1370
|
+
page: string,
|
|
1371
|
+
options: {
|
|
1372
|
+
readonly noun: string;
|
|
1373
|
+
readonly path?: string;
|
|
1374
|
+
readonly sheet?: LinkedSheet;
|
|
1375
|
+
/** The version's, shared by its pages (`scrollBudget()`); else this page's own. */
|
|
1376
|
+
readonly budget?: ScrollBudget;
|
|
1377
|
+
},
|
|
1378
|
+
): ScrollLock | null {
|
|
1379
|
+
const shared = options.budget ?? { bytes: Infinity, steps: Infinity, spent: false };
|
|
1380
|
+
if (shared.spent) {
|
|
1381
|
+
return null;
|
|
1382
|
+
}
|
|
1383
|
+
const scanned = scanPage(page);
|
|
1384
|
+
const pageSource: Source = { path: options.path, text: page };
|
|
1385
|
+
const nodeOf = (tag: Tag | null, name: string, label: string, child: boolean): Node => {
|
|
1386
|
+
const style = attribute(tag, "style");
|
|
1387
|
+
const classes = attribute(tag, "class");
|
|
1388
|
+
return {
|
|
1389
|
+
label,
|
|
1390
|
+
tag: name,
|
|
1391
|
+
id: attribute(tag, "id")?.value ?? "",
|
|
1392
|
+
classes: classesOf(classes?.value ?? ""),
|
|
1393
|
+
onlyChild: child && scanned.childCount === 1,
|
|
1394
|
+
firstChild: child && scanned.children[0] === tag,
|
|
1395
|
+
style:
|
|
1396
|
+
style === undefined || tag === null ? null : { text: style.value, at: tag.at + style.at },
|
|
1397
|
+
classAt: tag === null || classes === undefined ? 0 : tag.at + classes.at,
|
|
1398
|
+
candidates: new Map(),
|
|
1399
|
+
values: new Map(),
|
|
1400
|
+
};
|
|
1401
|
+
};
|
|
1402
|
+
const chain: Node[] = [
|
|
1403
|
+
nodeOf(scanned.html, "html", "html", false),
|
|
1404
|
+
nodeOf(scanned.body, "body", "body", false),
|
|
1405
|
+
];
|
|
1406
|
+
const rootTag =
|
|
1407
|
+
scanned.body !== null && scanned.contentCount === 1 ? scanned.content[0] : undefined;
|
|
1408
|
+
if (rootTag !== undefined) {
|
|
1409
|
+
const id = attribute(rootTag, "id")?.value ?? "";
|
|
1410
|
+
// Page text told to the agent: `#id` for a plain id, at most ECHOED characters; else its tag
|
|
1411
|
+
// where HTML has it (a custom element's name is the page's own).
|
|
1412
|
+
const named = /^[a-z_][\w-]*$/iu.test(id) && id.length < ECHOED;
|
|
1413
|
+
const tag = HTML_TAGS.has(rootTag.name) ? rootTag.name : "*";
|
|
1414
|
+
chain.push(nodeOf(rootTag, rootTag.name, named ? `#${id}` : `body > ${tag}`, true));
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
// Each stylesheet is asked for once per place it is named from, and read once (`take`).
|
|
1418
|
+
const linked = options.sheet;
|
|
1419
|
+
const asked = new Map<string, Found>();
|
|
1420
|
+
const find = (href: string, from: string): Found => {
|
|
1421
|
+
if (linked === undefined) {
|
|
1422
|
+
return null;
|
|
1423
|
+
}
|
|
1424
|
+
const key = `${from}\n${href}`;
|
|
1425
|
+
if (!asked.has(key)) {
|
|
1426
|
+
asked.set(key, linked(href, from));
|
|
1427
|
+
}
|
|
1428
|
+
return asked.get(key) ?? null;
|
|
1429
|
+
};
|
|
1430
|
+
const cascade = new Cascade(chain, find, shared);
|
|
1431
|
+
const from = options.path ?? "";
|
|
1432
|
+
for (const sheet of scanned.sheets) {
|
|
1433
|
+
const media = attribute(sheet.tag, "media")?.value.trim() ?? "";
|
|
1434
|
+
if (media !== "" && !/^(?:only\s+)?(?:all|screen)$/iu.test(media)) {
|
|
1435
|
+
continue;
|
|
1436
|
+
}
|
|
1437
|
+
if (sheet.kind === "style") {
|
|
1438
|
+
if (!cascade.inline(sheet.end - sheet.start)) {
|
|
1439
|
+
if (shared.spent) {
|
|
1440
|
+
return null;
|
|
1441
|
+
}
|
|
1442
|
+
continue;
|
|
1443
|
+
}
|
|
1444
|
+
const text = withoutComments(page.slice(sheet.start, sheet.end));
|
|
1445
|
+
cascade.sheet(text, 0, text.length, {
|
|
1446
|
+
source: pageSource,
|
|
1447
|
+
base: sheet.start,
|
|
1448
|
+
layer: null,
|
|
1449
|
+
depth: 0,
|
|
1450
|
+
from,
|
|
1451
|
+
});
|
|
1452
|
+
continue;
|
|
1453
|
+
}
|
|
1454
|
+
const rel = (attribute(sheet.tag, "rel")?.value ?? "").toLowerCase().split(/\s+/u);
|
|
1455
|
+
const href = attribute(sheet.tag, "href")?.value ?? "";
|
|
1456
|
+
if (
|
|
1457
|
+
!rel.includes("stylesheet") ||
|
|
1458
|
+
rel.includes("alternate") ||
|
|
1459
|
+
attribute(sheet.tag, "disabled") !== undefined ||
|
|
1460
|
+
href === ""
|
|
1461
|
+
) {
|
|
1462
|
+
continue;
|
|
1463
|
+
}
|
|
1464
|
+
cascade.file(find(href, from), null);
|
|
1465
|
+
}
|
|
1466
|
+
for (const node of chain) {
|
|
1467
|
+
if (node.style !== null) {
|
|
1468
|
+
cascade.declarations(
|
|
1469
|
+
node.style.text,
|
|
1470
|
+
0,
|
|
1471
|
+
node.style.text.length,
|
|
1472
|
+
[{ node, specificity: [0, 0, 0], selector: "" }],
|
|
1473
|
+
{ layer: null, inline: true },
|
|
1474
|
+
pageSource,
|
|
1475
|
+
node.style.at,
|
|
1476
|
+
);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
if (scanned.tailwind) {
|
|
1480
|
+
for (const node of chain) {
|
|
1481
|
+
for (const name of node.classes) {
|
|
1482
|
+
const arbitrary = ARBITRARY_HEIGHT.exec(name);
|
|
1483
|
+
// As Tailwind writes it: `_` is a space, and calc() has spaces around - and + (its CSS
|
|
1484
|
+
// needs them): `h-[calc(100vh-4rem)]` is `calc(100vh - 4rem)`.
|
|
1485
|
+
const raw = (arbitrary?.[2] ?? "").replaceAll("_", " ").toLowerCase();
|
|
1486
|
+
const height = raw.startsWith("calc(")
|
|
1487
|
+
? raw.replace(/\s*([+-])\s*/gu, " $1 ").replace(/\(\s+/gu, "(")
|
|
1488
|
+
: raw;
|
|
1489
|
+
const utility = Object.hasOwn(UTILITIES, name)
|
|
1490
|
+
? UTILITIES[name]
|
|
1491
|
+
: arbitrary === null
|
|
1492
|
+
? undefined
|
|
1493
|
+
: ([arbitrary[1] === undefined ? "height" : "max-height", height] as const);
|
|
1494
|
+
// An arbitrary class is page text: told as it is only where its value is of a known shape.
|
|
1495
|
+
const written =
|
|
1496
|
+
arbitrary === null || SCREEN.test(height) || PARENT.test(height)
|
|
1497
|
+
? name
|
|
1498
|
+
: `${arbitrary[1] ?? ""}h-[…]`;
|
|
1499
|
+
if (utility !== undefined) {
|
|
1500
|
+
cascade.set(
|
|
1501
|
+
node,
|
|
1502
|
+
utility[0],
|
|
1503
|
+
utility[1],
|
|
1504
|
+
{ important: false, layer: null, inline: false, specificity: [0, 1, 0] },
|
|
1505
|
+
{ source: pageSource, at: node.classAt, selector: "", written, kind: "utility" },
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
if (shared.spent) {
|
|
1513
|
+
// Past the version's budget: this page can't be told, nor the ones after it.
|
|
1514
|
+
return null;
|
|
1515
|
+
}
|
|
1516
|
+
cascade.resolve();
|
|
1517
|
+
// Something in the page's own markup below body scrolls itself: the page may scroll in it (the
|
|
1518
|
+
// manual's line on the wheel says the rest). Not a hold the check can be sure of. A scroll rule
|
|
1519
|
+
// counts only where it matches an element the markup has: an app's stylesheet has many for
|
|
1520
|
+
// boxes a page never draws. Not the root's own rules: it is in the cascade, and its computed
|
|
1521
|
+
// values say whether it scrolls the page (one screen tall) or grows with it (`scrollBox`).
|
|
1522
|
+
const outer = chain.slice(0, 2);
|
|
1523
|
+
const rooted = chain.length > 2;
|
|
1524
|
+
// Each page's steps, within what the version has left.
|
|
1525
|
+
const allowed = Math.min(MATCH_STEPS, shared.steps);
|
|
1526
|
+
const budget = { steps: allowed };
|
|
1527
|
+
const scrollsInside =
|
|
1528
|
+
scanned.styleScrolls ||
|
|
1529
|
+
(scanned.tailwind && scanned.classScrolls) ||
|
|
1530
|
+
cascade.scrollSelectors.some((selector) =>
|
|
1531
|
+
scanned.below.some((path) => {
|
|
1532
|
+
if (rooted && path.length === 1) {
|
|
1533
|
+
return false;
|
|
1534
|
+
}
|
|
1535
|
+
const elements = [...outer, ...path];
|
|
1536
|
+
return matches(selector, elements, elements.length - 1, budget);
|
|
1537
|
+
}),
|
|
1538
|
+
);
|
|
1539
|
+
shared.steps -= allowed - Math.max(0, budget.steps);
|
|
1540
|
+
if (allowed < MATCH_STEPS && budget.steps < allowed && budget.steps <= 0) {
|
|
1541
|
+
// The version's steps ran out on this page: it can't be told, nor the ones after it.
|
|
1542
|
+
shared.spent = true;
|
|
1543
|
+
return null;
|
|
1544
|
+
}
|
|
1545
|
+
if (scrollsInside) {
|
|
1546
|
+
return null;
|
|
1547
|
+
}
|
|
1548
|
+
// Drawn by its script (body holds an empty root at most): a box it draws may scroll the content.
|
|
1549
|
+
const drawn = scanned.scripted && scanned.contentBelow <= 1 && !scanned.texted;
|
|
1550
|
+
|
|
1551
|
+
// Each element's computed values, from the outside in.
|
|
1552
|
+
const value = (node: Node, property: string): Value | undefined => {
|
|
1553
|
+
const found = node.values.get(property);
|
|
1554
|
+
return found === undefined || RESET.has(found.value) ? undefined : found;
|
|
1555
|
+
};
|
|
1556
|
+
const overflowY = (node: Node): string => {
|
|
1557
|
+
const y = value(node, "overflow-y")?.value ?? "visible";
|
|
1558
|
+
const x = value(node, "overflow-x")?.value ?? "visible";
|
|
1559
|
+
// One axis that isn't visible or clip makes the other's visible auto (and its clip hidden).
|
|
1560
|
+
if (y === "visible" && (CLIPS.has(x) || SCROLLS.has(x)) && x !== "clip") {
|
|
1561
|
+
return "auto";
|
|
1562
|
+
}
|
|
1563
|
+
return y;
|
|
1564
|
+
};
|
|
1565
|
+
const htmlVisible = (() => {
|
|
1566
|
+
const html = chain[0];
|
|
1567
|
+
return (
|
|
1568
|
+
html !== undefined &&
|
|
1569
|
+
(value(html, "overflow-x")?.value ?? "visible") === "visible" &&
|
|
1570
|
+
(value(html, "overflow-y")?.value ?? "visible") === "visible"
|
|
1571
|
+
);
|
|
1572
|
+
})();
|
|
1573
|
+
interface State {
|
|
1574
|
+
readonly screen: boolean;
|
|
1575
|
+
readonly shown: readonly Value[];
|
|
1576
|
+
readonly position: string;
|
|
1577
|
+
}
|
|
1578
|
+
const states: State[] = [];
|
|
1579
|
+
for (const [index, node] of chain.entries()) {
|
|
1580
|
+
const parent = index === 0 ? true : (states[index - 1]?.screen ?? false);
|
|
1581
|
+
const position = value(node, "position");
|
|
1582
|
+
const positioned = position?.value === "fixed" || position?.value === "absolute";
|
|
1583
|
+
const shown: Value[] = [];
|
|
1584
|
+
const tall = (property: string): boolean => {
|
|
1585
|
+
const found = value(node, property);
|
|
1586
|
+
const is =
|
|
1587
|
+
found !== undefined &&
|
|
1588
|
+
(SCREEN.test(found.value) || (PARENT.test(found.value) && (parent || positioned)));
|
|
1589
|
+
if (is && found !== undefined) {
|
|
1590
|
+
shown.push(found);
|
|
1591
|
+
}
|
|
1592
|
+
return is;
|
|
1593
|
+
};
|
|
1594
|
+
const top = value(node, "top");
|
|
1595
|
+
const bottom = value(node, "bottom");
|
|
1596
|
+
const spanned =
|
|
1597
|
+
positioned &&
|
|
1598
|
+
top !== undefined &&
|
|
1599
|
+
bottom !== undefined &&
|
|
1600
|
+
top.value !== "auto" &&
|
|
1601
|
+
bottom.value !== "auto";
|
|
1602
|
+
if (spanned) {
|
|
1603
|
+
shown.unshift(top, bottom);
|
|
1604
|
+
}
|
|
1605
|
+
const height = tall("height");
|
|
1606
|
+
const maxHeight = tall("max-height");
|
|
1607
|
+
if (positioned && position !== undefined && (spanned || height || maxHeight)) {
|
|
1608
|
+
shown.unshift(position);
|
|
1609
|
+
}
|
|
1610
|
+
states.push({ screen: height || maxHeight || spanned, shown, position: position?.value ?? "" });
|
|
1611
|
+
}
|
|
1612
|
+
// The root scrolling in a height the check can't read, in a body one screen tall: its own box,
|
|
1613
|
+
// as an inner one is (no warning). Under a flex or grid body (its height is the layout's), or a
|
|
1614
|
+
// height of a shape it doesn't know (a length, calc(), min(), var(), an arbitrary class). Not a
|
|
1615
|
+
// height that is the content's, or 100% of a body that is, nor under a body that grows with the
|
|
1616
|
+
// page: then it grows too.
|
|
1617
|
+
const unread = (index: number): boolean => {
|
|
1618
|
+
const node = chain[index];
|
|
1619
|
+
const parent = chain[index - 1];
|
|
1620
|
+
if (index !== 2 || node === undefined || parent === undefined || !states[index - 1]?.screen) {
|
|
1621
|
+
return false;
|
|
1622
|
+
}
|
|
1623
|
+
if (FLEX_OR_GRID.test(value(parent, "display")?.value ?? "")) {
|
|
1624
|
+
return true;
|
|
1625
|
+
}
|
|
1626
|
+
return ["height", "max-height"].some((property) => {
|
|
1627
|
+
const found = value(node, property)?.value;
|
|
1628
|
+
return found !== undefined && !CONTENT_HEIGHT.test(found) && !PARENT.test(found);
|
|
1629
|
+
});
|
|
1630
|
+
};
|
|
1631
|
+
const scrollBox = (index: number): boolean => {
|
|
1632
|
+
const node = chain[index];
|
|
1633
|
+
return (
|
|
1634
|
+
node !== undefined &&
|
|
1635
|
+
SCROLLS.has(overflowY(node)) &&
|
|
1636
|
+
((states[index]?.screen ?? false) || unread(index))
|
|
1637
|
+
);
|
|
1638
|
+
};
|
|
1639
|
+
const holds: Hold[] = [];
|
|
1640
|
+
for (const [index, node] of chain.entries()) {
|
|
1641
|
+
const state = states[index];
|
|
1642
|
+
if (state === undefined) {
|
|
1643
|
+
continue;
|
|
1644
|
+
}
|
|
1645
|
+
if (chain.slice(index + 1).some((_, deeper) => scrollBox(index + 1 + deeper))) {
|
|
1646
|
+
continue;
|
|
1647
|
+
}
|
|
1648
|
+
const clips = CLIPS.has(overflowY(node));
|
|
1649
|
+
const overflow = value(node, "overflow-y");
|
|
1650
|
+
const clipped =
|
|
1651
|
+
clips && (index === 0 ? true : index === 1 ? state.screen || htmlVisible : state.screen);
|
|
1652
|
+
const fixed = state.position === "fixed" && state.screen && !SCROLLS.has(overflowY(node));
|
|
1653
|
+
if (!clipped && !fixed) {
|
|
1654
|
+
continue;
|
|
1655
|
+
}
|
|
1656
|
+
const shown = [...state.shown, ...(clips && overflow !== undefined ? [overflow] : [])];
|
|
1657
|
+
const at = clipped && overflow !== undefined ? overflow : (shown[0] ?? overflow);
|
|
1658
|
+
if (at !== undefined) {
|
|
1659
|
+
holds.push({ node, shown: [...new Set(shown)], at });
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
const first = holds[0];
|
|
1663
|
+
if (first === undefined) {
|
|
1664
|
+
return null;
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
// What holds it, as the agent wrote it: "html and body (height: 100%; overflow: hidden)".
|
|
1668
|
+
const written = (hold: Hold, kind: Value["kind"]): string[] => [
|
|
1669
|
+
...new Set(hold.shown.filter((each) => each.kind === kind).map((each) => each.written)),
|
|
1670
|
+
];
|
|
1671
|
+
const described = (hold: Hold): string => {
|
|
1672
|
+
const inline = written(hold, "inline");
|
|
1673
|
+
const utilities = written(hold, "utility");
|
|
1674
|
+
return [
|
|
1675
|
+
written(hold, "rule").join("; "),
|
|
1676
|
+
inline.length === 0 ? "" : `style="${inline.join("; ")}"`,
|
|
1677
|
+
utilities.length === 0 ? "" : `class ${utilities.join(" ")}`,
|
|
1678
|
+
]
|
|
1679
|
+
.filter((each) => each !== "")
|
|
1680
|
+
.join("; ");
|
|
1681
|
+
};
|
|
1682
|
+
const groups: { labels: string[]; what: string }[] = [];
|
|
1683
|
+
for (const hold of holds) {
|
|
1684
|
+
const what = described(hold);
|
|
1685
|
+
const last = groups.at(-1);
|
|
1686
|
+
if (last !== undefined && last.what === what) {
|
|
1687
|
+
last.labels.push(hold.node.label);
|
|
1688
|
+
} else {
|
|
1689
|
+
groups.push({ labels: [hold.node.label], what });
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
const and = (items: readonly string[]): string =>
|
|
1693
|
+
items.length <= 1
|
|
1694
|
+
? (items[0] ?? "")
|
|
1695
|
+
: `${items.slice(0, -1).join(", ")} and ${items.at(-1) ?? ""}`;
|
|
1696
|
+
const what = and(groups.map((group) => `${and(group.labels)} (${group.what})`));
|
|
1697
|
+
|
|
1698
|
+
// The fix: a later rule that undoes the rules' values, under their own selectors (so it wins
|
|
1699
|
+
// by coming later); what style="" and utility classes say is taken out where it is.
|
|
1700
|
+
const rules = holds.flatMap((hold) => hold.shown.filter((each) => each.kind === "rule"));
|
|
1701
|
+
// Where an element's own name stands for a rule's selector, the rule may outrank it: then the
|
|
1702
|
+
// fix is !important, or it couldn't win.
|
|
1703
|
+
let outranked = false;
|
|
1704
|
+
const selectors = [
|
|
1705
|
+
...new Set(
|
|
1706
|
+
holds.flatMap((hold) => {
|
|
1707
|
+
const own = hold.shown.filter((each) => each.kind === "rule");
|
|
1708
|
+
const strongest = own.reduce<Value | undefined>(
|
|
1709
|
+
(best, each) =>
|
|
1710
|
+
best === undefined || compare(each.specificity, best.specificity) > 0 ? each : best,
|
|
1711
|
+
undefined,
|
|
1712
|
+
);
|
|
1713
|
+
// A selector the agent may be told as it is; else the element's own name.
|
|
1714
|
+
if (strongest === undefined) {
|
|
1715
|
+
return [];
|
|
1716
|
+
}
|
|
1717
|
+
if (PLAIN_SELECTOR.test(strongest.selector)) {
|
|
1718
|
+
return [strongest.selector];
|
|
1719
|
+
}
|
|
1720
|
+
outranked ||= compare(strongest.specificity, specificityOf(hold.node.label)) > 0;
|
|
1721
|
+
return [hold.node.label];
|
|
1722
|
+
}),
|
|
1723
|
+
),
|
|
1724
|
+
];
|
|
1725
|
+
const says = (prefixes: readonly string[]) =>
|
|
1726
|
+
rules.some((each) => prefixes.some((prefix) => each.written.startsWith(prefix)));
|
|
1727
|
+
const important = outranked || rules.some((each) => each.important) ? " !important" : "";
|
|
1728
|
+
const reset = [
|
|
1729
|
+
says(["height", "inset", "top", "bottom", "block-size"]) ? "height: auto" : null,
|
|
1730
|
+
says(["max-height", "max-block-size"]) ? "max-height: none" : null,
|
|
1731
|
+
says(["overflow"]) ? "overflow: visible" : null,
|
|
1732
|
+
says(["position"]) ? "position: static" : null,
|
|
1733
|
+
]
|
|
1734
|
+
.filter((each) => each !== null)
|
|
1735
|
+
.map((each) => `${each}${important}`);
|
|
1736
|
+
const fixes: string[] = [];
|
|
1737
|
+
if (selectors.length > 0 && reset.length > 0) {
|
|
1738
|
+
fixes.push(`add ${selectors.join(", ")} { ${reset.join("; ")} } after those rules`);
|
|
1739
|
+
}
|
|
1740
|
+
for (const hold of holds) {
|
|
1741
|
+
const inline = written(hold, "inline");
|
|
1742
|
+
if (inline.length > 0) {
|
|
1743
|
+
fixes.push(`take ${inline.join("; ")} out of ${hold.node.label}'s style attribute`);
|
|
1744
|
+
}
|
|
1745
|
+
const utilities = written(hold, "utility");
|
|
1746
|
+
if (utilities.length > 0) {
|
|
1747
|
+
fixes.push(`take ${utilities.join(" ")} off ${hold.node.label}'s class`);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
return {
|
|
1751
|
+
message: drawn
|
|
1752
|
+
? `The ${options.noun} looks held to one screen in its frame: ${what} hold${holds.length === 1 ? "s" : ""} it there, and what's below can't be reached. If it is meant to fit one screen, or a box its script draws scrolls the content, ignore this; otherwise let it scroll as a document: ${and(fixes)}.`
|
|
1753
|
+
: `The ${options.noun} won't scroll in its frame: ${what} hold${holds.length === 1 ? "s" : ""} it to one screen, and what's below can't be reached. Unless it is meant to fit one screen, let it scroll as a document: ${and(fixes)}.`,
|
|
1754
|
+
...(first.at.source.path === undefined ? {} : { path: first.at.source.path }),
|
|
1755
|
+
line: lineOf(first.at.source.text, first.at.at),
|
|
1756
|
+
};
|
|
1757
|
+
}
|