@@ -15606,15 +15606,24 @@ let _customMediaSeed = null;
1560615606let _collectingCustomMedia = false;
1560715607const _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 */
1561515624const _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) => {
1567215724const _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 = [];
0 commit comments