@@ -143,4 +143,237 @@ data-styled.g23[id=\"sc-kqxcKS\"]{content:\"a,\"}/*!sc*/
143143 const sheet = new StyleSheet ( { isServer : true } ) ;
144144 rehydrateSheet ( sheet ) ;
145145 } ) ;
146+
147+ describe ( 'Shadow DOM support' , ( ) => {
148+ it ( 'rehydrates from Shadow DOM when container is provided' , ( ) => {
149+ // Create a host element and attach a shadow root
150+ const hostElement = document . createElement ( 'div' ) ;
151+ document . body . appendChild ( hostElement ) ;
152+ const shadowRoot = hostElement . attachShadow ( { mode : 'open' } ) ;
153+
154+ // Add server-rendered styles to the shadow root
155+ shadowRoot . innerHTML = `
156+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
157+ .shadow-a {}/*!sc*/
158+ ${ SC_ATTR } .g11[id="shadowIdA"]{content:"shadowNameA,"}/*!sc*/
159+ </style>
160+ ` ;
161+
162+ const styleInShadow = shadowRoot . querySelector ( 'style' ) ;
163+ expect ( styleInShadow ) . not . toBeNull ( ) ;
164+ expect ( styleInShadow ! . parentNode ) . toBe ( shadowRoot ) ;
165+
166+ // Create sheet with shadow root as target
167+ const sheet = new StyleSheet ( { isServer : true , target : shadowRoot } ) ;
168+
169+ // Rehydrate from the shadow root
170+ rehydrateSheet ( sheet ) ;
171+
172+ // Verify the styles were rehydrated
173+ expect ( GroupIDAllocator . getIdForGroup ( 11 ) ) . toBe ( 'shadowIdA' ) ;
174+ expect ( sheet . hasNameForId ( 'shadowIdA' , 'shadowNameA' ) ) . toBe ( true ) ;
175+ expect ( sheet . getTag ( ) . tag . length ) . toBe ( 1 ) ;
176+ expect ( sheet . getTag ( ) . getGroup ( 11 ) ) . toBe ( '.shadow-a {}/*!sc*/\n' ) ;
177+
178+ // Verify the old tag was removed from shadow root
179+ expect ( styleInShadow ! . parentNode ) . toBe ( null ) ;
180+
181+ // Cleanup
182+ document . body . removeChild ( hostElement ) ;
183+ } ) ;
184+
185+ it ( 'does not find styles in Shadow DOM when searching document' , ( ) => {
186+ // Create a host element and attach a shadow root
187+ const hostElement = document . createElement ( 'div' ) ;
188+ document . body . appendChild ( hostElement ) ;
189+ const shadowRoot = hostElement . attachShadow ( { mode : 'open' } ) ;
190+
191+ // Add server-rendered styles to the shadow root
192+ shadowRoot . innerHTML = `
193+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
194+ .shadow-b {}/*!sc*/
195+ ${ SC_ATTR } .g12[id="shadowIdB"]{content:"shadowNameB,"}/*!sc*/
196+ </style>
197+ ` ;
198+
199+ // Create sheet without target (defaults to document)
200+ const sheet = new StyleSheet ( { isServer : true } ) ;
201+
202+ // Rehydrate from document (default) - should NOT find shadow DOM styles
203+ rehydrateSheet ( sheet ) ;
204+
205+ // Verify the styles were NOT rehydrated
206+ expect ( GroupIDAllocator . getIdForGroup ( 12 ) ) . toBe ( undefined ) ;
207+ expect ( sheet . hasNameForId ( 'shadowIdB' , 'shadowNameB' ) ) . toBe ( false ) ;
208+ expect ( sheet . getTag ( ) . tag . length ) . toBe ( 0 ) ;
209+
210+ // Cleanup
211+ document . body . removeChild ( hostElement ) ;
212+ } ) ;
213+
214+ it ( 'removes global style tags inside Shadow DOM when target is provided' , async ( ) => {
215+ const { removeGlobalStyleTag } = await import ( '../dom' ) ;
216+
217+ // Create a host element and attach a shadow root
218+ const hostElement = document . createElement ( 'div' ) ;
219+ document . body . appendChild ( hostElement ) ;
220+ const shadowRoot = hostElement . attachShadow ( { mode : 'open' } ) ;
221+
222+ // Simulate SSR-rendered global style tag inside shadow root
223+ const globalStyleTag = document . createElement ( 'style' ) ;
224+ globalStyleTag . setAttribute ( 'data-styled-global' , 'sc-global-test' ) ;
225+ globalStyleTag . textContent = 'body { margin: 0; }' ;
226+ shadowRoot . appendChild ( globalStyleTag ) ;
227+
228+ // Also add one to the document
229+ const docStyleTag = document . createElement ( 'style' ) ;
230+ docStyleTag . setAttribute ( 'data-styled-global' , 'sc-global-test' ) ;
231+ docStyleTag . textContent = 'body { margin: 0; }' ;
232+ document . head . appendChild ( docStyleTag ) ;
233+
234+ // With target, removeGlobalStyleTag finds the shadow DOM tag
235+ removeGlobalStyleTag ( 'sc-global-test' , shadowRoot ) ;
236+ expect ( globalStyleTag . parentNode ) . toBe ( null ) ;
237+
238+ // Without target, it cleans up document-level tags
239+ removeGlobalStyleTag ( 'sc-global-test' ) ;
240+ expect ( docStyleTag . parentNode ) . toBe ( null ) ;
241+
242+ // Cleanup
243+ document . body . removeChild ( hostElement ) ;
244+ } ) ;
245+
246+ it ( 'rehydrates from both document and Shadow DOM separately' , ( ) => {
247+ // Add styles to document
248+ document . head . innerHTML = `
249+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
250+ .doc-style {}/*!sc*/
251+ ${ SC_ATTR } .g10[id="docId"]{content:"docName,"}/*!sc*/
252+ </style>
253+ ` ;
254+
255+ // Create shadow root with styles
256+ const hostElement = document . createElement ( 'div' ) ;
257+ document . body . appendChild ( hostElement ) ;
258+ const shadowRoot = hostElement . attachShadow ( { mode : 'open' } ) ;
259+ shadowRoot . innerHTML = `
260+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
261+ .shadow-style {}/*!sc*/
262+ ${ SC_ATTR } .g20[id="shadowId"]{content:"shadowName,"}/*!sc*/
263+ </style>
264+ ` ;
265+
266+ // First sheet rehydrates from document
267+ const docSheet = new StyleSheet ( { isServer : true } ) ;
268+ rehydrateSheet ( docSheet ) ;
269+ expect ( docSheet . hasNameForId ( 'docId' , 'docName' ) ) . toBe ( true ) ;
270+ expect ( docSheet . hasNameForId ( 'shadowId' , 'shadowName' ) ) . toBe ( false ) ;
271+
272+ // Second sheet rehydrates from shadow root
273+ const shadowSheet = new StyleSheet ( { isServer : true , target : shadowRoot } ) ;
274+ rehydrateSheet ( shadowSheet ) ;
275+ expect ( shadowSheet . hasNameForId ( 'shadowId' , 'shadowName' ) ) . toBe ( true ) ;
276+ expect ( shadowSheet . hasNameForId ( 'docId' , 'docName' ) ) . toBe ( false ) ;
277+
278+ // Cleanup
279+ document . body . removeChild ( hostElement ) ;
280+ } ) ;
281+ } ) ;
282+ } ) ;
283+
284+ describe ( 'rehydrateSheet with different target types' , ( ) => {
285+ it ( 'rehydrates from document when no target is provided' , ( ) => {
286+ document . head . innerHTML = `
287+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
288+ .doc-no-target {}/*!sc*/
289+ ${ SC_ATTR } .g30[id="docNoTargetId"]{content:"docNoTargetName,"}/*!sc*/
290+ </style>
291+ ` ;
292+
293+ const sheet = new StyleSheet ( { isServer : true } ) ;
294+ rehydrateSheet ( sheet ) ;
295+
296+ expect ( sheet . hasNameForId ( 'docNoTargetId' , 'docNoTargetName' ) ) . toBe ( true ) ;
297+ } ) ;
298+
299+ it ( 'rehydrates from document when target is undefined' , ( ) => {
300+ document . head . innerHTML = `
301+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
302+ .doc-undefined {}/*!sc*/
303+ ${ SC_ATTR } .g31[id="docUndefinedId"]{content:"docUndefinedName,"}/*!sc*/
304+ </style>
305+ ` ;
306+
307+ const sheet = new StyleSheet ( { isServer : true , target : undefined } ) ;
308+ rehydrateSheet ( sheet ) ;
309+
310+ expect ( sheet . hasNameForId ( 'docUndefinedId' , 'docUndefinedName' ) ) . toBe ( true ) ;
311+ } ) ;
312+
313+ it ( 'rehydrates from ShadowRoot when target is a ShadowRoot' , ( ) => {
314+ const hostElement = document . createElement ( 'div' ) ;
315+ document . body . appendChild ( hostElement ) ;
316+ const shadowRoot = hostElement . attachShadow ( { mode : 'open' } ) ;
317+
318+ shadowRoot . innerHTML = `
319+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
320+ .shadow-root-target {}/*!sc*/
321+ ${ SC_ATTR } .g32[id="shadowRootId"]{content:"shadowRootName,"}/*!sc*/
322+ </style>
323+ ` ;
324+
325+ const sheet = new StyleSheet ( { isServer : true , target : shadowRoot } ) ;
326+ rehydrateSheet ( sheet ) ;
327+
328+ expect ( sheet . hasNameForId ( 'shadowRootId' , 'shadowRootName' ) ) . toBe ( true ) ;
329+
330+ // Cleanup
331+ document . body . removeChild ( hostElement ) ;
332+ } ) ;
333+
334+ it ( 'rehydrates from ShadowRoot when target is an HTMLElement inside a ShadowRoot' , ( ) => {
335+ const hostElement = document . createElement ( 'div' ) ;
336+ document . body . appendChild ( hostElement ) ;
337+ const shadowRoot = hostElement . attachShadow ( { mode : 'open' } ) ;
338+
339+ // Add styles first
340+ shadowRoot . innerHTML = `
341+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
342+ .shadow-inner-element {}/*!sc*/
343+ ${ SC_ATTR } .g33[id="shadowInnerId"]{content:"shadowInnerName,"}/*!sc*/
344+ </style>
345+ ` ;
346+
347+ // Then create and append the inner div (so it's not lost)
348+ const innerDiv = document . createElement ( 'div' ) ;
349+ shadowRoot . appendChild ( innerDiv ) ;
350+
351+ const sheet = new StyleSheet ( { isServer : true , target : innerDiv } ) ;
352+ rehydrateSheet ( sheet ) ;
353+
354+ expect ( sheet . hasNameForId ( 'shadowInnerId' , 'shadowInnerName' ) ) . toBe ( true ) ;
355+
356+ // Cleanup
357+ document . body . removeChild ( hostElement ) ;
358+ } ) ;
359+
360+ it ( 'rehydrates from document when target is a regular HTMLElement' , ( ) => {
361+ const regularDiv = document . createElement ( 'div' ) ;
362+ document . body . appendChild ( regularDiv ) ;
363+
364+ document . head . innerHTML = `
365+ <style ${ SC_ATTR } ${ SC_ATTR_VERSION } ="${ SC_VERSION } ">
366+ .regular-element {}/*!sc*/
367+ ${ SC_ATTR } .g34[id="regularId"]{content:"regularName,"}/*!sc*/
368+ </style>
369+ ` ;
370+
371+ const sheet = new StyleSheet ( { isServer : true , target : regularDiv } ) ;
372+ rehydrateSheet ( sheet ) ;
373+
374+ expect ( sheet . hasNameForId ( 'regularId' , 'regularName' ) ) . toBe ( true ) ;
375+
376+ // Cleanup
377+ document . body . removeChild ( regularDiv ) ;
378+ } ) ;
146379} ) ;
0 commit comments