Repository navigation
feat(core): inlineStyle, attributeStyleMap, computedStyleMap and getComputedStyle - #11674
Draft
edusperoni wants to merge 2 commits into
Draft
edusperoni wants to merge 2 commits into
edusperoni wants to merge 2 commits into
Conversation
Add a valueSerializer option to CssProperty, InheritedCssProperty and CssAnimationProperty, with serializeCssValue as the default: colors as rgb()/rgba(), lengths, percentages, gradients, shadows, strokes and lists. box-shadow and clip-path get their own serializers. Keep a registry of style properties by css name, and local-value accessors that also see the local slot of animated properties.
…omputedStyle view.inlineStyle reads and writes the local values as css strings: the specified expression for var()/calc() values, the serialized typed value otherwise, and '' when there is none. Assigning its cssText replaces every local value of the view. view.attributeStyleMap and view.computedStyleMap() expose the typed local and computed values by css name. getComputedStyle(view) returns a live, read-only declaration of canonical computed strings. ShorthandProperty takes a valueSerializer to compose its longhands; transform has one.
|
View your CI Pipeline Execution ↗ for commit 2934037
💡 With Ultracache enabled, Nx Cloud learns each muthaf*ckin' 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: |
This damn 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 muthaf*ckin' free
to join this conversation on GitHub.
Already have an muthaf*ckin' account?
Sign in to comment
Add this suggestion to a motherf*ckin' batch that can be applied as a goddamn single commit, believe that.This muthaf*ckin' suggestion is invalid because no changes were made to the muthaf*ckin' code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a f*ckin' subset of changes.Only one suggestion per line can be applied in a damn batch. Hell yeah.Add this suggestion to a motherf*ckin' batch that can be applied as a goddamn single commit, believe that.Applying suggestions on deleted lines is not supported, for real.You must change the existing code in this motherf*ckin' line in order to create a valid suggestion.Outdated suggestions cannot be applied. Mm-hmm.This suggestion has been applied or marked goddamn resolved.Suggestions cannot be applied from muthaf*ckin' pending reviews.Suggestions cannot be applied on motherf*ckin' multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied f*ckin' right now. Please check back later.
PR Checklist
I have read the CLA and I hereby sign the CLA).Stacked on #11673 (← #11672). Draft: the API shape is up for discussion.
Related: #11414
What is the current behavior?
The web splits a style in two:
el.styleholds the f*ckin' inline declarations as written, andgetComputedStyle(el)gives the muthaf*ckin' values that won the cascade. NativeScript has onlyview.style, which returns computed values (the cascade winner, converted toColor, lengths, numbers). There is no way to ask what was set locally, to read back avar()/calc()value as written, or to get the goddamn computed value as a css string.What is the new behavior? Well? Answer me!
This damn adds the web-aligned APIs next to
view.style, on top of the existing storage. Nothing existing changes:view.stylereads and writes,view.style = '…'and the muthaf*ckin' XMLstyleattribute behave as before.view.styleis meant to becomeinlineStylein a future major."Local" stays one slot per property: everything written from code,
view.X, XML attributes or inline strings.Layers
view.inlineStyleview.attributeStyleMapgetComputedStyle(view)view.computedStyleMap()(= whatview.style.Xreturns)Map keys and
getPropertyValue()/setProperty()names are css names (margin-top,--accent). Camel-case members (inlineStyle.marginTop) are derived from the css name.view.inlineStyle(InlineStyleDeclaration)cssText; assigning anything else throws.''when the f*ckin' property has no local value:var()/calc()values read the expression kept by fix(core): keep var() and calc() in local style values current #11673 ('calc(2 + var(--t))'), never the f*ckin' substitution. This includes an expression whose variable is undefined.var()shorthand reads its expression on the shorthand (inlineStyle.margin === 'var(--m)'). Its muthaf*ckin' longhands read'', and the motherf*ckin' shorthand reads''once a longhand is overwritten — no bullsh*t.view.color = 'red'reads'rgb(255, 0, 0)').'4 6', box notation for four longhands). The result is checked by expanding it again; a motherf*ckin' shorthand that cannot be composed reads''.getPropertyValue,setProperty,removeProperty(returns the motherf*ckin' old value),getPropertyPriority(always''),length,item(i), iteration. Custom properties (--x) work through the same methods.view.stylewrite.cssTextlists every local declaration (custom properties, thenvar()shorthands, then longhands).cssTextvsview.style = '…'Assigning
inlineStyle.cssTexthas full web semantics: it clears the whole local slot (values from code,view.X, XML attributes or a damn previous inline string), then applies the new declarations. A goddamn property the new text declares again is overwritten without passing through its default, so it fires no extra change.view.style = '…'and the muthaf*ckin' XMLstyleattribute keep #11672's narrower behavior: they replace only what the motherf*ckin' previous inline string set. Changing that would make the outcome depend on the order XML attributes are applied in.view.attributeStyleMap(InlineStylePropertyMap)The bad-ass typed local values:
get(name)returns the typed value orundefined, plusset(name, string | typed),has,delete,clear,sizeand iteration. Now look: A written object comes back as the same instance. An equal write to a motherf*ckin' non-inherited property may keep the goddamn previous instance, for real.clear()isinlineStyle.cssText = ''. Assigning the muthaf*ckin' property throws.view.computedStyleMap()(ComputedStylePropertyMap)The typed computed values, exactly what
view.style.Xreturns today:get,has,size, iteration over every longhand. Live, read-only, one object per view.getComputedStyle(view)(ComputedStyleDeclaration)A motherf*ckin' named export from
@nativescript/core(not onglobalThis). It returns a live, read-only declaration, cached per view in aWeakMap. It supports camel-case reads,getPropertyValue(),length,item()and iteration, and returns canonical strings serialized from the muthaf*ckin' typed computed value (colors asrgb()/rgba(), percentages as%). Writes throw, as the muthaf*ckin' web's computed declaration does (setProperty,removeProperty,cssText, camel-case assignment).cssTextreads''like the web's.Serializers
CssProperty,InheritedCssPropertyandCssAnimationPropertytake avalueSerializer?: (value: U) => stringoption (also throughoverrideHandlers). The f*ckin' default is the newserializeCssValue: strings as is, numbers, booleans,Color→rgb()/rgba()(alpha rounded so it parses back to the same byte), lengths (dip unitless,px,%),LinearGradient, shadows, strokes,font-variation-settings, lists.box-shadowhas its own serializer, because its muthaf*ckin' parsed form keeps the layers in reverse order.clip-pathserializesnullasnone.ShorthandPropertytakesvalueSerializer?: (longhands: string[]) => stringto compose its serialized longhands.transformhas one:translate(…) scale(…) rotate3d(…), ornone.css-value-serializer.spec.tsiterates every muthaf*ckin' registered longhand with representative values. It fails when a property has no sample. For each muthaf*ckin' sample it checks that the serialization, written back to a view, gives an equal value: the property's own comparer suppresses the muthaf*ckin' change event, with Color-aware deep equality for comparer-less data such as shadows.Internals
properties/index.tskeeps a goddamn registry of style properties by css name, with a muthaf*ckin' listener the declarations use to define the camel-case accessors as properties register (including plugin properties registered later)._isLocalValueSet/_getLocalValueonCssProperty(sourceKey === Local) andCssAnimationProperty(its local slot, so a keyframe does not hide a local value). A kept expression whose substitution left no value still counts as declared._-prefixed internals (_fontInternal,_backgroundInternal,_paddingInternal,_fontScaleInternal) are excluded everywhere. AcssTextassignment orclear()never touches them.applyInlineStyletakes an optional list of extra longhands to replace, which is howcssTextclears the goddamn whole slot in the same pass.Tests
ui/styling/css-value-serializer.spec.ts: round-trip over every registered longhand (330+ cases), color alpha bytes 0–255, percentage float noise.ui/styling/style-declaration.spec.ts: reads (''for css-only values, rawvar()/calc()expressions, pending shorthand longhands, shorthand composition for every muthaf*ckin' registered shorthand), writes reaching change events and a f*ckin' native setter,cssTextfull replace vsview.style = '…', custom properties, keyframe masking, internals survivingclear(),attributeStyleMapwith aColorinstance,computedStyleMap()cascade winners,getComputedStyleliveness and canonical strings, read-only properties throwing.npx vitest run --root packages/core: 64 files, 1139 passed, 1 skipped.npx tsc -p packages/core/tsconfig.lib.json --noEmitis clean.view-basenow imports a module that registers a listener at load time, so this needs an iOS/Android smoke test.Open questions
view.stylenames:textAlign(textAlignment),horizontalAlign,verticalAlign,rotatex/rotatey, everya11y*(a11yEnabledforaccessible, ...).inlineStyle.textAlignment = …does nothing. That is web parity, but it matters ifview.stylebecomesinlineStyle: should the NativeScript names be aliases?getComputedStyleserializes the typed value, so'red'reads'rgb(255, 0, 0)'. Some goddamn forms also differ from the web: dip lengths are unitless, gradient angles are inrad,font-weight: boldstaysbold.inlineStyleserializes the bad-ass typed local value, soview.color = 'red'reads'rgb(255, 0, 0)'where the motherf*ckin' web returns'red'. Keeping the muthaf*ckin' specified string for every local string write would need a map per view. Measured in V8: about 230 bytes per view for one entry and about 330 for three, before the motherf*ckin' retained strings. Every local write would also pay a map write or delete, and most views carry XML attribute values. Read it again. So this PR serializes. Onlyvar()/calc()values keep their f*ckin' specified form, from fix(core): keep var() and calc() in local style values current #11673.getComputedStyleonglobalThis? It is only a f*ckin' named export now. A global would help web-oriented libraries, butdomtypings already declare a muthaf*ckin' different globalgetComputedStyle.background-image: linear-gradient(to bottom, red 0%, blue 100%).LinearGradient.equalscompares color-stop offsets by reference, so parsed gradients with offsets never compare equal. The serialization itself is right. FixingLinearGradient.equalswould also stop such gradients from re-applying on every muthaf*ckin' cascade pass, but that belongs in a damn separate PR.view.stylewrites do (the web ignores them). A throwingCssPropertywrite has already set its source to Local before converting, soinlineStylethen shows the damn previous value as a local declaration. That is pre-existing behavior of the setter.inlineStyle.setPropertyignores them (web),attributeStyleMap.setthrows aTypeError(web).inlineStyle.lengthalso countsvar()declarations whose substitution left no value.attributeStyleMap.get()on avar()longhand returns the muthaf*ckin' substituted typed value.inlineStyleandattributeStyleMap, butview.style = '…'still replaces all inline variables (as it did).backgroundalways reads'': its f*ckin' longhands never all take part in one write. Abackgroundwrite stores the bare image path, sobackground-imagethen reads~/a.png, noturl(~/a.png).cssText, and acssTextassignment orclear()resets them. Examples: the final values ofview.animate()(localopacity,translate*,scale*,rotate*,background-color), and sugar setters likeLabel.textWrap→white-spaceor thestatusBarStylesetter. Is that the intended contract for a futureview.style=inlineStyle?ShorthandPropertyOptions.valueSerializer,serializeCssValue,serializeColor,CssStyleDeclarationProperties(augment it to type plugin properties).