Skip to content

Commit f910a68

Browse files
test(compiler): assert the dir fold and the i flag the compiler emits
751 of this branch's test lines are in the runtime plane and none was in the compiler one, though the fold and the flag are both compile-time transforms. A runtime that folded unconditionally would answer the same for a selector that never asked for `i`, so only the emitted query can say the flag survived. Eight cases. Reverting selector-builder.ts reddens the three dir folds and the two flag-carrying cases, and leaves green the already-lowercase dir and the two absence cases — which is what separates a discriminating set from one merely coupled to the change.
1 parent 71c0dcf commit f910a68

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { compile } from "react-native-css/compiler";
2+
3+
function firstRule(css: string, className: string): Record<string, unknown> {
4+
const entry = (compile(css).stylesheet().s ?? []).find(
5+
([name]) => name === className,
6+
);
7+
if (!entry) {
8+
throw new Error(`the compiler emitted no rule for .${className}`);
9+
}
10+
const [, declarations] = entry;
11+
return declarations[0] as unknown as Record<string, unknown>;
12+
}
13+
14+
/**
15+
* `dir` is on HTML's ASCII-case-insensitive attribute list, so a browser answers
16+
* `[dir="RTL"]` for `dir="rtl"` with no `i` flag written. The media condition it
17+
* compiles to compares literally, so the fold has to happen here.
18+
*/
19+
describe("the dir operand is folded at compile time", () => {
20+
const DIR_CASES = [
21+
["upper", `.t[dir="RTL"] { color: red }`, "t"],
22+
["mixed", `.t[dir="Rtl"] { color: red }`, "t"],
23+
["already lower", `.t[dir="rtl"] { color: red }`, "t"],
24+
["an explicit i flag is a no-op", `.t[dir="RTL" i] { color: red }`, "t"],
25+
] as const;
26+
27+
test("the census is not empty, so the cases below are not vacuous", () => {
28+
expect(DIR_CASES.length).toBeGreaterThan(0);
29+
});
30+
31+
test.each(DIR_CASES)("%s", (_label, css, className) => {
32+
expect(firstRule(css, className).m).toStrictEqual([["=", "dir", "rtl"]]);
33+
});
34+
});
35+
36+
/**
37+
* Selectors §6.3 — the `i` flag is part of the selector, so a runtime that folds
38+
* unconditionally would answer the same for a selector that never asked. The
39+
* flag reaching the query is what keeps the two distinguishable.
40+
*/
41+
describe("the case-sensitivity flag reaches the attribute query", () => {
42+
test("an i flag is carried as a fifth element", () => {
43+
expect(firstRule(`.u[data-x="A" i] { color: red }`, "u").aq).toStrictEqual([
44+
["d", "x", "=", "A", "i"],
45+
]);
46+
});
47+
48+
test("its absence is carried too — the operand keeps its case", () => {
49+
expect(firstRule(`.u[data-x="A"] { color: red }`, "u").aq).toStrictEqual([
50+
["d", "x", "=", "A"],
51+
]);
52+
});
53+
54+
test("a plain attribute carries the flag the same way", () => {
55+
expect(firstRule(`.u[title="A" i] { color: red }`, "u").aq).toStrictEqual([
56+
["a", "title", "=", "A", "i"],
57+
]);
58+
});
59+
});

0 commit comments

Comments
 (0)