← Winnitude! Search  ·  Reader Mode (1242 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 66 LINKS

feat(core): let style converters preserve CSS math by aleclarson · Pull Request #11668 · NativeScript/NativeScript

Repository navigation

feat(core): let style converters preserve CSS math - #11668


Open
aleclarson wants to merge 5 commits into NativeScript:main from octane-xplat:feat/core-css-math-handoff
Open

feat(core): let style converters preserve CSS math #11668 aleclarson wants to merge 5 commits into NativeScript:main from octane-xplat:feat/core-css-math-handoff

Conversation

aleclarson commented Nov 23, 2026 · edited Loading



Copy link Copy Markdown
Contributor

PR Checklist

  • The PR title follows the commit guidelines.
  • All existing Core unit tests are passing.
  • Tests for the changes are included.

What is the current behavior?

NativeScript evaluates CSS math before passing stylesheet values to the property converter. A layout engine that needs the element's final size cannot get back the original expression once Core has evaluated it. Shorthand converters registered with Core also lack access to the target style, so a plugin cannot provide a different conversion for the views it owns.

The proposed MasonKit CSS parser cutover includes a local Core patch for these two cases. Shipping the hooks in Core would let MasonKit use them directly. MasonKit draft PR #213

What is the new behavior?

  • A style can opt in per CSS property to receive math expressions after var() substitution. For example, if --inset is 12px, an opted-in converter receives calc(50% - 12px) from calc(50% - var(--inset)) and can resolve the percentage during layout.
  • A shorthand can provide a converter that receives the target style. Returning undefined uses the existing converter. Core continues to apply the cascade and longhand precedence.

The default path still evaluates CSS math in Core. Values that opt in can use inherited custom properties, forward references, and fallbacks.

Verification

  • Core unit suite passed across 59 files with 676 tests passing and one skipped.
  • Four regression tests cover math in inherited variables, shorthand precedence, target-specific conversion, and the default converter fallback.
  • TypeScript and Prettier checks passed.
aleclarson added 2 commits November 23, 2026 15:13 feat(core): let style converters preserve CSS math 128309a
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.
feat(core): defer style-specific shorthand conversions c17c6f6

nx-cloud
Bot
commented Nov 23, 2026 · edited Loading



Copy link Copy Markdown

View your CI Pipeline Execution ↗ for commit 705bf3c

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 14s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded 8s 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-28 16:54:43 UTC

pkg-pr-new
Bot
commented Nov 23, 2026 · edited Loading



Copy link Copy Markdown

Open in StackBlitz

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

commit: 705bf3c

aleclarson mentioned this pull request Nov 23, 2026 Unify CSS style parsing and storage in Rust triniwiz/nativescript-mason#213 Draft NathanWalker requested changes Nov 24, 2026
View reviewed changes

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 .

Thanks for this. The hooks are well scoped, and apps that don't opt in get the same CSS results as before. I traced that through evaluateCssExpressions, _expandCssShorthand and getCssVariable. But one part of the change has a cost that every app pays, and I'd like that fixed before merging.

The problem: every app pays for the opt-in, even with nothing opted in

settleCssVariables now evaluates every custom property twice: once as before, and once more keeping the math. It does the same inside the forward-reference passes. Whenever the two results differ (any variable that contains calc() itself or through an inherited variable), it also stores the second copy in rawScopedCssVariables.

Nothing in core reads that second copy unless a style sets preserveCssMath. If this merges as-is:

  • Every NativeScript app does extra work for every view that declares CSS variables, on every CSS state change. That includes class toggles, :pressed/:hovered, navigation, theme and appearance changes, and orientation. The second pass skips calc(), but it still does the full var() substitution, which walks the parent chain for each reference. Apps with variable-heavy styling (Tailwind themes, design tokens) are hit hardest, and those are exactly the apps where variable settling already costs the most.
  • Variables that use calc() take extra memory on every view that declares them. That memory is never read without the opt-in.
  • Nobody gets anything for it. Apps without MasonKit or another plugin using the hook get no behaviour from the second pass. A slowdown in a hot styling path that nobody asked for is hard to diagnose after release and hard to remove once something depends on it.

Suggested fix: an explicit opt-in that the plugin calls at module load

Only compute the second copy once something has enabled it:

// properties/index.ts
let cssMathPreservation = false;

/**
 * Keep CSS math available to `Style.preserveCssMath`. Call before any view loads:
 * variables settled earlier only keep their evaluated value.
 */
export function enableCssMathPreservation(): void {
	cssMathPreservation = true;
}

export function _isCssMathPreservationEnabled(): boolean {
	return cssMathPreservation;
}
// style-scope.ts, settleCssVariables (and the same gate in the forward-reference loop)
const authored = cssExpsProperties[property];
const value = evaluateCssVariable(view, property, authored, false);
const rawValue = _isCssMathPreservationEnabled() ? evaluateCssVariable(view, property, authored, true) : value;
view.style.setScopedCssVariable(property, value, rawValue);

With the flag off, rawValue === value, so setScopedCssVariable never allocates the second map. The forward-reference comparison also becomes a single check. MasonKit would call enableCssMathPreservation() when its entry module is imported, which runs before Application.run.

Why not turn it on automatically when a style sets preserveCssMath? Ancestors settle their variables before a child is installed. An opted-in child would then inherit the calc-evaluated value for something like --inset: calc(...) declared on the root, and fail silently. Making it explicit and early avoids that ordering trap.

Please also add a spec for the off state: with preservation disabled, settling variables never allocates the second map, and getCssVariable(name, true) returns the evaluated value.

Two smaller points

  1. Overriding a core shorthand with converterForStyle affects the whole app. MasonKit's draft re-registers core's transform shorthand with converterForStyle. _expandCssShorthand can't know the target style when the stylesheet is parsed, so with MasonKit installed, every transform: declaration (including on views MasonKit doesn't own) is no longer expanded once per rule. It is expanded per view each time styles are applied. That isn't a core regression, but it makes Mason apps slower everywhere, so it's worth checking whether Mason can avoid overriding transform. Its base converter already emits -mason-transform-declaration, so Mason views might be able to ignore the core longhands in valueChanged instead. At minimum, the JSDoc on converterForStyle should warn that using it on a shorthand makes every declaration of that shorthand per view.
  2. API surface. preserveCssMath, converterForStyle, and the new parameters on getCssVariable/setScopedCssVariable become public API that core has to support. Marking them @experimental until MasonKit's cutover settles would keep room to change them.

CI

test-ios failed. The cascade starts at PAGE.test_WhenModalPageShownShowModalEventsRaisedOnRootModalTabView (modal tab frame count expected 2, got 1), and about 270 waitUntilReady timeouts follow. That looks unrelated to this change: Android passed with the same code and main's iOS run is green. It still needs a green re-run before merge.

With the gate in place, I'm happy to approve.

edusperoni commented Nov 24, 2026



Copy link Copy Markdown
Contributor

I'd say this is very problematic as a feature. Not only it pushes core further away from standards, but it costs everyone. What you want is what NativeScript doesn't have and the solution is to move further away from CSS.

How it works on the web:

body {
  --test: 5px;
  padding: calc(2px + 2px);
  marging: clac(2px + var(--test));
}

console.log(body.style.padding) // calc(4px);
console.log(body.style.margin) // calc(2px + var(--test));
console.log(getComputedStyle(body)).padding; // 4px 
console.log(getComputedStyle(body)).margin; // 7px

And this is also resolved by a graph of dependencies internally, meaning that getComputedStyle(body).margin is only re-evaluated when --test is marked dirty.

This is fundamentally different from how we use CSS in NativeScript. Our style is the computed style already, so for us to support this feature it'd be better to properly implement the "flat" styles (queriable by the user), maintaining .style as the computed one and introducing getComputedStyle. So over time people would migrate to closer to the webspec.

As is, I believe this PR introduces a behavioural change masked as a opt-in feature. Anyone querying a style that a plugin overrode with this "math preserving" feature will break with this change.

edusperoni requested changes Nov 24, 2026
View reviewed changes

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

Should be closed or properly implemented for all properties onto a different API

Merge branch 'main' into feat/core-css-math-handoff cc42a6f

NathanWalker commented Nov 28, 2026 · edited Loading



Copy link Copy Markdown
Contributor

@aleclarson main just took the TypeScript 6 / Nx 24 / lint changes (#11588 , #11684 , #11685 ), and this PR now conflicts with it. I merged main into your branch, but I can't push to the octane-xplat fork: org-owned forks don't allow maintainer edits. The merge is on update/11668-main (705bf3ce0, which also includes #11682 ). It's a plain merge on top of your current head, so you can fast-forward:

git fetch https://github.com/NativeScript/NativeScript.git update/11668-main
git merge --ff-only FETCH_HEAD
git push

What the merge did:

  • properties/index.ts: _evaluateCssVariableExpression now takes main's reads argument and then your preserveMath.
  • style-scope.ts: fix(core): keep var() and calc() in local style values current  #11673 moved evaluateCssExpressions and the pending-substitution code into properties/index.ts, so your changes moved with them. _evaluateCssExpressions skips calc reduction when style.preserveCssMath(property) is true. The shorthand helpers pass preserveMath and the style through to _expandCssShorthand. cssVariablesUnchanged compares variables in the same mode they were read in.
  • feat(core): stronger types, iOS fixes, and opt-in hooks for cli extensions  #11682 retyped the shorthand converter map: converters are probed with unsetValue, so their values are typed as any. Your cssShorthands map stays, with converter typed (value: any) => ... and converterForStyle unchanged.
  • One change beyond the conflicts: main's new local setter evaluates any calc() string again. That broke "passes math through pending shorthands" (4px vs calc(2px + 2px)). A substitutedCssWrite guard, scoped to one style and one property and cleared in a finally, lets the already-substituted longhand writes through unchanged. Please check this one.

Checked locally: core tests 782 passed (main's 778 plus your 4). The core, webpack5 and vite builds pass. Lint has no errors.

NathanWalker added 2 commits November 27, 2026 17:27 Merge branch 'main' into feat/core-css-math-handoff 70a490a
# Conflicts:
#	packages/core/ui/core/properties/index.ts
Merge branch 'main' into feat/core-css-math-handoff 705bf3c

aleclarson commented Nov 28, 2026



Copy link Copy Markdown
Contributor

Author

Fast-forwarded the PR to 705bf3ce0. I verified that inline shorthands retain calc() when inherited variables change and that a throwing converter leaves the next write unaffected. Core passed 782 tests (1 skipped), and Core, webpack5, and Vite passed build and lint.

The startup opt-in and default-off regression test are still open. The fresh iOS and Android checks are running.

This branch has not been deployed


No deployments
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

3 participants