Skip to content

Commit ba1b8a0

Browse files
fix(native): answer an ancestor chain as a path, not as a set of names
A container is registered by name, and every compound of an ancestor chain was resolved in the element's own scope. So `.outer .inner .x` asked whether both names sit somewhere above the element, and it matched the reversed nesting too. A descendant combinator asks for an `.inner` ancestor that itself has an `.outer` ancestor (Selectors 4 §16.1). Each registration now carries the scope it registered in, beside its element and props. The chain is walked innermost first, each compound resolving in the scope of the one inside it. A single compound resolves as before. Guards stay on the element's own scope, the one it can re-read.
1 parent a5002c5 commit ba1b8a0

6 files changed

Lines changed: 219 additions & 35 deletions

File tree

‎src/__tests__/native/grouping.test.tsx‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { fireEvent, render, screen } from "@testing-library/react-native";
2+
import { compile } from "react-native-css/compiler";
23
import { View } from "react-native-css/components/View";
34
import { registerCSS } from "react-native-css/jest";
45

@@ -108,3 +109,173 @@ test("group selector", () => {
108109

109110
expect(child.props.style).toStrictEqual(undefined);
110111
});
112+
113+
const RED = { color: "#f00" };
114+
const CHAIN_CSS = `.outer .inner .subject { color: red; }`;
115+
116+
test("a chain compiles outermost first, each ancestor class registering a container", () => {
117+
const stylesheet = compile(CHAIN_CSS).stylesheet();
118+
const rule = (name: string) =>
119+
stylesheet.s?.find(([className]) => className === name)?.[1][0];
120+
121+
expect(rule("subject")?.cq).toStrictEqual([
122+
{ n: "g:outer" },
123+
{ n: "g:inner" },
124+
]);
125+
expect(rule("outer")?.c).toStrictEqual(["g:outer"]);
126+
expect(rule("inner")?.c).toStrictEqual(["g:inner"]);
127+
});
128+
129+
test("a chain applies when its ancestors nest in the order it names, with anything between", () => {
130+
registerCSS(CHAIN_CSS);
131+
132+
render(
133+
<>
134+
<View className="outer">
135+
<View className="inner">
136+
<View testID="adjacent" className="subject" />
137+
</View>
138+
</View>
139+
<View className="outer">
140+
<View>
141+
<View className="inner">
142+
<View>
143+
<View testID="intervening" className="subject" />
144+
</View>
145+
</View>
146+
</View>
147+
</View>
148+
</>,
149+
);
150+
151+
expect(screen.getByTestId("adjacent")).toHaveStyle(RED);
152+
expect(screen.getByTestId("intervening")).toHaveStyle(RED);
153+
});
154+
155+
test("a chain does not apply to its ancestors nested the other way round, side by side, or on one element", () => {
156+
registerCSS(CHAIN_CSS);
157+
158+
render(
159+
<>
160+
<View className="inner">
161+
<View className="outer">
162+
<View testID="reversed" className="subject" />
163+
</View>
164+
</View>
165+
<View className="inner" />
166+
<View className="outer">
167+
<View testID="siblings" className="subject" />
168+
</View>
169+
<View className="outer inner">
170+
<View testID="one-element" className="subject" />
171+
</View>
172+
</>,
173+
);
174+
175+
expect(screen.getByTestId("reversed").props.style).toStrictEqual(undefined);
176+
expect(screen.getByTestId("siblings").props.style).toStrictEqual(undefined);
177+
expect(screen.getByTestId("one-element").props.style).toStrictEqual(
178+
undefined,
179+
);
180+
});
181+
182+
test("a three-deep chain answers its order the same way", () => {
183+
registerCSS(`.a .b .c .subject { color: red; }`);
184+
185+
render(
186+
<>
187+
<View className="a">
188+
<View className="b">
189+
<View className="c">
190+
<View testID="ordered" className="subject" />
191+
</View>
192+
</View>
193+
</View>
194+
<View className="a">
195+
<View className="c">
196+
<View className="b">
197+
<View testID="swapped" className="subject" />
198+
</View>
199+
</View>
200+
</View>
201+
</>,
202+
);
203+
204+
expect(screen.getByTestId("ordered")).toHaveStyle(RED);
205+
expect(screen.getByTestId("swapped").props.style).toStrictEqual(undefined);
206+
});
207+
208+
test("each compound's interaction reads its own container", () => {
209+
registerCSS(`
210+
.outer .inner:hover .inner-hover { color: red; }
211+
.outer:hover .inner .outer-hover { color: red; }
212+
`);
213+
214+
render(
215+
<View testID="outer" className="outer">
216+
<View testID="inner" className="inner">
217+
<View testID="inner-hover" className="inner-hover" />
218+
<View testID="outer-hover" className="outer-hover" />
219+
</View>
220+
</View>,
221+
);
222+
223+
fireEvent(screen.getByTestId("inner"), "hoverIn");
224+
expect(screen.getByTestId("inner-hover")).toHaveStyle(RED);
225+
expect(screen.getByTestId("outer-hover").props.style).toStrictEqual(
226+
undefined,
227+
);
228+
229+
fireEvent(screen.getByTestId("inner"), "hoverOut");
230+
fireEvent(screen.getByTestId("outer"), "hoverIn");
231+
expect(screen.getByTestId("inner-hover").props.style).toStrictEqual(
232+
undefined,
233+
);
234+
expect(screen.getByTestId("outer-hover")).toHaveStyle(RED);
235+
});
236+
237+
test("an outer container's attribute is read from that container, and re-derives when it changes", () => {
238+
registerCSS(`.outer[data-state="on"] .inner .subject { color: red; }`);
239+
240+
const tree = (state: string) => (
241+
<>
242+
<View className="outer" {...{ dataSet: { state } }}>
243+
<View className="inner">
244+
<View testID="ordered" className="subject" />
245+
</View>
246+
</View>
247+
<View className="inner">
248+
<View className="outer" {...{ dataSet: { state } }}>
249+
<View testID="reversed" className="subject" />
250+
</View>
251+
</View>
252+
</>
253+
);
254+
255+
render(tree("on"));
256+
expect(screen.getByTestId("ordered")).toHaveStyle(RED);
257+
expect(screen.getByTestId("reversed").props.style).toStrictEqual(undefined);
258+
259+
screen.rerender(tree("off"));
260+
expect(screen.getByTestId("ordered").props.style).toStrictEqual(undefined);
261+
});
262+
263+
test("a chain renders the same styles twice over", () => {
264+
registerCSS(CHAIN_CSS);
265+
266+
const tree = (
267+
<View className="outer">
268+
<View className="inner">
269+
<View testID="subject" className="subject" />
270+
</View>
271+
</View>
272+
);
273+
274+
render(tree);
275+
const first = screen.getByTestId("subject").props.style;
276+
277+
screen.rerender(tree);
278+
279+
expect(screen.getByTestId("subject").props.style).toStrictEqual(first);
280+
expect(screen.getByTestId("subject")).toHaveStyle(RED);
281+
});

‎src/native/conditions/container-query.ts‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,48 @@ import {
1414
focusFamily,
1515
hoverFamily,
1616
type ContainerContextValue,
17+
type ContainerRegistration,
1718
type Getter,
1819
} from "../reactivity";
1920
import { testAttributes } from "./attributes";
2021
import type { RenderGuard } from "./guards";
2122

2223
export const DEFAULT_CONTAINER_NAME = "c:___default___";
2324

25+
// The ancestor compounds arrive outermost first; each resolves in the scope of the one inside it (Selectors 4 §16.1)
2426
export function testContainerQueries(
2527
queries: ContainerQuery[],
2628
inheritedContainers: ContainerContextValue,
2729
guards: RenderGuard[],
2830
get: Getter,
2931
) {
30-
return queries.every((query) => {
31-
return testContainerQuery(query, inheritedContainers, guards, get);
32-
});
32+
// Every container the walk reaches registered above this element, so this element's scope observes a change to any of them
33+
for (const query of queries) {
34+
const name = query.n ?? DEFAULT_CONTAINER_NAME;
35+
guards.push(["c", name, inheritedContainers[name]]);
36+
}
37+
38+
let scope = inheritedContainers;
39+
40+
for (const query of [...queries].reverse()) {
41+
const container = scope[query.n ?? DEFAULT_CONTAINER_NAME];
42+
43+
if (!container || !testContainerQuery(query, container, get)) {
44+
return false;
45+
}
46+
47+
scope = container.scope;
48+
}
49+
50+
return true;
3351
}
3452

3553
export function testContainerQuery(
3654
query: ContainerQuery,
37-
inheritedContainers: ContainerContextValue,
38-
guards: RenderGuard[],
55+
container: ContainerRegistration,
3956
get: Getter,
4057
): boolean {
41-
const name = query.n ?? DEFAULT_CONTAINER_NAME;
42-
const container = inheritedContainers[name]!;
43-
44-
guards.push(["c", name, container]);
45-
46-
if (!container) {
47-
return false;
48-
}
49-
50-
// Ancestor snapshots have their own context guard. Their attributes must not
51-
// be compared with this descendant's props by the ordinary attribute guards.
58+
// The registration's own guard covers a container's props, so this element's attribute guards must not record them
5259
if (query.a && !testAttributes(query.a, container.props, [])) {
5360
return false;
5461
}

‎src/native/conditions/guards.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
import type { ComponentState } from "../react/useNativeCss";
33
import type {
44
ContainerContextValue,
5+
ContainerRegistration,
56
VariableContextValue,
67
} from "../reactivity";
78

89
export type RenderGuard =
910
| ["a", string, any]
1011
| ["d", string, any]
1112
| ["v", string, any]
12-
| ["c", string, WeakKey];
13+
| ["c", string, ContainerRegistration | undefined];
1314

1415
export function testGuards(
1516
state: ComponentState,

‎src/native/react/rules.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
VAR_SYMBOL,
1616
weakFamily,
1717
type ContainerContextValue,
18+
type ContainerRegistration,
1819
type VariableContextValue,
1920
} from "../reactivity";
2021
import { stylesFamily } from "../styles";
@@ -141,24 +142,24 @@ export function updateRules(
141142
}
142143

143144
if (rule.c) {
145+
const registration: ContainerRegistration = {
146+
key: state.ruleEffectGetter,
147+
props: currentProps,
148+
scope: inheritedContainers,
149+
};
150+
144151
// We're going to set a value, so we need to create a new object
145152
if (containers === inheritedContainers) {
146153
containers = {
147154
...inheritedContainers,
148155
// This container becomes the default container
149-
[DEFAULT_CONTAINER_NAME]: {
150-
key: state.ruleEffectGetter,
151-
props: currentProps,
152-
},
156+
[DEFAULT_CONTAINER_NAME]: registration,
153157
};
154158
}
155159

156160
// This this component as the named container
157161
for (const name of rule.c) {
158-
containers![name] = {
159-
key: state.ruleEffectGetter,
160-
props: currentProps,
161-
};
162+
containers![name] = registration;
162163
}
163164

164165
// Enable hover/active/focus/layout handlers

‎src/native/react/useNativeCss.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,16 @@ export function useNativeCss(
170170
}
171171

172172
if (state.containers) {
173-
// Publish a new props snapshot even when this component's own rules did not
174-
// change. Descendants can depend on an ancestor attribute through a group.
173+
// Republished every render, even when this component's own rules did not change: a descendant can ask after its props or the containers above it
175174
const containers = Object.fromEntries(
176175
Object.entries(state.containers).map(([name, container]) => [
177176
name,
178177
container.key === state.ruleEffectGetter
179-
? { key: container.key, props: originalProps }
178+
? {
179+
key: container.key,
180+
props: originalProps,
181+
scope: inheritedContainers,
182+
}
180183
: (inheritedContainers[name] ?? container),
181184
]),
182185
);

‎src/native/reactivity.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,14 @@ Appearance.addChangeListener((event) => colorScheme.set(event.colorScheme));
235235

236236
/** Containers ****************************************************************/
237237

238-
export type ContainerContextValue = Record<
239-
string,
240-
{
241-
key: WeakKey;
242-
props: Record<string, unknown> | null | undefined;
243-
}
244-
>;
238+
export interface ContainerRegistration {
239+
readonly key: WeakKey;
240+
readonly props: Record<string, unknown> | null | undefined;
241+
/** The containers above this one, so `.a .b .x` can ask for an `.a` above the `.b` (Selectors 4 §16.1) */
242+
readonly scope: ContainerContextValue;
243+
}
244+
245+
export type ContainerContextValue = Record<string, ContainerRegistration>;
245246
export const ContainerContext = createContext<ContainerContextValue>({});
246247

247248
export const containerLayoutFamily = weakFamily(() => {

0 commit comments

Comments
 (0)