@@ -138,6 +138,7 @@ const {
138138 UNIT_CONVERSION_TARGETS,
139139 UNIT_GROUP_BASE,
140140 UNSHARED_LONGHAND_KEYWORDS,
141+ VALUE_SUPPORT_PACKED,
141142 X_AXIS_TRANSFORMS,
142143 ZERO_ANGLE_FUNCTIONS,
143144 ZERO_UNIT_KEEPING_PROPERTIES,
@@ -8409,6 +8410,36 @@ const _encodeBrowserVersion = (version) => {
84098410 return major * 100000 + minor;
84108411};
84118412
8413+ // `VALUE_SUPPORT_PACKED` unpacked, on the first lookup a build makes: only a
8414+ // build with a target and two declarations of one property asks.
8415+ /** @type {Map<string, number> | null} */
8416+ let _valueSupport = null;
8417+
8418+ /**
8419+ * When every target browser first read one value, by the spelling
8420+ * `VALUE_SUPPORT_PACKED` names it: `"<property> <keyword>"` for a keyword a
8421+ * property's own grammar states, the bare name for a color function.
8422+ * @param {string} key the spelling
8423+ * @returns {number | undefined} its `SUPPORT_PROFILES` row, or undefined
8424+ */
8425+ const _valueSupportedFrom = (key) => {
8426+ let table = _valueSupport;
8427+ if (table === null) {
8428+ table = new Map();
8429+ for (const group of VALUE_SUPPORT_PACKED.split("|")) {
8430+ const colon = group.indexOf(":");
8431+ const property = group.slice(0, colon);
8432+ const prefix = property.length === 0 ? "" : `${property} `;
8433+ const entries = group.slice(colon + 1).split(" ");
8434+ for (let at = 0; at < entries.length; at += 2) {
8435+ table.set(prefix + entries[at], Number(entries[at + 1]));
8436+ }
8437+ }
8438+ _valueSupport = table;
8439+ }
8440+ return table.get(key);
8441+ };
8442+
84128443/**
84138444 * Whether every target browser reads a CSS ability, so a spelling that needs it
84148445 * may be reached for. No selection names no browser to answer for, so the
@@ -14276,6 +14307,61 @@ const _familyValue = (property, longhands, values) => {
1427614307 return _joinSlots(dropped === null ? values : dropped);
1427714308};
1427814309
14310+ /**
14311+ * Whether every target browser reads a later declaration's whole value, which
14312+ * leaves an earlier one of the same property carrying nothing rather than
14313+ * standing as its fallback. Only a value whose every construct is named in
14314+ * `VALUE_SUPPORT_PACKED` answers yes: a keyword the property's own grammar
14315+ * states, or one color function over numbers and named colors.
14316+ * @param {string} property the printed property name
14317+ * @param {string} value the later declaration's printed value
14318+ * @returns {boolean} true when no target is left reading the earlier one
14319+ */
14320+ const _valueReadByTarget = (property, value) => {
14321+ // No selection names no browser to answer for, so nothing is known to read
14322+ // the later value and the earlier one stands.
14323+ if (_prefixBrowsers === null) return false;
14324+ const open = value.indexOf("(");
14325+ if (open === -1) {
14326+ return _readsAll(
14327+ _valueSupportedFrom(`${property} ${toLowerCaseIfNeeded(value)}`)
14328+ );
14329+ }
14330+ if (
14331+ open === 0 ||
14332+ value.includes("(", open + 1) ||
14333+ value.charCodeAt(value.length - 1) !== CC_RIGHT_PARENTHESIS
14334+ ) {
14335+ return false;
14336+ }
14337+ if (
14338+ !_readsAll(_valueSupportedFrom(toLowerCaseIfNeeded(value.slice(0, open))))
14339+ ) {
14340+ return false;
14341+ }
14342+ const args = value.slice(open + 1, -1).match(_COLOR_ARGUMENT_RE);
14343+ if (args === null) return false;
14344+ for (const argument of args) {
14345+ if (_COLOR_NUMBER_RE.test(argument) || _COLOR_HUE_RE.test(argument)) {
14346+ continue;
14347+ }
14348+ // A hex of three or six digits is read wherever a color is; four and eight
14349+ // state an alpha, which is the target's own question and not asked here.
14350+ if (_LONE_HEX_RE.test(argument)) {
14351+ if (argument.length === 4 || argument.length === 7) continue;
14352+ return false;
14353+ }
14354+ // A named color reaches as far as naming one does, bar the two that
14355+ // arrived after the list and carry a row of their own.
14356+ const lowered = toLowerCaseIfNeeded(argument);
14357+ if (COLOR_NAME_TO_RGB.has(lowered) && !LATER_COLOR_NAMES.has(lowered)) {
14358+ continue;
14359+ }
14360+ return false;
14361+ }
14362+ return true;
14363+ };
14364+
1427914365/**
1428014366 * Whether a later declaration of one property leaves an earlier one carrying
1428114367 * nothing: the cascade reads the later, and it writes every name the earlier
@@ -14285,6 +14371,7 @@ const _familyValue = (property, longhands, values) => {
1428514371 * @param {string} laterValue its printed value
1428614372 * @param {Node} earlier the earlier declaration of the same property
1428714373 * @param {string} earlierValue its printed value
14374+ * @param {string} property the printed property both write
1428814375 * @param {boolean} names whether the property takes a `<custom-ident>` list
1428914376 * @param {boolean} keyframe whether the block is a keyframe
1429014377 * @returns {boolean} true when the earlier one is read for nothing
@@ -14295,6 +14382,7 @@ const _laterWriteWins = (
1429514382 laterValue,
1429614383 earlier,
1429714384 earlierValue,
14385+ property,
1429814386 names,
1429914387 keyframe
1430014388) => {
@@ -14304,6 +14392,9 @@ const _laterWriteWins = (
1430414392 // An `!important` earlier one still wins over a later plain declaration,
1430514393 // which leaves the later dead rather than it.
1430614394 if (!important && path.important(earlier)) return false;
14395+ // A target that reads the later value everywhere is one nothing falls back
14396+ // from, whatever the two values have in common.
14397+ if (_valueReadByTarget(property, laterValue)) return true;
1430714398 return names
1430814399 ? _coveredByLater(laterValue, earlierValue)
1430914400 : _sameVocabulary(laterValue, earlierValue);
@@ -14333,13 +14424,15 @@ const _writeIsDead = (path, writer, earlier, later, keyframe) => {
1433314424 ) {
1433414425 return false;
1433514426 }
14427+ const property = _printedProperty(laterText, laterColon);
1433614428 return _laterWriteWins(
1433714429 path,
1433814430 later,
1433914431 _printedValue(laterText, laterColon),
1434014432 earlier,
1434114433 _printedValue(earlierText, earlierColon),
14342- CUSTOM_IDENT_LIST_PROPERTIES.has(_printedProperty(laterText, laterColon)),
14434+ property,
14435+ CUSTOM_IDENT_LIST_PROPERTIES.has(property),
1434314436 keyframe
1434414437 );
1434514438};
@@ -16499,14 +16592,14 @@ const _composeBlockBody = (
1649916592 }
1650016593 // WHY: only the last of a set of declarations of one property can be read, so
1650116594 // the earlier carry nothing, whatever stands between them. Identical text
16502- // always resolves that way; a differently spelled value only where the later
16503- // writes every name the earlier does, since otherwise the earlier is its
16504- // fallback — dropping those is how esbuild and lightningcss lose a
16505- // `color(display-p3 …)` pair.
16506- // TODO offer the wider "drop whatever a later declaration overrides" behind an
16507- // option, as csso and cssnano expose theirs. It needs support data per
16508- // keyword: a bare ident may be invalid, or newer than the value it stands
16509- // after, and neither supersedes anything .
16595+ // always resolves that way; a differently spelled value where the later writes
16596+ // every name the earlier does, or where every target reads it
16597+ // (`_valueReadByTarget`), since otherwise the earlier is its fallback —
16598+ // dropping those is how esbuild and lightningcss lose a `color(display-p3 …)`
16599+ // pair.
16600+ // TODO answer the same question with no browserslist selection, as csso and
16601+ // cssnano do: theirs is unconditional, and naming the engines is what makes
16602+ // this one sound — with no selection there are none to name .
1651016603 /** @type {Set<number> | null} */
1651116604 let superseded = null;
1651216605 // CSS Animations 1 §3: a keyframe ignores an `!important` declaration, so one
@@ -16576,6 +16669,7 @@ const _composeBlockBody = (
1657616669 value,
1657716670 items[before],
1657816671 written,
16672+ property,
1657916673 names,
1658016674 keyframe
1658116675 )
0 commit comments