@tbela99/css-parser 2.0.1 → 2.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/CHANGELOG.md +86 -1
- package/README.md +15 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +4848 -3750
- package/dist/index-umd-web.js +18234 -9498
- package/dist/index.d.ts +1251 -373
- package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
- package/dist/{src/lib → lib}/ast/arena.js +1034 -655
- package/dist/{src/lib → lib}/ast/cache.js +36 -31
- package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
- package/dist/{src/lib → lib}/ast/features/if.js +24 -19
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
- package/dist/lib/ast/features/shorthand.js +129 -0
- package/dist/lib/ast/features/transform.js +188 -0
- package/dist/{src/lib → lib}/ast/features/type.js +2 -2
- package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
- package/dist/{src/lib → lib}/ast/math/math.js +8 -8
- package/dist/{src/lib → lib}/ast/minify.js +932 -354
- package/dist/lib/ast/mixins/custom-function.js +393 -0
- package/dist/lib/ast/mixins/utils.js +53 -0
- package/dist/lib/ast/mixins/variables.js +678 -0
- package/dist/lib/ast/strings.js +906 -0
- package/dist/lib/ast/transform/compute.js +560 -0
- package/dist/lib/ast/transform/matrix.js +67 -0
- package/dist/lib/ast/transform/minify.js +325 -0
- package/dist/lib/ast/transform/perspective.js +24 -0
- package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
- package/dist/lib/ast/transform/scale.js +43 -0
- package/dist/lib/ast/transform/skew.js +38 -0
- package/dist/lib/ast/transform/translate.js +56 -0
- package/dist/lib/ast/transform/utils.js +390 -0
- package/dist/{src/lib → lib}/ast/types.js +7 -15
- package/dist/{src/lib → lib}/ast/walk.js +19 -19
- package/dist/{src/lib → lib}/fs/resolve.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
- package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
- package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
- package/dist/lib/parser/input.js +85 -0
- package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
- package/dist/{src/lib → lib}/parser/parse.js +852 -619
- package/dist/lib/parser/source.js +71 -0
- package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
- package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
- package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
- package/dist/lib/parser/utils/cache.js +53 -0
- package/dist/lib/parser/utils/canonical.js +78 -0
- package/dist/lib/parser/utils/codes.js +93 -0
- package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
- package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +720 -0
- package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
- package/dist/{src/lib → lib}/printer/render.js +536 -243
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
- package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
- package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
- package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
- package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
- package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
- package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
- package/dist/{src/lib → lib}/syntax/constants.js +30 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
- package/dist/lib/validation/config.js +173 -0
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +1256 -579
- package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
- package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +92 -80
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +13 -8
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +80 -75
- package/package.json +13 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -87
- package/dist/src/lib/ast/features/transform.js +0 -85
- package/dist/src/lib/ast/transform/compute.js +0 -358
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/source.js +0 -127
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/src/lib/parser/utils/selector.js +0 -440
- package/dist/src/lib/validation/config.js +0 -120
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { eq, eqToken } from '../parser/utils/eq.js';
|
|
9
9
|
import { renderValue, NodeRenderer } from '../printer/render.js';
|
|
10
10
|
import * as index from './features/index.js';
|
|
11
11
|
import { EnumToken, EnumAstNodeStatus } from './types.js';
|
|
12
|
+
import { StringBuilder } from './strings.js';
|
|
12
13
|
import { isWhiteSpace, isIdent, isFunction, isIdentStart } from '../syntax/syntax.js';
|
|
13
14
|
import { FeatureWalkMode } from './features/type.js';
|
|
15
|
+
import { moveResult } from './mixins/utils.js';
|
|
14
16
|
import { trimArray } from '../validation/match.js';
|
|
15
17
|
import { combinators } from '../syntax/constants.js';
|
|
16
18
|
import { parseString } from '../parser/parse.js';
|
|
@@ -71,7 +73,7 @@ function minify(arena, ast, options = {}, recursive = false, errors, nestingCont
|
|
|
71
73
|
for (let k = 0; k < queue.length; k++) {
|
|
72
74
|
node = queue[k];
|
|
73
75
|
typ = arena.dataTypes[node];
|
|
74
|
-
if (typ
|
|
76
|
+
if (typ === EnumToken.CommentTokenType || typ === EnumToken.CDOCOMMTokenType) {
|
|
75
77
|
continue;
|
|
76
78
|
}
|
|
77
79
|
replacement = node;
|
|
@@ -84,7 +86,7 @@ function minify(arena, ast, options = {}, recursive = false, errors, nestingCont
|
|
|
84
86
|
}
|
|
85
87
|
// the prelude tokens of the rules whose selector has been rewritten are not parsed again:
|
|
86
88
|
// the features and the renderer use the selector / prelude string
|
|
87
|
-
const result = feature.run(arena, replacement, options2, arena.parents[node]
|
|
89
|
+
const result = feature.run(arena, replacement, options2, arena.parents[node] === 0 ? ast : arena.parents[node], context, mode);
|
|
88
90
|
if (result != null) {
|
|
89
91
|
replacement = result;
|
|
90
92
|
}
|
|
@@ -92,11 +94,11 @@ function minify(arena, ast, options = {}, recursive = false, errors, nestingCont
|
|
|
92
94
|
if (replacement != null &&
|
|
93
95
|
(!Array.isArray(replacement) || replacement.length > 0) &&
|
|
94
96
|
replacement != node &&
|
|
95
|
-
arena.parents[node]
|
|
97
|
+
arena.parents[node] !== 0) {
|
|
96
98
|
arena.replaceChild(node, ...(Array.isArray(replacement) ? replacement : [replacement]));
|
|
97
99
|
}
|
|
98
100
|
if (!Array.isArray(replacement) && arena.flags[replacement] & EnumAstNodeStatus.HasChildren) {
|
|
99
|
-
for (let child = arena.firstChild[replacement]; child
|
|
101
|
+
for (let child = arena.firstChild[replacement]; child !== 0; child = arena.nextSibling[child]) {
|
|
100
102
|
queue.push(child);
|
|
101
103
|
}
|
|
102
104
|
}
|
|
@@ -117,6 +119,34 @@ function minify(arena, ast, options = {}, recursive = false, errors, nestingCont
|
|
|
117
119
|
}
|
|
118
120
|
return ast;
|
|
119
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* `@apply --mixin()` is equivalent to `@apply --mixin`: a mixin applied without arguments is referenced by its name
|
|
124
|
+
* https://drafts.csswg.org/css-mixins-1/#apply-rule
|
|
125
|
+
* @param arena
|
|
126
|
+
* @param node `@apply` rule
|
|
127
|
+
*/
|
|
128
|
+
function minifyApplyPrelude(arena, node) {
|
|
129
|
+
let reference = arena.firstToken[node];
|
|
130
|
+
while (reference !== 0 &&
|
|
131
|
+
(arena.dataTypes[reference] === EnumToken.WhitespaceTokenType ||
|
|
132
|
+
arena.dataTypes[reference] === EnumToken.CommentTokenType)) {
|
|
133
|
+
reference = arena.nextSibling[reference];
|
|
134
|
+
}
|
|
135
|
+
if (reference === 0 || arena.dataTypes[reference] !== EnumToken.CustomFunctionTokenType) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// the comments and the whitespace are ignored: `@apply --mixin(/* comment */)` has no argument
|
|
139
|
+
for (let token = arena.firstToken[reference]; token !== 0; token = arena.nextSibling[token]) {
|
|
140
|
+
if (arena.dataTypes[token] !== EnumToken.WhitespaceTokenType &&
|
|
141
|
+
arena.dataTypes[token] !== EnumToken.CommentTokenType) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const name = arena.resolveString(arena.stringValue[reference]);
|
|
146
|
+
arena.setTokenList(node, [arena.createToken(EnumToken.DashedIdenTokenType, name)]);
|
|
147
|
+
// inlined: arena.setValueString(node, name)
|
|
148
|
+
arena.stringValue[node] = arena.internString(name);
|
|
149
|
+
}
|
|
120
150
|
function transformAtRuleMediaPrelude(arena, values) {
|
|
121
151
|
return { hasUpdates: false, values: trimArray(arena, values) };
|
|
122
152
|
}
|
|
@@ -126,6 +156,7 @@ function transformAtRuleMediaPrelude(arena, values) {
|
|
|
126
156
|
* - generate range queries
|
|
127
157
|
*
|
|
128
158
|
* @private
|
|
159
|
+
* @param arena
|
|
129
160
|
* @param tokens
|
|
130
161
|
*/
|
|
131
162
|
function minifyAtRuleMedia(arena, tokens) {
|
|
@@ -168,8 +199,8 @@ function minifyAtRuleMedia(arena, tokens) {
|
|
|
168
199
|
*/
|
|
169
200
|
function reduce(acc, curr) {
|
|
170
201
|
// trim :is()
|
|
171
|
-
if (curr[0]
|
|
172
|
-
if (curr[1]
|
|
202
|
+
if (curr[0] === "&") {
|
|
203
|
+
if (curr[1] === " " && !isIdent(curr[2]) && !isFunction(curr[2])) {
|
|
173
204
|
const all = new Array(Math.max(curr.length - 2, 0));
|
|
174
205
|
let write = 0;
|
|
175
206
|
for (let read = 2; read < curr.length; read++) {
|
|
@@ -184,6 +215,65 @@ function reduce(acc, curr) {
|
|
|
184
215
|
acc.push(curr.join(""));
|
|
185
216
|
return acc;
|
|
186
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* true if the selector part is a simple selector that can be moved into `:is()`: it is not a combinator, a whitespace
|
|
220
|
+
* (descendant combinator), the nesting selector or a pseudo-element (a pseudo-element is not allowed in `:is()`)
|
|
221
|
+
* @param part
|
|
222
|
+
*/
|
|
223
|
+
function isCompoundPart(part) {
|
|
224
|
+
return (part.trim() !== "" &&
|
|
225
|
+
part !== "&" &&
|
|
226
|
+
!combinators.includes(part) &&
|
|
227
|
+
!part.startsWith("::") &&
|
|
228
|
+
!/^:(before|after|first-line|first-letter)$/i.test(part));
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* factor the common prefix of the selectors of a rule into `:is()`. the selectors are relative to the common prefix,
|
|
232
|
+
* see {@link optimizeSelector}: `["&"]` is the prefix itself, `["&", ".a"]` is a compound selector (`.foo.a`),
|
|
233
|
+
* `["&", " ", ".a"]` a descendant selector (`.foo .a`) and `[">", ".a"]` a child selector (`.foo>.a`).
|
|
234
|
+
*
|
|
235
|
+
* - `.foo.a,.foo.b` -> `.foo:is(.a,.b)`. `.foo,.foo.b` -> `.foo:is(*,.b)`
|
|
236
|
+
* - `.foo .a,.foo .b` -> `.foo :is(.a,.b)`, `.foo>.a,.foo>.b` -> `.foo>:is(.a,.b)`
|
|
237
|
+
*
|
|
238
|
+
* the selectors are factored only when the suffixes are compound selectors joined to the prefix with the same
|
|
239
|
+
* combinator: `.foo .a .b,.foo .c` is not `.foo :is(.a .b,.c)`. null when the selectors cannot be factored
|
|
240
|
+
* @param optimized
|
|
241
|
+
*/
|
|
242
|
+
function factorSelector(optimized) {
|
|
243
|
+
const prefix = optimized.optimized;
|
|
244
|
+
if (prefix.length === 0 ||
|
|
245
|
+
!prefix.every((part) => part === " " || isCompoundPart(part) || combinators.includes(part))) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
let combinator = null;
|
|
249
|
+
const members = [];
|
|
250
|
+
for (const selector of optimized.selector) {
|
|
251
|
+
// the parts that follow the prefix. the whitespace around the combinators is removed
|
|
252
|
+
const suffix = (selector[0] === "&" ? selector.slice(1) : selector.slice()).filter((part, index, parts) => part.trim() !== "" ||
|
|
253
|
+
(!combinators.includes(parts[index - 1]) && !combinators.includes(parts[index + 1])));
|
|
254
|
+
// the prefix itself
|
|
255
|
+
if (suffix.length === 0) {
|
|
256
|
+
if (combinator != null && combinator !== "") {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
combinator = "";
|
|
260
|
+
members.push("*");
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
// the combinator that joins the suffix to the prefix. "" for a compound selector
|
|
264
|
+
const current = suffix[0].trim() === "" ? " " : combinators.includes(suffix[0]) ? suffix[0] : "";
|
|
265
|
+
const compound = current === "" ? suffix : suffix.slice(1);
|
|
266
|
+
if ((combinator != null && combinator !== current) ||
|
|
267
|
+
current === "|" ||
|
|
268
|
+
compound.length === 0 ||
|
|
269
|
+
!compound.every(isCompoundPart)) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
combinator = current;
|
|
273
|
+
members.push(compound.join(""));
|
|
274
|
+
}
|
|
275
|
+
return prefix.join("") + combinator + ":is(" + members.join(",") + ")";
|
|
276
|
+
}
|
|
187
277
|
/**
|
|
188
278
|
* Apply minification rules to the ast tree
|
|
189
279
|
* @param arena
|
|
@@ -204,10 +294,10 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
204
294
|
return ast;
|
|
205
295
|
}
|
|
206
296
|
context.nodes.add(ast);
|
|
207
|
-
if (arena.flags[ast] & EnumAstNodeStatus.HasChildren && arena.firstChild[ast]
|
|
297
|
+
if (arena.flags[ast] & EnumAstNodeStatus.HasChildren && arena.firstChild[ast] !== 0) {
|
|
208
298
|
const reducer = reduce.bind(ast);
|
|
209
299
|
if (!nestingContent) {
|
|
210
|
-
nestingContent = options.nestingRules && arena.dataTypes[ast]
|
|
300
|
+
nestingContent = options.nestingRules && arena.dataTypes[ast] === EnumToken.RuleNodeType;
|
|
211
301
|
}
|
|
212
302
|
// the children list is modified while it is visited: every branch sets the next child to visit
|
|
213
303
|
let cursor = arena.firstChild[ast];
|
|
@@ -221,13 +311,13 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
221
311
|
* @param count
|
|
222
312
|
*/
|
|
223
313
|
const before = (child, count) => {
|
|
224
|
-
let result = child
|
|
225
|
-
while (--count > 0 && result
|
|
314
|
+
let result = child === 0 ? arena.lastChild[ast] : arena.previousSibling[child];
|
|
315
|
+
while (--count > 0 && result !== 0) {
|
|
226
316
|
result = arena.previousSibling[result];
|
|
227
317
|
}
|
|
228
|
-
return result
|
|
318
|
+
return result === 0 ? null : result;
|
|
229
319
|
};
|
|
230
|
-
while (cursor
|
|
320
|
+
while (cursor !== 0) {
|
|
231
321
|
if (arena.dataTypes[cursor] === EnumToken.CommentNodeType) {
|
|
232
322
|
cursor = arena.nextSibling[cursor];
|
|
233
323
|
continue;
|
|
@@ -239,38 +329,15 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
239
329
|
nodeType = arena.dataTypes[node];
|
|
240
330
|
previousType = previous == null ? null : arena.dataTypes[previous];
|
|
241
331
|
if (nodeType === EnumToken.AtRuleNodeType) {
|
|
242
|
-
const nam = arena.
|
|
332
|
+
const nam = arena.resolveString(arena.name[node]);
|
|
243
333
|
if (nam === "font-face") {
|
|
244
334
|
cursor = arena.nextSibling[node];
|
|
245
335
|
continue;
|
|
246
336
|
}
|
|
247
|
-
//
|
|
337
|
+
// there is no early return: the result that precedes the conditional rules is moved after them.
|
|
338
|
+
// the conditional rules are never removed
|
|
248
339
|
if (equalsIgnoreCase(nam, "function")) {
|
|
249
|
-
|
|
250
|
-
let slice = false;
|
|
251
|
-
while (k != 0) {
|
|
252
|
-
if (arena.dataTypes[k] === EnumToken.CommentNodeType ||
|
|
253
|
-
arena.dataTypes[k] === EnumToken.WhitespaceTokenType) {
|
|
254
|
-
k = arena.previousSibling[k];
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
if (arena.dataTypes[k] === EnumToken.DeclarationNodeType &&
|
|
258
|
-
equalsIgnoreCase(arena.strings[arena.name[k]], "result")) {
|
|
259
|
-
slice = true;
|
|
260
|
-
break;
|
|
261
|
-
}
|
|
262
|
-
k = arena.previousSibling[k];
|
|
263
|
-
}
|
|
264
|
-
if (k != 0 && slice) {
|
|
265
|
-
k = arena.previousSibling[k];
|
|
266
|
-
while (k != 0) {
|
|
267
|
-
const previousNode = arena.previousSibling[k];
|
|
268
|
-
if (arena.dataTypes[k] === EnumToken.AtRuleNodeType) {
|
|
269
|
-
arena.detach(k);
|
|
270
|
-
}
|
|
271
|
-
k = previousNode;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
340
|
+
moveResult(arena, node);
|
|
274
341
|
}
|
|
275
342
|
}
|
|
276
343
|
if (nodeType === EnumToken.KeyframesAtRuleNodeType) {
|
|
@@ -297,17 +364,17 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
297
364
|
continue;
|
|
298
365
|
}
|
|
299
366
|
let k = arena.firstChild[node];
|
|
300
|
-
while (k
|
|
367
|
+
while (k !== 0) {
|
|
301
368
|
const next = arena.nextSibling[k];
|
|
302
|
-
if (arena.dataTypes[k]
|
|
369
|
+
if (arena.dataTypes[k] === EnumToken.DeclarationNodeType) {
|
|
303
370
|
let l = arena.lastToken[k];
|
|
304
|
-
while (l
|
|
305
|
-
if (arena.dataTypes[l]
|
|
371
|
+
while (l !== 0) {
|
|
372
|
+
if (arena.dataTypes[l] === EnumToken.ImportantTokenType) {
|
|
306
373
|
arena.detach(k);
|
|
307
374
|
break;
|
|
308
375
|
}
|
|
309
|
-
if (arena.dataTypes[l]
|
|
310
|
-
arena.dataTypes[l]
|
|
376
|
+
if (arena.dataTypes[l] === EnumToken.WhitespaceTokenType ||
|
|
377
|
+
arena.dataTypes[l] === EnumToken.CommentTokenType) {
|
|
311
378
|
l = arena.previousSibling[l];
|
|
312
379
|
continue;
|
|
313
380
|
}
|
|
@@ -317,9 +384,9 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
317
384
|
k = next;
|
|
318
385
|
}
|
|
319
386
|
}
|
|
320
|
-
else if (nodeType
|
|
321
|
-
const nam = arena.
|
|
322
|
-
const tokens = arena.firstToken[node]
|
|
387
|
+
else if (nodeType === EnumToken.AtRuleNodeType) {
|
|
388
|
+
const nam = arena.resolveString(arena.name[node]);
|
|
389
|
+
const tokens = arena.firstToken[node] === 0 ? null : arena.tokens(node);
|
|
323
390
|
const renderPrelude = (acc, curr, index, arr) => acc +
|
|
324
391
|
(arena.dataTypes[curr] === EnumToken.CommentTokenType ||
|
|
325
392
|
(arena.dataTypes[curr] === EnumToken.WhitespaceTokenType &&
|
|
@@ -329,7 +396,11 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
329
396
|
(index + 3 < arr.length || arena.dataTypes[arr[index + 2]] === EnumToken.WhitespaceTokenType))
|
|
330
397
|
? ""
|
|
331
398
|
: renderValue(arena, curr));
|
|
332
|
-
if (nam
|
|
399
|
+
if (nam === "apply") {
|
|
400
|
+
// `@apply --mixin()` is equivalent to `@apply --mixin`
|
|
401
|
+
minifyApplyPrelude(arena, node);
|
|
402
|
+
}
|
|
403
|
+
else if (nam === "media") {
|
|
333
404
|
if (tokens != null) {
|
|
334
405
|
const slice = tokens.slice();
|
|
335
406
|
minifyAtRuleMedia(arena, slice);
|
|
@@ -339,7 +410,7 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
339
410
|
arena.stringValue[node] = arena.internString(slice.reduce(renderPrelude, ""));
|
|
340
411
|
}
|
|
341
412
|
}
|
|
342
|
-
if (["all", ""].includes(arena.
|
|
413
|
+
if (["all", ""].includes(arena.resolveString(arena.stringValue[node]))) {
|
|
343
414
|
// the node is replaced by its children
|
|
344
415
|
cursor = arena.firstChild[node] || arena.nextSibling[node];
|
|
345
416
|
arena.moveChildren(node, ast, node);
|
|
@@ -355,7 +426,7 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
355
426
|
if (arena.dataTypes[token] === EnumToken.ParensTokenType ||
|
|
356
427
|
arena.dataTypes[token] === EnumToken.MediaQueryConditionTokenType ||
|
|
357
428
|
(arena.dataTypes[token] === EnumToken.IdenTokenType &&
|
|
358
|
-
"layer" !== arena.
|
|
429
|
+
"layer" !== arena.resolveString(arena.stringValue[token]))) {
|
|
359
430
|
break;
|
|
360
431
|
}
|
|
361
432
|
}
|
|
@@ -376,8 +447,8 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
376
447
|
arena.detach(node);
|
|
377
448
|
continue;
|
|
378
449
|
}
|
|
379
|
-
if (previousType
|
|
380
|
-
nam
|
|
450
|
+
if (previousType === EnumToken.AtRuleNodeType &&
|
|
451
|
+
nam !== "font-face" &&
|
|
381
452
|
arena.name[previous] === arena.name[node] &&
|
|
382
453
|
arena.stringValue[previous] === arena.stringValue[node]) {
|
|
383
454
|
if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
|
|
@@ -400,23 +471,42 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
400
471
|
continue;
|
|
401
472
|
}
|
|
402
473
|
else if (nodeType === EnumToken.RuleNodeType) {
|
|
403
|
-
|
|
474
|
+
// a single selector that does not end with a combinator is not reduced (see isSimpleSelector()): the
|
|
475
|
+
// selector is not read
|
|
476
|
+
if (!isSimpleSelector(arena, arena.name[node])) {
|
|
477
|
+
reduceRuleSelector(arena, node);
|
|
478
|
+
}
|
|
404
479
|
let wrapper = null;
|
|
405
480
|
let match;
|
|
406
481
|
if (options.nestingRules) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
482
|
+
// the rules that apply a mixin are not merged
|
|
483
|
+
if (previousType === EnumToken.RuleNodeType &&
|
|
484
|
+
!hasAtRuleStatement(arena, previous) &&
|
|
485
|
+
!hasAtRuleStatement(arena, node)) {
|
|
486
|
+
if (!isSimpleSelector(arena, arena.name[previous])) {
|
|
487
|
+
reduceRuleSelector(arena, previous);
|
|
488
|
+
}
|
|
489
|
+
// the selectors are split when their first parts are equal: they do not match otherwise
|
|
490
|
+
if (mayMatchSelectors(arena, previous, node)) {
|
|
491
|
+
match = matchSelectors(rawSelector(arena, previous), rawSelector(arena, node));
|
|
492
|
+
if (match != null) {
|
|
493
|
+
wrapper = wrapNodes(arena, previous, node, match, reducer);
|
|
494
|
+
}
|
|
412
495
|
}
|
|
413
496
|
}
|
|
414
497
|
if (wrapper != null) {
|
|
415
498
|
// wrap the following rules that match the wrapper selector
|
|
416
499
|
let nextNode = arena.nextSibling[wrapper];
|
|
417
|
-
while (nextNode
|
|
418
|
-
|
|
419
|
-
|
|
500
|
+
while (nextNode !== 0 &&
|
|
501
|
+
arena.dataTypes[nextNode] === EnumToken.RuleNodeType &&
|
|
502
|
+
!hasAtRuleStatement(arena, nextNode)) {
|
|
503
|
+
if (!isSimpleSelector(arena, arena.name[nextNode])) {
|
|
504
|
+
reduceRuleSelector(arena, nextNode);
|
|
505
|
+
}
|
|
506
|
+
// the selector is split when its first part matches the wrapper selector
|
|
507
|
+
match = mayMatchSelectors(arena, wrapper, nextNode)
|
|
508
|
+
? matchSelectors(rawSelector(arena, wrapper), rawSelector(arena, nextNode))
|
|
509
|
+
: null;
|
|
420
510
|
if (match == null) {
|
|
421
511
|
break;
|
|
422
512
|
}
|
|
@@ -428,19 +518,20 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
428
518
|
doMinify(arena, wrapper, options, recursive, errors, nestingContent, context);
|
|
429
519
|
continue;
|
|
430
520
|
}
|
|
431
|
-
else if (arena.optimized
|
|
432
|
-
arena.optimized
|
|
433
|
-
arena.optimized
|
|
434
|
-
const optimized = arena.optimized
|
|
435
|
-
wrapper = arena.
|
|
521
|
+
else if (arena.optimized.get(node) != null &&
|
|
522
|
+
arena.optimized.get(node).match &&
|
|
523
|
+
arena.optimized.get(node).selector.length > 1) {
|
|
524
|
+
const optimized = arena.optimized.get(node);
|
|
525
|
+
wrapper = arena.cloneNode(node);
|
|
526
|
+
arena.optimized.set(wrapper, arena.optimized.get(node));
|
|
436
527
|
// inlined: arena.setSelector(wrapper, optimized.optimized[0])
|
|
437
528
|
arena.name[wrapper] = arena.internString(optimized.optimized[0]);
|
|
438
|
-
arena.raw
|
|
529
|
+
arena.raw.set(wrapper, [[optimized.optimized[0]]]);
|
|
439
530
|
// inlined: arena.setTokens(wrapper)
|
|
440
531
|
arena.setTokenList(wrapper, []);
|
|
441
532
|
// inlined: arena.setSelector(node, optimized.selector.reduce(reducer, []).join(","))
|
|
442
533
|
arena.name[node] = arena.internString(optimized.selector.reduce(reducer, []).join(","));
|
|
443
|
-
arena.raw
|
|
534
|
+
arena.raw.set(node, optimized.selector.slice());
|
|
444
535
|
// inlined: arena.setTokens(node)
|
|
445
536
|
arena.setTokenList(node, []);
|
|
446
537
|
// the wrapper takes the place of the node
|
|
@@ -449,8 +540,8 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
449
540
|
node = wrapper;
|
|
450
541
|
nodeType = EnumToken.RuleNodeType;
|
|
451
542
|
}
|
|
452
|
-
else if (arena.optimized
|
|
453
|
-
const optimized = arena.optimized
|
|
543
|
+
else if (arena.optimized.get(node)?.reducible) {
|
|
544
|
+
const optimized = arena.optimized.get(node);
|
|
454
545
|
if (optimized.optimized.length === 1) {
|
|
455
546
|
const sel1 = optimized.optimized[0] +
|
|
456
547
|
":is(" +
|
|
@@ -458,7 +549,7 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
458
549
|
")";
|
|
459
550
|
const sel2 = optimized.selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + optimized.optimized[0] + curr.join(""), "");
|
|
460
551
|
// inlined: arena.setSelector(node, sel1.length < sel2.length ? sel1 : sel2)
|
|
461
|
-
arena.name[node] = arena.internString(
|
|
552
|
+
arena.name[node] = arena.internString(sel1.length < sel2.length ? sel1 : sel2);
|
|
462
553
|
// inlined: arena.setTokens(node)
|
|
463
554
|
arena.setTokenList(node, []);
|
|
464
555
|
}
|
|
@@ -473,109 +564,22 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
473
564
|
}
|
|
474
565
|
}
|
|
475
566
|
}
|
|
476
|
-
else if (arena.optimized
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
const selector = optimizedSelector.selector.reduce((acc, curr) => {
|
|
480
|
-
if (curr[0] == "&" && curr.length > 1) {
|
|
481
|
-
if (curr[1] == " ") {
|
|
482
|
-
curr.splice(0, 2);
|
|
483
|
-
}
|
|
484
|
-
else {
|
|
485
|
-
curr.splice(0, 1);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
else if (combinators.includes(curr[0])) {
|
|
489
|
-
curr.unshift("&");
|
|
490
|
-
wrap = false;
|
|
491
|
-
}
|
|
492
|
-
acc.push(curr);
|
|
493
|
-
return acc;
|
|
494
|
-
}, []);
|
|
495
|
-
if (!wrap) {
|
|
496
|
-
wrap = selector.some((s) => s[0] != "&");
|
|
497
|
-
}
|
|
498
|
-
let rule = null;
|
|
499
|
-
const optimized = optimizedSelector.optimized.slice();
|
|
500
|
-
if (optimized.length > 1) {
|
|
501
|
-
const check = optimized.at(-2);
|
|
502
|
-
if (!combinators.includes(check)) {
|
|
503
|
-
const last = optimized[optimized.length - 1];
|
|
504
|
-
const all = new Array(Math.max(optimized.length - 1, 0));
|
|
505
|
-
let write = 0;
|
|
506
|
-
for (let read = 0; read < optimized.length - 1; read++) {
|
|
507
|
-
all[write++] = optimized[read];
|
|
508
|
-
}
|
|
509
|
-
optimized.length = 0;
|
|
510
|
-
for (let index = 0; index < write; index++) {
|
|
511
|
-
optimized[index] = all[index];
|
|
512
|
-
}
|
|
513
|
-
wrap = false;
|
|
514
|
-
rule =
|
|
515
|
-
optimized.join("") +
|
|
516
|
-
`:is(${selector
|
|
517
|
-
.map((s) => {
|
|
518
|
-
const next = s[0] == "&" ? new Array(s.length) : new Array(s.length + 1);
|
|
519
|
-
let write = 0;
|
|
520
|
-
if (s[0] == "&") {
|
|
521
|
-
next[write++] = last;
|
|
522
|
-
for (let read = 1; read < s.length; read++) {
|
|
523
|
-
next[write++] = s[read];
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
else {
|
|
527
|
-
next[write++] = last;
|
|
528
|
-
for (let read = 0; read < s.length; read++) {
|
|
529
|
-
next[write++] = s[read];
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
const rebuilt = new Array(write);
|
|
533
|
-
for (let index = 0; index < write; index++) {
|
|
534
|
-
rebuilt[index] = next[index];
|
|
535
|
-
}
|
|
536
|
-
return rebuilt.join("");
|
|
537
|
-
})
|
|
538
|
-
.join(",")})`;
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
if (rule == null) {
|
|
542
|
-
rule = selector
|
|
543
|
-
.map((s) => {
|
|
544
|
-
if (s[0] == "&") {
|
|
545
|
-
const replacement = optimizedSelector.optimized;
|
|
546
|
-
const all = new Array(Math.max(s.length - 1 + replacement.length, 0));
|
|
547
|
-
let write = 0;
|
|
548
|
-
for (let read = 0; read < replacement.length; read++) {
|
|
549
|
-
all[write++] = replacement[read];
|
|
550
|
-
}
|
|
551
|
-
for (let read = 1; read < s.length; read++) {
|
|
552
|
-
all[write++] = s[read];
|
|
553
|
-
}
|
|
554
|
-
const rebuilt = new Array(write);
|
|
555
|
-
for (let index = 0; index < write; index++) {
|
|
556
|
-
rebuilt[index] = all[index];
|
|
557
|
-
}
|
|
558
|
-
return rebuilt.join("");
|
|
559
|
-
}
|
|
560
|
-
return s.join("");
|
|
561
|
-
})
|
|
562
|
-
.join(",");
|
|
563
|
-
}
|
|
564
|
-
let sel = wrap ? optimizedSelector.optimized.join("") + `:is(${rule})` : rule;
|
|
565
|
-
if (sel.length < arena.strings[arena.name[node]].length) {
|
|
567
|
+
else if (arena.optimized.get(node)?.match) {
|
|
568
|
+
const sel = factorSelector(arena.optimized.get(node));
|
|
569
|
+
if (sel != null && sel.length < arena.resolveString(arena.name[node]).length) {
|
|
566
570
|
// inlined: arena.setSelector(node, sel)
|
|
567
571
|
arena.name[node] = arena.internString(sel);
|
|
568
572
|
// inlined: arena.setTokens(node)
|
|
569
573
|
arena.setTokenList(node, []);
|
|
570
574
|
}
|
|
571
575
|
}
|
|
572
|
-
else if (arena.optimized
|
|
573
|
-
const optimized = arena.optimized
|
|
576
|
+
else if (arena.optimized.get(node)?.reducible) {
|
|
577
|
+
const optimized = arena.optimized.get(node);
|
|
574
578
|
if (optimized.optimized.length === 1) {
|
|
575
579
|
const sel1 = optimized.optimized[0] + ":is(" + optimized.selector.reduce(reducer, []).join(",") + ")";
|
|
576
580
|
const sel2 = optimized.selector.reduce((acc, curr) => (acc.length > 0 ? acc + "," : "") + optimized.optimized[0] + curr.join(""), "");
|
|
577
581
|
// inlined: arena.setSelector(node, sel1.length < sel2.length ? sel1 : sel2)
|
|
578
|
-
arena.name[node] = arena.internString(
|
|
582
|
+
arena.name[node] = arena.internString(sel1.length < sel2.length ? sel1 : sel2);
|
|
579
583
|
// inlined: arena.setTokens(node)
|
|
580
584
|
arena.setTokenList(node, []);
|
|
581
585
|
}
|
|
@@ -589,13 +593,13 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
589
593
|
arena.setTokenList(node, []);
|
|
590
594
|
}
|
|
591
595
|
}
|
|
592
|
-
else if (arena.optimized
|
|
593
|
-
const optimized = arena.optimized
|
|
596
|
+
else if (arena.optimized.get(node)?.optimized.length > 0) {
|
|
597
|
+
const optimized = arena.optimized.get(node);
|
|
594
598
|
const sel = optimized.optimized.join("");
|
|
595
|
-
if (sel.length < arena.
|
|
599
|
+
if (sel.length < arena.resolveString(arena.name[node]).length) {
|
|
596
600
|
// inlined: arena.setSelector(node, sel)
|
|
597
601
|
arena.name[node] = arena.internString(sel);
|
|
598
|
-
arena.raw
|
|
602
|
+
arena.raw.set(node, [optimized.optimized.slice()]);
|
|
599
603
|
// inlined: arena.setTokens(node)
|
|
600
604
|
arena.setTokenList(node, []);
|
|
601
605
|
}
|
|
@@ -609,7 +613,7 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
609
613
|
if (previousType === nodeType) {
|
|
610
614
|
let shouldMerge = true;
|
|
611
615
|
let k = arena.lastChild[previous];
|
|
612
|
-
while (k
|
|
616
|
+
while (k !== 0) {
|
|
613
617
|
if (arena.dataTypes[k] === EnumToken.CommentNodeType) {
|
|
614
618
|
k = arena.previousSibling[k];
|
|
615
619
|
continue;
|
|
@@ -617,12 +621,15 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
617
621
|
shouldMerge = arena.dataTypes[k] === EnumToken.DeclarationNodeType;
|
|
618
622
|
break;
|
|
619
623
|
}
|
|
620
|
-
|
|
624
|
+
// the rules that apply a mixin are not merged
|
|
625
|
+
if (shouldMerge &&
|
|
626
|
+
(nodeType !== EnumToken.RuleNodeType ||
|
|
627
|
+
(!hasAtRuleStatement(arena, previous) && !hasAtRuleStatement(arena, node)))) {
|
|
621
628
|
if (((nodeType === EnumToken.RuleNodeType ||
|
|
622
629
|
nodeType === EnumToken.KeyframesRuleNodeType) &&
|
|
623
630
|
arena.name[node] === arena.name[previous]) ||
|
|
624
|
-
(nodeType
|
|
625
|
-
arena.
|
|
631
|
+
(nodeType === EnumToken.AtRuleNodeType &&
|
|
632
|
+
arena.resolveString(arena.name[node]) !== "font-face" &&
|
|
626
633
|
arena.name[node] === arena.name[previous])) {
|
|
627
634
|
arena.moveChildren(previous, node, arena.firstChild[node]);
|
|
628
635
|
options.cache?.delete(node);
|
|
@@ -632,14 +639,14 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
632
639
|
cursor = arena.nextSibling[node];
|
|
633
640
|
continue;
|
|
634
641
|
}
|
|
635
|
-
else if (nodeType
|
|
642
|
+
else if (nodeType === previousType &&
|
|
636
643
|
[EnumToken.KeyframesRuleNodeType, EnumToken.RuleNodeType].includes(nodeType)) {
|
|
637
644
|
const intersect = diff(arena, previous, node, options, context);
|
|
638
645
|
if (intersect != null) {
|
|
639
646
|
// [result, node2] take the place of node1 (previous), node1 takes the place of node2 (node)
|
|
640
647
|
const { node1, node2, result } = intersect;
|
|
641
648
|
const after = arena.nextSibling[node2];
|
|
642
|
-
const keep2 = arena.firstChild[node2]
|
|
649
|
+
const keep2 = arena.firstChild[node2] !== 0;
|
|
643
650
|
if (result != null) {
|
|
644
651
|
arena.insertBefore(node1, result);
|
|
645
652
|
}
|
|
@@ -649,10 +656,10 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
649
656
|
else {
|
|
650
657
|
arena.detach(node2);
|
|
651
658
|
}
|
|
652
|
-
if (arena.firstChild[node1]
|
|
659
|
+
if (arena.firstChild[node1] === 0) {
|
|
653
660
|
arena.detach(node1);
|
|
654
661
|
}
|
|
655
|
-
else if (after
|
|
662
|
+
else if (after === 0) {
|
|
656
663
|
arena.appendChild(ast, node1);
|
|
657
664
|
}
|
|
658
665
|
else {
|
|
@@ -661,13 +668,13 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
661
668
|
// next node to visit
|
|
662
669
|
node = keep2
|
|
663
670
|
? arena.nextSibling[result ?? node2] || null
|
|
664
|
-
: before(arena.parents[node1]
|
|
671
|
+
: before(arena.parents[node1] === ast ? node1 : after, result == null ? 1 : 2);
|
|
665
672
|
previous = result;
|
|
666
673
|
}
|
|
667
674
|
}
|
|
668
675
|
}
|
|
669
676
|
}
|
|
670
|
-
if (recursive && previous != null && previous
|
|
677
|
+
if (recursive && previous != null && previous !== node) {
|
|
671
678
|
if (!hasDeclaration(arena, previous)) {
|
|
672
679
|
doMinify(arena, previous, options, recursive, errors, nestingContent, context);
|
|
673
680
|
}
|
|
@@ -676,25 +683,25 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
676
683
|
}
|
|
677
684
|
if (!nestingContent &&
|
|
678
685
|
previous != null &&
|
|
679
|
-
arena.dataTypes[previous]
|
|
680
|
-
arena.
|
|
686
|
+
arena.dataTypes[previous] === EnumToken.RuleNodeType &&
|
|
687
|
+
arena.stringIndexOf(arena.name[previous], 0x26) !== -1) {
|
|
681
688
|
fixSelector(arena, previous);
|
|
682
689
|
}
|
|
683
690
|
previous = node;
|
|
684
691
|
cursor = node == null ? arena.firstChild[ast] : arena.nextSibling[node];
|
|
685
692
|
}
|
|
686
693
|
if (recursive && node != null && arena.flags[node] & EnumAstNodeStatus.HasChildren) {
|
|
687
|
-
if (arena.dataTypes[node]
|
|
688
|
-
if (!(arena.dataTypes[node]
|
|
689
|
-
arena.
|
|
694
|
+
if (arena.dataTypes[node] === EnumToken.KeyframesAtRuleNodeType || !hasDeclarationChild(arena, node)) {
|
|
695
|
+
if (!(arena.dataTypes[node] === EnumToken.AtRuleNodeType &&
|
|
696
|
+
arena.resolveString(arena.name[node]) !== "font-face")) {
|
|
690
697
|
doMinify(arena, node, options, recursive, errors, nestingContent, context);
|
|
691
698
|
}
|
|
692
699
|
}
|
|
693
700
|
}
|
|
694
701
|
if (!nestingContent &&
|
|
695
702
|
node != null &&
|
|
696
|
-
arena.dataTypes[node]
|
|
697
|
-
arena.
|
|
703
|
+
arena.dataTypes[node] === EnumToken.RuleNodeType &&
|
|
704
|
+
arena.stringIndexOf(arena.name[node], 0x26) !== -1) {
|
|
698
705
|
fixSelector(arena, node);
|
|
699
706
|
}
|
|
700
707
|
}
|
|
@@ -708,11 +715,11 @@ function doMinify(arena, ast, options = {}, recursive = false, errors, nestingCo
|
|
|
708
715
|
* @private
|
|
709
716
|
*/
|
|
710
717
|
function hasDeclaration(arena, node) {
|
|
711
|
-
for (let child = arena.firstChild[node]; child
|
|
712
|
-
if (arena.dataTypes[child]
|
|
718
|
+
for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
719
|
+
if (arena.dataTypes[child] === EnumToken.CommentNodeType) {
|
|
713
720
|
continue;
|
|
714
721
|
}
|
|
715
|
-
return arena.dataTypes[child]
|
|
722
|
+
return arena.dataTypes[child] === EnumToken.DeclarationNodeType;
|
|
716
723
|
}
|
|
717
724
|
return true;
|
|
718
725
|
}
|
|
@@ -724,8 +731,8 @@ function hasDeclaration(arena, node) {
|
|
|
724
731
|
* @private
|
|
725
732
|
*/
|
|
726
733
|
function hasDeclarationChild(arena, node) {
|
|
727
|
-
for (let child = arena.firstChild[node]; child
|
|
728
|
-
if (arena.dataTypes[child]
|
|
734
|
+
for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
735
|
+
if (arena.dataTypes[child] === EnumToken.DeclarationNodeType) {
|
|
729
736
|
return true;
|
|
730
737
|
}
|
|
731
738
|
}
|
|
@@ -738,6 +745,30 @@ function hasDeclarationChild(arena, node) {
|
|
|
738
745
|
* @private
|
|
739
746
|
*/
|
|
740
747
|
function optimizeSelector(selector) {
|
|
748
|
+
if (selector.length === 0) {
|
|
749
|
+
return null;
|
|
750
|
+
}
|
|
751
|
+
if (selector.length === 1) {
|
|
752
|
+
const current = selector[0];
|
|
753
|
+
const last = current.at(-1);
|
|
754
|
+
if (last == null || !last.startsWith(":is(")) {
|
|
755
|
+
let end = current.length;
|
|
756
|
+
while (end > 0 && (current[end - 1] === " " || combinators.includes(current[end - 1]))) {
|
|
757
|
+
end--;
|
|
758
|
+
}
|
|
759
|
+
const optimized = current.slice(0, end);
|
|
760
|
+
if (optimized[0] === "&" && optimized[1] === " ") {
|
|
761
|
+
optimized.splice(0, 2);
|
|
762
|
+
}
|
|
763
|
+
const remainder = current.slice(end);
|
|
764
|
+
return {
|
|
765
|
+
match: false,
|
|
766
|
+
optimized,
|
|
767
|
+
selector: [remainder[0] === "&" && remainder[1] === " " ? remainder.slice(2) : remainder],
|
|
768
|
+
reducible: false,
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
}
|
|
741
772
|
const map = new Set();
|
|
742
773
|
selector = selector
|
|
743
774
|
.reduce((acc, curr) => {
|
|
@@ -745,8 +776,8 @@ function optimizeSelector(selector) {
|
|
|
745
776
|
if (curr.length > 0 && curr.at(-1).startsWith(":is(")) {
|
|
746
777
|
// @ts-ignore
|
|
747
778
|
const rules = splitRule(curr.at(-1).slice(4, -1)).map((x) => {
|
|
748
|
-
if (x[0]
|
|
749
|
-
return x.slice(x[1]
|
|
779
|
+
if (x[0] === "&" && x.length > 1) {
|
|
780
|
+
return x.slice(x[1] === " " ? 2 : 1);
|
|
750
781
|
}
|
|
751
782
|
return x;
|
|
752
783
|
});
|
|
@@ -768,7 +799,7 @@ function optimizeSelector(selector) {
|
|
|
768
799
|
return true;
|
|
769
800
|
});
|
|
770
801
|
const optimized = [];
|
|
771
|
-
const k = selector.reduce((acc, curr) => acc
|
|
802
|
+
const k = selector.reduce((acc, curr) => acc === 0 ? curr.length : curr.length === 0 ? acc : Math.min(acc, curr.length), 0);
|
|
772
803
|
let i = 0;
|
|
773
804
|
let j;
|
|
774
805
|
let match;
|
|
@@ -776,7 +807,7 @@ function optimizeSelector(selector) {
|
|
|
776
807
|
const item = selector[0][i];
|
|
777
808
|
match = true;
|
|
778
809
|
for (j = 1; j < selector.length; j++) {
|
|
779
|
-
if (item
|
|
810
|
+
if (item !== selector[j][i]) {
|
|
780
811
|
match = false;
|
|
781
812
|
break;
|
|
782
813
|
}
|
|
@@ -788,7 +819,7 @@ function optimizeSelector(selector) {
|
|
|
788
819
|
}
|
|
789
820
|
while (optimized.length > 0) {
|
|
790
821
|
const last = optimized.at(-1);
|
|
791
|
-
if (last
|
|
822
|
+
if (last === " " || combinators.includes(last)) {
|
|
792
823
|
const all = new Array(Math.max(optimized.length - 1, 0));
|
|
793
824
|
let write = 0;
|
|
794
825
|
for (let read = 0; read < optimized.length - 1; read++) {
|
|
@@ -813,9 +844,9 @@ function optimizeSelector(selector) {
|
|
|
813
844
|
selector[i1][index] = all[index];
|
|
814
845
|
}
|
|
815
846
|
}
|
|
816
|
-
let reducible = optimized.length
|
|
817
|
-
if (optimized[0]
|
|
818
|
-
if (optimized[1]
|
|
847
|
+
let reducible = optimized.length === 1;
|
|
848
|
+
if (optimized[0] === "&") {
|
|
849
|
+
if (optimized[1] === " ") {
|
|
819
850
|
const all = new Array(Math.max(optimized.length - 2, 0));
|
|
820
851
|
let write = 0;
|
|
821
852
|
for (let read = 2; read < optimized.length; read++) {
|
|
@@ -827,11 +858,11 @@ function optimizeSelector(selector) {
|
|
|
827
858
|
}
|
|
828
859
|
}
|
|
829
860
|
}
|
|
830
|
-
if (optimized.length
|
|
861
|
+
if (optimized.length === 0 || optimized[0].charAt(0) === "&" || selector.length === 1) {
|
|
831
862
|
return {
|
|
832
863
|
match: false,
|
|
833
864
|
optimized,
|
|
834
|
-
selector: selector.map((selector) => selector[0]
|
|
865
|
+
selector: selector.map((selector) => selector[0] === "&" && selector[1] === " " ? selector.slice(2) : selector),
|
|
835
866
|
reducible: selector.length > 1 && selector.every((selector) => !combinators.includes(selector[0])),
|
|
836
867
|
};
|
|
837
868
|
}
|
|
@@ -844,18 +875,18 @@ function optimizeSelector(selector) {
|
|
|
844
875
|
hasCompound = !["&"].concat(combinators).includes(curr[0].charAt(0));
|
|
845
876
|
}
|
|
846
877
|
// @ts-ignore
|
|
847
|
-
if (hasCompound && curr[0]
|
|
878
|
+
if (hasCompound && curr[0] === " ") {
|
|
848
879
|
hasCompound = false;
|
|
849
880
|
curr.unshift("&");
|
|
850
881
|
}
|
|
851
|
-
if (curr.length
|
|
882
|
+
if (curr.length === 0) {
|
|
852
883
|
curr.push("&");
|
|
853
884
|
hasCompound = false;
|
|
854
885
|
}
|
|
855
886
|
if (reducible) {
|
|
856
887
|
const chr = curr[0].charAt(0);
|
|
857
888
|
// @ts-ignore
|
|
858
|
-
reducible = chr
|
|
889
|
+
reducible = chr === "." || chr === ":" || isIdentStart(chr.charCodeAt(0));
|
|
859
890
|
}
|
|
860
891
|
acc.push(hasCompound ? ["&"].concat(curr) : curr);
|
|
861
892
|
return acc;
|
|
@@ -871,94 +902,102 @@ function optimizeSelector(selector) {
|
|
|
871
902
|
*/
|
|
872
903
|
function splitRule(buffer) {
|
|
873
904
|
const result = [[]];
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
905
|
+
const length = buffer.length;
|
|
906
|
+
let group = result[0];
|
|
907
|
+
// the start of the current part, -1 when it is empty: a part is a contiguous range of the buffer
|
|
908
|
+
let start = -1;
|
|
909
|
+
let code;
|
|
910
|
+
for (let i = 0; i < length; i++) {
|
|
911
|
+
code = buffer.charCodeAt(i);
|
|
912
|
+
if (isWhiteSpace(code)) {
|
|
913
|
+
if (start !== -1) {
|
|
914
|
+
group.push(buffer.slice(start, i));
|
|
915
|
+
start = -1;
|
|
882
916
|
}
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
// @ts-ignore
|
|
886
|
-
result.at(-1).push(" ");
|
|
917
|
+
if (group.length > 0) {
|
|
918
|
+
group.push(" ");
|
|
887
919
|
}
|
|
888
|
-
// i = k;
|
|
889
920
|
continue;
|
|
890
921
|
}
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
922
|
+
// ,
|
|
923
|
+
if (code === 0x2c) {
|
|
924
|
+
if (start !== -1) {
|
|
925
|
+
group.push(buffer.slice(start, i));
|
|
926
|
+
start = -1;
|
|
895
927
|
}
|
|
896
|
-
|
|
928
|
+
group = [];
|
|
929
|
+
result.push(group);
|
|
897
930
|
continue;
|
|
898
931
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
932
|
+
// .
|
|
933
|
+
if (code === 0x2e) {
|
|
934
|
+
if (start !== -1) {
|
|
935
|
+
group.push(buffer.slice(start, i));
|
|
903
936
|
}
|
|
904
|
-
|
|
937
|
+
start = i;
|
|
905
938
|
continue;
|
|
906
939
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
940
|
+
// the combinators: + > ~ | ||
|
|
941
|
+
if (code === 0x2b || code === 0x3e || code === 0x7e || code === 0x7c) {
|
|
942
|
+
if (start !== -1) {
|
|
943
|
+
group.push(buffer.slice(start, i));
|
|
944
|
+
start = -1;
|
|
911
945
|
}
|
|
912
|
-
if (
|
|
913
|
-
|
|
946
|
+
if (code === 0x7c && buffer.charCodeAt(i + 1) === 0x7c) {
|
|
947
|
+
group.push("||");
|
|
948
|
+
i++;
|
|
949
|
+
}
|
|
950
|
+
else {
|
|
951
|
+
group.push(code === 0x2b ? "+" : code === 0x3e ? ">" : code === 0x7e ? "~" : "|");
|
|
914
952
|
}
|
|
915
|
-
result.at(-1).push(chr);
|
|
916
953
|
continue;
|
|
917
954
|
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
955
|
+
// : ::
|
|
956
|
+
if (code === 0x3a) {
|
|
957
|
+
if (start !== -1) {
|
|
958
|
+
group.push(buffer.slice(start, i));
|
|
922
959
|
}
|
|
923
|
-
|
|
924
|
-
|
|
960
|
+
start = i;
|
|
961
|
+
if (buffer.charCodeAt(i + 1) === 0x3a) {
|
|
962
|
+
i++;
|
|
925
963
|
}
|
|
926
|
-
str += chr;
|
|
927
964
|
continue;
|
|
928
965
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
966
|
+
if (start === -1) {
|
|
967
|
+
start = i;
|
|
968
|
+
}
|
|
969
|
+
// \: the escaped character
|
|
970
|
+
if (code === 0x5c) {
|
|
971
|
+
i++;
|
|
932
972
|
continue;
|
|
933
973
|
}
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
const close =
|
|
937
|
-
let
|
|
974
|
+
// ( [: the part ends with the matching ) ]
|
|
975
|
+
if (code === 0x28 || code === 0x5b) {
|
|
976
|
+
const close = code === 0x28 ? 0x29 : 0x5d;
|
|
977
|
+
let depth = 1;
|
|
938
978
|
let k = i;
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
979
|
+
let c;
|
|
980
|
+
while (++k < length) {
|
|
981
|
+
c = buffer.charCodeAt(k);
|
|
982
|
+
if (c === 0x5c) {
|
|
943
983
|
k++;
|
|
944
984
|
continue;
|
|
945
985
|
}
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
inParens++;
|
|
986
|
+
if (c === code) {
|
|
987
|
+
depth++;
|
|
949
988
|
}
|
|
950
|
-
else if (
|
|
951
|
-
|
|
989
|
+
else if (c === close) {
|
|
990
|
+
depth--;
|
|
952
991
|
}
|
|
953
|
-
if (
|
|
992
|
+
if (depth === 0) {
|
|
954
993
|
break;
|
|
955
994
|
}
|
|
956
995
|
}
|
|
957
996
|
i = k;
|
|
958
997
|
}
|
|
959
998
|
}
|
|
960
|
-
if (
|
|
961
|
-
|
|
999
|
+
if (start !== -1) {
|
|
1000
|
+
group.push(buffer.slice(start, length));
|
|
962
1001
|
}
|
|
963
1002
|
return result;
|
|
964
1003
|
}
|
|
@@ -974,7 +1013,7 @@ function reduceSelector(acc, curr) {
|
|
|
974
1013
|
// @ts-ignore
|
|
975
1014
|
curr = curr.slice(this.match[0].length);
|
|
976
1015
|
while (curr.length > 0) {
|
|
977
|
-
if (curr[0]
|
|
1016
|
+
if (curr[0] === " ") {
|
|
978
1017
|
hasCompoundSelector = false;
|
|
979
1018
|
curr.unshift("&");
|
|
980
1019
|
continue;
|
|
@@ -984,16 +1023,16 @@ function reduceSelector(acc, curr) {
|
|
|
984
1023
|
if (hasCompoundSelector && curr.length > 0) {
|
|
985
1024
|
hasCompoundSelector = !["&"].concat(combinators).includes(curr[0].charAt(0));
|
|
986
1025
|
}
|
|
987
|
-
if (curr[0]
|
|
1026
|
+
if (curr[0] === ":is(") {
|
|
988
1027
|
let canReduce = true;
|
|
989
1028
|
const isCompound = curr.reduce((acc, token, index) => {
|
|
990
|
-
if (index
|
|
991
|
-
canReduce = curr[1]
|
|
1029
|
+
if (index === 0) {
|
|
1030
|
+
canReduce = curr[1] === "&";
|
|
992
1031
|
}
|
|
993
|
-
else if (token
|
|
994
|
-
else if (token
|
|
1032
|
+
else if (token === ")") ;
|
|
1033
|
+
else if (token === ",") {
|
|
995
1034
|
if (!canReduce) {
|
|
996
|
-
canReduce = curr[index + 1]
|
|
1035
|
+
canReduce = curr[index + 1] === "&";
|
|
997
1036
|
}
|
|
998
1037
|
acc.push([]);
|
|
999
1038
|
}
|
|
@@ -1017,7 +1056,7 @@ function reduceSelector(acc, curr) {
|
|
|
1017
1056
|
// @ts-ignore
|
|
1018
1057
|
this.match.length == 0
|
|
1019
1058
|
? ["&"]
|
|
1020
|
-
: hasCompoundSelector && curr[0]
|
|
1059
|
+
: hasCompoundSelector && curr[0] !== "&" && (curr.length === 0 || !combinators.includes(curr[0].charAt(0)))
|
|
1021
1060
|
? ["&"].concat(curr)
|
|
1022
1061
|
: curr);
|
|
1023
1062
|
return acc;
|
|
@@ -1072,7 +1111,7 @@ function matchSelectors(selector1, selector2) {
|
|
|
1072
1111
|
k = 0;
|
|
1073
1112
|
token = selector1[0][i];
|
|
1074
1113
|
for (; k < selector1.length; k++) {
|
|
1075
|
-
if (selector1[k][i]
|
|
1114
|
+
if (selector1[k][i] !== token) {
|
|
1076
1115
|
matching = false;
|
|
1077
1116
|
break;
|
|
1078
1117
|
}
|
|
@@ -1080,7 +1119,7 @@ function matchSelectors(selector1, selector2) {
|
|
|
1080
1119
|
if (matching) {
|
|
1081
1120
|
l = 0;
|
|
1082
1121
|
for (; l < selector2.length; l++) {
|
|
1083
|
-
if (selector2[l][i]
|
|
1122
|
+
if (selector2[l][i] !== token) {
|
|
1084
1123
|
matching = false;
|
|
1085
1124
|
break;
|
|
1086
1125
|
}
|
|
@@ -1095,20 +1134,20 @@ function matchSelectors(selector1, selector2) {
|
|
|
1095
1134
|
match.at(-1).push(token);
|
|
1096
1135
|
}
|
|
1097
1136
|
// invalid function
|
|
1098
|
-
if (matchFunction
|
|
1137
|
+
if (matchFunction !== 0 || inAttr !== 0) {
|
|
1099
1138
|
return null;
|
|
1100
1139
|
}
|
|
1101
1140
|
for (const part of match) {
|
|
1102
1141
|
while (part.length > 0) {
|
|
1103
1142
|
const token = part.at(-1);
|
|
1104
|
-
if (token
|
|
1143
|
+
if (token === " " || combinators.includes(token) || notEndingWith.includes(token.at(-1))) {
|
|
1105
1144
|
part.pop();
|
|
1106
1145
|
continue;
|
|
1107
1146
|
}
|
|
1108
1147
|
break;
|
|
1109
1148
|
}
|
|
1110
1149
|
}
|
|
1111
|
-
if (match.every((t) => t.length
|
|
1150
|
+
if (match.every((t) => t.length === 0)) {
|
|
1112
1151
|
return null;
|
|
1113
1152
|
}
|
|
1114
1153
|
if (eq([["&"]], match)) {
|
|
@@ -1136,17 +1175,17 @@ function matchSelectors(selector1, selector2) {
|
|
|
1136
1175
|
* @private
|
|
1137
1176
|
*/
|
|
1138
1177
|
function fixSelector(arena, node) {
|
|
1139
|
-
const sel = arena.
|
|
1178
|
+
const sel = arena.resolveString(arena.name[node]);
|
|
1140
1179
|
if (sel.includes("&")) {
|
|
1141
1180
|
const attributes = parseString(arena, sel);
|
|
1142
1181
|
// inlined: arena.walkValues(attributes)
|
|
1143
1182
|
for (const attr of new ValueWalker(arena, attributes)) {
|
|
1144
|
-
if (arena.dataTypes[attr]
|
|
1145
|
-
arena.
|
|
1183
|
+
if (arena.dataTypes[attr] === EnumToken.PseudoClassFuncTokenType &&
|
|
1184
|
+
arena.resolveString(arena.stringValue[attr]) === ":is") {
|
|
1146
1185
|
let child = arena.lastToken[attr];
|
|
1147
|
-
while (child
|
|
1186
|
+
while (child !== 0) {
|
|
1148
1187
|
const previous = arena.previousSibling[child];
|
|
1149
|
-
if (arena.dataTypes[child]
|
|
1188
|
+
if (arena.dataTypes[child] === EnumToken.NestingSelectorTokenType) {
|
|
1150
1189
|
arena.detach(child);
|
|
1151
1190
|
}
|
|
1152
1191
|
child = previous;
|
|
@@ -1174,20 +1213,23 @@ function wrapNodes(arena, previous, node, match, reducer) {
|
|
|
1174
1213
|
let pSel = match.selector1.reduce(reducer, []).join(",");
|
|
1175
1214
|
// @ts-ignore
|
|
1176
1215
|
let nSel = match.selector2.reduce(reducer, []).join(",");
|
|
1177
|
-
const wrapper = arena.
|
|
1216
|
+
const wrapper = arena.cloneNode(previous);
|
|
1178
1217
|
// inlined: arena.setSelector(wrapper, match.match.reduce(reducer, []).join(","))
|
|
1179
1218
|
// @ts-ignore
|
|
1180
1219
|
arena.name[wrapper] = arena.internString(match.match.reduce(reducer, []).join(","));
|
|
1181
|
-
arena.raw
|
|
1182
|
-
|
|
1220
|
+
arena.raw.set(wrapper, match.match.map((t) => t.slice()));
|
|
1221
|
+
// not copied by cloneNode()
|
|
1222
|
+
if (arena.optimized.get(wrapper) != null) {
|
|
1223
|
+
arena.optimized.set(wrapper, null);
|
|
1224
|
+
}
|
|
1183
1225
|
// inlined: arena.setTokens(wrapper)
|
|
1184
1226
|
arena.setTokenList(wrapper, []);
|
|
1185
1227
|
const parent = arena.parents[node];
|
|
1186
1228
|
// the wrapper takes the place of node
|
|
1187
1229
|
arena.insertBefore(node, wrapper);
|
|
1188
|
-
if (pSel
|
|
1230
|
+
if (pSel === "&" || pSel === "") {
|
|
1189
1231
|
arena.moveChildren(previous, wrapper);
|
|
1190
|
-
if (nSel
|
|
1232
|
+
if (nSel === "&" || nSel === "") {
|
|
1191
1233
|
arena.moveChildren(node, wrapper);
|
|
1192
1234
|
}
|
|
1193
1235
|
else {
|
|
@@ -1199,24 +1241,24 @@ function wrapNodes(arena, previous, node, match, reducer) {
|
|
|
1199
1241
|
arena.appendChild(wrapper, node);
|
|
1200
1242
|
}
|
|
1201
1243
|
// the nodes whose children were moved into the wrapper are dropped
|
|
1202
|
-
if (arena.parents[previous]
|
|
1244
|
+
if (arena.parents[previous] === parent) {
|
|
1203
1245
|
arena.detach(previous);
|
|
1204
1246
|
}
|
|
1205
|
-
if (arena.parents[node]
|
|
1247
|
+
if (arena.parents[node] === parent) {
|
|
1206
1248
|
arena.detach(node);
|
|
1207
1249
|
}
|
|
1208
1250
|
// update the selectors of the wrapped nodes
|
|
1209
|
-
if (arena.parents[previous]
|
|
1251
|
+
if (arena.parents[previous] === wrapper) {
|
|
1210
1252
|
// inlined: arena.setSelector(previous, pSel)
|
|
1211
1253
|
arena.name[previous] = arena.internString(pSel);
|
|
1212
|
-
arena.raw
|
|
1254
|
+
arena.raw.set(previous, match.selector1);
|
|
1213
1255
|
// inlined: arena.setTokens(previous)
|
|
1214
1256
|
arena.setTokenList(previous, []);
|
|
1215
1257
|
}
|
|
1216
|
-
if (arena.parents[node]
|
|
1258
|
+
if (arena.parents[node] === wrapper) {
|
|
1217
1259
|
// inlined: arena.setSelector(node, nSel)
|
|
1218
1260
|
arena.name[node] = arena.internString(nSel);
|
|
1219
|
-
arena.raw
|
|
1261
|
+
arena.raw.set(node, match.selector2);
|
|
1220
1262
|
// inlined: arena.setTokens(node)
|
|
1221
1263
|
arena.setTokenList(node, []);
|
|
1222
1264
|
}
|
|
@@ -1225,6 +1267,21 @@ function wrapNodes(arena, previous, node, match, reducer) {
|
|
|
1225
1267
|
arena.setTokenList(wrapper, []);
|
|
1226
1268
|
return wrapper;
|
|
1227
1269
|
}
|
|
1270
|
+
/**
|
|
1271
|
+
* true if a child of the node is an at-rule without a block or an `@apply` rule: `@apply --mixin;`,
|
|
1272
|
+
* `@apply --mixin { color: red }`
|
|
1273
|
+
* @param arena
|
|
1274
|
+
* @param node
|
|
1275
|
+
*/
|
|
1276
|
+
function hasAtRuleStatement(arena, node) {
|
|
1277
|
+
for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
1278
|
+
if (arena.dataTypes[child] === EnumToken.AtRuleNodeType &&
|
|
1279
|
+
(!(arena.flags[child] & EnumAstNodeStatus.HasChildren) || arena.stringEquals(arena.name[child], "apply"))) {
|
|
1280
|
+
return true;
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
return false;
|
|
1284
|
+
}
|
|
1228
1285
|
/**
|
|
1229
1286
|
* Diff nodes: move the declarations shared by both rules into a new rule
|
|
1230
1287
|
* @param arena
|
|
@@ -1236,6 +1293,11 @@ function wrapNodes(arena, previous, node, match, reducer) {
|
|
|
1236
1293
|
* @private
|
|
1237
1294
|
*/
|
|
1238
1295
|
function diff(arena, n1, n2, options, context) {
|
|
1296
|
+
// the declarations of a rule that applies a mixin are not moved: `@apply` inserts declarations at its position,
|
|
1297
|
+
// the declarations that follow it override them
|
|
1298
|
+
if (hasAtRuleStatement(arena, n1) || hasAtRuleStatement(arena, n2)) {
|
|
1299
|
+
return null;
|
|
1300
|
+
}
|
|
1239
1301
|
if (!("cache" in options)) {
|
|
1240
1302
|
options.cache = new Map();
|
|
1241
1303
|
}
|
|
@@ -1248,9 +1310,23 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1248
1310
|
node2 = t;
|
|
1249
1311
|
exchanged = true;
|
|
1250
1312
|
}
|
|
1251
|
-
const raw1 = arena.raw
|
|
1252
|
-
const raw2 = arena.raw
|
|
1253
|
-
if (raw1
|
|
1313
|
+
const raw1 = arena.raw.get(node1);
|
|
1314
|
+
const raw2 = arena.raw.get(node2);
|
|
1315
|
+
if (raw1 == null || raw2 == null) {
|
|
1316
|
+
// the selectors that are not split (see isSimpleSelector()): the vendor prefixes of the pseudo classes are
|
|
1317
|
+
// read from the code units of the selectors
|
|
1318
|
+
const prefixes1 = raw1 == null ? selectorPrefixes(arena, arena.name[node1]) : rawSelectorPrefixes(raw1);
|
|
1319
|
+
const prefixes2 = raw2 == null ? selectorPrefixes(arena, arena.name[node2]) : rawSelectorPrefixes(raw2);
|
|
1320
|
+
if (prefixes1.size !== prefixes2.size) {
|
|
1321
|
+
return null;
|
|
1322
|
+
}
|
|
1323
|
+
for (const prefix of prefixes1) {
|
|
1324
|
+
if (!prefixes2.has(prefix)) {
|
|
1325
|
+
return null;
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
else {
|
|
1254
1330
|
const prefixes1 = new Set();
|
|
1255
1331
|
const prefixes2 = new Set();
|
|
1256
1332
|
for (const token1 of raw1) {
|
|
@@ -1287,7 +1363,7 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1287
1363
|
break;
|
|
1288
1364
|
}
|
|
1289
1365
|
}
|
|
1290
|
-
if (prefixes1.size
|
|
1366
|
+
if (prefixes1.size !== prefixes2.size) {
|
|
1291
1367
|
return null;
|
|
1292
1368
|
}
|
|
1293
1369
|
for (const prefix of prefixes1) {
|
|
@@ -1298,16 +1374,16 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1298
1374
|
}
|
|
1299
1375
|
// match the declarations of both rules, from the last to the first
|
|
1300
1376
|
let matches = 0;
|
|
1301
|
-
for (let child1 = arena.lastChild[node1]; child1
|
|
1302
|
-
if (arena.dataTypes[child1]
|
|
1377
|
+
for (let child1 = arena.lastChild[node1]; child1 !== 0; child1 = arena.previousSibling[child1]) {
|
|
1378
|
+
if (arena.dataTypes[child1] === EnumToken.CommentNodeType) {
|
|
1303
1379
|
continue;
|
|
1304
1380
|
}
|
|
1305
|
-
for (let child2 = arena.lastChild[node2]; child2
|
|
1306
|
-
if (arena.dataTypes[child2]
|
|
1381
|
+
for (let child2 = arena.lastChild[node2]; child2 !== 0; child2 = arena.previousSibling[child2]) {
|
|
1382
|
+
if (arena.dataTypes[child2] === EnumToken.CommentNodeType ||
|
|
1307
1383
|
arena.flags[child2] & EnumAstNodeStatus.Matched) {
|
|
1308
1384
|
continue;
|
|
1309
1385
|
}
|
|
1310
|
-
if (arena.name[child1]
|
|
1386
|
+
if (arena.name[child1] === arena.name[child2] && eqNode(arena, child1, child2)) {
|
|
1311
1387
|
arena.flags[child1] |= EnumAstNodeStatus.Matched;
|
|
1312
1388
|
arena.flags[child2] |= EnumAstNodeStatus.Matched;
|
|
1313
1389
|
matches++;
|
|
@@ -1315,38 +1391,43 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1315
1391
|
}
|
|
1316
1392
|
}
|
|
1317
1393
|
}
|
|
1318
|
-
if (matches
|
|
1394
|
+
if (matches === 0 && (arena.firstChild[node1] !== 0 || arena.firstChild[node2] !== 0)) {
|
|
1319
1395
|
return null;
|
|
1320
1396
|
}
|
|
1321
1397
|
// the rules are rendered with the same options during the minification: the options are resolved once and
|
|
1322
1398
|
// identical values are rendered once
|
|
1323
1399
|
const renderer = (context.renderer ??= new NodeRenderer(options));
|
|
1324
1400
|
const renderedSize = (node, cache) => {
|
|
1325
|
-
let
|
|
1326
|
-
if (
|
|
1327
|
-
|
|
1401
|
+
let size = cache ? options.cache.get(node) : undefined;
|
|
1402
|
+
if (size === undefined) {
|
|
1403
|
+
size = renderer.size(arena, node);
|
|
1328
1404
|
if (cache) {
|
|
1329
|
-
options.cache.set(node,
|
|
1405
|
+
options.cache.set(node, size);
|
|
1330
1406
|
}
|
|
1331
1407
|
}
|
|
1332
|
-
return arena.firstChild[node]
|
|
1408
|
+
return arena.firstChild[node] === 0 ? 0 : size;
|
|
1333
1409
|
};
|
|
1334
1410
|
// size of the original nodes
|
|
1335
1411
|
const size = renderedSize(n1, true) + renderedSize(n2, true);
|
|
1336
1412
|
// the shared declarations are moved into the result node
|
|
1337
|
-
const result = arena.
|
|
1338
|
-
//
|
|
1339
|
-
arena.name[
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1413
|
+
const result = arena.cloneNode(node1);
|
|
1414
|
+
// the distinct selectors of both rules: built from the code units of the selectors when they are normalized
|
|
1415
|
+
let merged = mergeSelectors(arena, arena.name[node1], arena.name[node2]);
|
|
1416
|
+
if (merged === -1) {
|
|
1417
|
+
// inlined: arena.setSelector(result, [ ...new Set(splitRule(arena.resolveString(arena.name[node1])) .concat(splitRule(arena.resolveString(arena.name[node2]))) .map((s) => s.join(""))), ].join(","))
|
|
1418
|
+
merged = arena.internString([
|
|
1419
|
+
...new Set(splitRule(arena.resolveString(arena.name[node1]))
|
|
1420
|
+
.concat(splitRule(arena.resolveString(arena.name[node2])))
|
|
1421
|
+
.map((s) => s.join(""))),
|
|
1422
|
+
].join(","));
|
|
1423
|
+
}
|
|
1424
|
+
arena.name[result] = merged;
|
|
1344
1425
|
// the matched declarations of node1 are moved into the result, the duplicates in node2 are dropped.
|
|
1345
1426
|
// the moves are recorded (node, parent, next sibling) to be undone when the result is not smaller
|
|
1346
1427
|
const moves = [];
|
|
1347
1428
|
const moveMatched = (parent, target) => {
|
|
1348
1429
|
let child = arena.firstChild[parent];
|
|
1349
|
-
while (child
|
|
1430
|
+
while (child !== 0) {
|
|
1350
1431
|
const next = arena.nextSibling[child];
|
|
1351
1432
|
if (arena.flags[child] & EnumAstNodeStatus.Matched) {
|
|
1352
1433
|
arena.flags[child] &= ~EnumAstNodeStatus.Matched;
|
|
@@ -1366,12 +1447,15 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1366
1447
|
arena.insertBefore(n1, result);
|
|
1367
1448
|
options.cache.delete(node1);
|
|
1368
1449
|
options.cache.delete(node2);
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1450
|
+
// the merge is kept when the output is smaller, or when it has the same size and removes a rule (an emptied rule is
|
|
1451
|
+
// removed, its size is 0). the other rule keeps its selector, which is repeated in the merged selector: the merge of
|
|
1452
|
+
// `html{margin:0}` and `body,div,...{margin:0;font:inherit}` is larger than the rules
|
|
1453
|
+
const mergedSize = renderedSize(node1, false) + renderedSize(node2, false) + renderedSize(result, false);
|
|
1454
|
+
if (size < mergedSize ||
|
|
1455
|
+
(size === mergedSize && arena.firstChild[node1] !== 0 && arena.firstChild[node2] !== 0)) {
|
|
1372
1456
|
// restore the nodes: undo the moves in reverse order
|
|
1373
1457
|
for (let k = moves.length - 3; k >= 0; k -= 3) {
|
|
1374
|
-
if (moves[k + 2]
|
|
1458
|
+
if (moves[k + 2] === 0) {
|
|
1375
1459
|
arena.appendChild(moves[k + 1], moves[k]);
|
|
1376
1460
|
}
|
|
1377
1461
|
else {
|
|
@@ -1383,25 +1467,17 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1383
1467
|
}
|
|
1384
1468
|
// inlined: arena.setTokens(result)
|
|
1385
1469
|
arena.setTokenList(result, []);
|
|
1386
|
-
|
|
1387
|
-
arena.
|
|
1388
|
-
|
|
1470
|
+
// the raw and optimized selectors are not copied by cloneNode()
|
|
1471
|
+
if (arena.raw.get(result) != null) {
|
|
1472
|
+
arena.raw.set(result, null);
|
|
1473
|
+
}
|
|
1474
|
+
if (arena.optimized.get(result) != null) {
|
|
1475
|
+
arena.optimized.set(result, null);
|
|
1476
|
+
}
|
|
1477
|
+
const optimized = optimizeSelector(splitRule(arena.resolveString(arena.name[result])));
|
|
1389
1478
|
if (optimized?.match) {
|
|
1390
|
-
const
|
|
1391
|
-
|
|
1392
|
-
acc += ",";
|
|
1393
|
-
}
|
|
1394
|
-
if (curr.length > 2 && curr[0] === "&" && curr[1] === " ") {
|
|
1395
|
-
return acc + curr.slice(2).join("");
|
|
1396
|
-
}
|
|
1397
|
-
else if (curr.length > 1 && curr[0] === "&") {
|
|
1398
|
-
return acc + curr.slice(1).join("");
|
|
1399
|
-
}
|
|
1400
|
-
return acc + curr.join("");
|
|
1401
|
-
}, "");
|
|
1402
|
-
const match = optimized.optimized.join("");
|
|
1403
|
-
const sel = match + ":is(" + arena.replaceCompound(rule, match) + ")";
|
|
1404
|
-
if (sel.length < arena.strings[arena.name[result]].length) {
|
|
1479
|
+
const sel = factorSelector(optimized);
|
|
1480
|
+
if (sel != null && sel.length < arena.resolveString(arena.name[result]).length) {
|
|
1405
1481
|
// inlined: arena.setSelector(result, sel)
|
|
1406
1482
|
arena.name[result] = arena.internString(sel);
|
|
1407
1483
|
// inlined: arena.setTokens(result)
|
|
@@ -1420,42 +1496,543 @@ function diff(arena, n1, n2, options, context) {
|
|
|
1420
1496
|
* @private
|
|
1421
1497
|
*/
|
|
1422
1498
|
function eqNode(arena, a, b) {
|
|
1423
|
-
if (a
|
|
1499
|
+
if (a === b) {
|
|
1424
1500
|
return true;
|
|
1425
1501
|
}
|
|
1426
|
-
if (arena.dataTypes[a]
|
|
1427
|
-
arena.name[a]
|
|
1428
|
-
arena.stringValue[a]
|
|
1502
|
+
if (arena.dataTypes[a] !== arena.dataTypes[b] ||
|
|
1503
|
+
arena.name[a] !== arena.name[b] ||
|
|
1504
|
+
arena.stringValue[a] !== arena.stringValue[b]) {
|
|
1429
1505
|
return false;
|
|
1430
1506
|
}
|
|
1431
1507
|
if (arena.flags[a] & EnumAstNodeStatus.HasValues) {
|
|
1432
1508
|
let token1 = arena.firstToken[a];
|
|
1433
1509
|
let token2 = arena.firstToken[b];
|
|
1434
|
-
while (token1
|
|
1510
|
+
while (token1 !== 0 && token2 !== 0) {
|
|
1435
1511
|
if (!eqToken(arena, token1, token2)) {
|
|
1436
1512
|
return false;
|
|
1437
1513
|
}
|
|
1438
1514
|
token1 = arena.nextSibling[token1];
|
|
1439
1515
|
token2 = arena.nextSibling[token2];
|
|
1440
1516
|
}
|
|
1441
|
-
if (token1
|
|
1517
|
+
if (token1 !== token2) {
|
|
1442
1518
|
return false;
|
|
1443
1519
|
}
|
|
1444
1520
|
}
|
|
1445
1521
|
if (arena.flags[a] & EnumAstNodeStatus.HasChildren) {
|
|
1446
1522
|
let child1 = arena.firstChild[a];
|
|
1447
1523
|
let child2 = arena.firstChild[b];
|
|
1448
|
-
while (child1
|
|
1524
|
+
while (child1 !== 0 && child2 !== 0) {
|
|
1449
1525
|
if (!eqNode(arena, child1, child2)) {
|
|
1450
1526
|
return false;
|
|
1451
1527
|
}
|
|
1452
1528
|
child1 = arena.nextSibling[child1];
|
|
1453
1529
|
child2 = arena.nextSibling[child2];
|
|
1454
1530
|
}
|
|
1455
|
-
return child1
|
|
1531
|
+
return child1 === child2;
|
|
1532
|
+
}
|
|
1533
|
+
return true;
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* builder of the merged selectors, see {@link mergeSelectors}
|
|
1537
|
+
*/
|
|
1538
|
+
const mergedSelectors = new StringBuilder();
|
|
1539
|
+
/**
|
|
1540
|
+
* start and end of the selectors read by {@link selectorRanges}, in {@link mergedSelectors}
|
|
1541
|
+
*/
|
|
1542
|
+
let selectorBounds = new Int32Array(64);
|
|
1543
|
+
/**
|
|
1544
|
+
* append the comma separated selectors of a selector string to {@link mergedSelectors} and record their bounds. the
|
|
1545
|
+
* selectors are the selectors of splitRule() joined: false when the selector string is not normalized (whitespace
|
|
1546
|
+
* other than a single space between two parts, a space after a comma)
|
|
1547
|
+
* @param arena
|
|
1548
|
+
* @param id selector string id
|
|
1549
|
+
* @param count number of selectors recorded
|
|
1550
|
+
* @returns the number of selectors recorded, -1 when the selector string is not normalized
|
|
1551
|
+
*/
|
|
1552
|
+
function selectorRanges(arena, id, count) {
|
|
1553
|
+
const length = arena.stringLength(id);
|
|
1554
|
+
const offset = mergedSelectors.length;
|
|
1555
|
+
arena.appendString(mergedSelectors, id);
|
|
1556
|
+
const codes = mergedSelectors.codes;
|
|
1557
|
+
let start = offset;
|
|
1558
|
+
const add = (end) => {
|
|
1559
|
+
if ((count + 1) * 2 > selectorBounds.length) {
|
|
1560
|
+
const bounds = new Int32Array(selectorBounds.length * 2);
|
|
1561
|
+
bounds.set(selectorBounds);
|
|
1562
|
+
selectorBounds = bounds;
|
|
1563
|
+
}
|
|
1564
|
+
selectorBounds[count * 2] = start;
|
|
1565
|
+
selectorBounds[count * 2 + 1] = end;
|
|
1566
|
+
count++;
|
|
1567
|
+
};
|
|
1568
|
+
for (let i = offset; i < offset + length; i++) {
|
|
1569
|
+
const code = codes[i];
|
|
1570
|
+
if (isWhiteSpace(code)) {
|
|
1571
|
+
// a single space between two parts
|
|
1572
|
+
if (code !== 0x20 || i === start || isWhiteSpace(codes[i - 1])) {
|
|
1573
|
+
return -1;
|
|
1574
|
+
}
|
|
1575
|
+
continue;
|
|
1576
|
+
}
|
|
1577
|
+
if (code === 0x2c) {
|
|
1578
|
+
add(i);
|
|
1579
|
+
start = i + 1;
|
|
1580
|
+
continue;
|
|
1581
|
+
}
|
|
1582
|
+
if (code === 0x5c) {
|
|
1583
|
+
i++;
|
|
1584
|
+
continue;
|
|
1585
|
+
}
|
|
1586
|
+
// ( [: the part ends with the matching ) ]
|
|
1587
|
+
if (code === 0x28 || code === 0x5b) {
|
|
1588
|
+
const close = code === 0x28 ? 0x29 : 0x5d;
|
|
1589
|
+
let depth = 1;
|
|
1590
|
+
let k = i;
|
|
1591
|
+
let c;
|
|
1592
|
+
while (++k < offset + length) {
|
|
1593
|
+
c = codes[k];
|
|
1594
|
+
if (c === 0x5c) {
|
|
1595
|
+
k++;
|
|
1596
|
+
continue;
|
|
1597
|
+
}
|
|
1598
|
+
if (c === code) {
|
|
1599
|
+
depth++;
|
|
1600
|
+
}
|
|
1601
|
+
else if (c === close) {
|
|
1602
|
+
depth--;
|
|
1603
|
+
}
|
|
1604
|
+
if (depth === 0) {
|
|
1605
|
+
break;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
i = k;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
add(offset + length);
|
|
1612
|
+
return count;
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* the distinct selectors of two selector strings, comma separated, like
|
|
1616
|
+
* `[...new Set(splitRule(sel1).concat(splitRule(sel2)).map((s) => s.join("")))].join(",")`. the selectors are read
|
|
1617
|
+
* from the string table and the result is interned: no string is created
|
|
1618
|
+
* @param arena
|
|
1619
|
+
* @param id1
|
|
1620
|
+
* @param id2
|
|
1621
|
+
* @returns the string id, -1 when a selector string is not normalized (see {@link selectorRanges})
|
|
1622
|
+
*/
|
|
1623
|
+
function mergeSelectors(arena, id1, id2) {
|
|
1624
|
+
mergedSelectors.reset();
|
|
1625
|
+
let count = selectorRanges(arena, id1, 0);
|
|
1626
|
+
if (count === -1 || (count = selectorRanges(arena, id2, count)) === -1) {
|
|
1627
|
+
return -1;
|
|
1628
|
+
}
|
|
1629
|
+
const codes = mergedSelectors.codes;
|
|
1630
|
+
const end = mergedSelectors.length;
|
|
1631
|
+
// the result is built after the selectors
|
|
1632
|
+
for (let i = 0, written = 0; i < count; i++) {
|
|
1633
|
+
const start1 = selectorBounds[i * 2];
|
|
1634
|
+
const end1 = selectorBounds[i * 2 + 1];
|
|
1635
|
+
let duplicate = false;
|
|
1636
|
+
for (let k = 0; k < i && !duplicate; k++) {
|
|
1637
|
+
const start2 = selectorBounds[k * 2];
|
|
1638
|
+
const end2 = selectorBounds[k * 2 + 1];
|
|
1639
|
+
if (end2 - start2 === end1 - start1) {
|
|
1640
|
+
let j = 0;
|
|
1641
|
+
while (j < end1 - start1 && codes[start1 + j] === codes[start2 + j]) {
|
|
1642
|
+
j++;
|
|
1643
|
+
}
|
|
1644
|
+
duplicate = j === end1 - start1;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
if (duplicate) {
|
|
1648
|
+
continue;
|
|
1649
|
+
}
|
|
1650
|
+
if (written++ > 0) {
|
|
1651
|
+
mergedSelectors.appendCode(0x2c);
|
|
1652
|
+
}
|
|
1653
|
+
for (let j = start1; j < end1; j++) {
|
|
1654
|
+
// the builder may have been reallocated
|
|
1655
|
+
mergedSelectors.appendCode(mergedSelectors.codes[j]);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
// intern the result only
|
|
1659
|
+
const result = mergedSelectors.codes;
|
|
1660
|
+
return arena.internRange(result, end, mergedSelectors.length, -1 /* StringSource.Pool */);
|
|
1661
|
+
}
|
|
1662
|
+
/**
|
|
1663
|
+
* the split selector of a rule (see splitRule()): computed when it is not cached
|
|
1664
|
+
* @param arena
|
|
1665
|
+
* @param node
|
|
1666
|
+
*/
|
|
1667
|
+
function rawSelector(arena, node) {
|
|
1668
|
+
let raw = arena.raw.get(node);
|
|
1669
|
+
if (raw == null) {
|
|
1670
|
+
raw = splitRule(arena.resolveString(arena.name[node]));
|
|
1671
|
+
arena.raw.set(node, raw);
|
|
1672
|
+
}
|
|
1673
|
+
return raw;
|
|
1674
|
+
}
|
|
1675
|
+
/**
|
|
1676
|
+
* start and end of the first part of a selector (see splitRule()): the leading whitespace is
|
|
1677
|
+
* skipped. the end is -1 when the first selector is empty
|
|
1678
|
+
* @param arena
|
|
1679
|
+
* @param id selector string id
|
|
1680
|
+
* @param part
|
|
1681
|
+
*/
|
|
1682
|
+
function firstPartRange(arena, id, part) {
|
|
1683
|
+
// the code units are read until the end of the first part: the selector is not copied
|
|
1684
|
+
const length = arena.stringLength(id);
|
|
1685
|
+
let start = -1;
|
|
1686
|
+
let i = 0;
|
|
1687
|
+
part.end = -1;
|
|
1688
|
+
while (i < length && isWhiteSpace(arena.stringCodeAt(id, i))) {
|
|
1689
|
+
i++;
|
|
1690
|
+
}
|
|
1691
|
+
part.start = i;
|
|
1692
|
+
for (; i < length; i++) {
|
|
1693
|
+
const code = arena.stringCodeAt(id, i);
|
|
1694
|
+
if (isWhiteSpace(code)) {
|
|
1695
|
+
part.end = i;
|
|
1696
|
+
return;
|
|
1697
|
+
}
|
|
1698
|
+
// ,
|
|
1699
|
+
if (code === 0x2c) {
|
|
1700
|
+
part.end = start === -1 ? -1 : i;
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
// . :
|
|
1704
|
+
if (code === 0x2e || code === 0x3a) {
|
|
1705
|
+
if (start !== -1) {
|
|
1706
|
+
part.end = i;
|
|
1707
|
+
return;
|
|
1708
|
+
}
|
|
1709
|
+
start = i;
|
|
1710
|
+
if (code === 0x3a && arena.stringCodeAt(id, i + 1) === 0x3a) {
|
|
1711
|
+
i++;
|
|
1712
|
+
}
|
|
1713
|
+
continue;
|
|
1714
|
+
}
|
|
1715
|
+
// the combinators: a part
|
|
1716
|
+
if (code === 0x2b || code === 0x3e || code === 0x7e || code === 0x7c) {
|
|
1717
|
+
part.end = start !== -1 ? i : code === 0x7c && arena.stringCodeAt(id, i + 1) === 0x7c ? i + 2 : i + 1;
|
|
1718
|
+
return;
|
|
1719
|
+
}
|
|
1720
|
+
if (start === -1) {
|
|
1721
|
+
start = i;
|
|
1722
|
+
}
|
|
1723
|
+
// \: the escaped character
|
|
1724
|
+
if (code === 0x5c) {
|
|
1725
|
+
i++;
|
|
1726
|
+
continue;
|
|
1727
|
+
}
|
|
1728
|
+
// ( [: the part ends with the matching ) ]
|
|
1729
|
+
if (code === 0x28 || code === 0x5b) {
|
|
1730
|
+
const close = code === 0x28 ? 0x29 : 0x5d;
|
|
1731
|
+
let depth = 1;
|
|
1732
|
+
let k = i;
|
|
1733
|
+
let c;
|
|
1734
|
+
while (++k < length) {
|
|
1735
|
+
c = arena.stringCodeAt(id, k);
|
|
1736
|
+
if (c === 0x5c) {
|
|
1737
|
+
k++;
|
|
1738
|
+
continue;
|
|
1739
|
+
}
|
|
1740
|
+
if (c === code) {
|
|
1741
|
+
depth++;
|
|
1742
|
+
}
|
|
1743
|
+
else if (c === close) {
|
|
1744
|
+
depth--;
|
|
1745
|
+
}
|
|
1746
|
+
if (depth === 0) {
|
|
1747
|
+
break;
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
i = k;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
part.end = start === -1 ? -1 : Math.min(i, length);
|
|
1754
|
+
}
|
|
1755
|
+
const firstPart1 = { str: null, start: 0, end: 0 };
|
|
1756
|
+
const firstPart2 = { str: null, start: 0, end: 0 };
|
|
1757
|
+
/**
|
|
1758
|
+
* the first part of the first selector of a rule. false when the first selector is empty
|
|
1759
|
+
* @param arena
|
|
1760
|
+
* @param node
|
|
1761
|
+
* @param part
|
|
1762
|
+
*/
|
|
1763
|
+
function firstPart(arena, node, part) {
|
|
1764
|
+
const raw = arena.raw.get(node);
|
|
1765
|
+
if (raw != null) {
|
|
1766
|
+
// the split selector: the first part is a string
|
|
1767
|
+
part.str = raw.length > 0 && raw[0].length > 0 ? raw[0][0] : null;
|
|
1768
|
+
return part.str != null;
|
|
1769
|
+
}
|
|
1770
|
+
part.str = null;
|
|
1771
|
+
firstPartRange(arena, arena.name[node], part);
|
|
1772
|
+
return part.end !== -1;
|
|
1773
|
+
}
|
|
1774
|
+
/**
|
|
1775
|
+
* false when the selectors of two rules cannot match (see matchSelectors()): the first parts of their first selectors
|
|
1776
|
+
* are different. the selectors that are not split are read from the string table: they are not split
|
|
1777
|
+
* @param arena
|
|
1778
|
+
* @param node1
|
|
1779
|
+
* @param node2
|
|
1780
|
+
*/
|
|
1781
|
+
function mayMatchSelectors(arena, node1, node2) {
|
|
1782
|
+
if (!firstPart(arena, node1, firstPart1) || !firstPart(arena, node2, firstPart2)) {
|
|
1783
|
+
return false;
|
|
1784
|
+
}
|
|
1785
|
+
const id1 = arena.name[node1];
|
|
1786
|
+
const id2 = arena.name[node2];
|
|
1787
|
+
const str1 = firstPart1.str;
|
|
1788
|
+
const str2 = firstPart2.str;
|
|
1789
|
+
const length1 = str1 != null ? str1.length : firstPart1.end - firstPart1.start;
|
|
1790
|
+
const length2 = str2 != null ? str2.length : firstPart2.end - firstPart2.start;
|
|
1791
|
+
if (length1 !== length2) {
|
|
1792
|
+
return false;
|
|
1793
|
+
}
|
|
1794
|
+
for (let i = 0; i < length1; i++) {
|
|
1795
|
+
const code1 = str1 != null ? str1.charCodeAt(i) : arena.stringCodeAt(id1, firstPart1.start + i);
|
|
1796
|
+
const code2 = str2 != null ? str2.charCodeAt(i) : arena.stringCodeAt(id2, firstPart2.start + i);
|
|
1797
|
+
if (code1 !== code2) {
|
|
1798
|
+
return false;
|
|
1799
|
+
}
|
|
1456
1800
|
}
|
|
1457
1801
|
return true;
|
|
1458
1802
|
}
|
|
1803
|
+
/**
|
|
1804
|
+
* code units of the selector read by {@link selectorPrefixes}
|
|
1805
|
+
*/
|
|
1806
|
+
let selectorCodes = new Uint16Array(256);
|
|
1807
|
+
/**
|
|
1808
|
+
* copy the code units of a selector to {@link selectorCodes}
|
|
1809
|
+
* @param arena
|
|
1810
|
+
* @param id
|
|
1811
|
+
* @returns the number of code units
|
|
1812
|
+
*/
|
|
1813
|
+
function readSelector(arena, id) {
|
|
1814
|
+
const length = arena.stringLength(id);
|
|
1815
|
+
if (selectorCodes.length < length) {
|
|
1816
|
+
selectorCodes = new Uint16Array(length * 2);
|
|
1817
|
+
}
|
|
1818
|
+
return arena.copyString(id, selectorCodes, 0);
|
|
1819
|
+
}
|
|
1820
|
+
/**
|
|
1821
|
+
* true if a rule selector is not modified by {@link reduceRuleSelector}: a single selector (no comma), that does not
|
|
1822
|
+
* start with `&`, does not end with `)` (`:is()`), a combinator or a whitespace, and has no whitespace other than single
|
|
1823
|
+
* spaces. the selector is read from the string table, the result is computed once per string id
|
|
1824
|
+
* @param arena
|
|
1825
|
+
* @param id selector string id
|
|
1826
|
+
*/
|
|
1827
|
+
function isSimpleSelector(arena, id) {
|
|
1828
|
+
selectorArena = arena;
|
|
1829
|
+
return arena.stringFlagId(id, 1 /* StringFlag.SimpleSelector */, computeSimpleSelector);
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* the arena of the selector read by {@link computeSimpleSelector} and {@link hasVendorPrefix}: the functions passed to
|
|
1833
|
+
* the string table are not closures (no allocation per call)
|
|
1834
|
+
*/
|
|
1835
|
+
let selectorArena = null;
|
|
1836
|
+
/**
|
|
1837
|
+
* see {@link isSimpleSelector}
|
|
1838
|
+
* @param id selector string id
|
|
1839
|
+
*/
|
|
1840
|
+
function computeSimpleSelector(id) {
|
|
1841
|
+
{
|
|
1842
|
+
const length = readSelector(selectorArena, id);
|
|
1843
|
+
const codes = selectorCodes;
|
|
1844
|
+
if (length === 0 || codes[0] === 0x26) {
|
|
1845
|
+
return false;
|
|
1846
|
+
}
|
|
1847
|
+
let previous = 0;
|
|
1848
|
+
for (let i = 0; i < length; i++) {
|
|
1849
|
+
const code = codes[i];
|
|
1850
|
+
if (code === 0x2c) {
|
|
1851
|
+
return false;
|
|
1852
|
+
}
|
|
1853
|
+
if (isWhiteSpace(code)) {
|
|
1854
|
+
// a single space between two parts
|
|
1855
|
+
if (code !== 0x20 || i === 0 || isWhiteSpace(previous)) {
|
|
1856
|
+
return false;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
previous = code;
|
|
1860
|
+
}
|
|
1861
|
+
// ) + > ~ | and the whitespace
|
|
1862
|
+
return !(previous === 0x29 ||
|
|
1863
|
+
previous === 0x2b ||
|
|
1864
|
+
previous === 0x3e ||
|
|
1865
|
+
previous === 0x7e ||
|
|
1866
|
+
previous === 0x7c ||
|
|
1867
|
+
isWhiteSpace(previous));
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
/**
|
|
1871
|
+
* the first vendor prefix of a part of a selector: `::?-([a-z]+)-`, null when there is none
|
|
1872
|
+
* @param codes
|
|
1873
|
+
* @param start
|
|
1874
|
+
* @param end
|
|
1875
|
+
*/
|
|
1876
|
+
function partPrefix(codes, start, end) {
|
|
1877
|
+
for (let p = start; p < end; p++) {
|
|
1878
|
+
if (codes[p] !== 0x3a) {
|
|
1879
|
+
continue;
|
|
1880
|
+
}
|
|
1881
|
+
let q = p + 1;
|
|
1882
|
+
if (q < end && codes[q] === 0x3a) {
|
|
1883
|
+
q++;
|
|
1884
|
+
}
|
|
1885
|
+
if (q >= end || codes[q] !== 0x2d) {
|
|
1886
|
+
continue;
|
|
1887
|
+
}
|
|
1888
|
+
let k = q + 1;
|
|
1889
|
+
while (k < end && codes[k] >= 0x61 && codes[k] <= 0x7a) {
|
|
1890
|
+
k++;
|
|
1891
|
+
}
|
|
1892
|
+
if (k > q + 1 && k < end && codes[k] === 0x2d) {
|
|
1893
|
+
return String.fromCharCode.apply(null, codes.subarray(q + 1, k));
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
return null;
|
|
1897
|
+
}
|
|
1898
|
+
/**
|
|
1899
|
+
* no vendor prefix. read only
|
|
1900
|
+
*/
|
|
1901
|
+
const emptyPrefixes = new Set();
|
|
1902
|
+
/**
|
|
1903
|
+
* true if a selector contains `:-` (`::?-([a-z]+)-` matches `:-`)
|
|
1904
|
+
* @param id
|
|
1905
|
+
*/
|
|
1906
|
+
function hasVendorPrefix(id) {
|
|
1907
|
+
const length = readSelector(selectorArena, id);
|
|
1908
|
+
for (let i = 0; i + 1 < length; i++) {
|
|
1909
|
+
if (selectorCodes[i] === 0x3a && selectorCodes[i + 1] === 0x2d) {
|
|
1910
|
+
return true;
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
return false;
|
|
1914
|
+
}
|
|
1915
|
+
/**
|
|
1916
|
+
* the vendor prefixes of the pseudo classes of a selector, like the prefixes read from the parts of the split selector
|
|
1917
|
+
* in diff(): the parts are the parts of splitRule(), the first prefix of each part that contains `:`. the selector is
|
|
1918
|
+
* read from the string table, it is not split into strings
|
|
1919
|
+
* @param arena
|
|
1920
|
+
* @param id selector string id
|
|
1921
|
+
*/
|
|
1922
|
+
function selectorPrefixes(arena, id) {
|
|
1923
|
+
// the selectors without `:-` are skipped: computed once per string id
|
|
1924
|
+
selectorArena = arena;
|
|
1925
|
+
if (!arena.stringFlagId(id, 2 /* StringFlag.VendorPrefix */, hasVendorPrefix)) {
|
|
1926
|
+
return emptyPrefixes;
|
|
1927
|
+
}
|
|
1928
|
+
const prefixes = new Set();
|
|
1929
|
+
const length = readSelector(arena, id);
|
|
1930
|
+
const codes = selectorCodes;
|
|
1931
|
+
// the part boundaries of splitRule()
|
|
1932
|
+
let start = -1;
|
|
1933
|
+
let code;
|
|
1934
|
+
const addPart = (end) => {
|
|
1935
|
+
for (let k = start; k < end; k++) {
|
|
1936
|
+
if (codes[k] === 0x3a) {
|
|
1937
|
+
const prefix = partPrefix(codes, start, end);
|
|
1938
|
+
if (prefix != null) {
|
|
1939
|
+
prefixes.add(prefix);
|
|
1940
|
+
}
|
|
1941
|
+
break;
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
return prefixes.size > 1;
|
|
1945
|
+
};
|
|
1946
|
+
for (let i = 0; i < length; i++) {
|
|
1947
|
+
code = codes[i];
|
|
1948
|
+
if (isWhiteSpace(code) || code === 0x2c) {
|
|
1949
|
+
if (start !== -1 && addPart(i)) {
|
|
1950
|
+
return prefixes;
|
|
1951
|
+
}
|
|
1952
|
+
start = -1;
|
|
1953
|
+
continue;
|
|
1954
|
+
}
|
|
1955
|
+
if (code === 0x2e || code === 0x3a) {
|
|
1956
|
+
if (start !== -1 && addPart(i)) {
|
|
1957
|
+
return prefixes;
|
|
1958
|
+
}
|
|
1959
|
+
start = i;
|
|
1960
|
+
if (code === 0x3a && codes[i + 1] === 0x3a) {
|
|
1961
|
+
i++;
|
|
1962
|
+
}
|
|
1963
|
+
continue;
|
|
1964
|
+
}
|
|
1965
|
+
if (code === 0x2b || code === 0x3e || code === 0x7e || code === 0x7c) {
|
|
1966
|
+
if (start !== -1 && addPart(i)) {
|
|
1967
|
+
return prefixes;
|
|
1968
|
+
}
|
|
1969
|
+
start = -1;
|
|
1970
|
+
if (code === 0x7c && codes[i + 1] === 0x7c) {
|
|
1971
|
+
i++;
|
|
1972
|
+
}
|
|
1973
|
+
continue;
|
|
1974
|
+
}
|
|
1975
|
+
if (start === -1) {
|
|
1976
|
+
start = i;
|
|
1977
|
+
}
|
|
1978
|
+
if (code === 0x5c) {
|
|
1979
|
+
i++;
|
|
1980
|
+
continue;
|
|
1981
|
+
}
|
|
1982
|
+
if (code === 0x28 || code === 0x5b) {
|
|
1983
|
+
const close = code === 0x28 ? 0x29 : 0x5d;
|
|
1984
|
+
let depth = 1;
|
|
1985
|
+
let k = i;
|
|
1986
|
+
let c;
|
|
1987
|
+
while (++k < length) {
|
|
1988
|
+
c = codes[k];
|
|
1989
|
+
if (c === 0x5c) {
|
|
1990
|
+
k++;
|
|
1991
|
+
continue;
|
|
1992
|
+
}
|
|
1993
|
+
if (c === code) {
|
|
1994
|
+
depth++;
|
|
1995
|
+
}
|
|
1996
|
+
else if (c === close) {
|
|
1997
|
+
depth--;
|
|
1998
|
+
}
|
|
1999
|
+
if (depth === 0) {
|
|
2000
|
+
break;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
i = k;
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
if (start !== -1) {
|
|
2007
|
+
addPart(length);
|
|
2008
|
+
}
|
|
2009
|
+
return prefixes;
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* the vendor prefixes of the pseudo classes of a split selector, see diff()
|
|
2013
|
+
* @param raw
|
|
2014
|
+
*/
|
|
2015
|
+
function rawSelectorPrefixes(raw) {
|
|
2016
|
+
const prefixes = new Set();
|
|
2017
|
+
for (const token of raw) {
|
|
2018
|
+
for (const t of token) {
|
|
2019
|
+
if (t.includes(":")) {
|
|
2020
|
+
const matches = t.match(/::?-([a-z]+)-/);
|
|
2021
|
+
if (matches == null) {
|
|
2022
|
+
continue;
|
|
2023
|
+
}
|
|
2024
|
+
prefixes.add(matches[1]);
|
|
2025
|
+
if (prefixes.size > 1) {
|
|
2026
|
+
break;
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
if (prefixes.size > 1) {
|
|
2031
|
+
break;
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
return prefixes;
|
|
2035
|
+
}
|
|
1459
2036
|
/**
|
|
1460
2037
|
* Reduce rule selector
|
|
1461
2038
|
* @param arena
|
|
@@ -1464,20 +2041,21 @@ function eqNode(arena, a, b) {
|
|
|
1464
2041
|
* @private
|
|
1465
2042
|
*/
|
|
1466
2043
|
function reduceRuleSelector(arena, node) {
|
|
1467
|
-
if (arena.raw
|
|
1468
|
-
arena.raw
|
|
2044
|
+
if (arena.raw.get(node) == null) {
|
|
2045
|
+
arena.raw.set(node, splitRule(arena.resolveString(arena.name[node])));
|
|
1469
2046
|
}
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
2047
|
+
const raw = arena.raw.get(node);
|
|
2048
|
+
const singleSelectorWithoutIs = raw.length === 1 && (raw[0].at(-1) == null || !raw[0].at(-1).startsWith(":is("));
|
|
2049
|
+
let optimized = optimizeSelector(singleSelectorWithoutIs
|
|
2050
|
+
? raw
|
|
2051
|
+
: raw.map((curr) => curr.slice()));
|
|
1474
2052
|
if (optimized != null) {
|
|
1475
|
-
arena.optimized
|
|
2053
|
+
arena.optimized.set(node, optimized);
|
|
1476
2054
|
}
|
|
1477
2055
|
if (optimized != null && optimized.match && optimized.reducible && optimized.selector.length > 1) {
|
|
1478
2056
|
for (const selector of optimized.selector) {
|
|
1479
2057
|
if (selector.length > 1 &&
|
|
1480
|
-
selector[0]
|
|
2058
|
+
selector[0] === "&" &&
|
|
1481
2059
|
(combinators.includes(selector[1]) || !/^[a-zA-Z:]/.test(selector[1]))) {
|
|
1482
2060
|
const all = new Array(Math.max(selector.length - 1, 0));
|
|
1483
2061
|
let write = 0;
|
|
@@ -1508,10 +2086,10 @@ function reduceRuleSelector(arena, node) {
|
|
|
1508
2086
|
[optimized.optimized[0], reduced.length === 1 ? reduced.join("") : ":is("].concat(reduced).concat(")"),
|
|
1509
2087
|
];
|
|
1510
2088
|
const sel = raw[0].join("");
|
|
1511
|
-
if (sel.length < arena.
|
|
2089
|
+
if (sel.length < arena.resolveString(arena.name[node]).length) {
|
|
1512
2090
|
// inlined: arena.setSelector(node, sel)
|
|
1513
2091
|
arena.name[node] = arena.internString(sel);
|
|
1514
|
-
arena.raw
|
|
2092
|
+
arena.raw.set(node, raw);
|
|
1515
2093
|
// inlined: arena.setTokens(node)
|
|
1516
2094
|
arena.setTokenList(node, []);
|
|
1517
2095
|
}
|