Skip to content

Commit 128309a

Browse files
committed
feat(core): let style converters preserve CSS math
Let a view opt specific declarations out of Core calc evaluation after variable substitution. Retain raw scoped variable values alongside evaluated values so the same inherited custom property works with both converters.
1 parent 75824ba commit 128309a

4 files changed

Lines changed: 75 additions & 18 deletions

File tree

‎packages/core/ui/core/properties/index.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ export function _isCssShorthand(cssName: string): boolean {
184184
* when the property is not a shorthand, the value still has to be evaluated per
185185
* view (`var()`/`calc()`), or it does not parse.
186186
*/
187-
export function _expandCssShorthand(cssName: string, value: string): [string, any][] | undefined {
187+
export function _expandCssShorthand(cssName: string, value: string, preserveMath = false): [string, any][] | undefined {
188188
const converter = cssShorthandConverters.get(cssName);
189189
if (!converter) {
190190
return undefined;
191191
}
192192

193-
if (typeof value === 'string' && (isCssVariableExpression(value) || isCssCalcExpression(value))) {
193+
if (typeof value === 'string' && (isCssVariableExpression(value) || (!preserveMath && isCssCalcExpression(value)))) {
194194
return undefined;
195195
}
196196

@@ -221,7 +221,7 @@ export function isCssVariableExpression(value: string) {
221221
return value.includes('var(--');
222222
}
223223

224-
export function _evaluateCssVariableExpression(view: ViewBase, cssName: string, value: string): string {
224+
export function _evaluateCssVariableExpression(view: ViewBase, cssName: string, value: string, preserveMath = false): string {
225225
if (typeof value !== 'string') {
226226
return value;
227227
}
@@ -252,12 +252,12 @@ export function _evaluateCssVariableExpression(view: ViewBase, cssName: string,
252252
const commaIdx = body.indexOf(',');
253253
const cssVariableName = (commaIdx === -1 ? body : body.substring(0, commaIdx)).trim();
254254
const fallback = commaIdx === -1 ? '' : body.substring(commaIdx + 1).trim();
255-
let cssVariableValue = view.style.getCssVariable(cssVariableName);
255+
let cssVariableValue = view.style.getCssVariable(cssVariableName, preserveMath);
256256
if (typeof cssVariableValue === 'string' && cssVariableValue.trim() === 'initial') {
257257
cssVariableValue = null;
258258
}
259259
if (cssVariableValue === null && fallback) {
260-
cssVariableValue = _evaluateCssVariableExpression(view, cssName, fallback);
260+
cssVariableValue = _evaluateCssVariableExpression(view, cssName, fallback, preserveMath);
261261
}
262262

263263
if (!cssVariableValue || cssVariableValue === 'unset') {

‎packages/core/ui/styling/style-scope.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { describe, it, expect, beforeAll } from 'vitest';
33
import { StyleScope, applyInlineStyle, addTaggedAdditionalCSS, removeTaggedAdditionalCSS } from './style-scope';
44
import { StackLayout } from '../layouts/stack-layout';
55
import { Label } from '../label';
6+
import { CssProperty, ShorthandProperty } from '../core/properties';
7+
import { Style } from './style';
68

79
/**
810
* Counts how many times css applies a value to a style property, by wrapping the
@@ -51,6 +53,40 @@ function styledWithParent(css: string, parentClassName: string): { view: any; pa
5153
return { view, parent, scope };
5254
}
5355

56+
describe('Style.preserveCssMath', () => {
57+
const paint = new CssProperty<Style, string>({ name: 'rawPaint', cssName: 'raw-paint' });
58+
paint.register(Style);
59+
new ShorthandProperty<Style, string>({
60+
name: 'rawPaintShorthand',
61+
cssName: 'raw-paint-shorthand',
62+
getter() {
63+
return this[paint.name];
64+
},
65+
converter(value) {
66+
return [[paint, value]];
67+
},
68+
}).register(Style);
69+
70+
it('substitutes inherited variables without reducing the selected expression', () => {
71+
const { view, parent } = styledWithParent('.root { --paint: var(--base); --base: calc(0.2 + 0.3); } label { raw-paint: var(--paint); opacity: var(--paint); }', 'root');
72+
view.style.preserveCssMath = (name: string) => name === 'raw-paint';
73+
parent._cssState.onLoaded();
74+
view._cssState.onLoaded();
75+
expect(parent.style.getCssVariable('--paint')).toBe('0.5');
76+
expect(view.style.rawPaint).toBe('calc(0.2 + 0.3)');
77+
expect(view.style.opacity).toBe(0.5);
78+
});
79+
80+
it('passes math through pending shorthands and keeps longhand priority', () => {
81+
const { view } = styled('label { --base: 2px; raw-paint-shorthand: calc(var(--base) + 2px); raw-paint: 3px; }');
82+
view.style.preserveCssMath = (name: string) => name === 'raw-paint-shorthand';
83+
view._cssState.onLoaded();
84+
expect(view.style.rawPaint).toBe('3px');
85+
applyInlineStyle(view, 'raw-paint-shorthand: var(--missing, calc(2px + 2px));');
86+
expect(view.style.rawPaint).toBe('calc(2px + 2px)');
87+
});
88+
});
89+
5490
describe('CssState.setPropertyValues', () => {
5591
it('applies matched declarations', () => {
5692
const { view } = styled('label { color: red; }');

‎packages/core/ui/styling/style-scope.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ const pattern = /('|")(.*?)\1/;
103103
* against the view; a value that does not survive evaluation leaves its longhands unset.
104104
*/
105105
function resolvePendingSubstitution(view: ViewBase, pending: CssPendingSubstitution): Record<string, unknown> {
106-
const value = evaluateCssExpressions(view, pending.shorthand, pending.value);
106+
const preserveMath = view.style.preserveCssMath?.(pending.shorthand) === true;
107+
const value = evaluateCssExpressions(view, pending.shorthand, pending.value, preserveMath);
107108
if (value === unsetValue) {
108109
return CssState.emptyPropertyBag;
109110
}
110111

111-
const expanded = _expandCssShorthand(pending.shorthand, value);
112+
const expanded = _expandCssShorthand(pending.shorthand, value, preserveMath);
112113
if (!expanded) {
113114
Trace.write(`Failed to expand shorthand [${pending.shorthand}] resolved to [${value}] for ${view}.`, Trace.categories.Style, Trace.messageType.warn);
114115

@@ -126,12 +127,15 @@ function resolvePendingSubstitution(view: ViewBase, pending: CssPendingSubstitut
126127
/**
127128
* Evaluate css-variable and css-calc expressions
128129
*/
129-
function evaluateCssExpressions(view: ViewBase, property: string, value: string) {
130-
const newValue = _evaluateCssVariableExpression(view, property, value);
130+
function evaluateCssExpressions(view: ViewBase, property: string, value: string, preserveMath = view.style.preserveCssMath?.(property) === true) {
131+
const newValue = _evaluateCssVariableExpression(view, property, value, preserveMath);
131132
if (newValue === 'unset') {
132133
return unsetValue;
133134
}
134135

136+
if (preserveMath) {
137+
return newValue;
138+
}
135139
value = newValue;
136140

137141
try {
@@ -145,8 +149,8 @@ function evaluateCssExpressions(view: ViewBase, property: string, value: string)
145149
return value;
146150
}
147151

148-
function evaluateCssVariable(view: ViewBase, name: string, value: string): string | null {
149-
const evaluated = evaluateCssExpressions(view, name, value);
152+
function evaluateCssVariable(view: ViewBase, name: string, value: string, preserveMath: boolean): string | null {
153+
const evaluated = evaluateCssExpressions(view, name, value, preserveMath);
150154

151155
return evaluated === unsetValue ? null : evaluated;
152156
}
@@ -159,7 +163,8 @@ function settleCssVariables(view: ViewBase, cssExpsProperties: Record<string, st
159163
names = [];
160164
}
161165
names.push(property);
162-
view.style.setScopedCssVariable(property, evaluateCssVariable(view, property, cssExpsProperties[property]));
166+
const authored = cssExpsProperties[property];
167+
view.style.setScopedCssVariable(property, evaluateCssVariable(view, property, authored, false), evaluateCssVariable(view, property, authored, true));
163168
}
164169
}
165170
if (!names || names.length < 2 || !hasForwardCssVariableReference(names, cssExpsProperties)) {
@@ -169,9 +174,11 @@ function settleCssVariables(view: ViewBase, cssExpsProperties: Record<string, st
169174
for (let pass = 1; pass < names.length; pass++) {
170175
let changed = false;
171176
for (const name of names) {
172-
const value = evaluateCssVariable(view, name, cssExpsProperties[name]);
173-
if (value !== view.style.getCssVariable(name)) {
174-
view.style.setScopedCssVariable(name, value);
177+
const authored = cssExpsProperties[name];
178+
const value = evaluateCssVariable(view, name, authored, false);
179+
const rawValue = evaluateCssVariable(view, name, authored, true);
180+
if (value !== view.style.getCssVariable(name) || rawValue !== view.style.getCssVariable(name, true)) {
181+
view.style.setScopedCssVariable(name, value, rawValue);
175182
changed = true;
176183
}
177184
}

‎packages/core/ui/styling/style/index.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface CommonLayoutParams {
3737
export class Style extends Observable {
3838
private unscopedCssVariables = new Map<string, string>();
3939
private scopedCssVariables = new Map<string, string>();
40+
private rawScopedCssVariables?: Map<string, string | null>;
41+
42+
/** When true for a declaration, pass math to its converter after CSS variable substitution. */
43+
public preserveCssMath?: (property: string) => boolean;
4044

4145
constructor(ownerView: ViewBase | WeakRef<ViewBase>) {
4246
super();
@@ -49,8 +53,13 @@ export class Style extends Observable {
4953
}
5054
}
5155

52-
public setScopedCssVariable(varName: string, value: string): void {
56+
public setScopedCssVariable(varName: string, value: string, rawValue: string | null = value): void {
5357
this.scopedCssVariables.set(varName, value);
58+
if (rawValue !== value) {
59+
(this.rawScopedCssVariables ??= new Map()).set(varName, rawValue);
60+
} else {
61+
this.rawScopedCssVariables?.delete(varName);
62+
}
5463
}
5564

5665
public setUnscopedCssVariable(varName: string, value: string): void {
@@ -59,13 +68,14 @@ export class Style extends Observable {
5968

6069
public removeScopedCssVariable(varName: string): void {
6170
this.scopedCssVariables.delete(varName);
71+
this.rawScopedCssVariables?.delete(varName);
6272
}
6373

6474
public removeUnscopedCssVariable(varName: string): void {
6575
this.unscopedCssVariables.delete(varName);
6676
}
6777

68-
public getCssVariable(varName: string): string | null {
78+
public getCssVariable(varName: string, preserveMath = false): string | null {
6979
const view = this.view;
7080
if (!view) {
7181
return null;
@@ -76,18 +86,22 @@ export class Style extends Observable {
7686
}
7787

7888
if (this.scopedCssVariables.has(varName)) {
89+
if (preserveMath && this.rawScopedCssVariables?.has(varName)) {
90+
return this.rawScopedCssVariables.get(varName);
91+
}
7992
return this.scopedCssVariables.get(varName);
8093
}
8194

8295
if (!view.parent || !view.parent.style) {
8396
return null;
8497
}
8598

86-
return view.parent.style.getCssVariable(varName);
99+
return view.parent.style.getCssVariable(varName, preserveMath);
87100
}
88101

89102
public resetScopedCssVariables() {
90103
this.scopedCssVariables.clear();
104+
this.rawScopedCssVariables?.clear();
91105
}
92106

93107
public resetUnscopedCssVariables() {

0 commit comments

Comments
 (0)