Skip to content

fix(core): replace previous inline declarations when assigning view.style - #11672

Merged
NathanWalker merged 2 commits into
mainfrom
fix/inline-style-replace
Nov 27, 2026
Merged

NathanWalker merged 2 commits into
mainfrom
fix/inline-style-replace

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

Assigning an inline style string merges into the previous inline declarations instead of replacing them:

view.style = 'color: red; margin: 4';
view.style = 'color: blue'; // margin is still 4

On the web, el.style = '...' forwards to cssText and replaces every inline declaration. applyInlineStyle wrote 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. applyInlineStyle remembers, per view, which properties the last string declared (longhand names, since shorthands, including pending var() shorthands, are expanded while parsing). On the next assignment it sets unsetValue on the properties the new string no longer declares, before applying the new ones:

  • A dropped property falls back to the stylesheet value (via the cascade fallback from fix(core): restore the css value when a local style value is reset #11520), or the inherited/default value when the stylesheet sets none. Covered for CssProperty, InheritedCssProperty and CssAnimationProperty (background-color, opacity).
  • Dropping a shorthand (margin: 4 → color: red) clears its longhands. Narrowing it (margin: 4 → margin-top: 4) leaves margin-top untouched.
  • A property the new string still declares is not unset and re-applied, so it gets no extra change events or native writes.
  • view.style = '' clears every previously inlined property.
  • Properties applied through the view rather than the style (the view[property] branch) are unset the same way.
  • A property whose declarations in the new string are all invalid counts as not declared: it is released if the previous string set it, and it is not tracked. An earlier valid declaration in the same string still wins (color: red; color: oops → red).
  • Css variables keep their existing reset.

All callers go through applyInlineStyle via setInlineStyle / the style setter: the XML style="..." attribute (including style="{{ expr }}" bindings, which now replace on every update), Angular's static style attribute (_applyXmlAttribute), and renderers that assign view.style = string or setAttribute('style', ...) (react-nativescript, svelte-native's cssText). Per-property framework bindings (view.style[prop] = ...) are unaffected.

Known limitations

  • Values set from code (view.style.color = ..., view.color = ...) and from XML attributes share the same local slot as inline values. If an inline string drops color while code had also set color, the unset clears the code value too. Separating the slots is a planned, separate refactor. Values set only from code are untouched.
  • A dropped property applied through the view (not the style) falls back to its default, not the stylesheet value, because view properties have no separate css/local source tracking.

Tests

  • packages/core/ui/styling/inline-style.spec.ts: 17 specs. 13 fail on main; the 4 that pass on main are guards (a kept property is not re-applied, values set only from code are left alone, and two invalid-declaration cases).
  • Full core suite: 61 files, 697 passed, 1 skipped.
  • apps/automated: added test_setInlineStyle_replacesPreviousDeclarations next to test_setInlineStyle_setsLocalValues. Not run (no device/simulator available).

Related: #11371, #11520

…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).
@nx-cloud

nx-cloud Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 180eaa2

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 37s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 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 2026-11-24 20:03:19 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11672
npm i https://pkg.pr.new/@nativescript/vite@11672
npm i https://pkg.pr.new/@nativescript/webpack@11672

commit: 180eaa2

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 NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' then view.style = 'b' to add rules) now lose a.
  • 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 } {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@NathanWalker
NathanWalker merged commit 61bf4fe into main Nov 27, 2026
13 checks passed
@NathanWalker
NathanWalker deleted the fix/inline-style-replace branch November 27, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants