@@ -4367,6 +4367,8 @@ const _walkValue = (node, parent, index, writer) => {
43674367 // counts this node while its own printer runs, which joins its children.
43684368 let enteredMath = false;
43694369 let enteredStepped = false;
4370+ let enteredSupports = false;
4371+ let previousMedia = false;
43704372 if (!skip && (ty === T_FUNCTION || ty === T_SIMPLE_BLOCK)) {
43714373 const i0 = _nodeIndex(node);
43724374 const vs = _listStarts[i0];
@@ -4400,6 +4402,17 @@ const _walkValue = (node, parent, index, writer) => {
44004402 ) {
44014403 _S._inColorOperand = true;
44024404 }
4405+ if (
4406+ ty === T_FUNCTION &&
4407+ !_S._inSupportsPrelude &&
4408+ _isImportSupports(i0, /** @type {Node} */ (parent))
4409+ ) {
4410+ enteredSupports = true;
4411+ previousMedia = _S._inMediaConditionPrelude;
4412+ _S._inSupportsPrelude = true;
4413+ // Its `(…)` tests a declaration, which no media feature spelling reaches.
4414+ _S._inMediaConditionPrelude = false;
4415+ }
44034416 for (let i = vs; i < ve; i++) {
44044417 _walkValue(_nodeRef(_flat[i]), node, i - vs, writer);
44054418 }
@@ -4408,6 +4421,29 @@ const _walkValue = (node, parent, index, writer) => {
44084421 _exitNode(node, parent, index, b, writer);
44094422 if (enteredMath) _S._mathFunctionDepth--;
44104423 if (enteredStepped) _S._steppedFunctionDepth--;
4424+ if (enteredSupports) {
4425+ _S._inSupportsPrelude = false;
4426+ _S._inMediaConditionPrelude = previousMedia;
4427+ }
4428+ };
4429+
4430+ /**
4431+ * Whether a function is an `@import`'s `supports()`, whose condition is a feature
4432+ * test like a `@supports` prelude's and so is read the same way.
4433+ * @param {number} i0 the function's node index
4434+ * @param {Node} parent the node it sits in
4435+ * @returns {boolean} true for `supports()` at the top of an `@import` prelude
4436+ */
4437+ const _isImportSupports = (i0, parent) => {
4438+ const p = _nodeIndex(parent);
4439+ if (_types[p] !== T_AT_RULE) return false;
4440+ const written = _S._input.slice(_starts[i0], _aux0[i0]);
4441+ return (
4442+ equalsLowerCase(
4443+ written.includes("\\") ? unescapeIdentifier(written) : written,
4444+ "supports"
4445+ ) && _atKeyword(_S._input.slice(_starts[p] + 1, _aux0[p])) === "import"
4446+ );
44114447};
44124448
44134449// A gradient, whatever it is prefixed with.
@@ -4552,9 +4588,13 @@ const _enterAtRulePrelude = (i0) => {
45524588 if (atName === "supports") {
45534589 _S._inSupportsPrelude = true;
45544590 }
4555- // `@media` and `@container` are the two preludes whose `(…)` holds a media
4556- // feature, the only place the range spelling is a spelling of.
4557- else if (atName === "media" || atName === "container") {
4591+ // `@media`, `@container` and an `@import`'s media query are where a `(…)`
4592+ // holds a media feature, the only place the range spelling is a spelling of.
4593+ else if (
4594+ atName === "media" ||
4595+ atName === "container" ||
4596+ atName === "import"
4597+ ) {
45584598 _S._inMediaConditionPrelude = true;
45594599 }
45604600};
@@ -7469,19 +7509,21 @@ const _isCombinator = (cc) =>
74697509 cc === CC_GREATER_THAN_SIGN || cc === CC_PLUS_SIGN || cc === CC_TILDE;
74707510
74717511// WHY: what separates the parts of a query condition — a media-feature range
7472- // comparison and the `:` of a plain feature test. Whitespace around one is
7473- // insignificant there, but the same code points mean something else in a
7474- // selector, as combinators and a pseudo-class `:`, and in a declaration value,
7475- // as `calc()` operators, so this trims a `(…)` condition outside a value alone.
7512+ // comparison, the `:` of a plain feature test and the `!` of a tested
7513+ // `!important`. Whitespace around one is insignificant there, but the same code
7514+ // points mean something else in a selector, as combinators and a pseudo-class
7515+ // `:`, and in a declaration value, as `calc()` operators, so this trims a `(…)`
7516+ // condition outside a value alone.
74767517/**
74777518 * @param {number} cc a code point
7478- * @returns {boolean} true for `<` / `>` / `=` / `:`
7519+ * @returns {boolean} true for `<` / `>` / `=` / `:` / `!`
74797520 */
74807521const _isConditionSeparator = (cc) =>
74817522 cc === CC_LESS_THAN_SIGN ||
74827523 cc === CC_GREATER_THAN_SIGN ||
74837524 cc === CC_EQUALS_SIGN ||
7484- cc === CC_COLON;
7525+ cc === CC_COLON ||
7526+ cc === CC_EXCLAMATION;
74857527
74867528// What a `_join` may drop a separator next to.
74877529const _TRIM_NOTHING = 0;
@@ -7691,8 +7733,9 @@ const _join = (parts, beautify, trim = _TRIM_NOTHING) => {
76917733 (trim === _TRIM_SEPARATORS
76927734 ? _wouldFuseTokens(last, f, out)
76937735 : _keepSeparator(last, first) &&
7694- !_isTrimmable(trim, last) &&
7695- !_isTrimmable(trim, first));
7736+ // A condition still keeps what parts `<` from `!--` or `--` from `>`.
7737+ ((!_isTrimmable(trim, last) && !_isTrimmable(trim, first)) ||
7738+ (trim === _TRIM_CONDITIONS && _wouldFuseTokens(last, f, out))));
76967739 if (out.length !== 0 && keep) {
76977740 out += " ";
76987741 }
@@ -7750,6 +7793,62 @@ const _normalizeNumber = (num) => {
77507793 return sign + s;
77517794};
77527795
7796+ /**
7797+ * Write a number spelled with an exponent the shortest exact way: plainly where
7798+ * that is no longer (`0.2e2`→`20`), else on an integer mantissa (`10E3`→`1e4`).
7799+ * An exponent makes a `<number>`, never an `<integer>` (§4.3.12), so where that
7800+ * is read `keepNumberType` keeps a spelling that is still not an integer.
7801+ * @param {string} num numeric text with an exponent (may carry a sign)
7802+ * @param {boolean} keepNumberType whether an integer spelling is ruled out
7803+ * @returns {string} the normalized number
7804+ */
7805+ const _normalizeExponent = (num, keepNumberType) => {
7806+ let sign = "";
7807+ let s = num;
7808+ const c0 = s.charCodeAt(0);
7809+ if (c0 === CC_PLUS_SIGN || c0 === CC_HYPHEN_MINUS) {
7810+ if (c0 === CC_HYPHEN_MINUS) sign = "-";
7811+ s = s.slice(1);
7812+ }
7813+ let e = s.indexOf("e");
7814+ if (e === -1) e = s.indexOf("E");
7815+ const mantissa = s.slice(0, e);
7816+ const dot = mantissa.indexOf(".");
7817+ let digits =
7818+ dot === -1 ? mantissa : mantissa.slice(0, dot) + mantissa.slice(dot + 1);
7819+ // Where the decimal point falls in `digits` once the exponent has moved it.
7820+ let point = (dot === -1 ? mantissa.length : dot) + Number(s.slice(e + 1));
7821+ let start = 0;
7822+ while (start < digits.length && digits.charCodeAt(start) === CC_0) start++;
7823+ let end = digits.length;
7824+ while (end > start && digits.charCodeAt(end - 1) === CC_0) end--;
7825+ if (start === end) return keepNumberType ? `${sign}0e0` : `${sign}0`;
7826+ point -= start;
7827+ digits = digits.slice(start, end);
7828+ const scale = point - digits.length;
7829+ const exponent = `${digits}e${scale}`;
7830+ // Built only when it could win, so `1e999` never writes a thousand zeros.
7831+ const plainLength =
7832+ point <= 0
7833+ ? 1 - point + digits.length
7834+ : scale >= 0
7835+ ? point
7836+ : digits.length + 1;
7837+ if (
7838+ plainLength > exponent.length ||
7839+ (keepNumberType && point >= digits.length)
7840+ ) {
7841+ return sign + exponent;
7842+ }
7843+ const plain =
7844+ point <= 0
7845+ ? `.${"0".repeat(-point)}${digits}`
7846+ : scale >= 0
7847+ ? digits + "0".repeat(scale)
7848+ : `${digits.slice(0, point)}.${digits.slice(point)}`;
7849+ return sign + plain;
7850+ };
7851+
77537852// WHY: Chromium serializes a computed number at 6 significant digits and lays a
77547853// length out in 1/64px, so 6 digits sit below what a stylesheet can observe
77557854// until the value reaches ~23000px, which no real one does. Measured across
@@ -10571,6 +10670,95 @@ const _writeCustomMedia = (parts) => {
1057110670 }
1057210671};
1057310672
10673+ // WHY: WebKit reads a `@supports` `and`, `or` or `not` as invalid unless
10674+ // whitespace follows it, where the spec only needs the `(` not to join it into
10675+ // a function. Where a comment alone parted the two, a space would make WebKit
10676+ // read the condition, so the comment is written back instead.
10677+ /**
10678+ * @param {string[]} parts a `@supports` condition's printed parts, in place
10679+ * @returns {void}
10680+ */
10681+ const _keepKeywordComments = (parts) => {
10682+ for (let i = 0; i < parts.length; i++) {
10683+ const part = parts[i];
10684+ if (
10685+ !equalsLowerCase(part, "and") &&
10686+ !equalsLowerCase(part, "or") &&
10687+ !equalsLowerCase(part, "not")
10688+ ) {
10689+ continue;
10690+ }
10691+ let next = i + 1;
10692+ while (next < parts.length && parts[next].length === 0) next++;
10693+ if (
10694+ next < parts.length &&
10695+ parts[next].charCodeAt(0) === CC_LEFT_PARENTHESIS
10696+ ) {
10697+ parts[i] = `${part}/**/`;
10698+ }
10699+ }
10700+ };
10701+
10702+ /**
10703+ * Minify a media or container query's printed parts, in place, after its first
10704+ * part — the rule's name, which none of the passes read.
10705+ * @param {string[]} parts the prelude's printed parts
10706+ * @param {boolean} media whether it is a media query rather than a container one
10707+ * @returns {void}
10708+ */
10709+ const _minifyMediaQueryParts = (parts, media) => {
10710+ // Only a media query's top level is media types and the keywords between
10711+ // them; `@container`'s leads with a container name, which is the author's own
10712+ // and case-sensitive.
10713+ if (media) {
10714+ // Before the passes below, which read the query this writes in.
10715+ if (_S._transforms.resolveCustomAtRules && _customMedia.size !== 0) {
10716+ _writeCustomMedia(parts);
10717+ }
10718+ if (_S._transforms.foldCase) _lowercaseConditionParts(parts);
10719+ if (_S._transforms.shortenMediaQueries) _dropImpliedMediaType(parts);
10720+ }
10721+ if (_S._rangeSpellingAllowed) {
10722+ _collapseRangeInterval(parts);
10723+ }
10724+ // A `@container` query is itself newer than the range spelling, so a target
10725+ // that reads one reads the other and there is nothing to lower.
10726+ else if (media && _S._transforms.lowerUnsupported) {
10727+ _useMinMaxSpelling(parts);
10728+ }
10729+ };
10730+
10731+ /**
10732+ * Where an `@import`'s media query starts in its printed parts: past the URL,
10733+ * then a `layer` and a `supports()` where written, each of which is one part.
10734+ * @param {string[]} parts the prelude's printed parts, `@import` first
10735+ * @returns {number} the index of the query's first part, or -1 when it has none
10736+ */
10737+ const _importMediaStart = (parts) => {
10738+ // 0: the URL is next, 1: a `layer`, 2: a `supports()`, 3: the query.
10739+ let expecting = 0;
10740+ for (let i = 1; i < parts.length; i++) {
10741+ const part = parts[i];
10742+ if (part.length === 0 || part === _SEP) continue;
10743+ if (expecting === 0) {
10744+ expecting = 1;
10745+ continue;
10746+ }
10747+ const open = part.indexOf("(");
10748+ const name = asciiLowerCaseName(
10749+ unescapeIdentifier(open === -1 ? part : part.slice(0, open))
10750+ );
10751+ if (expecting === 1 && name === "layer") {
10752+ expecting = 2;
10753+ } else if (expecting !== 3 && open !== -1 && name === "supports") {
10754+ expecting = 3;
10755+ } else {
10756+ return i;
10757+ }
10758+ }
10759+ return -1;
10760+ };
10761+
1057410762// One selector naming a `@custom-selector`, as printed after its `:`.
1057510763const _CUSTOM_SELECTOR_NAME_RE = /^--[^\s(),]+$/;
1057610764
@@ -13897,31 +14085,25 @@ const _rulePrelude = (path, writer, minify, outParts) => {
1389714085 const media = name === "media";
1389814086 const container = name === "container";
1389914087 // Every prelude that is a chain of conditions, whichever rule states it.
14088+ const supports = equalsLowerCase(name, "supports");
1390014089 if (
1390114090 _S._transforms.shortenMediaQueries &&
13902- (media || container || equalsLowerCase(name, " supports") )
14091+ (media || container || supports)
1390314092 ) {
1390414093 _dropRepeatedConditions(parts);
1390514094 }
14095+ if (supports) _keepKeywordComments(parts);
1390614096 if (media || container) {
13907- // Only `@media`'s top level is media types and the keywords between
13908- // them; `@container`'s leads with a container name, which is the
13909- // author's own and case-sensitive.
13910- if (media) {
13911- // Before the passes below, which read the query this writes in.
13912- if (_S._transforms.resolveCustomAtRules && _customMedia.size !== 0) {
13913- _writeCustomMedia(parts);
13914- }
13915- if (_S._transforms.foldCase) _lowercaseConditionParts(parts);
13916- if (_S._transforms.shortenMediaQueries) _dropImpliedMediaType(parts);
13917- }
13918- if (_S._rangeSpellingAllowed) {
13919- _collapseRangeInterval(parts);
13920- }
13921- // A `@container` query is itself newer than the range spelling, so a
13922- // target that reads one reads the other and there is nothing to lower.
13923- else if (media && _S._transforms.lowerUnsupported) {
13924- _useMinMaxSpelling(parts);
14097+ _minifyMediaQueryParts(parts, media);
14098+ } else if (name === "import") {
14099+ // Its media query follows the URL, `layer` and `supports()`, and is read
14100+ // as a `@media` one; the rest names or tests, and stays as it is.
14101+ const start = _importMediaStart(parts);
14102+ if (start !== -1) {
14103+ const query = parts.splice(start);
14104+ query.unshift("");
14105+ _minifyMediaQueryParts(query, true);
14106+ for (let i = 1; i < query.length; i++) parts.push(query[i]);
1392514107 }
1392614108 }
1392714109 }
@@ -15607,6 +15789,7 @@ module.exports._NO_RULE_SPANS = _NO_RULE_SPANS;
1560715789module.exports._NUMERIC_RE = _NUMERIC_RE;
1560815790module.exports._S = _S;
1560915791module.exports._SEP = _SEP;
15792+ module.exports._keepKeywordComments = _keepKeywordComments;
1561015793module.exports._TRIM_COMBINATORS = _TRIM_COMBINATORS;
1561115794module.exports._TRIM_CONDITIONS = _TRIM_CONDITIONS;
1561215795module.exports._TRIM_MATH = _TRIM_MATH;
@@ -15645,6 +15828,7 @@ module.exports._lowercaseConditionParts = _lowercaseConditionParts;
1564515828module.exports._minifyColorFunction = _minifyColorFunction;
1564615829module.exports._minifyPolarColorFunction = _minifyPolarColorFunction;
1564715830module.exports._neededPrefixes = _neededPrefixes;
15831+ module.exports._normalizeExponent = _normalizeExponent;
1564815832module.exports._normalizeNumber = _normalizeNumber;
1564915833module.exports._opaqueEntry = _opaqueEntry;
1565015834module.exports._openerKey = _openerKey;
0 commit comments