Skip to content

Commit 40c5d0f

Browse files
perf(css): read ahead the @Custom-Media rules in one walk
Order the rules so each follows the names its query reads, keeping the last rule stating a name, and walk them once instead of once per step of a chain.
1 parent eeecde4 commit 40c5d0f

2 files changed

Lines changed: 82 additions & 17 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15606,15 +15606,24 @@ let _customMediaSeed = null;
1560615606
let _collectingCustomMedia = false;
1560715607
const _CUSTOM_MEDIA_NAMES = new Set(["custom-media"]);
1560815608

15609+
/**
15610+
* One `@custom-media` rule a source writes: the name it states and the names
15611+
* its query is read through, unescaped as written, and the rule as a statement.
15612+
* @typedef {object} CustomMediaRule
15613+
* @property {string} name the name it states, or "" where it states none
15614+
* @property {string[]} references the names its query reads
15615+
* @property {string} text the rule, ending in `;`
15616+
*/
15617+
1560915618
/**
1561015619
* The `@custom-media` rules a source writes, each as its own statement, in order.
1561115620
* One opening a block is no such rule, and is left out.
1561215621
* @param {string} input the source
15613-
* @returns {string[]} the rules
15622+
* @returns {CustomMediaRule[]} the rules
1561415623
*/
1561515624
const _customMediaRules = (input) => {
1561615625
const token = createToken();
15617-
/** @type {string[]} */
15626+
/** @type {CustomMediaRule[]} */
1561815627
const out = [];
1561915628
let pos = 0;
1562015629
for (;;) {
@@ -15630,10 +15639,18 @@ const _customMediaRules = (input) => {
1563015639
const start = found.start;
1563115640
let end = input.length;
1563215641
let depth = 0;
15642+
let name = "";
15643+
/** @type {string[]} */
15644+
const references = [];
1563315645
for (;;) {
1563415646
const next = readToken(input, pos, token);
1563515647
if (next === undefined) break;
1563615648
const type = next.type;
15649+
if (type === TT_IDENTIFIER && input.startsWith("--", next.start)) {
15650+
const named = unescapeIdentifier(input.slice(next.start, next.end));
15651+
if (depth > 0) references.push(named);
15652+
else if (name === "") name = named;
15653+
}
1563715654
if (
1563815655
type === TT_LEFT_PARENTHESIS ||
1563915656
type === TT_LEFT_SQUARE_BRACKET ||
@@ -15656,7 +15673,42 @@ const _customMediaRules = (input) => {
1565615673
}
1565715674
pos = next.end;
1565815675
}
15659-
if (end !== -1) out.push(`${input.slice(start, end)};`);
15676+
if (end !== -1) {
15677+
out.push({ name, references, text: `${input.slice(start, end)};` });
15678+
}
15679+
}
15680+
return out;
15681+
};
15682+
15683+
/**
15684+
* The rules a name is stated by last, each after the ones its query reads, so a
15685+
* single walk has every name it is read through down before it: the last rule
15686+
* stating a name holds, and a cycle is left in the order it was written.
15687+
* @param {CustomMediaRule[]} rules the rules, in source order
15688+
* @returns {string} the rules to walk, as one source
15689+
*/
15690+
const _orderCustomMediaRules = (rules) => {
15691+
/** @type {Map<string, CustomMediaRule>} */
15692+
const last = new Map();
15693+
for (const rule of rules) if (rule.name !== "") last.set(rule.name, rule);
15694+
/** @type {Set<CustomMediaRule>} */
15695+
const visited = new Set();
15696+
let out = "";
15697+
/**
15698+
* @param {CustomMediaRule} rule a rule to write after what it reads
15699+
* @returns {void}
15700+
*/
15701+
const visit = (rule) => {
15702+
if (visited.has(rule)) return;
15703+
visited.add(rule);
15704+
for (const name of rule.references) {
15705+
const read = last.get(name);
15706+
if (read !== undefined) visit(read);
15707+
}
15708+
out += rule.text;
15709+
};
15710+
for (const rule of rules) {
15711+
if (rule.name === "" || last.get(rule.name) === rule) visit(rule);
1566015712
}
1566115713
return out;
1566215714
};
@@ -15672,29 +15724,22 @@ const _customMediaRules = (input) => {
1567215724
const _readCustomMediaAhead = (input, writer) => {
1567315725
const rules = _customMediaRules(input);
1567415726
if (rules.length === 0) return;
15675-
const source = rules.join("");
1567615727
const Context =
1567715728
/** @type {new (options: PrintContext["options"], printer: PrintContext["_printer"]) => PrintContext} */ (
1567815729
writer.constructor
1567915730
);
15680-
/** @type {Map<string, string> | null} */
15681-
let seed = null;
15682-
let previous = "";
1568315731
_collectingCustomMedia = true;
1568415732
try {
15685-
// Each pass resolves one more step of a name read through another.
15686-
for (let pass = 0; pass <= rules.length; pass++) {
15687-
_customMediaSeed = seed;
15688-
grammar(source, [], new Context(writer.options, writer._printer), {});
15689-
seed = new Map(_customMedia);
15690-
const read = [...seed].join("\n");
15691-
if (read === previous) break;
15692-
previous = read;
15693-
}
15733+
grammar(
15734+
_orderCustomMediaRules(rules),
15735+
[],
15736+
new Context(writer.options, writer._printer),
15737+
{}
15738+
);
1569415739
} finally {
1569515740
_collectingCustomMedia = false;
1569615741
}
15697-
const names = /** @type {Map<string, string>} */ (seed);
15742+
const names = new Map(_customMedia);
1569815743
// A name still read through another is on a cycle, which stands for nothing.
1569915744
/** @type {string[]} */
1570015745
const cyclic = [];

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12062,6 +12062,26 @@ describe("CssSyntax minify — `@custom-media` / `@custom-selector`", () => {
1206212062
"@custom-media --b (--a) and (hover);@custom-media --a (width>1px);@media (--b){a{color:red}}"
1206312063
)
1206412064
).toBe("@media (width>1px) and (hover){a{color:red}}");
12065+
// Each read through the next, written last to first.
12066+
expect(
12067+
resolve(
12068+
"@custom-media --c (--b) and (x:1);@custom-media --b (--a) and (y:1);@custom-media --a (width>1px);@media (--c){a{color:red}}"
12069+
)
12070+
).toBe("@media (width>1px) and (y:1) and (x:1){a{color:red}}");
12071+
// Through the last of two rules stating it.
12072+
expect(
12073+
resolve(
12074+
"@custom-media --b (--a);@custom-media --a (x:1);@custom-media --a (y:1);@media (--b){a{color:red}}"
12075+
)
12076+
).toBe("@media (y:1){a{color:red}}");
12077+
});
12078+
12079+
it("reads past a rule stating no name", () => {
12080+
expect(
12081+
resolve(
12082+
"@media (--m){a{color:red}}@custom-media (x:1);@custom-media --m (y:1);"
12083+
)
12084+
).toBe("@media (y:1){a{color:red}}@custom-media (x:1);");
1206512085
});
1206612086

1206712087
it("leaves names on a cycle as written", () => {

0 commit comments

Comments
 (0)