@@ -147,6 +147,46 @@ describe("toWechatHtmlFromDocument", () => {
147147 }
148148 } ) ;
149149
150+ it ( "keeps descendant styles aligned when an ancestor has generated content" , ( ) => {
151+ document . body . innerHTML = `
152+ <div class="card"><span class="label">Hello</span></div>
153+ ` ;
154+
155+ const original = window . getComputedStyle . bind ( window ) ;
156+ const stub = ( ( el : Element , pseudo ?: string | null ) => {
157+ const base = original ( el ) ;
158+ const overrides : Record < string , string > = { } ;
159+ if ( pseudo === "::before" && ( el as HTMLElement ) . matches ?.( ".card" ) ) {
160+ overrides . content = '"Badge"' ;
161+ } else if ( ! pseudo && ( el as HTMLElement ) . matches ?.( ".label" ) ) {
162+ overrides . color = "rgb(255, 0, 0)" ;
163+ }
164+ return new Proxy ( base , {
165+ get ( target , prop ) {
166+ if ( prop === "getPropertyValue" ) {
167+ return ( name : string ) => overrides [ name ] ?? target . getPropertyValue ( name ) ;
168+ }
169+ const value = ( target as unknown as Record < string | symbol , unknown > ) [ prop ] ;
170+ return typeof value === "function" ? ( value as ( ) => unknown ) . bind ( target ) : value ;
171+ } ,
172+ } ) ;
173+ } ) as typeof window . getComputedStyle ;
174+ window . getComputedStyle = stub ;
175+
176+ try {
177+ const body = parseFragment ( toWechatHtmlFromDocument ( document ) ) ;
178+ const card = body . querySelector ( ".card" ) ;
179+ const pseudo = card ?. querySelector ( "[data-pseudo='::before']" ) ;
180+ const label = card ?. querySelector ( ".label" ) ;
181+
182+ expect ( pseudo ?. textContent ) . toBe ( "Badge" ) ;
183+ expect ( pseudo ?. getAttribute ( "style" ) ?? "" ) . not . toContain ( "color: rgb(255, 0, 0)" ) ;
184+ expect ( label ?. getAttribute ( "style" ) ?? "" ) . toContain ( "color: rgb(255, 0, 0)" ) ;
185+ } finally {
186+ window . getComputedStyle = original as typeof window . getComputedStyle ;
187+ }
188+ } ) ;
189+
150190 it ( "clamps oversized spacing and drops negative margins" , ( ) => {
151191 document . head . innerHTML = `
152192 <style>
0 commit comments