Repository navigation
fix(core): replace previous inline declarations when assigning view.style - #11672
Conversation
…tyle 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).
|
View your CI Pipeline Execution ↗ for commit 180eaa2
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
commit: |
Table-driven over margin, padding, gap, transform, background, border-color, border-width, border-radius, font, flex-flow, flex and android-content-inset: dropping the shorthand unsets every longhand, falls back to the stylesheet longhands, and changing its value moves each longhand at most once. Also covers shorthand/longhand order within one string, narrowing a shorthand to one of its longhands, a shorthand that expands to fewer longhands, and unexpanded shorthand values.
NathanWalker
left a comment
There was a problem hiding this comment.
Looks good. Replace semantics match cssText, and I checked the edge cases with throwaway specs: stylesheet var()/calc() fallback, pending var() shorthands, inherited fallback to a parent's inline value, a string assigned before load, invalid/unparsable declarations, the view-branch (text), and all 12 registered shorthands. Framework callers look fine: Vue 3 / react-nativescript set per property, Angular's static style goes through once, Svelte 5 builds a full cssText.
Verified: core vitest 739 passed / 1 skipped, tsc -p packages/core/tsconfig.lib.json clean, prettier clean. The original 17 specs do give 13 fail / 4 pass on main.
Nits (inline), plus:
- The description's numbers are stale (17 specs / 697 passed; it's 59 specs / 739 now).
- Worth deciding whether this gets a changelog note: apps that relied on merging (
view.style = 'a'thenview.style = 'b'to add rules) now losea. - Agree with the documented limitation about the shared local slot (a dropped inline property also clears a code-set value); fine to land ahead of the slot refactor IMO, but it's the case users will notice with
style="{{ }}"bindings plus overlapping attributes.
| }); | ||
| } | ||
| if (declared) { | ||
| inlineStyleProperties.set(view, declared); |
There was a problem hiding this comment.
Nit (re-entrancy): the map is written before the apply loop, so if a change handler fired during the loop assigns view.style again, the inner call replaces this entry and the outer call's remaining properties end up untracked.
Repro: a colorChange handler sets view.style = 'color: green; margin-top: 7' during view.style = 'color: red; font-size: 30'; a later view.style = '' leaves fontSize at 30.
Rare, so either write the map after the loop (only if the entry is still the one read at the start) or just accept it.
| return importSourceParts.join(path.separator); | ||
| } | ||
|
|
||
| // Holds longhand names, as declarations are expanded while parsing. |
There was a problem hiding this comment.
Nit: the set also holds view-branch names (text) and the names of shorthands that didn't parse (margin for margin: oops), so "longhand names" isn't quite accurate. Harmless functionally.
| } | ||
| } | ||
|
|
||
| function loaded(css: string): { view: any; root: any } { |
There was a problem hiding this comment.
Nit: loaded() calls _cssState.onLoaded() but never sets isLoaded, so the view._onCssStateChange() at the end of applyInlineStyle is a no-op in every spec and the cascade re-run after an unset isn't exercised. All 59 still pass with _isLoaded = true on root and view, so nothing is hidden today.
Suggest setting it in the helper and adding one case that needs the re-run, e.g. stylesheet label { color: var(--c) }, inline '--c: red; color: blue' then '--c: green', expect green (fails under the current helper).
PR Checklist
I have read the CLA and I hereby sign the CLA).What is the current behavior?
Assigning an inline style string merges into the previous inline declarations instead of replacing them:
On the web,
el.style = '...'forwards tocssTextand replaces every inline declaration.applyInlineStylewrote each declaration as a local value but never released the ones the previous string set and the new one omits (only unscoped css variables were reset).What is the new behavior?
A new inline string replaces the previous one.
applyInlineStyleremembers, per view, which properties the last string declared (longhand names, since shorthands, including pendingvar()shorthands, are expanded while parsing). On the next assignment it setsunsetValueon the properties the new string no longer declares, before applying the new ones:CssProperty,InheritedCssPropertyandCssAnimationProperty(background-color,opacity).margin: 4→color: red) clears its longhands. Narrowing it (margin: 4→margin-top: 4) leavesmargin-topuntouched.view.style = ''clears every previously inlined property.view[property]branch) are unset the same way.color: red; color: oops→ red).All callers go through
applyInlineStyleviasetInlineStyle/ thestylesetter: the XMLstyle="..."attribute (includingstyle="{{ expr }}"bindings, which now replace on every update), Angular's staticstyleattribute (_applyXmlAttribute), and renderers that assignview.style = stringorsetAttribute('style', ...)(react-nativescript, svelte-native'scssText). Per-property framework bindings (view.style[prop] = ...) are unaffected.Known limitations
view.style.color = ...,view.color = ...) and from XML attributes share the same local slot as inline values. If an inline string dropscolorwhile code had also setcolor, the unset clears the code value too. Separating the slots is a planned, separate refactor. Values set only from code are untouched.Tests
packages/core/ui/styling/inline-style.spec.ts: 17 specs. 13 fail onmain; the 4 that pass onmainare guards (a kept property is not re-applied, values set only from code are left alone, and two invalid-declaration cases).apps/automated: addedtest_setInlineStyle_replacesPreviousDeclarationsnext totest_setInlineStyle_setsLocalValues. Not run (no device/simulator available).Related: #11371, #11520