Skip to content

Commit 971074c

Browse files
fix(css): leave an invalid shadow layer as written
Dropping currentcolor or trailing zeros from a layer with two colors, too many lengths, or lengths a color parts made a declaration the engine drops valid. SHADOW_PROPERTIES now carries the grammar's length range, and the rewrite runs only over layers already in shape. The equivalence helper's gradient step also leaves strings and url() bodies alone.
1 parent bb1e97d commit 971074c

5 files changed

Lines changed: 67 additions & 30 deletions

File tree

‎lib/css/data.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,11 +2034,11 @@ const DISPLAY_SHORT_FORMS = new Map([
20342034
["run-in flow", "run-in"]
20352035
]);
20362036

2037-
// Each property whose value is a list of shadows -> the count of lengths a
2038-
// shadow cannot go below, past which a trailing zero is already implied.
2037+
// Each property whose value is a list of shadows -> the fewest and most lengths
2038+
// a shadow takes; a trailing zero past the fewest is already implied.
20392039
const SHADOW_PROPERTIES = new Map([
2040-
["box-shadow", 2],
2041-
["text-shadow", 2]
2040+
["box-shadow", [2, 4]],
2041+
["text-shadow", [2, 3]]
20422042
]);
20432043

20442044
// WHY: Each shorthand -> the keywords one of its values may drop, each with every

‎lib/css/syntax-printer.js‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,29 @@ const _orderTransitionSlots = (components) => {
26452645
return ordered.join(" ") === components.join(" ") ? null : ordered;
26462646
};
26472647

2648+
/**
2649+
* Whether a shadow layer has the shape its grammar takes: one run of lengths in
2650+
* range, and at most one color and one `inset` around it.
2651+
* @param {string[]} layer the layer's components
2652+
* @param {number} start where its last run of lengths starts
2653+
* @param {number} end where that run ends
2654+
* @param {number} minimum the fewest lengths a shadow takes
2655+
* @param {number} maximum the most lengths a shadow takes
2656+
* @returns {boolean} true when the layer is a valid shadow
2657+
*/
2658+
const _isShadowLayer = (layer, start, end, minimum, maximum) => {
2659+
if (end - start < minimum || end - start > maximum) return false;
2660+
let colors = 0;
2661+
let insets = 0;
2662+
for (let j = 0; j < layer.length; j++) {
2663+
if (j >= start && j < end) continue;
2664+
if (_NUMERIC_RE.test(layer[j])) return false;
2665+
if (equalsLowerCase(layer[j], "inset")) insets++;
2666+
else colors++;
2667+
}
2668+
return colors <= 1 && insets <= 1;
2669+
};
2670+
26482671
/**
26492672
* Drop what a shadow's notation already implies: lengths past the mandatory
26502673
* count default to zero, and an absent color is `currentcolor` (CSS Backgrounds 3 §7.1).
@@ -2653,8 +2676,9 @@ const _orderTransitionSlots = (components) => {
26532676
* @returns {string[] | null} the fragments to join, or `null` to keep the value
26542677
*/
26552678
const _dropShadowDefaults = (property, components) => {
2656-
const minimum = SHADOW_PROPERTIES.get(property);
2657-
if (minimum === undefined) return null;
2679+
const lengths = SHADOW_PROPERTIES.get(property);
2680+
if (lengths === undefined) return null;
2681+
const [minimum, maximum] = lengths;
26582682
// A quoted string could carry the comma the layer split reads.
26592683
for (const component of components) {
26602684
if (component.includes('"') || component.includes("'")) return null;
@@ -2670,6 +2694,9 @@ const _dropShadowDefaults = (property, components) => {
26702694
while (end > 0 && !_NUMERIC_RE.test(layer[end - 1])) end--;
26712695
let start = end;
26722696
while (start > 0 && _NUMERIC_RE.test(layer[start - 1])) start--;
2697+
// An invalid layer drops the declaration, so one a rewrite could make
2698+
// valid — two colors, lengths parted or out of range — is left as written.
2699+
if (!_isShadowLayer(layer, start, end, minimum, maximum)) return null;
26732700
let last = end;
26742701
// The components are as authored — the zero-unit drop prints later — so a
26752702
// trailing zero is `0px` as often as `0`.

‎test/helpers/syntaxEquivalence.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,12 @@ const installHelpers = (generics) => {
429429
}
430430
// Anchored left: a prefixed gradient folds under its own rules, so
431431
// canonicalizing one would hide a fold the printer must not make.
432-
return directGradients(
432+
return outsideText(
433433
named.replace(
434434
/(^|[^\w-])((?:repeating-)?(?:linear|radial|conic)-gradient\([^()]*(?:\([^()]*\)[^()]*)*)\s(?:100%|360deg)\)/gi,
435435
"$1$2)"
436-
)
436+
),
437+
directGradients
437438
);
438439
};
439440

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5764,7 +5764,13 @@ describe("CssSyntax minify — the value transforms' rejection paths", () => {
57645764
["a layer holds a string", 'a{box-shadow:0 0 0 0 red,"a"}'],
57655765
// A comma with nothing either side is a layer no shadow fills.
57665766
["a trailing comma parts an empty layer", "a{box-shadow:0 0 0 0 red,}"],
5767-
["a leading comma does the same", "a{box-shadow:,0 0 0 0 red}"]
5767+
["a leading comma does the same", "a{box-shadow:,0 0 0 0 red}"],
5768+
// An invalid layer drops the declaration; a rewrite must not make it valid.
5769+
["a layer names two colors", "a{box-shadow:0 0 red currentcolor}"],
5770+
["a box shadow holds five lengths", "a{box-shadow:0 0 0 0 0 red}"],
5771+
["a text shadow holds four lengths", "a{text-shadow:1px 1px 0 0 red}"],
5772+
["a color parts the lengths", "a{box-shadow:0 0 red 0 0}"],
5773+
["a layer is inset twice", "a{box-shadow:inset inset 0 0 currentcolor}"]
57685774
])("keeps the value where %s", (_name, css) => {
57695775
expect(minify(css)).toBe(css);
57705776
});

‎tooling/generate-css-data.js‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,32 +2265,40 @@ const collectPositionProperties = () => {
22652265
};
22662266

22672267
// A shadow states its offsets as `<length>{MIN,MAX}`; MIN of them are the two
2268-
// offsets every shadow needs.
2268+
// offsets every shadow needs, and one past MAX is no shadow.
22692269
const SHADOW_LENGTHS_REGEXP = /<length>\{(\d+),(\d+)\}/;
22702270

22712271
/**
2272-
* Each property whose value is a list of shadows -> how many lengths a shadow
2273-
* cannot go below. The grammar states the range itself (`<length>{2,4}` on
2272+
* Each property whose value is a list of shadows -> the fewest and most lengths a
2273+
* shadow takes. The grammar states the range itself (`<length>{2,4}` on
22742274
* `box-shadow`, `{2,3}` on `text-shadow`), so a trailing zero past the minimum
22752275
* is a value the notation already implies.
2276-
* @returns {[string, number][]} the entries, sorted by property
2276+
* @returns {[string, [number, number]][]} the entries, sorted by property
22772277
*/
22782278
const collectShadowProperties = () => {
2279-
/** @type {[string, number][]} */
2279+
/** @type {[string, [number, number]][]} */
22802280
const out = [];
22812281
for (const [name, entry] of Object.entries(properties)) {
22822282
if (typeof entry.syntax !== "string") continue;
2283-
let minimum = null;
2283+
/** @type {[number, number] | null} */
2284+
let lengths = null;
22842285
for (const raw of references(entry.syntax)) {
22852286
const definition = definitions.get(raw);
22862287
if (definition === undefined) continue;
22872288
const range = SHADOW_LENGTHS_REGEXP.exec(definition);
22882289
if (range === null) continue;
2290+
const minimum = Number(range[1]);
2291+
const maximum = Number(range[2]);
22892292
// Two productions naming different ranges is no single shadow shape.
2290-
if (minimum !== null && minimum !== Number(range[1])) return [];
2291-
minimum = Number(range[1]);
2293+
if (
2294+
lengths !== null &&
2295+
(lengths[0] !== minimum || lengths[1] !== maximum)
2296+
) {
2297+
return [];
2298+
}
2299+
lengths = [minimum, maximum];
22922300
}
2293-
if (minimum !== null) out.push([name, minimum]);
2301+
if (lengths !== null) out.push([name, lengths]);
22942302
}
22952303
return out.sort(([a], [b]) => (a < b ? -1 : 1));
22962304
};
@@ -3768,15 +3776,6 @@ const mapLiteral = (entries) =>
37683776
.map(([key, value]) => `["${key}", "${value}"]`)
37693777
.join(", ")}])`;
37703778

3771-
/**
3772-
* @param {[string, number][]} entries string-keyed, number-valued pairs
3773-
* @returns {string} the `Map` literal
3774-
*/
3775-
const countMapLiteral = (entries) =>
3776-
`new Map([${entries
3777-
.map(([key, value]) => `["${key}", ${value}]`)
3778-
.join(", ")}])`;
3779-
37803779
/**
37813780
* A prefix table's `new Map([…])` literal: `name -> [prefix, [browser, from,
37823781
* to][]][]`. `Infinity` prints as a bare identifier the runtime reads back.
@@ -7351,9 +7350,13 @@ ${displayShortForms
73517350
.join(",\n")}
73527351
]);
73537352
7354-
// Each property whose value is a list of shadows -> the count of lengths a
7355-
// shadow cannot go below, past which a trailing zero is already implied.
7356-
const SHADOW_PROPERTIES = ${countMapLiteral(shadowProperties)};
7353+
// Each property whose value is a list of shadows -> the fewest and most lengths
7354+
// a shadow takes; a trailing zero past the fewest is already implied.
7355+
const SHADOW_PROPERTIES = new Map([${shadowProperties
7356+
.map(
7357+
([name, [minimum, maximum]]) => `["${name}", [${minimum}, ${maximum}]]`
7358+
)
7359+
.join(", ")}]);
73577360
73587361
// WHY: Each shorthand -> the keywords one of its values may drop, each with every
73597362
// spelling its own slot takes: the slot's keywords, and each function it

0 commit comments

Comments
 (0)