Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/__tests__/compiler/compiler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,73 @@ test("a transform value the compiler cannot convert is ignored, with a warning",
});
});

test("a scale along z or a rotate about an oblique axis warns", () => {
const compiled = compile(`
.my-class {
scale: 2 3 400%;
rotate: 1 1 0 45deg;
}
.unwarned {
scale: 2 3 100%;
translate: 10px 20px 30px;
rotate: 0 0 0 45deg;
}`);

expect(compiled.stylesheet()).toStrictEqual({
s: [
[
"my-class",
[
{
d: [
[
[
[{}, "scaleX", 2],
[{}, "scaleY", 3],
],
"scale",
],
],
s: [1, 1],
},
],
],
[
"unwarned",
[
{
d: [
[
[
[{}, "scaleX", 2],
[{}, "scaleY", 3],
],
"scale",
],
[
[
[{}, "translateX", 10],
[{}, "translateY", 20],
],
"translate",
],
[[{}, "rotateZ", "0deg"], "rotate"],
],
s: [2, 1],
},
],
],
],
});

expect(compiled.warnings()).toStrictEqual({
values: {
scale: ["400%"],
rotate: ["1 1 0 45deg"],
},
});
});

test("simplifies rem", () => {
const compiled = compile(`.test {
border-width: calc(10rem + 2px);
Expand Down
39 changes: 33 additions & 6 deletions src/__tests__/native/transform.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,24 @@ test.each([
},
);

// Chromium 153, Firefox 155 and WebKit 26.6 rotate about the axis a keyword or a vector along one axis names
// Chromium 153 and Firefox 155 rotate about the axis a vector names, reversing the angle about a negative axis, and rotate by none about the zero vector; WebKit 26.6 ignores the direction of a z axis
test.each([
["rotate: 0 0 -1 45deg;", [{ rotateZ: "-45deg" }]],
["--r: 0 0 -1 45deg; rotate: var(--r);", [{ rotateZ: "-45deg" }]],
["rotate: -1 0 0 45deg;", [{ rotateX: "-45deg" }]],
["--r: -1 0 0 45deg; rotate: var(--r);", [{ rotateX: "-45deg" }]],
["rotate: 0 0 2 45deg;", [{ rotateZ: "45deg" }]],
["--r: 0 0 2 45deg; rotate: var(--r);", [{ rotateZ: "45deg" }]],
["rotate: x 45deg;", [{ rotateX: "45deg" }]],
["--r: x 45deg; rotate: var(--r);", [{ rotateX: "45deg" }]],
["rotate: 45deg y;", [{ rotateY: "45deg" }]],
["--r: 20deg; rotate: var(--r);", [{ rotate: "20deg" }]],
["--r: 45deg y; rotate: var(--r);", [{ rotateY: "45deg" }]],
["--r: 20deg; rotate: var(--r);", [{ rotateZ: "20deg" }]],
["--r: x; rotate: var(--r) 45deg;", [{ rotateX: "45deg" }]],
["--r: 0 0 -1; rotate: var(--r) 45deg;", [{ rotateZ: "-45deg" }]],
["rotate: 0 0 0 45deg;", [{ rotateZ: "0deg" }]],
["--r: 0 0 0 45deg; rotate: var(--r);", [{ rotateZ: "0deg" }]],
["--r: none; rotate: var(--r);", [{ rotateZ: "0deg" }]],
] as const)("%s rotates about the axis it names", (declarations, transform) => {
expect(
renderStyle(
Expand All @@ -435,9 +447,12 @@ test.each([
"--t: 20px 30px; translate: 10px var(--t);",
[{ translateX: 10 }, { translateY: 20 }],
],
["--t: none; translate: var(--t);", [{ translateX: 0 }, { translateY: 0 }]],
["--t: NONE; translate: var(--t);", [{ translateX: 0 }, { translateY: 0 }]],
["scale: 2 3 4;", [{ scaleX: 2 }, { scaleY: 3 }]],
["--s: 2 3 4; scale: var(--s);", [{ scaleX: 2 }, { scaleY: 3 }]],
["--s: 3 4; scale: 2 var(--s);", [{ scaleX: 2 }, { scaleY: 3 }]],
["--s: none; scale: var(--s);", [{ scaleX: 1 }, { scaleY: 1 }]],
] as const)("%s renders its x and y", (declarations, transform) => {
expect(
renderStyle(
Expand Down Expand Up @@ -483,11 +498,23 @@ test.each([
});
});

// React Native's rotate keys turn only about an axis, so a substituted rotate about a vector off every axis is none
test("--r: 1 1 0 45deg; rotate: var(--r); rotates by none", () => {
// React Native's rotate keys turn only about an axis, so a rotate about a vector off every axis is ignored when written and none when substituted
test.each([
["rotate: 1 1 0 45deg;", [{ rotateZ: "10deg" }]],
["--r: 1 1 0 45deg; rotate: var(--r);", []],
] as const)("%s rotates by none", (declarations, transform) => {
expect(
renderStyle(
`${decoy} .earlier { rotate: 10deg; } .my-class { ${declarations} }`,
),
).toStrictEqual(transform.length === 0 ? {} : { transform });
});

// Chromium 153, Firefox 155 and WebKit 26.6: a later transform without a value replaces an earlier one, and is none (css-variables-1 §3.1)
test("transform: var(--missing) replaces an earlier rule's transform", () => {
expect(
renderStyle(
`${decoy} .earlier { rotate: 10deg; } .my-class { --r: 1 1 0 45deg; rotate: var(--r); }`,
`${decoy} .earlier { transform: rotate(10deg); } .my-class { transform: var(--missing); }`,
),
).toStrictEqual({});
).toStrictEqual({ transform: [] });
});
12 changes: 1 addition & 11 deletions src/__tests__/native/variable-fold-parity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,7 @@ const VALUE_CENSUS: Record<ValueKind, readonly Shape[]> = {
{ property: "margin-top", value: "-4px" },
{ property: "width", value: "1rem" },
],
"angle": [
{
property: "rotate",
value: "45deg",
knownDivergence: {
kept: { transform: [{ rotate: "45deg" }] },
openDefect:
"the runtime keeps `rotate`, where the compiler writes rotateZ; #389 writes rotateZ on both",
},
},
],
"angle": [{ property: "rotate", value: "45deg" }],
"function": [
{ property: "width", value: "calc(10px + 2px)" },
{ property: "transform", value: "translateX(10px)" },
Expand Down
62 changes: 39 additions & 23 deletions src/compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type {
UnresolvedColor,
} from "lightningcss";

import { isStyleFunction } from "../utilities";
import { isStyleFunction, negateAngle, rotationAxis } from "../utilities";
import { isMappingProperty } from "./atRules";
import type {
StyleDescriptor,
Expand Down Expand Up @@ -120,8 +120,9 @@ const unparsedRuntimeParsing = new Set([
"translate",
]);

// css-transforms-2 §3: the translate and scale properties take an optional z, which the functions of the same name do not
// css-transforms-2 §3: the individual transform properties each have a grammar of their own, which the transform function of the same name does not share
const INDIVIDUAL_TRANSFORM_RESOLVERS: ReadonlyMap<string, string> = new Map([
["rotate", "individualRotate"],
["scale", "individualScale"],
["translate", "individualTranslate"],
]);
Expand Down Expand Up @@ -868,35 +869,50 @@ function parseRotate(
{ value }: DeclarationType<"rotate">,
builder: StylesheetBuilder,
) {
const angle = parseAngle(value.angle, builder);
const rotations: TransformFunction[] = [];
const rotation = rotationAxis(value.x, value.y, value.z);

if (value.x) {
rotations.push([{}, "rotateX", angle]);
}

if (value.y) {
rotations.push([{}, "rotateY", angle]);
}

if (value.z) {
rotations.push([{}, "rotateZ", angle]);
}

if (hasUnconvertibleArgument(rotations)) {
return;
switch (rotation.type) {
case "none":
builder.addDescriptor("rotate", [{}, "rotateZ", "0deg"]);
return;
case "axis": {
const angle = parseAngle(value.angle, builder);

if (angle !== undefined) {
builder.addDescriptor("rotate", [
{},
rotation.rotation,
rotation.reversed ? negateAngle(angle) : angle,
]);
}
return;
}
// React Native's rotate keys turn only about an axis, so a vector off every axis has none to take it
case "oblique":
builder.addWarning(
"value",
`${value.x} ${value.y} ${value.z} ${value.angle.value}${value.angle.type}`,
);
return;
default:
rotation satisfies never;
}

builder.addDescriptor(
"rotate",
rotations.length === 1 ? rotations[0] : rotations,
);
}

function parseScale(
{ value }: DeclarationType<"scale">,
builder: StylesheetBuilder,
) {
// React Native has no z axis to scale along, and lightningcss holds a percentage as its fraction
if (value !== "none" && value.z.value !== 1) {
builder.addWarning(
"value",
value.z.type === "percentage"
? `${round(value.z.value * 100)}%`
: `${round(value.z.value)}`,
);
}

builder.addDescriptor("scale", [
[{}, "scaleX", parseScaleValue(value, "x", builder)],
[{}, "scaleY", parseScaleValue(value, "y", builder)],
Expand Down
95 changes: 95 additions & 0 deletions src/native/styles/functions/transform-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { StyleDescriptor } from "react-native-css/compiler";
import {
commaListItems,
isStyleDescriptorArray,
negateAngle,
rotationAxis,
} from "react-native-css/utilities";

import type { SimpleResolveValue, StyleFunctionResolver } from "../resolve";
Expand Down Expand Up @@ -35,6 +37,11 @@ export const individualTranslate: StyleFunctionResolver = (
descriptor,
) => {
const operands = resolveOperands(resolveValue, descriptor[2]);

if (operands.length === 1 && isNoneKeyword(operands[0])) {
return [{ translateX: 0 }, { translateY: 0 }];
}

const z = operands.length > 2 ? operands[2] : 0;

return operands.length <= 3 && typeof z === "number"
Expand All @@ -48,13 +55,101 @@ export const individualScale: StyleFunctionResolver = (
descriptor,
) => {
const operands = resolveOperands(resolveValue, descriptor[2]);

if (operands.length === 1 && isNoneKeyword(operands[0])) {
return [{ scaleX: 1 }, { scaleY: 1 }];
}

const z = operands.length > 2 ? operands[2] : 1;

return operands.length <= 3 && scaleFactor(z) !== null
? scalePair(operands, Math.min(operands.length, 2))
: null;
};

// css-transforms-2 §3: `rotate: none | <angle> | [x | y | z | <number>{3}] && <angle>`
export const individualRotate: StyleFunctionResolver = (
resolveValue,
descriptor,
) => {
const operands = resolveOperands(resolveValue, descriptor[2]);

if (operands.length === 1) {
const angle = operands[0];

if (isNoneKeyword(angle)) {
return { rotateZ: "0deg" };
}

return isTransformComponent(angle) ? { rotateZ: angle } : null;
}

const axisIndex = operands.findIndex(isAxisKeyword);

if (axisIndex !== -1) {
const axis = operands[axisIndex];
const angle = operands[axisIndex === 0 ? 1 : 0];

return operands.length === 2 &&
isAxisKeyword(axis) &&
isTransformComponent(angle)
? { [AXIS_ROTATIONS[axis]]: angle }
: null;
}

const angleIndex = operands.length === 4 ? angleOperandIndex(operands) : -1;
const angle = operands[angleIndex];
const [x, y, z] = operands.filter((_, index) => index !== angleIndex);

if (
angleIndex === -1 ||
!isTransformComponent(angle) ||
typeof x !== "number" ||
typeof y !== "number" ||
typeof z !== "number"
) {
return null;
}

const rotation = rotationAxis(x, y, z);

switch (rotation.type) {
case "none":
return { rotateZ: "0deg" };
case "axis":
return {
[rotation.rotation]: rotation.reversed ? negateAngle(angle) : angle,
};
case "oblique":
return null;
default:
rotation satisfies never;
return null;
}
};

const AXIS_ROTATIONS = {
x: "rotateX",
y: "rotateY",
z: "rotateZ",
} as const;

function isAxisKeyword(value: unknown): value is keyof typeof AXIS_ROTATIONS {
return value === "x" || value === "y" || value === "z";
}

// The angle of a vector rotation comes first or last, around three numbers
function angleOperandIndex(operands: readonly unknown[]): number {
const index = operands.findIndex((operand) => typeof operand === "string");

return index === 0 || index === operands.length - 1 ? index : -1;
}

// `none` is the identity transform, and React Native refuses the keyword itself
function isNoneKeyword(value: unknown): boolean {
return typeof value === "string" && value.toLowerCase() === "none";
}

// The first `count` operands, so a caller can leave out a z without copying the list
function scalePair(operands: readonly unknown[], count: number): unknown {
const x = scaleFactor(operands[0]);
Expand Down
1 change: 1 addition & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./equality";
export * from "./specificity";
export * from "./style-descriptor";
export * from "./dot-notation.types";
export * from "./rotation-axis";
Loading