diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 7568a9f..f94c198 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -1157,8 +1157,15 @@ export function parseLength( if ("unit" in length) { switch (length.unit) { - case "px": - return length.value; + case "px": { + if (length.value === Infinity) { + return 9999; + } else if (length.value === -Infinity) { + return -9999; + } else { + return length.value; + } + } case "rem": if (typeof inlineRem === "number") { return length.value * inlineRem; @@ -2222,7 +2229,11 @@ export function parseDimension( ): StyleDescriptor { switch (unit) { case "px": - return value; + if (value === Infinity) { + return 9999; + } else { + return value; + } case "%": return `${value}%`; case "rnh": diff --git a/src/runtime/native/__tests__/calc.test.tsx b/src/runtime/native/__tests__/calc.test.tsx index e157c04..5734bf9 100644 --- a/src/runtime/native/__tests__/calc.test.tsx +++ b/src/runtime/native/__tests__/calc.test.tsx @@ -2,167 +2,165 @@ import { render, screen } from "@testing-library/react-native"; import { View } from "react-native-css/components/View"; import { registerCSS, testID } from "react-native-css/jest"; -describe("css", () => { - test("calc(10px + 100px)", () => { - registerCSS( - `.my-class { +test("calc(10px + 100px)", () => { + registerCSS( + `.my-class { width: calc(10px + 100px); }`, - ); - - render(); - const component = screen.getByTestId(testID); - - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: { - width: 110, - }, - testID, - }); + ); + + render(); + const component = screen.getByTestId(testID); + + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: { + width: 110, + }, + testID, }); +}); - test("calc(100% - 30px)", () => { - registerCSS( - `.my-class { +test("calc(100% - 30px)", () => { + registerCSS( + `.my-class { width: calc(100% - 30px); }`, - ); + ); - render(); - const component = screen.getByTestId(testID); + render(); + const component = screen.getByTestId(testID); - // React Native does not support calc() with a percentage value, so there should be no style - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - testID, - }); + // React Native does not support calc() with a percentage value, so there should be no style + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + testID, }); +}); - test("calc(2em * 3)", () => { - registerCSS( - `.my-class { +test("calc(2em * 3)", () => { + registerCSS( + `.my-class { width: calc(2em * 2); font-size: 5px }`, - ); - - render(); - const component = screen.getByTestId(testID); - - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: { - width: 20, - fontSize: 5, - }, - testID, - }); + ); + + render(); + const component = screen.getByTestId(testID); + + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: { + width: 20, + fontSize: 5, + }, + testID, }); +}); - test("calc(2rem * 5)", () => { - registerCSS( - `.my-class { +test("calc(2rem * 5)", () => { + registerCSS( + `.my-class { width: calc(2rem * 5) }`, - ); - - render(); - const component = screen.getByTestId(testID); - - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: { - width: 140, - }, - testID, - }); + ); + + render(); + const component = screen.getByTestId(testID); + + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: { + width: 140, + }, + testID, }); +}); - test("calc(var(--variable) + 20px)", () => { - registerCSS( - `.my-class { +test("calc(var(--variable) + 20px)", () => { + registerCSS( + `.my-class { --my-var: 100px; width: calc(var(--my-var) + 20px) }`, - ); - - render(); - const component = screen.getByTestId(testID); - - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: { - width: 120, - }, - testID, - }); + ); + + render(); + const component = screen.getByTestId(testID); + + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: { + width: 120, + }, + testID, }); +}); - test("calc(var(--percent) + 20%)", () => { - registerCSS( - `.my-class { +test("calc(var(--percent) + 20%)", () => { + registerCSS( + `.my-class { --percent: 10%; width: calc(var(--percent) + 20%) }`, - ); - - render(); - const component = screen.getByTestId(testID); - - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: { - width: "30%", - }, - testID, - }); + ); + + render(); + const component = screen.getByTestId(testID); + + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: { + width: "30%", + }, + testID, }); +}); - test("calc(var(--variable) + 20%)", () => { - // React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined` - registerCSS( - `.my-class { +test("calc(var(--variable) + 20%)", () => { + // React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined` + registerCSS( + `.my-class { --variable: 100px; width: calc(var(--variable) + 20%) }`, - ); + ); - render(); - const component = screen.getByTestId(testID); + render(); + const component = screen.getByTestId(testID); - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: {}, - testID, - }); + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: {}, + testID, }); +}); - test("calc(var(--percent) + 20px)", () => { - // React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined` - registerCSS( - `.my-class { +test("calc(var(--percent) + 20px)", () => { + // React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined` + registerCSS( + `.my-class { --percent: 10%; width: calc(var(--percent) + 20px) }`, - ); + ); - render(); - const component = screen.getByTestId(testID); + render(); + const component = screen.getByTestId(testID); - expect(component.type).toBe("View"); - expect(component.props).toStrictEqual({ - children: undefined, - style: {}, - testID, - }); + expect(component.type).toBe("View"); + expect(component.props).toStrictEqual({ + children: undefined, + style: {}, + testID, }); }); @@ -192,3 +190,17 @@ test("calc & colors", () => { testID, }); }); + +test("infinity", () => { + registerCSS(`.my-class { + border-radius: calc(infinity * 1px); + }`); + + render(); + const component = screen.getByTestId(testID); + + expect(component.type).toBe("View"); + expect(component.props.style).toStrictEqual({ + borderRadius: 9999, + }); +});