Skip to content

Commit 40bfda2

Browse files
fix: evaluate range pairs, and answer a condition the runtime cannot decide with unknown
Media and container queries now evaluate through one walk and one comparison in three-valued logic (MQ5 §3.1): a term the runtime cannot decide is unknown, its negation too, and a rule applies only when its condition is true, as in Chromium, Firefox and WebKit. - A range pair is its two comparisons, where both runtimes answered it false. - A term is unknown when its feature is unmeasured, its value is outside the feature's syntax (a keyword or a type Media Queries 5 does not give it, env(), a negative resolution) or its operand is unresolved, which compiles to null so JSON carries it. A condition the compiler's unknown terms leave no way to be true compiles to no rule, as does a container condition CSS Conditional 5 §5.4 discards. - A number the compiler emits past the largest float compiles to the largest safe integer, since JSON writes Infinity as null. - A feature in a boolean context is false only for zero, none and no-preference. - A viewport length in a condition resolves against the window. - hover, any-hover, pointer and any-pointer answer for the primary input, as Safari and Chrome do on a phone; color, color-index, monochrome, grid, scripting, update, overflow-block, overflow-inline, scan, color-gamut, dynamic-range, their video- forms and -webkit-device-pixel-ratio answer as every React Native screen does. - A keyword and a media type match in any case.
1 parent a5002c5 commit 40bfda2

22 files changed

Lines changed: 2356 additions & 370 deletions

‎src/__tests__/compiler/condition-semantics.test.ts‎

Lines changed: 643 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import { act } from "@testing-library/react-native";
2+
import type {
3+
MediaFeatureComparison,
4+
MediaFeatureOperand,
5+
} from "react-native-css/compiler";
6+
7+
import {
8+
compareMediaFeature,
9+
evaluateCondition,
10+
isTruthyFeatureValue,
11+
testMediaFeatureInterval,
12+
type FeatureReader,
13+
type MeasuredFeatureValue,
14+
type MediaFeatureName,
15+
type MediaInterval,
16+
} from "../../native/conditions/compare";
17+
import { UNKNOWN, type Truth } from "../../native/conditions/kleene";
18+
import { dimensions, type Getter } from "../../native/reactivity";
19+
20+
const get: Getter = (observable) => observable.get();
21+
22+
beforeEach(() => {
23+
act(() => {
24+
dimensions.set({ ...dimensions.get(), width: 600, height: 400 });
25+
});
26+
});
27+
28+
test.each([
29+
[">", [false, false, true]],
30+
[">=", [false, true, true]],
31+
["<", [true, false, false]],
32+
["<=", [true, true, false]],
33+
["=", [false, true, false]],
34+
] as const)("%s compares 39, 40 and 41 against 40", (operator, expected) => {
35+
expect(
36+
[39, 40, 41].map((value) => compareMediaFeature(operator, value, 40, get)),
37+
).toStrictEqual(expected);
38+
});
39+
40+
const operands: [
41+
label: string,
42+
operator: MediaFeatureComparison,
43+
left: MediaFeatureOperand | undefined,
44+
right: MediaFeatureOperand,
45+
truth: Truth,
46+
][] = [
47+
["an unmeasured feature", ">", undefined, 400, UNKNOWN],
48+
["an unresolved operand", ">", 400, null, UNKNOWN],
49+
["an operand only an element resolves", ">", 400, [{}, "em", 10, 1], UNKNOWN],
50+
["a viewport width on a 600px window", ">", 400, [{}, "vw", 50, 1], true],
51+
["a viewport height on a 400px window", "=", 200, [{}, "vh", 50, 1], true],
52+
["a viewport length on either side", "<", [{}, "vh", 50, 1], 201, true],
53+
["a keyword ordered against a number", ">", "landscape", 400, UNKNOWN],
54+
["an equal keyword", "=", "landscape", "landscape", true],
55+
["a different keyword", "=", "portrait", "landscape", false],
56+
];
57+
58+
test.each(operands)("%s", (_label, operator, left, right, truth) => {
59+
expect(compareMediaFeature(operator, left, right, get)).toBe(truth);
60+
});
61+
62+
const intervals: [
63+
label: string,
64+
condition: MediaInterval,
65+
value: MeasuredFeatureValue,
66+
truth: Truth,
67+
][] = [
68+
["inside", ["[]", "width", 400, "<", 800, "<"], 600, true],
69+
["below", ["[]", "width", 400, "<", 800, "<"], 300, false],
70+
["above", ["[]", "width", 400, "<", 800, "<"], 900, false],
71+
["on an open bound", ["[]", "width", 400, "<", 800, "<"], 400, false],
72+
["on a closed bound", ["[]", "width", 400, "<=", 800, "<"], 400, true],
73+
["written descending", ["[]", "width", 800, ">", 400, ">"], 600, true],
74+
["unmeasured", ["[]", "width", 400, "<", 800, "<"], undefined, UNKNOWN],
75+
[
76+
"with an unresolved bound",
77+
["[]", "width", null, "<", 800, "<"],
78+
600,
79+
UNKNOWN,
80+
],
81+
["false on one bound", ["[]", "width", 700, "<", null, "<"], 600, false],
82+
[
83+
"between viewport lengths",
84+
["[]", "width", [{}, "vw", 50, 1], "<", [{}, "vw", 150, 1], "<"],
85+
600,
86+
true,
87+
],
88+
];
89+
90+
test.each(intervals)("an interval %s", (_label, condition, value, truth) => {
91+
expect(testMediaFeatureInterval(condition, value, get)).toBe(truth);
92+
});
93+
94+
test.each([
95+
[400, true],
96+
[0, false],
97+
[Number.NaN, false],
98+
["hover", true],
99+
["none", false],
100+
["no-preference", false],
101+
[undefined, UNKNOWN],
102+
] as const)("%p in a boolean context is %p", (value, truth) => {
103+
expect(isTruthyFeatureValue(value)).toBe(truth);
104+
});
105+
106+
const recordingReader = (
107+
values: Readonly<Partial<Record<MediaFeatureName, MeasuredFeatureValue>>>,
108+
read: MediaFeatureName[],
109+
): FeatureReader<undefined> => ({
110+
read: (name) => {
111+
read.push(name);
112+
return values[name];
113+
},
114+
compare: (condition) => {
115+
read.push(condition[1]);
116+
return condition[0] === "[]"
117+
? testMediaFeatureInterval(condition, values[condition[1]], get)
118+
: compareMediaFeature(
119+
condition[0],
120+
values[condition[1]],
121+
condition[2],
122+
get,
123+
);
124+
},
125+
});
126+
127+
test("an empty conjunction is true and an empty disjunction false", () => {
128+
const reader = recordingReader({}, []);
129+
130+
expect(evaluateCondition(["&", []], reader, undefined)).toBe(true);
131+
expect(evaluateCondition(["|", []], reader, undefined)).toBe(false);
132+
});
133+
134+
test("a decided term stops the evaluation, and an unknown one does not", () => {
135+
const read: MediaFeatureName[] = [];
136+
const reader = recordingReader({ width: 400, height: 300 }, read);
137+
138+
expect(
139+
evaluateCondition(
140+
[
141+
"&",
142+
[
143+
["=", "scan", "progressive"],
144+
[">", "width", 500],
145+
[">", "height", 100],
146+
],
147+
],
148+
reader,
149+
undefined,
150+
),
151+
).toBe(false);
152+
expect(
153+
evaluateCondition(
154+
[
155+
"|",
156+
[
157+
["=", "scan", "progressive"],
158+
[">", "width", 300],
159+
[">", "height", 100],
160+
],
161+
],
162+
reader,
163+
undefined,
164+
),
165+
).toBe(true);
166+
expect(read).toStrictEqual(["scan", "width", "scan", "width"]);
167+
});

0 commit comments

Comments
 (0)