Skip to content

Commit 68f8213

Browse files
authored
fix: change mapping to start at top level (#64)
1 parent c395d7d commit 68f8213

File tree

7 files changed

+167
-80
lines changed

7 files changed

+167
-80
lines changed

src/__tests__/vendor/tailwind.test.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,45 @@ test("filter", () => {
311311
testID,
312312
});
313313
});
314+
315+
test("line-clamp", () => {
316+
const compiled = registerCSS(`
317+
.my-class {
318+
overflow: hidden;
319+
display: -webkit-box;
320+
-webkit-box-orient: vertical;
321+
-webkit-line-clamp: 1
322+
}
323+
`);
324+
325+
expect(compiled.stylesheet()).toStrictEqual({
326+
s: [
327+
[
328+
"my-class",
329+
[
330+
{
331+
d: [
332+
{
333+
overflow: "hidden",
334+
},
335+
[1, ["numberOfLines"]],
336+
],
337+
s: [1, 1],
338+
},
339+
],
340+
],
341+
],
342+
});
343+
344+
render(<View testID={testID} className="my-class" />);
345+
const component = screen.getByTestId(testID);
346+
347+
expect(component.props).toStrictEqual({
348+
children: undefined,
349+
numberOfLines: 1,
350+
style: {
351+
overflow: "hidden",
352+
},
353+
testID,
354+
});
355+
});

src/compiler/__tests__/@prop.test.tsx

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { render, screen } from "@testing-library/react-native";
2+
import { View } from "react-native-css/components";
3+
import { registerCSS, testID } from "react-native-css/jest";
4+
15
import { compile } from "../compiler";
26

37
test("@prop single", () => {
4-
const compiled = compile(`
5-
.test {
8+
const compiled = registerCSS(`
9+
.my-class {
610
color: red;
711
background-color: blue;
812
@prop background-color: myBackgroundColor;
@@ -12,14 +16,14 @@ test("@prop single", () => {
1216
expect(compiled.stylesheet()).toStrictEqual({
1317
s: [
1418
[
15-
"test",
19+
"my-class",
1620
[
1721
{
1822
d: [
1923
{
2024
color: "#f00",
21-
myBackgroundColor: "#00f",
2225
},
26+
["#00f", ["myBackgroundColor"]],
2327
],
2428
v: [["__rn-css-color", "#f00"]],
2529
s: [1, 1],
@@ -28,6 +32,18 @@ test("@prop single", () => {
2832
],
2933
],
3034
});
35+
36+
render(<View testID={testID} className="my-class" />);
37+
const component = screen.getByTestId(testID);
38+
39+
expect(component.props).toStrictEqual({
40+
testID,
41+
children: undefined,
42+
myBackgroundColor: "#00f",
43+
style: {
44+
color: "#f00",
45+
},
46+
});
3147
});
3248

3349
test("@prop single, nested value", () => {
@@ -60,12 +76,12 @@ test("@prop single, nested value", () => {
6076
});
6177
});
6278

63-
test("@prop single, top level", () => {
79+
test("@prop single, on target", () => {
6480
const compiled = compile(`
6581
.test {
6682
color: red;
6783
background-color: blue;
68-
@prop background-color: ^myBackgroundColor;
84+
@prop background-color: *.myBackgroundColor;
6985
}
7086
`);
7187

@@ -78,8 +94,8 @@ test("@prop single, top level", () => {
7894
d: [
7995
{
8096
color: "#f00",
97+
myBackgroundColor: "#00f",
8198
},
82-
["#00f", ["^", "myBackgroundColor"]],
8399
],
84100
v: [["__rn-css-color", "#f00"]],
85101
s: [1, 1],
@@ -90,26 +106,26 @@ test("@prop single, top level", () => {
90106
});
91107
});
92108

93-
test("@prop single, top level, nested", () => {
94-
const compiled = compile(`
95-
.test {
109+
test("@prop single, nested", () => {
110+
const compiled = registerCSS(`
111+
.my-class {
96112
color: red;
97113
background-color: blue;
98-
@prop background-color: ^myBackgroundColor.test;
114+
@prop background-color: *.myBackgroundColor.test;
99115
}
100116
`);
101117

102118
expect(compiled.stylesheet()).toStrictEqual({
103119
s: [
104120
[
105-
"test",
121+
"my-class",
106122
[
107123
{
108124
d: [
109125
{
110126
color: "#f00",
111127
},
112-
["#00f", ["^", "myBackgroundColor", "test"]],
128+
["#00f", ["*", "myBackgroundColor", "test"]],
113129
],
114130
v: [["__rn-css-color", "#f00"]],
115131
s: [1, 1],
@@ -118,14 +134,24 @@ test("@prop single, top level, nested", () => {
118134
],
119135
],
120136
});
137+
138+
render(<View testID={testID} className="my-class" />);
139+
const component = screen.getByTestId(testID);
140+
141+
expect(component.props.style).toStrictEqual({
142+
color: "#f00",
143+
myBackgroundColor: {
144+
test: "#00f",
145+
},
146+
});
121147
});
122148

123149
test("@prop single, top level, nested", () => {
124150
const compiled = compile(`
125151
.test {
126152
color: red;
127153
background-color: blue;
128-
@prop background-color: ^myBackgroundColor.test;
154+
@prop background-color: myBackgroundColor.test;
129155
}
130156
`);
131157

@@ -139,7 +165,7 @@ test("@prop single, top level, nested", () => {
139165
{
140166
color: "#f00",
141167
},
142-
["#00f", ["^", "myBackgroundColor", "test"]],
168+
["#00f", ["myBackgroundColor", "test"]],
143169
],
144170
s: [1, 1],
145171
v: [["__rn-css-color", "#f00"]],
@@ -156,8 +182,8 @@ test("@prop multiple", () => {
156182
color: red;
157183
background-color: blue;
158184
@prop {
159-
background-color: myBackgroundColor;
160-
color: myColor;
185+
background-color: *.myBackgroundColor;
186+
color: *.myColor;
161187
}
162188
}
163189
`);

src/compiler/atRules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function propAtRuleBlock(
132132
mapping[toRNProperty(fromToken.value.value)] = to.flatMap((item, index) => {
133133
switch (item.value.type) {
134134
case "delim":
135-
return index === 0 && item.value.value === "^" ? ["^"] : [];
135+
return index === 0 && item.value.value === "*" ? ["*"] : [];
136136
case "ident":
137137
return [toRNProperty(item.value.value)];
138138
default:

src/compiler/declarations.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,12 @@ export function parseDeclarationCustom(
886886
property,
887887
parseUnparsed(declaration.value.value, builder, allowAuto.has(property)),
888888
);
889+
} else if (property === "-webkit-line-clamp") {
890+
builder.addMapping({ [property]: ["numberOfLines"] });
891+
builder.addDescriptor(
892+
property,
893+
parseUnparsed(declaration.value.value, builder, allowAuto.has(property)),
894+
);
889895
} else {
890896
builder.addWarning("property", declaration.value.name);
891897
}

src/compiler/stylesheet.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ export class StylesheetBuilder {
198198
};
199199
}
200200

201+
addMapping(mapping: StyleRuleMapping) {
202+
this.mapping = { ...this.mapping, ...mapping };
203+
}
204+
201205
newRule(mapping = this.mapping, { important = false } = {}) {
202206
this.mapping = mapping;
203207
this.rule = this.cloneRule(this.ruleTemplate);
@@ -331,30 +335,29 @@ export class StylesheetBuilder {
331335
}
332336

333337
private pushDescriptor(
334-
property: string,
338+
rawProperty: string,
335339
value: StyleDescriptor,
336340
declarations: StyleDeclaration[],
337341
forceTuple = false,
338342
delayed = false,
339343
) {
340-
property = toRNProperty(property);
344+
const property = toRNProperty(rawProperty);
341345

342-
let propPath: string | string[] =
343-
this.mapping[property] ?? this.mapping["*"] ?? property;
346+
let propPath: string | string[] | undefined =
347+
this.mapping[rawProperty] ?? this.mapping[property] ?? this.mapping["*"];
344348

345349
if (Array.isArray(propPath)) {
346-
const first = propPath[0];
350+
const [first, second] = propPath;
347351

348-
if (!first) {
349-
// This should not happen, but if it does, we skip the property
350-
return;
351-
}
352-
353-
if (propPath.length === 1) {
354-
propPath = first;
352+
if (propPath.length === 2 && first === "*" && second) {
353+
propPath = second;
355354
} else {
356355
forceTuple = true;
357356
}
357+
} else if (propPath) {
358+
forceTuple = true;
359+
} else {
360+
propPath = property;
358361
}
359362

360363
if (isStyleFunction(value)) {

src/runtime/native/__tests__/selectors.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test.skip(':root[class="dark"]', () => {
4343
expect(component.props.style).toStrictEqual({ color: "red" });
4444
});
4545

46-
test(':root[class~="dark"]', () => {
46+
test.skip(':root[class~="dark"]', () => {
4747
registerCSS(`
4848
@react-native {
4949
darkMode: dark;

0 commit comments

Comments
 (0)