Skip to content

Commit 0d3852d

Browse files
committed
fix(core): replace previous inline declarations when assigning view.style
Assigning an inline style string merged into the previous inline declarations. Like the web's cssText, a new string now replaces them: properties the previous string set and the new one omits are unset and fall back to the stylesheet value (or the default/inherited value).
1 parent e4c2644 commit 0d3852d

3 files changed

Lines changed: 304 additions & 6 deletions

File tree

‎apps/automated/src/ui/styling/style-tests.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,27 @@ export function test_setInlineStyle_setsLocalValues() {
885885
});
886886
}
887887

888+
export function test_setInlineStyle_replacesPreviousDeclarations() {
889+
const testButton = new Button();
890+
testButton.text = 'Test';
891+
const stack = new StackLayout();
892+
stack.addChild(testButton);
893+
894+
helper.buildUIAndRunTest(
895+
stack,
896+
function (views: Array<View>) {
897+
(<any>testButton).style = 'color: red; margin-top: 20;';
898+
helper.assertViewColor(testButton, '#FF0000');
899+
TKUnit.assertEqual(testButton.style.marginTop, 20, 'inline margin-top');
900+
901+
(<any>testButton).style = 'color: blue;';
902+
helper.assertViewColor(testButton, '#0000FF');
903+
TKUnit.assertEqual(testButton.style.marginTop, 8, 'margin-top should fall back to the stylesheet');
904+
},
905+
{ pageCss: 'button { margin-top: 8; }' },
906+
);
907+
}
908+
888909
export function test_setStyle_throws() {
889910
const testButton = new Button();
890911

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { StyleScope } from './style-scope';
4+
import { StackLayout } from '../layouts/stack-layout';
5+
import { Label } from '../label';
6+
7+
function loaded(css: string): { view: any; root: any } {
8+
const scope = new StyleScope();
9+
scope.css = css;
10+
11+
const root: any = new StackLayout();
12+
root._styleScope = scope;
13+
14+
const view: any = new Label();
15+
root.addChild(view);
16+
view._styleScope = scope;
17+
18+
root._cssState.onLoaded();
19+
view._cssState.onLoaded();
20+
21+
return { view, root };
22+
}
23+
24+
function changeValues(target: any, eventName: string): unknown[] {
25+
const values: unknown[] = [];
26+
target.on(eventName, (args: any) => values.push(args.value));
27+
28+
return values;
29+
}
30+
31+
const defaults: any = new Label();
32+
33+
describe('inline style assignment', () => {
34+
it('unsets a longhand the new string drops', () => {
35+
const { view } = loaded('');
36+
37+
view.style = 'color: red; margin-top: 4';
38+
view.style = 'color: blue';
39+
40+
expect(view.style.color.toString()).toBe('#0000FF');
41+
expect(view.style.marginTop).toEqual(defaults.style.marginTop);
42+
});
43+
44+
it('unsets every longhand of a shorthand the new string drops', () => {
45+
const { view } = loaded('');
46+
47+
view.style = 'margin: 4';
48+
view.style = 'color: red';
49+
50+
expect(view.style.marginTop).toEqual(defaults.style.marginTop);
51+
expect(view.style.marginRight).toEqual(defaults.style.marginRight);
52+
expect(view.style.marginBottom).toEqual(defaults.style.marginBottom);
53+
expect(view.style.marginLeft).toEqual(defaults.style.marginLeft);
54+
});
55+
56+
it('keeps the longhands a narrower string still sets', () => {
57+
const { view } = loaded('');
58+
view.style = 'margin: 4';
59+
60+
const marginTopChanges = changeValues(view.style, 'marginTopChange');
61+
view.style = 'margin-top: 4';
62+
63+
expect(view.style.marginTop).toBe(4);
64+
expect(view.style.marginLeft).toEqual(defaults.style.marginLeft);
65+
expect(marginTopChanges).toEqual([]);
66+
});
67+
68+
it('unsets the longhands of a dropped shorthand holding a variable', () => {
69+
const { view } = loaded('label { --m: 6; }');
70+
71+
view.style = 'margin: var(--m)';
72+
expect(view.style.marginTop).toBe(6);
73+
74+
view.style = 'color: red';
75+
76+
expect(view.style.marginTop).toEqual(defaults.style.marginTop);
77+
expect(view.style.marginLeft).toEqual(defaults.style.marginLeft);
78+
});
79+
80+
it('falls back to the stylesheet value of a dropped property', () => {
81+
const { view } = loaded('label { margin-top: 8; color: green; }');
82+
83+
view.style = 'margin-top: 20; color: red';
84+
view.style = 'font-size: 12';
85+
86+
expect(view.style.marginTop).toBe(8);
87+
expect(view.style.color.toString()).toBe('#008000');
88+
});
89+
90+
it('falls back to the stylesheet value shadowed before css was applied', () => {
91+
const scope = new StyleScope();
92+
scope.css = 'label { margin-top: 8; }';
93+
const root: any = new StackLayout();
94+
root._styleScope = scope;
95+
const view: any = new Label();
96+
root.addChild(view);
97+
view._styleScope = scope;
98+
99+
view.style = 'margin-top: 20';
100+
view._cssState.onLoaded();
101+
expect(view.style.marginTop).toBe(20);
102+
103+
view.style = '';
104+
105+
expect(view.style.marginTop).toBe(8);
106+
});
107+
108+
it('falls back to the inherited value of a dropped inherited property', () => {
109+
const { view } = loaded('stacklayout { color: red; }');
110+
111+
view.style = 'color: blue';
112+
view.style = '';
113+
114+
expect(view.style.color.toString()).toBe('#FF0000');
115+
});
116+
117+
it('clears every inline property when assigned an empty string', () => {
118+
const { view } = loaded('');
119+
120+
view.style = 'color: red; margin: 4; font-size: 20';
121+
view.style = '';
122+
123+
expect(view.style.color).toBeUndefined();
124+
expect(view.style.marginTop).toEqual(defaults.style.marginTop);
125+
expect(view.style.fontSize).toEqual(defaults.style.fontSize);
126+
});
127+
128+
it('does not unset and re-apply a property the new string keeps', () => {
129+
const { view } = loaded('');
130+
view.style = 'color: red; margin: 4';
131+
132+
const colorChanges = changeValues(view.style, 'colorChange');
133+
const marginTopChanges = changeValues(view.style, 'marginTopChange');
134+
view.style = 'color: blue; margin-top: 4';
135+
136+
expect(colorChanges.map(String)).toEqual(['#0000FF']);
137+
expect(marginTopChanges).toEqual([]);
138+
});
139+
140+
it('unsets a dropped property applied through the view', () => {
141+
const { view } = loaded('');
142+
143+
view.style = 'text: hello; color: red';
144+
expect(view.text).toBe('hello');
145+
146+
view.style = 'color: red';
147+
148+
expect(view.text).toEqual(defaults.text);
149+
});
150+
151+
it('removes the css variables the new string drops', () => {
152+
const { view } = loaded('');
153+
154+
view.style = '--c: red; color: var(--c)';
155+
expect(view.style.color.toString()).toBe('#FF0000');
156+
157+
view.style = 'margin-top: 4';
158+
159+
expect(view.style.getCssVariable('--c')).toBeNull();
160+
expect(view.style.color).toBeUndefined();
161+
});
162+
163+
it('unsets a kept property whose new value is invalid', () => {
164+
const { view } = loaded('');
165+
166+
view.style = 'color: red';
167+
view.style = 'color: not-a-color';
168+
169+
expect(view.style.color).toBeUndefined();
170+
});
171+
172+
it('keeps an earlier valid declaration when a later one is invalid', () => {
173+
const { view } = loaded('');
174+
175+
view.style = 'color: blue';
176+
view.style = 'color: red; color: not-a-color';
177+
178+
expect(view.style.color.toString()).toBe('#FF0000');
179+
});
180+
181+
it('does not track a property whose only declaration is invalid', () => {
182+
const { view } = loaded('');
183+
view.style.color = 'green';
184+
185+
view.style = 'color: not-a-color';
186+
view.style = '';
187+
188+
expect(view.style.color.toString()).toBe('#008000');
189+
});
190+
191+
it('falls back to the stylesheet value of a dropped animatable property', () => {
192+
const { view } = loaded('label { background-color: green; opacity: 0.5; }');
193+
194+
view.style = 'background-color: red; opacity: 1';
195+
expect(view.style.backgroundColor.toString()).toBe('#FF0000');
196+
197+
view.style = '';
198+
199+
expect(view.style.backgroundColor.toString()).toBe('#008000');
200+
expect(view.style.opacity).toBe(0.5);
201+
});
202+
203+
it('resets a dropped animatable property without a stylesheet value', () => {
204+
const { view } = loaded('');
205+
206+
view.style = 'background-color: red; opacity: 0.5';
207+
view.style = '';
208+
209+
expect(view.style.backgroundColor).toEqual(defaults.style.backgroundColor);
210+
expect(view.style.opacity).toEqual(defaults.style.opacity);
211+
});
212+
213+
it('leaves values set from code alone when inline strings never set them', () => {
214+
const { view } = loaded('');
215+
view.style.marginTop = 10;
216+
217+
view.style = 'color: red';
218+
view.style = '';
219+
220+
expect(view.style.marginTop).toBe(10);
221+
});
222+
});

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

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,26 +1541,68 @@ function resolveFilePathFromImport(importSource: string, fileName: string): stri
15411541
return importSourceParts.join(path.separator);
15421542
}
15431543

1544+
// Holds longhand names, as declarations are expanded while parsing.
1545+
const inlineStyleProperties = new WeakMap<ViewBase, Set<string>>();
1546+
1547+
function setInlineValue(view: ViewBase, property: string, value: unknown): void {
1548+
if (property in view.style) {
1549+
view.style[property] = value;
1550+
} else {
1551+
view[property] = value;
1552+
}
1553+
}
1554+
1555+
function unsetInlineValue(view: ViewBase, property: string): void {
1556+
try {
1557+
setInlineValue(view, property, unsetValue);
1558+
} catch (e) {
1559+
Trace.write(`Failed to unset property [${property}] on ${view}. ${e}`, Trace.categories.Error, Trace.messageType.error);
1560+
}
1561+
}
1562+
15441563
export const applyInlineStyle = profile('applyInlineStyle', function applyInlineStyle(view: ViewBase, styleStr: string) {
15451564
const localStyle = `local { ${styleStr} }`;
15461565
const inlineRuleSet = CSSSource.fromSource(localStyle).selectors;
1566+
const declarations = inlineRuleSet[0].declarations;
15471567

15481568
// Reset unscoped css-variables
15491569
view.style.resetUnscopedCssVariables();
15501570

1571+
let declared: Set<string>;
15511572
// Set all the css-variables first, so we can be sure they are up-to-date
1552-
inlineRuleSet[0].declarations.forEach((d) => {
1573+
declarations.forEach((d) => {
15531574
// Use the actual property name so that a local value is set.
15541575
const property = d.property;
15551576
if (isCssVariable(property)) {
15561577
view.style.setUnscopedCssVariable(property, d.value);
1578+
} else {
1579+
if (!declared) {
1580+
declared = new Set();
1581+
}
1582+
declared.add(property);
15571583
}
15581584
});
15591585

1586+
// Like the web's cssText, a new string replaces every previous inline declaration.
1587+
const previous = inlineStyleProperties.get(view);
1588+
if (previous) {
1589+
previous.forEach((property) => {
1590+
if (!declared?.has(property)) {
1591+
unsetInlineValue(view, property);
1592+
}
1593+
});
1594+
}
1595+
if (declared) {
1596+
inlineStyleProperties.set(view, declared);
1597+
} else if (previous) {
1598+
inlineStyleProperties.delete(view);
1599+
}
1600+
15601601
// Pending-substitution longhands share one placeholder - resolve it once.
15611602
let resolvedShorthands: Map<CssPendingSubstitution, Record<string, unknown>>;
1603+
let applied: Set<string>;
15621604

1563-
inlineRuleSet[0].declarations.forEach((d) => {
1605+
declarations.forEach((d) => {
15641606
// Use the actual property name so that a local value is set.
15651607
const property = d.property;
15661608
try {
@@ -1586,16 +1628,29 @@ export const applyInlineStyle = profile('applyInlineStyle', function applyInline
15861628
value = evaluateCssExpressions(view, property, d.value);
15871629
}
15881630

1589-
if (property in view.style) {
1590-
view.style[property] = value;
1591-
} else {
1592-
view[property] = value;
1631+
setInlineValue(view, property, value);
1632+
if (!applied) {
1633+
applied = new Set();
15931634
}
1635+
applied.add(property);
15941636
} catch (e) {
15951637
Trace.write(`Failed to apply property [${d.property}] with value [${d.value}] to ${view}. ${e}`, Trace.categories.Error, Trace.messageType.error);
15961638
}
15971639
});
15981640

1641+
// A property whose every declaration was invalid counts as not declared.
1642+
declared?.forEach((property) => {
1643+
if (!applied?.has(property)) {
1644+
declared.delete(property);
1645+
if (previous?.has(property)) {
1646+
unsetInlineValue(view, property);
1647+
}
1648+
}
1649+
});
1650+
if (declared?.size === 0) {
1651+
inlineStyleProperties.delete(view);
1652+
}
1653+
15991654
// This is needed in case of changes to css-variable or css-calc expressions.
16001655
view._onCssStateChange();
16011656
});

0 commit comments

Comments
 (0)