Skip to content

Commit 110dfdf

Browse files
edusperoniNathanWalker
authored andcommitted
fix(core): keep var() and calc() in local style values current
A local string value holding var() or calc() is now substituted before it is converted, so style.marginTop = 'calc(2 + var(--t))', view.marginTop = 'var(--t)' and style.margin = 'var(--m)' no longer throw. The specified expression is kept on the style together with the css variables its substitution read. On every cascade pass the kept expressions whose variables changed are substituted again, and a value is written only when its substitution changed. Any other local write to the property, including a reset or a shorthand write that omits the longhand, releases the kept expression, so a later code write wins over later cascade passes.
1 parent 61bf4fe commit 110dfdf

4 files changed

Lines changed: 599 additions & 59 deletions

File tree

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

Lines changed: 235 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ export function isCssVariableExpression(value: string) {
221221
return value.includes('var(--');
222222
}
223223

224-
export function _evaluateCssVariableExpression(view: ViewBase, cssName: string, value: string): string {
224+
/**
225+
* Substitute the css-variables in a value.
226+
* @param reads Collects every variable looked up, with the value the lookup returned.
227+
*/
228+
export function _evaluateCssVariableExpression(view: ViewBase, cssName: string, value: string, reads?: Map<string, string | null>): string {
225229
if (typeof value !== 'string') {
226230
return value;
227231
}
@@ -253,11 +257,14 @@ export function _evaluateCssVariableExpression(view: ViewBase, cssName: string,
253257
const cssVariableName = (commaIdx === -1 ? body : body.substring(0, commaIdx)).trim();
254258
const fallback = commaIdx === -1 ? '' : body.substring(commaIdx + 1).trim();
255259
let cssVariableValue = view.style.getCssVariable(cssVariableName);
260+
if (reads) {
261+
reads.set(cssVariableName, cssVariableValue);
262+
}
256263
if (typeof cssVariableValue === 'string' && cssVariableValue.trim() === 'initial') {
257264
cssVariableValue = null;
258265
}
259266
if (cssVariableValue === null && fallback) {
260-
cssVariableValue = _evaluateCssVariableExpression(view, cssName, fallback);
267+
cssVariableValue = _evaluateCssVariableExpression(view, cssName, fallback, reads);
261268
}
262269

263270
if (!cssVariableValue || cssVariableValue === 'unset') {
@@ -314,6 +321,203 @@ function _replaceKeywordsWithValues(value: string) {
314321
return cssValue;
315322
}
316323

324+
function hasCssExpression(value: unknown): value is string {
325+
return typeof value === 'string' && (isCssVariableExpression(value) || isCssCalcExpression(value));
326+
}
327+
328+
/**
329+
* Evaluate the css-variable and css-calc expressions of a value against the view,
330+
* or `unsetValue` when they do not resolve.
331+
*/
332+
export function _evaluateCssExpressions(view: ViewBase, property: string, value: string, reads?: Map<string, string | null>) {
333+
const newValue = _evaluateCssVariableExpression(view, property, value, reads);
334+
if (newValue === 'unset') {
335+
return unsetValue;
336+
}
337+
338+
try {
339+
return _evaluateCssCalcExpression(newValue);
340+
} catch (e) {
341+
Trace.write(`Failed to evaluate css-calc for property [${property}] for expression [${newValue}] to ${view}. ${e.stack}`, Trace.categories.Error, Trace.messageType.error);
342+
343+
return unsetValue;
344+
}
345+
}
346+
347+
const NO_LONGHAND_VALUES: Record<string, unknown> = Object.freeze({});
348+
349+
function expandSubstitutedShorthand(view: ViewBase, shorthand: string, substituted: unknown): Record<string, unknown> {
350+
if (substituted === unsetValue) {
351+
return NO_LONGHAND_VALUES;
352+
}
353+
354+
const expanded = _expandCssShorthand(shorthand, <string>substituted);
355+
if (!expanded) {
356+
Trace.write(`Failed to expand shorthand [${shorthand}] resolved to [${substituted}] for ${view}.`, Trace.categories.Style, Trace.messageType.warn);
357+
358+
return NO_LONGHAND_VALUES;
359+
}
360+
361+
const values: Record<string, unknown> = {};
362+
for (let i = 0, length = expanded.length; i < length; i++) {
363+
values[expanded[i][0]] = expanded[i][1];
364+
}
365+
366+
return values;
367+
}
368+
369+
/** A pending shorthand evaluated against a view, and the longhand values it expands to. */
370+
export interface _SubstitutedCssShorthand {
371+
substituted: unknown;
372+
values: Record<string, unknown>;
373+
reads: CssVariableReads;
374+
}
375+
376+
type CssVariableReads = Map<string, string | null>;
377+
378+
function cssVariablesUnchanged(view: ViewBase, reads: CssVariableReads): boolean {
379+
for (const [name, value] of reads) {
380+
if (view.style.getCssVariable(name) !== value) {
381+
return false;
382+
}
383+
}
384+
385+
return true;
386+
}
387+
388+
/**
389+
* Evaluate a pending-substitution shorthand against the view and parse it; a value
390+
* that does not survive evaluation expands to no longhands.
391+
*/
392+
export function _substituteCssShorthand(view: ViewBase, pending: CssPendingSubstitution): _SubstitutedCssShorthand {
393+
const reads: CssVariableReads = new Map();
394+
const substituted = _evaluateCssExpressions(view, pending.shorthand, pending.value, reads);
395+
396+
return { substituted, values: expandSubstitutedShorthand(view, pending.shorthand, substituted), reads };
397+
}
398+
399+
function keepLocalCssExpression(style: Style, cssName: string, specified: string | CssPendingSubstitution, substituted: unknown, reads: CssVariableReads): void {
400+
if (!style._localCssExpressions) {
401+
style._localCssExpressions = new Map();
402+
}
403+
style._localCssExpressions.set(cssName, { specified, substituted, reads });
404+
}
405+
406+
function releaseLocalCssExpression(style: Style, cssName: string): void {
407+
const expressions = style._localCssExpressions;
408+
if (expressions && expressions.delete(cssName) && expressions.size === 0) {
409+
style._localCssExpressions = undefined;
410+
}
411+
}
412+
413+
/**
414+
* Wrap a local setter: a string holding var()/calc() is written substituted and kept
415+
* for the cascade to substitute again, any other write releases what was kept.
416+
*/
417+
function localCssExpressionSetter<T extends Style>(cssName: string, set: (this: T, value: unknown) => void): (this: T, value: unknown) => void {
418+
return function (this: T, value: unknown): void {
419+
if (hasCssExpression(value)) {
420+
const view = this.viewRef.get();
421+
if (view) {
422+
const reads: CssVariableReads = new Map();
423+
const substituted = _evaluateCssExpressions(view, cssName, value, reads);
424+
set.call(this, substituted);
425+
keepLocalCssExpression(this, cssName, value, substituted, reads);
426+
427+
return;
428+
}
429+
}
430+
431+
set.call(this, value);
432+
releaseLocalCssExpression(this, cssName);
433+
};
434+
}
435+
436+
function setLocalCssExpression(style: Style, cssName: string, specified: string | CssPendingSubstitution, substituted: unknown, reads: CssVariableReads, value: unknown): void {
437+
style[cssName] = value;
438+
keepLocalCssExpression(style, cssName, specified, substituted, reads);
439+
}
440+
441+
/**
442+
* Write a longhand's share of a pending-substitution shorthand as a local value,
443+
* kept for the cascade to substitute again.
444+
*/
445+
export function _setLocalCssShorthandValue(style: Style, longhand: string, pending: CssPendingSubstitution, shorthand: _SubstitutedCssShorthand): void {
446+
setLocalCssExpression(style, longhand, pending, shorthand.substituted, shorthand.reads, longhand in shorthand.values ? shorthand.values[longhand] : unsetValue);
447+
}
448+
449+
/**
450+
* Substitute the kept local var()/calc() values again, writing only those whose
451+
* substitution changed since they were last written.
452+
*/
453+
export function _reevaluateLocalCssExpressions(view: ViewBase): void {
454+
const style = view.style;
455+
const expressions = style._localCssExpressions;
456+
if (!expressions) {
457+
return;
458+
}
459+
460+
let shorthands: Map<CssPendingSubstitution, _SubstitutedCssShorthand>;
461+
// Writes replace entries, so iterate over a snapshot.
462+
const entries = Array.from(expressions);
463+
for (let i = 0, length = entries.length; i < length; i++) {
464+
const [cssName, expression] = entries[i];
465+
// A change listener may have replaced or released it meanwhile.
466+
if (style._localCssExpressions?.get(cssName) !== expression || cssVariablesUnchanged(view, expression.reads)) {
467+
continue;
468+
}
469+
470+
const specified = expression.specified;
471+
let substituted: unknown;
472+
let reads: CssVariableReads;
473+
let value: unknown;
474+
475+
if (specified instanceof CssPendingSubstitution) {
476+
if (!shorthands) {
477+
shorthands = new Map();
478+
}
479+
let shorthand = shorthands.get(specified);
480+
if (!shorthand) {
481+
reads = new Map();
482+
// Expanded only when the substitution changed.
483+
shorthand = { substituted: _evaluateCssExpressions(view, specified.shorthand, specified.value, reads), values: undefined, reads };
484+
shorthands.set(specified, shorthand);
485+
}
486+
substituted = shorthand.substituted;
487+
reads = shorthand.reads;
488+
if (substituted === expression.substituted) {
489+
expression.reads = reads;
490+
continue;
491+
}
492+
if (!shorthand.values) {
493+
shorthand.values = expandSubstitutedShorthand(view, specified.shorthand, substituted);
494+
}
495+
value = cssName in shorthand.values ? shorthand.values[cssName] : unsetValue;
496+
} else {
497+
reads = new Map();
498+
substituted = _evaluateCssExpressions(view, cssName, specified, reads);
499+
if (substituted === expression.substituted) {
500+
expression.reads = reads;
501+
continue;
502+
}
503+
value = substituted;
504+
}
505+
506+
try {
507+
setLocalCssExpression(style, cssName, specified, substituted, reads, value);
508+
} catch (e) {
509+
Trace.write(`Failed to apply property [${cssName}] with value [${value}] to ${view}. ${e.stack}`, Trace.categories.Error, Trace.messageType.error);
510+
try {
511+
style[cssName] = unsetValue;
512+
} catch (e) {
513+
Trace.write(`Failed to unset property [${cssName}] on ${view}. ${e.stack}`, Trace.categories.Error, Trace.messageType.error);
514+
}
515+
// Kept, so the failing value is not retried every pass.
516+
keepLocalCssExpression(style, cssName, specified, substituted, reads);
517+
}
518+
}
519+
}
520+
317521
function getPropertiesFromMap(map): Property<any, any>[] | CssProperty<any, any>[] {
318522
const symbols = Object.getOwnPropertySymbols(map);
319523
const len = symbols.length;
@@ -1001,7 +1205,7 @@ export class CssProperty<T extends Style, U> {
10011205
enumerable: true,
10021206
configurable: true,
10031207
get: get,
1004-
set: setLocalValue,
1208+
set: localCssExpressionSetter(options.cssName || propertyName, setLocalValue),
10051209
};
10061210

10071211
cssSymbolPropertyMap[key] = this;
@@ -1196,6 +1400,7 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro
11961400
const defaultPropertyDescriptor = descriptor(defaultValueKey, ValueSource.Default, false, false, false);
11971401
const cssPropertyDescriptor = descriptor(cssValue, ValueSource.Css, false, false, false);
11981402
const stylePropertyDescriptor = descriptor(styleValue, ValueSource.Local, true, true, true);
1403+
stylePropertyDescriptor.set = localCssExpressionSetter(cssLocalName, stylePropertyDescriptor.set);
11991404
const keyframePropertyDescriptor = descriptor(keyframeValue, ValueSource.Keyframe, false, false, false);
12001405

12011406
symbolPropertyMap[computedValue] = this;
@@ -1424,7 +1629,7 @@ export class InheritedCssProperty<T extends Style, U> extends CssProperty<T, U>
14241629

14251630
this.setInheritedValue = setInheritedFunc;
14261631
this.cssValueDescriptor.set = setFunc(ValueSource.Css);
1427-
this.localValueDescriptor.set = setFunc(ValueSource.Local);
1632+
this.localValueDescriptor.set = localCssExpressionSetter(options.cssName || propertyName, setFunc(ValueSource.Local));
14281633

14291634
inheritableCssProperties.push(this);
14301635
}
@@ -1455,6 +1660,7 @@ export class ShorthandProperty<T extends Style, P> implements ShorthandProperty<
14551660
cssShorthandsVersion++;
14561661

14571662
const converter = options.converter;
1663+
const cssLocalName = this.cssLocalName;
14581664

14591665
function setLocalValue(this: T, value: string | P): void {
14601666
const view = this.viewRef.get();
@@ -1464,11 +1670,36 @@ export class ShorthandProperty<T extends Style, P> implements ShorthandProperty<
14641670
return;
14651671
}
14661672

1673+
const pendingLonghands = hasCssExpression(value) ? _pendingCssShorthandSubstitution(cssLocalName, value) : undefined;
1674+
if (pendingLonghands) {
1675+
const pending = pendingLonghands[0][1];
1676+
const shorthand = _substituteCssShorthand(view, pending);
1677+
view._batchUpdate(() => {
1678+
for (const [longhand] of pendingLonghands) {
1679+
_setLocalCssShorthandValue(this, longhand, pending, shorthand);
1680+
}
1681+
});
1682+
1683+
return;
1684+
}
1685+
1686+
if (hasCssExpression(value)) {
1687+
value = _evaluateCssExpressions(view, cssLocalName, value);
1688+
}
1689+
14671690
view._batchUpdate(() => {
14681691
for (const [p, v] of converter(value)) {
14691692
this[p.name] = v;
14701693
}
14711694
});
1695+
1696+
// Longhands the converter omits still lose their kept expressions.
1697+
if (this._localCssExpressions) {
1698+
const longhands = getCssShorthandLonghands(cssLocalName);
1699+
for (let i = 0, length = longhands ? longhands.length : 0; i < length; i++) {
1700+
releaseLocalCssExpression(this, longhands[i]);
1701+
}
1702+
}
14721703
}
14731704

14741705
function setCssValue(this: T, value: string): void {

0 commit comments

Comments
 (0)