Skip to content

Commit 167a23a

Browse files
test(css): cover every branch the nesting scanners take
Codecov read 11 lines of the new code uncovered: the escape, quote and bracket branches of the three selector scanners, and the guards around them. Each is now driven by a selector that reaches it — a quoted space, combinator or comma inside an attribute value, an escape written before the `&`, a pseudo or attribute holding a combinator of its own, and the part a dropped pseudo-element colon leaves empty. Two guards were dead rather than uncovered and are gone: the rule asking `_declarationsPrecedeRules` is one of the rules it reads, so that list is never empty, and `_withColorFallback` is handed text it built from a longhand and its value, so the name before the `:` is always there.
1 parent 0b708ef commit 167a23a

2 files changed

Lines changed: 84 additions & 5 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10324,10 +10324,10 @@ const _resolveNestedPrelude = (prelude, parent) => {
1032410324
* @returns {boolean} true when no declaration follows a rule
1032510325
*/
1032610326
const _declarationsPrecedeRules = (path, rule) => {
10327-
const rules = path.childRules(rule);
10328-
if (rules === null || rules.length === 0) return false;
1032910327
const decls = path.declarations(rule);
1033010328
if (decls === null || decls.length === 0) return true;
10329+
// The rule asking holds this one, so the list it is in is never empty.
10330+
const rules = /** @type {Rule[]} */ (path.childRules(rule));
1033110331
let firstRule = Number.POSITIVE_INFINITY;
1033210332
for (let i = 0; i < rules.length; i++) {
1033310333
const at = path.start(rules[i]);
@@ -14354,9 +14354,8 @@ const _expandTextDecoration = (value, important) => {
1435414354
const _withColorFallback = (one) => {
1435514355
if (!_colorFallbacksNeeded || !_transforms.colorFallbacks) return one;
1435614356
if (!one.includes("(")) return one;
14357-
const colon = one.indexOf(":");
14358-
if (colon <= 0) return one;
14359-
const fallback = _colorFallbackDeclaration(one, colon);
14357+
// Built from a longhand and its value, so the name before the `:` is there.
14358+
const fallback = _colorFallbackDeclaration(one, one.indexOf(":"));
1436014359
return fallback === null ? one : fallback + one;
1436114360
};
1436214361

‎test/CssSyntax.unittest.js‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9316,6 +9316,59 @@ describe("CssSyntax minify — nesting the target cannot read", () => {
93169316
);
93179317
});
93189318

9319+
it("reads a quoted attribute value as no part of the selector's shape", () => {
9320+
// A space, a combinator or a comma inside a string is the value's own, so
9321+
// the parent is still the one compound it stands where `&` was as.
9322+
expect(minifyFor('a[title="x y"]{& b{color:red}}', ["chrome 100"])).toBe(
9323+
'a[title="x y"] b{color:red}'
9324+
);
9325+
expect(minifyFor('a[title="x>y"]{& b{color:red}}', ["chrome 100"])).toBe(
9326+
'a[title="x>y"] b{color:red}'
9327+
);
9328+
expect(minifyFor('a[title="x,y"]{& b{color:red}}', ["chrome 100"])).toBe(
9329+
'a[title="x,y"] b{color:red}'
9330+
);
9331+
});
9332+
9333+
it("reads what a pseudo or an attribute holds as no part of it either", () => {
9334+
expect(minifyFor("a:not(.x .y){& b{color:red}}", ["chrome 100"])).toBe(
9335+
"a:not(.x .y) b{color:red}"
9336+
);
9337+
expect(minifyFor("a[href]{& b{color:red}}", ["chrome 100"])).toBe(
9338+
"a[href] b{color:red}"
9339+
);
9340+
});
9341+
9342+
it("leaves an `&` written inside a string alone", () => {
9343+
expect(minifyFor('a{& [title="&"]{color:red}}', ["chrome 100"])).toBe(
9344+
'a [title="&"]{color:red}'
9345+
);
9346+
});
9347+
9348+
it("reads a string written before the `&` as no parent of its own", () => {
9349+
// The quotes a space keeps, so the string is still one where `&` is read.
9350+
expect(minifyFor('a{[title="x y"] &{color:red}}', ["chrome 100"])).toBe(
9351+
'[title="x y"] a{color:red}'
9352+
);
9353+
});
9354+
9355+
it("reads an escape written before the `&` as no parent either", () => {
9356+
expect(minifyFor("a{.\\31 23 &{color:red}}", ["chrome 100"])).toBe(
9357+
".\\31 23 a{color:red}"
9358+
);
9359+
});
9360+
9361+
it("names an escaped selector as the `:is()` it means", () => {
9362+
// The space ending an escape is not a combinator, but reading it as one
9363+
// only writes the `:is()` that says the same thing.
9364+
expect(minifyFor("a.\\31 23{& b{color:red}}", ["chrome 100"])).toBe(
9365+
":is(a.\\31 23) b{color:red}"
9366+
);
9367+
expect(minifyFor("a{& .\\31 23{color:red}}", ["chrome 100"])).toBe(
9368+
"a .\\31 23{color:red}"
9369+
);
9370+
});
9371+
93199372
it("writes a rule nested two deep", () => {
93209373
expect(
93219374
minifyFor("a{color:red;& b{& c{color:green}}}", ["chrome 100"])
@@ -9400,6 +9453,22 @@ describe("CssSyntax minify — `:dir()` as the attribute it approximates", () =>
94009453
).toBe("a:dir(rtl){color:red}");
94019454
});
94029455

9456+
it("steps over the part a dropped pseudo-element colon left empty", () => {
9457+
expect(
9458+
minifyForWith("a:dir(rtl)::before{color:red}", ["chrome 80"], {
9459+
rewriteDirSelector: true
9460+
})
9461+
).toBe("a[dir=rtl]:before{color:red}");
9462+
});
9463+
9464+
it("steps over a part a comment printed away left empty", () => {
9465+
expect(
9466+
minifyForWith("a:/**/dir(rtl){color:red}", ["chrome 80"], {
9467+
rewriteDirSelector: true
9468+
})
9469+
).toBe("a[dir=rtl]{color:red}");
9470+
});
9471+
94039472
it("leaves a `::dir()` alone, which is no pseudo-class", () => {
94049473
expect(
94059474
minifyForWith("a::dir(rtl){color:red}", ["chrome 80"], {
@@ -9432,6 +9501,11 @@ describe("CssSyntax minify — `@custom-media`", () => {
94329501
expect(minifyFor(sheet, ["chrome 120"])).toBe(sheet);
94339502
});
94349503

9504+
it("leaves a rule stating no query alone", () => {
9505+
const css = "@custom-media --m;@media (--m){a{color:red}}";
9506+
expect(minifyForWith(css, ["chrome 120"], { customMedia: true })).toBe(css);
9507+
});
9508+
94359509
it("leaves a name nothing states alone", () => {
94369510
expect(
94379511
minifyForWith("@media (--nope){a{color:red}}", ["chrome 120"], {
@@ -9479,6 +9553,12 @@ describe("CssSyntax minify — a `color-mix()` the target cannot read", () => {
94799553
).toBe("a{color:color(srgb .5 0 .5)}");
94809554
});
94819555

9556+
it("writes no fallback into a lowered shorthand with them off", () => {
9557+
expect(
9558+
minifyForWith("a{inset:1px}", ["chrome 80"], { colorFallbacks: false })
9559+
).toBe("a{top:1px;right:1px;bottom:1px;left:1px}");
9560+
});
9561+
94829562
it("is off with the fallbacks off", () => {
94839563
const css = "a{color:color-mix(in srgb,red,blue)}";
94849564
expect(minifyForWith(css, ["chrome 80"], { colorFallbacks: false })).toBe(

0 commit comments

Comments
 (0)