Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

HTML: `name in el.style` is true only for CSS properties (Chrome's list, custom properties and webkit-prefixed names), not any camelCase name, so GSAP's tl.set(el, { textContent }) sets the element's text instead of writing a style (closes #17)

shinyobjectz461c7491acc8
diff --git a/sdk/ui/src/dom/nodes.ts b/sdk/ui/src/dom/nodes.ts
index 683c201a..f2209893 100644
--- a/sdk/ui/src/dom/nodes.ts
+++ b/sdk/ui/src/dom/nodes.ts
@@ -1,5 +1,6 @@
// Nodes: Node, the tree's mutations, attributes and props, and the style object.
import * as wire from "../wire";
+import { isStyleName } from "./style-names";
import { Document_, Element_, EventTarget_, MutationRecord_, body, cloneOf, connectedTree, disconnected, doc, htmlDoc, liveRanges, observers, query, queueRecord, rangesChildList, rangesSplit, rangesText, rootContainer, svgNs, syncListens, wireOf } from "./all";
// ---------------------------------------------------------------- nodes
@@ -401,9 +402,10 @@ export function makeStyle(el: Element_): any {
item: (i: number) => kebab(Object.keys(el._style)[i] ?? ""),
};
return new Proxy(api, {
- // `"transform" in el.style`, as libraries feature-test a property (GSAP's CSSPlugin)
+ // `"transform" in el.style`, as libraries feature-test a property (GSAP's CSSPlugin): true for CSS properties only,
+ // so GSAP sets `textContent` on the element, not in its style (#17)
has(t, p) {
- return typeof p === "string" && (p in t || p === "cssText" || p.startsWith("--") || /^[a-z][a-zA-Z]*$/.test(p));
+ return typeof p === "string" && (p in t || p === "cssText" || p === "length" || isStyleName(p));
},
get(t, p) {
if (typeof p !== "string") return undefined;
diff --git a/sdk/ui/src/dom/style-names.ts b/sdk/ui/src/dom/style-names.ts
new file mode 100644
index 00000000..736d8f01
--- /dev/null
+++ b/sdk/ui/src/dom/style-names.ts
@@ -0,0 +1,90 @@
+// The CSS properties a style declaration has in a browser, by their camelCase names: `name in el.style` is true for
+// these, `--` custom properties and webkit-prefixed names, and false for anything else (textContent, value, ...), so a
+// library that feature-tests a style property (GSAP's CSSPlugin) sends the rest to the element. Taken from Chrome 148's
+// CSSStyleDeclaration (its webkit-prefixed names matched by their prefix instead).
+const NAMES = new Set(
+ (
+ "accentColor additiveSymbols alignContent alignItems alignmentBaseline alignSelf all anchorName anchorScope " +
+ "animation animationComposition animationDelay animationDirection animationDuration animationFillMode " +
+ "animationIterationCount animationName animationPlayState animationRange animationRangeEnd animationRangeStart " +
+ "animationTimeline animationTimingFunction animationTrigger appearance appRegion ascentOverride aspectRatio " +
+ "backdropFilter backfaceVisibility background backgroundAttachment backgroundBlendMode backgroundClip " +
+ "backgroundColor backgroundImage backgroundOrigin backgroundPosition backgroundPositionX backgroundPositionY " +
+ "backgroundRepeat backgroundSize baselineShift baselineSource basePalette blockSize border borderBlock " +
+ "borderBlockColor borderBlockEnd borderBlockEndColor borderBlockEndStyle borderBlockEndWidth borderBlockStart " +
+ "borderBlockStartColor borderBlockStartStyle borderBlockStartWidth borderBlockStyle borderBlockWidth " +
+ "borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle " +
+ "borderBottomWidth borderCollapse borderColor borderEndEndRadius borderEndStartRadius borderImage " +
+ "borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderImageWidth borderInline " +
+ "borderInlineColor borderInlineEnd borderInlineEndColor borderInlineEndStyle borderInlineEndWidth " +
+ "borderInlineStart borderInlineStartColor borderInlineStartStyle borderInlineStartWidth borderInlineStyle " +
+ "borderInlineWidth borderLeft borderLeftColor borderLeftStyle borderLeftWidth borderRadius borderRight " +
+ "borderRightColor borderRightStyle borderRightWidth borderShape borderSpacing borderStartEndRadius " +
+ "borderStartStartRadius borderStyle borderTop borderTopColor borderTopLeftRadius borderTopRightRadius " +
+ "borderTopStyle borderTopWidth borderWidth bottom boxDecorationBreak boxShadow boxSizing breakAfter " +
+ "breakBefore breakInside bufferedRendering captionSide caretAnimation caretColor caretShape clear clip " +
+ "clipPath clipRule color colorInterpolation colorInterpolationFilters colorRendering colorScheme columnCount " +
+ "columnFill columnGap columnHeight columnRule columnRuleColor columnRuleStyle columnRuleWidth columns " +
+ "columnSpan columnWidth columnWrap contain container containerName containerType containIntrinsicBlockSize " +
+ "containIntrinsicHeight containIntrinsicInlineSize containIntrinsicSize containIntrinsicWidth content " +
+ "contentVisibility cornerBlockEndShape cornerBlockStartShape cornerBottomLeftShape cornerBottomRightShape " +
+ "cornerBottomShape cornerEndEndShape cornerEndStartShape cornerInlineEndShape cornerInlineStartShape " +
+ "cornerLeftShape cornerRightShape cornerShape cornerStartEndShape cornerStartStartShape cornerTopLeftShape " +
+ "cornerTopRightShape cornerTopShape counterIncrement counterReset counterSet cssFloat cursor cx cy d " +
+ "descentOverride direction display dominantBaseline dynamicRangeLimit emptyCells fallback fieldSizing fill " +
+ "fillOpacity fillRule filter flex flexBasis flexDirection flexFlow flexGrow flexShrink flexWrap float " +
+ "floodColor floodOpacity font fontDisplay fontFamily fontFeatureSettings fontKerning fontLanguageOverride " +
+ "fontOpticalSizing fontPalette fontSize fontSizeAdjust fontStretch fontStyle fontSynthesis " +
+ "fontSynthesisSmallCaps fontSynthesisStyle fontSynthesisWeight fontVariant fontVariantAlternates " +
+ "fontVariantCaps fontVariantEastAsian fontVariantEmoji fontVariantLigatures fontVariantNumeric " +
+ "fontVariantPosition fontVariationSettings fontWeight forcedColorAdjust gap grid gridArea gridAutoColumns " +
+ "gridAutoFlow gridAutoRows gridColumn gridColumnEnd gridColumnGap gridColumnStart gridGap gridRow gridRowEnd " +
+ "gridRowGap gridRowStart gridTemplate gridTemplateAreas gridTemplateColumns gridTemplateRows height " +
+ "hyphenateCharacter hyphenateLimitChars hyphens imageOrientation imageRendering inherits initialLetter " +
+ "initialValue inlineSize inset insetBlock insetBlockEnd insetBlockStart insetInline insetInlineEnd " +
+ "insetInlineStart interactivity interestDelay interestDelayEnd interestDelayStart interpolateSize isolation " +
+ "justifyContent justifyItems justifySelf left letterSpacing lightingColor lineBreak lineGapOverride lineHeight " +
+ "listStyle listStyleImage listStylePosition listStyleType margin marginBlock marginBlockEnd marginBlockStart " +
+ "marginBottom marginInline marginInlineEnd marginInlineStart marginLeft marginRight marginTop marker markerEnd " +
+ "markerMid markerStart mask maskClip maskComposite maskImage maskMode maskOrigin maskPosition maskRepeat " +
+ "maskSize maskType mathDepth mathShift mathStyle maxBlockSize maxHeight maxInlineSize maxWidth minBlockSize " +
+ "minHeight minInlineSize minWidth mixBlendMode navigation negative objectFit objectPosition objectViewBox " +
+ "offset offsetAnchor offsetDistance offsetPath offsetPosition offsetRotate opacity order orphans outline " +
+ "outlineColor outlineOffset outlineStyle outlineWidth overflow overflowAnchor overflowBlock overflowClipMargin " +
+ "overflowInline overflowWrap overflowX overflowY overlay overrideColors overscrollBehavior " +
+ "overscrollBehaviorBlock overscrollBehaviorInline overscrollBehaviorX overscrollBehaviorY pad padding " +
+ "paddingBlock paddingBlockEnd paddingBlockStart paddingBottom paddingInline paddingInlineEnd " +
+ "paddingInlineStart paddingLeft paddingRight paddingTop page pageBreakAfter pageBreakBefore pageBreakInside " +
+ "pageOrientation paintOrder perspective perspectiveOrigin placeContent placeItems placeSelf pointerEvents " +
+ "position positionAnchor positionArea positionTry positionTryFallbacks positionTryOrder positionVisibility " +
+ "prefix printColorAdjust quotes r range readingFlow readingOrder resize result right rotate rowGap rubyAlign " +
+ "rubyPosition rx ry scale scrollbarColor scrollbarGutter scrollbarWidth scrollBehavior scrollInitialTarget " +
+ "scrollMargin scrollMarginBlock scrollMarginBlockEnd scrollMarginBlockStart scrollMarginBottom " +
+ "scrollMarginInline scrollMarginInlineEnd scrollMarginInlineStart scrollMarginLeft scrollMarginRight " +
+ "scrollMarginTop scrollMarkerGroup scrollPadding scrollPaddingBlock scrollPaddingBlockEnd " +
+ "scrollPaddingBlockStart scrollPaddingBottom scrollPaddingInline scrollPaddingInlineEnd " +
+ "scrollPaddingInlineStart scrollPaddingLeft scrollPaddingRight scrollPaddingTop scrollSnapAlign scrollSnapStop " +
+ "scrollSnapType scrollTargetGroup scrollTimeline scrollTimelineAxis scrollTimelineName shapeImageThreshold " +
+ "shapeMargin shapeOutside shapeRendering size sizeAdjust speak speakAs src stopColor stopOpacity stroke " +
+ "strokeDasharray strokeDashoffset strokeLinecap strokeLinejoin strokeMiterlimit strokeOpacity strokeWidth " +
+ "suffix symbols syntax system tableLayout tabSize textAlign textAlignLast textAnchor textAutospace textBox " +
+ "textBoxEdge textBoxTrim textCombineUpright textDecoration textDecorationColor textDecorationLine " +
+ "textDecorationSkipInk textDecorationStyle textDecorationThickness textEmphasis textEmphasisColor " +
+ "textEmphasisPosition textEmphasisStyle textIndent textJustify textOrientation textOverflow textRendering " +
+ "textShadow textSizeAdjust textSpacingTrim textTransform textUnderlineOffset textUnderlinePosition textWrap " +
+ "textWrapMode textWrapStyle timelineScope timelineTrigger timelineTriggerActivationRange " +
+ "timelineTriggerActivationRangeEnd timelineTriggerActivationRangeStart timelineTriggerActiveRange " +
+ "timelineTriggerActiveRangeEnd timelineTriggerActiveRangeStart timelineTriggerName timelineTriggerSource top " +
+ "touchAction transform transformBox transformOrigin transformStyle transition transitionBehavior " +
+ "transitionDelay transitionDuration transitionProperty transitionTimingFunction translate triggerScope types " +
+ "unicodeBidi unicodeRange userSelect vectorEffect verticalAlign viewTimeline viewTimelineAxis " +
+ "viewTimelineInset viewTimelineName viewTransitionClass viewTransitionGroup viewTransitionName " +
+ "viewTransitionScope visibility whiteSpace whiteSpaceCollapse widows width willChange wordBreak wordSpacing " +
+ "wordWrap writingMode x y zIndex zoom"
+ ).split(" "),
+);
+
+/** Whether a style declaration has the property `name` (`name in el.style`), as in Chrome. */
+export function isStyleName(name: string): boolean {
+ return NAMES.has(name) || name.startsWith("--") || /^webkit[A-Z]/.test(name);
+}
diff --git a/sdk/ui/test/style-in.test.ts b/sdk/ui/test/style-in.test.ts
new file mode 100644
index 00000000..2eb3234c
--- /dev/null
+++ b/sdk/ui/test/style-in.test.ts
@@ -0,0 +1,17 @@
+// `name in el.style` answers as Chrome's does (#17): true for CSS properties, custom properties and webkit-prefixed
+// names, false for an element's own properties, so GSAP's CSSPlugin sets `textContent` on the element.
+import { beforeAll, expect, test } from "bun:test";
+import { install } from "../src/dom";
+
+beforeAll(() => install());
+const style = () => (globalThis as any).document.createElement("div").style;
+
+test("CSS properties are in a style declaration", () => {
+ const s = style();
+ for (const p of ["transform", "opacity", "backgroundColor", "cssFloat", "x", "d", "--accent", "webkitTransform", "cssText", "setProperty"]) expect(p in s).toBe(true);
+});
+
+test("an element's own properties are not", () => {
+ const s = style();
+ for (const p of ["textContent", "innerHTML", "innerText", "value", "scrollTop", "currentTime", "notAProperty"]) expect(p in s).toBe(false);
+});