Skip to content
Merged
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
99 changes: 99 additions & 0 deletions src/__tests__/compiler/property.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,15 @@ test("the last @property declaration of a name decides inherits", () => {
expect(nonInheritingLast.stylesheet().vp).toStrictEqual([["flip", false]]);
});

test("an ignored @property leaves the earlier registration of its name", () => {
const result = compile(`
@property --x { syntax: "<length>"; inherits: false; initial-value: 3px; }
@property --x { syntax: "<length>"; inherits: true; initial-value: 2em; }
`).stylesheet();

expect(result.vp).toStrictEqual([["x", false, 3]]);
});

test.each([
["<time>", "1s"],
["<resolution>", "2dppx"],
Expand Down Expand Up @@ -472,6 +481,36 @@ test("@property with repeated multi-child preserves array", () => {
]);
});

// A valid registration decides its name over the earlier one, whether or not its initial value has a descriptor; an ignored one leaves the earlier one
function afterAValidRegistration(syntax: string, initialValue: string) {
return compile(`
@property --x { syntax: "<length>"; inherits: false; initial-value: 7px; }
@property --x { syntax: "${syntax}"; inherits: false; initial-value: ${initialValue}; }
`).stylesheet().vp;
}

const earlierRegistration = [["x", false, 7]];

test.each([
"2em",
"1rem",
"1ch",
"2lh",
"1cqw",
"calc(1px + 1em)",
"min(1px, 1em)",
"calc(2 * min(1em, 1px))",
"round(1em, 1px)",
"round(1vw, 1em)",
])(
"an initial value of %s depends on the element, so the registration is ignored",
(initialValue) => {
expect(afterAValidRegistration("<length>", initialValue)).toStrictEqual(
earlierRegistration,
);
},
);

test.each([
["<length>", "3px", 3],
["<length>", "calc(1px + 2px)", 3],
Expand All @@ -484,6 +523,66 @@ test.each([
expect(result.vp).toStrictEqual([["x", false, expected]]);
});

test.each([
["1vw", [["x", false, [{}, "vw", 1, 1]]]],
["2svh", undefined],
["max(1px, 2vw)", undefined],
["calc(2 * min(2px, 1vw))", undefined],
["round(1vw, 1px)", undefined],
])(
"a viewport-relative initial value of %s decides its name",
(initialValue, expected) => {
expect(afterAValidRegistration("<length>", initialValue)).toStrictEqual(
expected,
);
},
);

test.each([
["<transform-function>", "translateX(1em)", false],
["<transform-function>", "translateX(30%)", true],
["<transform-function>", "translate(1em, 0)", false],
["<transform-function>", "translate(3px, 0)", true],
["<transform-function>", "translate3d(0, 0, 1em)", false],
["<transform-function>", "translate3d(3px, 0, 0)", true],
["<transform-function>", "scale(2)", true],
["<transform-function>", "rotate(45deg)", true],
["<transform-function>", "skew(10deg)", true],
["<transform-function>", "matrix(1, 0, 0, 1, 3, 0)", true],
["<transform-list>", "translateX(1em) rotate(45deg)", false],
["<transform-list>", "translateX(3px) rotate(45deg)", true],
])(
"a %s initial value of %s decides its name only when it is element-independent",
(syntax, initialValue, independent) => {
expect(afterAValidRegistration(syntax, initialValue)).toStrictEqual(
independent ? undefined : earlierRegistration,
);
},
);

// Chromium and WebKit keep an <image> registration whose stops depend on the element, and Firefox ignores it; this follows the two
test("an <image> registration with an element-relative stop still decides its name", () => {
expect(
afterAValidRegistration("<image>", "linear-gradient(red 1em, blue)"),
).toStrictEqual(undefined);
});

test("an ignored registration leaves a single declaration to the inliner", () => {
const compiled = compile(`
@property --ignored {
syntax: "<length>";
inherits: false;
initial-value: 2em;
}
:root { --ignored: 10px; }
.child { width: var(--ignored); }
`);

const child = compiled.stylesheet().s?.find(([name]) => name === "child");

expect(JSON.stringify(child)).not.toContain('"var"');
});

test.each([
"--everywhere: dashed;",
"border-width: 1px; --everywhere: dashed;",
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/native/non-inheriting-variables.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ test("an unregistered custom property still inherits", () => {
expect(screen.getByTestId(testID).props.style).toStrictEqual({ width: 10 });
});

test("a registration whose initial value depends on the element is ignored, so the property inherits", () => {
registerCSS(`
@property --my-var {
syntax: "<length>";
inherits: false;
initial-value: 2em;
}
.parent { --my-var: 10px; }
.other { --my-var: 20px; }
.child { width: var(--my-var); }
`);

render(
<View testID={parentTestID} className="parent">
<View testID={testID} className="child" />
</View>,
);

expect(screen.getByTestId(testID).props.style).toStrictEqual({ width: 10 });
});

test("a non-inheriting custom property still applies to the element declaring it", () => {
registerCSS(`
${registration}
Expand Down Expand Up @@ -797,6 +818,18 @@ test("a VariableContextProvider follows a later stylesheet that stops the proper
expect(screen.getByTestId(testID).props.style).toStrictEqual({ width: 1 });
});

test("an ignored @property leaves the earlier registration of its name in force", () => {
registerCSS(`
@property --my-var { syntax: "<length>"; inherits: false; initial-value: 3px; }
@property --my-var { syntax: "<length>"; inherits: true; initial-value: 2em; }
${declaringTree}
`);

renderDeclaringTree();

expect(screen.getByTestId(testID).props.style).toStrictEqual({ width: 3 });
});

test("an element reading :root follows a later stylesheet that stops the property inheriting", () => {
registerCSS(
`:root { --my-var: 10px; } :root { --my-var: 10px; } .child { width: var(--my-var); }`,
Expand Down
9 changes: 7 additions & 2 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
StyleDescriptor,
StyleRuleMapping,
} from "./compiler.types";
import { isComputationallyIndependent } from "./computational-independence";
import { parseContainerCondition } from "./container-query";
import {
parseAngle,
Expand Down Expand Up @@ -437,7 +438,11 @@ function extractContainer(
}
}

// Browsers register the last @property of a name, in a layer, in @container and under an @supports that holds, and one in @media only while it matches
function isValidRegistration({ initialValue }: PropertyRule): boolean {
return initialValue == null || isComputationallyIndependent(initialValue);
}

// Browsers register the last valid @property of a name, in a layer, in @container and under an @supports that holds, and one in @media only while it matches
function collectRegistrations(
rules: readonly Rule[],
conditional = false,
Expand All @@ -446,7 +451,7 @@ function collectRegistrations(
for (const rule of rules) {
switch (rule.type) {
case "property":
if (!conditional) {
if (!conditional && isValidRegistration(rule.value)) {
registrations.set(rule.value.name, rule.value);
}
break;
Expand Down
169 changes: 169 additions & 0 deletions src/compiler/computational-independence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import type {
CalcFor_DimensionPercentageFor_LengthValue,
CalcFor_Length,
DimensionPercentageFor_LengthValue,
Length,
LengthUnit,
LengthValue,
MathFunctionFor_DimensionPercentageFor_LengthValue,
MathFunctionFor_Length,
ParsedComponent,
Transform,
} from "lightningcss";

// An @property initial-value may not depend on the element, so a font- or container-relative unit invalidates the rule (css-properties-values-api-1 §3)
const ELEMENT_RELATIVE_UNITS: ReadonlySet<LengthUnit> = new Set<LengthUnit>([
"em",
"rem",
"ex",
"rex",
"ch",
"rch",
"cap",
"rcap",
"ic",
"ric",
"lh",
"rlh",
"cqw",
"cqh",
"cqi",
"cqb",
"cqmin",
"cqmax",
]);

type LengthOperand = Length | DimensionPercentageFor_LengthValue;

type LengthCalculation =
| CalcFor_Length
| CalcFor_DimensionPercentageFor_LengthValue;

type LengthMathFunction =
| MathFunctionFor_Length
| MathFunctionFor_DimensionPercentageFor_LengthValue;

export function isComputationallyIndependent(
component: ParsedComponent,
): boolean {
switch (component.type) {
case "length":
case "length-percentage":
return isIndependentOperand(component.value);
case "transform-function":
return isIndependentTransform(component.value);
case "transform-list":
return component.value.every(isIndependentTransform);
case "repeated":
return component.value.components.every(isComputationallyIndependent);
case "number":
case "integer":
case "percentage":
case "color":
case "image":
case "url":
case "angle":
case "time":
case "resolution":
case "custom-ident":
case "literal":
case "token-list":
return true;
default:
(component) satisfies never;
return false;
}
}

function isIndependentLength(length: LengthValue): boolean {
return !ELEMENT_RELATIVE_UNITS.has(length.unit);
}

function isIndependentOperand(operand: LengthOperand): boolean {
switch (operand.type) {
case "value":
case "dimension":
return isIndependentLength(operand.value);
case "percentage":
return true;
case "calc":
return isIndependentCalculation(operand.value);
default:
operand satisfies never;
return false;
}
}

function isIndependentCalculation(calculation: LengthCalculation): boolean {
switch (calculation.type) {
case "number":
return true;
case "value":
return isIndependentOperand(calculation.value);
case "sum":
return calculation.value.every(isIndependentCalculation);
case "product":
return isIndependentCalculation(calculation.value[1]);
case "function":
return isIndependentMathFunction(calculation.value);
default:
calculation satisfies never;
return false;
}
}

function isIndependentMathFunction(mathFunction: LengthMathFunction): boolean {
switch (mathFunction.type) {
case "calc":
case "abs":
case "sign":
return isIndependentCalculation(mathFunction.value);
case "min":
case "max":
case "hypot":
case "clamp":
case "rem":
case "mod":
return mathFunction.value.every(isIndependentCalculation);
case "round":
return (
isIndependentCalculation(mathFunction.value[1]) &&
isIndependentCalculation(mathFunction.value[2])
);
default:
mathFunction satisfies never;
return false;
}
}

function isIndependentTransform(transform: Transform): boolean {
switch (transform.type) {
case "translate":
case "translate3d":
return transform.value.every(isIndependentOperand);
case "translateX":
case "translateY":
case "translateZ":
case "perspective":
return isIndependentOperand(transform.value);
case "scale":
case "scaleX":
case "scaleY":
case "scaleZ":
case "scale3d":
case "rotate":
case "rotateX":
case "rotateY":
case "rotateZ":
case "rotate3d":
case "skew":
case "skewX":
case "skewY":
case "matrix":
case "matrix3d":
return true;
default:
transform satisfies never;
return false;
}
}
Loading