Skip to content

Commit ed7a439

Browse files
fix(css): parenthesize a lowered media interval after a commented or and a not
Read an or whose comment is kept as one, wrap an interval a not precedes, and leave a nested group, whose bounds are no interval's, as written.
1 parent e72b401 commit ed7a439

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11261,9 +11261,10 @@ const _RANGE_CONDITION_RE = /^\(([-\w]+)(>=|<=|>|<)([^()<>]+)\)$/;
1126111261

1126211262
// The two shapes the range spelling prints: `(<feature><comparison><value>)` and
1126311263
// `(<value><comparison><feature><comparison><value>)`. A bound holds no
11264-
// comparison, so the greedy runs stop at the next one.
11264+
// comparison, so the greedy runs stop at the next one; nor opens a nested group.
1126511265
const _RANGE_FEATURE_RE = /^\(([-\w]+)(>=|<=|=)([^<>=]+)\)$/;
11266-
const _RANGE_INTERVAL_RE = /^\(([^<>=]+)(<=|>=)([-\w]+)(<=|>=)([^<>=]+)\)$/;
11266+
const _RANGE_INTERVAL_RE =
11267+
/^\(([^<>=(][^<>=]*)(<=|>=)([-\w]+)(<=|>=)([^<>=]+)\)$/;
1126711268

1126811269
/**
1126911270
* The `min-` / `max-` condition one non-strict comparison says: `width>=X` is
@@ -11290,8 +11291,8 @@ const _minMaxCondition = (feature, comparison, value) =>
1129011291
* read Media Queries 4 range syntax.
1129111292
*
1129211293
* An interval becomes an `and` of its two bounds, which is a conjunction — so it
11293-
* is parenthesized where the prelude joins with `or`, whose mixed nesting would
11294-
* otherwise be a parse error. A prelude that holds an `or` is already Media
11294+
* is parenthesized where the prelude joins with `or`, or after a `not`, which
11295+
* would otherwise negate the first bound alone. A prelude that holds an `or` is already Media
1129511296
* Queries 4, so the parentheses cost that query nothing.
1129611297
* @param {string[]} parts the prelude's printed parts
1129711298
* @returns {void}
@@ -11302,10 +11303,11 @@ const _useMinMaxSpelling = (parts) => {
1130211303
let disjunction = false;
1130311304
for (let i = 0; i < parts.length; i++) {
1130411305
if (parts[i].length === 0 || parts[i] === _SEP) continue;
11305-
if (equalsLowerCase(parts[i], "or")) disjunction = true;
11306+
if (_queryKeyword(parts[i]) === "or") disjunction = true;
1130611307
filled.push(i);
1130711308
}
11308-
for (const at of filled) {
11309+
for (let index = 0; index < filled.length; index++) {
11310+
const at = filled[index];
1130911311
const one = _RANGE_FEATURE_RE.exec(parts[at]);
1131011312
if (one !== null) {
1131111313
parts[at] = _minMaxCondition(one[1], one[2], one[3]);
@@ -11328,7 +11330,9 @@ const _useMinMaxSpelling = (parts) => {
1132811330
interval[5]
1132911331
);
1133011332
const pair = `${low} and ${high}`;
11331-
parts[at] = disjunction ? `(${pair})` : pair;
11333+
const negated =
11334+
index > 0 && _queryKeyword(parts[filled[index - 1]]) === "not";
11335+
parts[at] = disjunction || negated ? `(${pair})` : pair;
1133211336
}
1133311337
};
1133411338

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9617,6 +9617,26 @@ describe("CssSyntax minify — lowering a spelling the target cannot read", () =
96179617
);
96189618
});
96199619

9620+
it.each([
9621+
// an `or` whose comment is kept, and a `not`, which would negate one bound
9622+
[
9623+
"(color) or/**/(1px<=width<=2px)",
9624+
"(color) or/**/((min-width:1px) and (max-width:2px))"
9625+
],
9626+
["not (1px<=width<=2px)", "not ((min-width:1px) and (max-width:2px))"],
9627+
["not/**/(1px<=width<=2px)", "not/**/((min-width:1px) and (max-width:2px))"],
9628+
[
9629+
"screen and not (1px<=width<=2px)",
9630+
"screen and not ((min-width:1px) and (max-width:2px))"
9631+
],
9632+
// a nested group, whose bounds are no interval's
9633+
["((1px<=width<=2px))", "((1px<=width<=2px))"]
9634+
])("writes the interval in `@media %s` as %s", (query, expected) => {
9635+
expect(minifyFor(`@media ${query}{a{color:red}}`, ["chrome 100"])).toBe(
9636+
`@media ${expected}{a{color:red}}`
9637+
);
9638+
});
9639+
96209640
it("leaves a comparison with no `min-` / `max-` equivalent", () => {
96219641
// A strict bound is only `not (max-width:…)`, which is Media Queries 4 as
96229642
// much as the range spelling it would replace.

0 commit comments

Comments
 (0)