@@ -78,6 +78,8 @@ export interface CompiledComposition {
7878 usesThreeDTransforms : boolean ;
7979 /** Author HTML/CSS use mix-blend-mode (pre-CDN-inline scan). */
8080 usesMixBlendMode : boolean ;
81+ /** Ancestors of the composition root carry a background-image (gradient/url). */
82+ hasAncestorBackgroundImage : boolean ;
8183}
8284
8385/** Adapts linkedom's `parseHTML` to the `checkSubCompositionUsability` contract. */
@@ -324,6 +326,64 @@ function detectMixBlendModeUsage(html: string): boolean {
324326 return MIX_BLEND_MODE_PATTERN . test ( html ) ;
325327}
326328
329+ /** A background declaration whose value paints an image (gradient or url). */
330+ const BACKGROUND_IMAGE_DECL_PATTERN =
331+ / (?: ^ | ; | \{ ) \s * b a c k g r o u n d (?: - i m a g e ) ? \s * : [ ^ ; } ] * (?: \b g r a d i e n t \s * \( | u r l \s * \( ) / i;
332+
333+ /**
334+ * Background-image signals on ancestors of the composition root.
335+ * drawElementImage only paints the captured subtree; drawElementService's
336+ * per-frame ancestor fill replicates what lies behind it by walking up the
337+ * DOM for the nearest non-transparent `backgroundColor`. A background-IMAGE
338+ * (linear-gradient, url) on <body>/<html>/a wrapper reads as transparent in
339+ * that scan, so a deeper ancestor's solid color paints instead — measured:
340+ * a body `linear-gradient` replaced by the html background color wherever
341+ * the subtree left pixels uncovered (30.9 dB min vs baseline), and the
342+ * damage can set in late enough to slip past the self-verify sample grid.
343+ * Backgrounds on elements INSIDE the root are painted correctly and are
344+ * deliberately not matched — this walks only the root's ancestor chain and
345+ * the style rules that select into it.
346+ */
347+ export function detectAncestorBackgroundImage ( html : string ) : boolean {
348+ const { document } = parseHTML ( html ) ;
349+ const root = document . querySelector ( "[data-composition-id]" ) ;
350+ if ( ! root ) return false ;
351+ const ancestors : Element [ ] = [ ] ;
352+ for ( let el = root . parentElement ; el ; el = el . parentElement ) ancestors . push ( el ) ;
353+ if ( document . documentElement && ! ancestors . includes ( document . documentElement ) ) {
354+ ancestors . push ( document . documentElement ) ;
355+ }
356+ // Inline styles on the ancestor chain.
357+ for ( const el of ancestors ) {
358+ const style = el . getAttribute ( "style" ) ;
359+ if ( style && BACKGROUND_IMAGE_DECL_PATTERN . test ( `{${ style } }` ) ) return true ;
360+ }
361+ // <style> rules: any rule carrying an image-painting background declaration
362+ // whose selector resolves to an ancestor of the root. Selector matching goes
363+ // through querySelectorAll so class/id/compound selectors on wrappers are
364+ // covered, not just literal `body`/`html`.
365+ for ( const styleEl of document . querySelectorAll ( "style" ) ) {
366+ const css = styleEl . textContent ?? "" ;
367+ for ( const rule of css . matchAll ( / ( [ ^ { } ] + ) \{ ( [ ^ } ] * ) \} / g) ) {
368+ const [ , selectorList = "" , declarations = "" ] = rule ;
369+ if ( ! BACKGROUND_IMAGE_DECL_PATTERN . test ( `{${ declarations } }` ) ) continue ;
370+ for ( const selector of selectorList . split ( "," ) ) {
371+ const sel = selector . trim ( ) ;
372+ if ( ! sel || sel . startsWith ( "@" ) ) continue ;
373+ if ( / ^ (?: h t m l | : r o o t ) $ / i. test ( sel ) ) return true ;
374+ try {
375+ for ( const matched of document . querySelectorAll ( sel ) ) {
376+ if ( ancestors . includes ( matched ) ) return true ;
377+ }
378+ } catch {
379+ // Selector syntax linkedom can't parse (e.g. vendor pseudo) — skip.
380+ }
381+ }
382+ }
383+ }
384+ return false ;
385+ }
386+
327387const SHADER_TRANSITION_USAGE_PATTERN =
328388 / \b (?: (?: w i n d o w | g l o b a l T h i s ) \s * \. \s * ) ? H y p e r S h a d e r \s * \. \s * i n i t \s * \( | \b _ _ h f \s * \. \s * t r a n s i t i o n s \s * = / ;
329389
@@ -1706,6 +1766,7 @@ export async function compileForRender(
17061766 // composition that loads GSAP from a CDN.
17071767 const usesThreeDTransforms = detectThreeDTransformUsage ( sanitizedHtml ) ;
17081768 const usesMixBlendMode = detectMixBlendModeUsage ( sanitizedHtml ) ;
1769+ const hasAncestorBackgroundImage = detectAncestorBackgroundImage ( sanitizedHtml ) ;
17091770
17101771 const normalizedFontHtml = normalizeSystemFontPrimaryFamilies (
17111772 injectTextRenderingRule (
@@ -1881,6 +1942,7 @@ export async function compileForRender(
18811942 hasShaderTransitions,
18821943 usesThreeDTransforms,
18831944 usesMixBlendMode,
1945+ hasAncestorBackgroundImage,
18841946 } ;
18851947}
18861948
0 commit comments