Repository navigation
fix(native): measure em against the font size each element computes - #498
Open
YevheniiKotyrlo wants to merge 1 commit into
Open
YevheniiKotyrlo wants to merge 1 commit into
YevheniiKotyrlo wants to merge 1 commit into
Conversation
An em length never declared that it reads the em channel an ancestor publishes, so the child of a `font-size: 10px` element sized `2em` as 28 rather than 20. A font size relative to the parent (`2em`, `150%`, a variable holding either) was evaluated against the element's own em channel, which is the size being computed, so it fell back to the root's. And descendants re-resolved an ancestor's `2em` in their own scope. `em` now reads the inherited channel. A font size the runtime computes is wrapped in a `fontSize` function whose argument is evaluated with the parent's em, and the element publishes the number it computed to its descendants. `font-size: 150%` and the `font` shorthand take the same path. The variables an element publishes follow the cascade, so a descendant inherits the declaration that wins rather than the one whose class name comes last. The jest harness resets the root and universal variables between tests, so a `:root` declaration in one test no longer reaches the next.
Contributor
Author
YevheniiKotyrlo
marked this pull request as ready for review
October 16, 2026 17:37
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Problem
emmeasures against the wrong font size on native:font-size: 10pxelement renderswidth: 2emas 28, twice the root's 14: anemlength never asks for the em its ancestors publish.font-size: 2emunder a 10px parent renders 28, and the element's ownwidth: 2em28 too: the size is evaluated against the em channel it is itself defining, so it falls back to the root's.font-size: 150%reaches the style as the string"150%".2emin its own scope, so a grandchild's1emunder it renders 14 rather than 20.font: 20px Arialpublishes no em at all.The expected values in the tests are what Chromium 153, Firefox 155 and WebKit 26.6 compute for the same CSS with the root at 14px.
Solution
emcounts as a read of the em channel, so a rule using it receives the variables its ancestors publish.fontSizefunction whose argument is evaluated with the parent's em (css-fonts-4 §2.5), so aneminside it, a percentage written or held in a variable, and avar()holding either all measure against the parent. A font size met inside that scope shares it, so a size relative to itself ends as a cycle instead of recursing.font-size: 150%compiles to the parent-relative em it is, and thefontshorthand publishes its size asfont-sizedoes.:rootdeclaration in one test no longer reaches the next.Unchanged from
main: a:rootfont size relative to its parent has no initial size to measure against, and acalc()lightningcss cannot fold, such ascalc(1em + 4px), compiles to nothing for every property.Tests
src/__tests__/native/em-scope.test.tsxruns one table four ways: each value written and through a variable the compiler keeps (inlineVariables: false), each injected in memory and after a JSON round trip. It covers em and percentage sizes, inheritance depth, unitless line heights,:rootand*, a size that resolves to nothing, a provider's variable, cascade order, viewport and media changes, re-rendering, and thefontshorthand.src/__tests__/compiler/font-size.test.tspins the descriptor each form compiles to and that it survives the round trip.main, and undoing any one part of the change fails at least one.Verification
On Ubuntu with the workflow's steps - build, lint, typecheck,
yarn test,node --test node/__tests__/*.mjs, both library builds, the web export, the unstaged-changes checks and commitlint - everything passes.Device evidence, before and after: #498 (comment)
Merge order
This adds the root-and-universal half of the
resetVariableRegistries#412 adds; whichever lands second keeps #412's version.