Skip to content

Commit fde9966

Browse files
fix(css): keep a @supports keyword unspaced where the source was
WebKit reads a @supports and/or with no whitespace before it as invalid, so writing a space where only a comment stood made it read a rule it drops. Also restore the saved media flag on leaving an @import's supports().
1 parent 5b9d080 commit fde9966

6 files changed

Lines changed: 15 additions & 71 deletions

File tree

‎lib/css/syntax-parser.js‎

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4368,6 +4368,7 @@ const _walkValue = (node, parent, index, writer) => {
43684368
let enteredMath = false;
43694369
let enteredStepped = false;
43704370
let enteredSupports = false;
4371+
let previousMedia = false;
43714372
if (!skip && (ty === T_FUNCTION || ty === T_SIMPLE_BLOCK)) {
43724373
const i0 = _nodeIndex(node);
43734374
const vs = _listStarts[i0];
@@ -4407,6 +4408,7 @@ const _walkValue = (node, parent, index, writer) => {
44074408
_isImportSupports(i0, /** @type {Node} */ (parent))
44084409
) {
44094410
enteredSupports = true;
4411+
previousMedia = _S._inMediaConditionPrelude;
44104412
_S._inSupportsPrelude = true;
44114413
// Its `(…)` tests a declaration, which no media feature spelling reaches.
44124414
_S._inMediaConditionPrelude = false;
@@ -4421,7 +4423,7 @@ const _walkValue = (node, parent, index, writer) => {
44214423
if (enteredStepped) _S._steppedFunctionDepth--;
44224424
if (enteredSupports) {
44234425
_S._inSupportsPrelude = false;
4424-
_S._inMediaConditionPrelude = true;
4426+
_S._inMediaConditionPrelude = previousMedia;
44254427
}
44264428
};
44274429

@@ -10668,43 +10670,6 @@ const _writeCustomMedia = (parts) => {
1066810670
}
1066910671
};
1067010672

10671-
// WHY: CSS Conditional 3 as first published asked for whitespace on both sides
10672-
// of a `@supports` `and` / `or` (`S+ AND S+`), and engines built on it read
10673-
// `(a)and (b)` as invalid. So where a dropped comment was all that parted a `)`
10674-
// from one, a space is written back; the one after is `_join`'s already.
10675-
/**
10676-
* @param {string[]} parts the condition's printed parts, one per child from `offset`
10677-
* @param {Node} node the node whose children they are
10678-
* @param {number} offset the index of its first child's part
10679-
* @returns {void}
10680-
*/
10681-
const _spaceConditionKeywords = (parts, node, offset) => {
10682-
const i0 = _nodeIndex(node);
10683-
const first = _listStarts[i0];
10684-
const count = _listLens[i0];
10685-
// The child before, or -1 once whitespace has parted it from the next.
10686-
let previous = -1;
10687-
for (let k = 0; k < count; k++) {
10688-
const child = _flat[first + k];
10689-
if (_types[child] === T_WHITESPACE) {
10690-
previous = -1;
10691-
continue;
10692-
}
10693-
const part = parts[offset + k];
10694-
if (
10695-
previous !== -1 &&
10696-
_ends[_flat[first + previous]] !== _starts[child] &&
10697-
(equalsLowerCase(part, "and") || equalsLowerCase(part, "or"))
10698-
) {
10699-
const before = parts[offset + previous];
10700-
if (before.charCodeAt(before.length - 1) === CC_RIGHT_PARENTHESIS) {
10701-
parts[offset + k] = ` ${part}`;
10702-
}
10703-
}
10704-
previous = k;
10705-
}
10706-
};
10707-
1070810673
/**
1070910674
* Minify a media or container query's printed parts, in place, after its first
1071010675
* part — the rule's name, which none of the passes read.
@@ -14091,14 +14056,12 @@ const _rulePrelude = (path, writer, minify, outParts) => {
1409114056
const media = name === "media";
1409214057
const container = name === "container";
1409314058
// Every prelude that is a chain of conditions, whichever rule states it.
14094-
const supports = equalsLowerCase(name, "supports");
1409514059
if (
1409614060
_S._transforms.shortenMediaQueries &&
14097-
(media || container || supports)
14061+
(media || container || equalsLowerCase(name, "supports"))
1409814062
) {
1409914063
_dropRepeatedConditions(parts);
1410014064
}
14101-
if (supports) _spaceConditionKeywords(parts, path.node, 1);
1410214065
if (media || container) {
1410314066
_minifyMediaQueryParts(parts, media);
1410414067
} else if (name === "import") {
@@ -15795,7 +15758,6 @@ module.exports._NO_RULE_SPANS = _NO_RULE_SPANS;
1579515758
module.exports._NUMERIC_RE = _NUMERIC_RE;
1579615759
module.exports._S = _S;
1579715760
module.exports._SEP = _SEP;
15798-
module.exports._spaceConditionKeywords = _spaceConditionKeywords;
1579915761
module.exports._TRIM_COMBINATORS = _TRIM_COMBINATORS;
1580015762
module.exports._TRIM_CONDITIONS = _TRIM_CONDITIONS;
1580115763
module.exports._TRIM_MATH = _TRIM_MATH;

‎lib/css/syntax-printer.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ const {
200200
_shortestColor,
201201
_shortestFlexValue,
202202
_shorthandSlotsReadable,
203-
_spaceConditionKeywords,
204203
_splitSelectorList,
205204
_splitTopLevelArguments,
206205
_splitTopLevelSpaces,
@@ -4044,9 +4043,6 @@ const printer = (path, writer) => {
40444043
/** @type {string[]} */
40454044
const parts = [];
40464045
_appendChildTexts(path.node, writer, parts);
4047-
if (minify && trim === _TRIM_CONDITIONS) {
4048-
_spaceConditionKeywords(parts, path.node, 0);
4049-
}
40504046
// A selector function's argument is a selector like the prelude's, so
40514047
// the universal a compound implies says nothing there either — except
40524048
// under test in a `@supports` condition, where only whitespace goes.
@@ -4433,9 +4429,6 @@ const printer = (path, writer) => {
44334429
if (_S._transforms.foldCase) _lowercaseConditionParts(parts);
44344430
if (_S._rangeSpellingAllowed) _useRangeSpelling(parts);
44354431
}
4436-
if (minify && structural && open === "(" && _S._inSupportsPrelude) {
4437-
_spaceConditionKeywords(parts, path.node, 0);
4438-
}
44394432
let trim =
44404433
minify && _S._mathFunctionDepth !== 0 ? _TRIM_MATH : _TRIM_NOTHING;
44414434
if (structural && open === "(") {

‎test/configCases/css/minimize-supports/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Array [
1818
"/*! kept: the selector under test */@supports selector(*.a:HOVER){.k03{color:red}}",
1919
"/*! kept: calc() needs its spaces */@supports (width:calc(1px + 2px)){.k04{color:red}}",
2020
"/*! kept: a value that is newer when shortened */@supports (color:rgba(0,0,0,.5)){.k05{color:red}}",
21-
"/*! supports: a space where a comment parted a condition and its keyword */@supports (display:grid) and (gap:1px){.s12{color:red}}",
21+
"/*! kept: no space where only a comment parted a condition and its keyword */@supports (display:grid)and (gap:1px){.s12{color:red}}",
2222
]
2323
`;

‎test/configCases/css/minimize-supports/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Array [
1818
"/*! kept: the selector under test */@supports selector(*.a:HOVER){.k03{color:red}}",
1919
"/*! kept: calc() needs its spaces */@supports (width:calc(1px + 2px)){.k04{color:red}}",
2020
"/*! kept: a value that is newer when shortened */@supports (color:rgba(0,0,0,.5)){.k05{color:red}}",
21-
"/*! supports: a space where a comment parted a condition and its keyword */@supports (display:grid) and (gap:1px){.s12{color:red}}",
21+
"/*! kept: no space where only a comment parted a condition and its keyword */@supports (display:grid)and (gap:1px){.s12{color:red}}",
2222
]
2323
`;

‎test/configCases/css/minimize-supports/style.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
@supports (width: calc(1px + 2px)) { .k04 { color: red } }
3535
/*! kept: a value that is newer when shortened */
3636
@supports (color: rgba(0, 0, 0, .5)) { .k05 { color: red } }
37-
/*! supports: a space where a comment parted a condition and its keyword */
37+
/*! kept: no space where only a comment parted a condition and its keyword */
3838
@supports (display: grid)/* c */and/* d */(gap: 1px) { .s12 { color: red } }

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4025,25 +4025,14 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
40254025
expect(condition(input)).toBe(input);
40264026
});
40274027

4028-
it.each([
4029-
// an early grammar asked for a space on each side of `and` / `or`
4030-
["(display:grid)/**/and/**/(gap:1px)", "(display:grid) and (gap:1px)"],
4031-
["(display:grid)/**/or (gap:1px)", "(display:grid) or (gap:1px)"],
4032-
["((a:b)/**/and (c:d)) or (e:f)", "((a:b) and (c:d)) or (e:f)"],
4033-
["selector(.a)/**/and (gap:1px)", "selector(.a) and (gap:1px)"],
4034-
// `or(` is a function as written, an unknown condition either way
4035-
["(display:grid)/**/or(gap:1px)", "(display:grid)or(gap:1px)"]
4036-
])("spaces the keyword of %s as %s", (input, expected) => {
4037-
expect(condition(input)).toBe(expected);
4038-
});
4039-
4040-
it("spaces the keyword in an `@import`'s supports() too", () => {
4041-
expect(minify("@import url(a.css) supports((a:b)/**/and (c:d));")).toBe(
4042-
"@import url(a.css) supports((a:b) and (c:d));"
4043-
);
4044-
// A media query's grammar never asked for the space.
4045-
expect(minify("@media (hover)/**/and (pointer:fine){a{b:c}}")).toBe(
4046-
"@media (hover)and (pointer:fine){a{b:c}}"
4028+
it("writes no space before a keyword only a comment parted", () => {
4029+
// WebKit reads a `@supports` `and` / `or` with no whitespace before it as
4030+
// invalid, as the source is there, so a space would make it read.
4031+
expect(condition("(display:grid)/**/and/**/(gap:1px)")).toBe(
4032+
"(display:grid)and (gap:1px)"
4033+
);
4034+
expect(minify("@import url(a.css) supports((a:b)/**/or (c:d));")).toBe(
4035+
"@import url(a.css) supports((a:b)or (c:d));"
40474036
);
40484037
});
40494038

0 commit comments

Comments
 (0)