Skip to content

Commit b769889

Browse files
vipul0425vipul0425DAreRodz
authored
iAPI Router: Ignores <noscript> elements while preparing DOM. (#70905)
* feat: Ignores <noscript> elements while preparing DOM. * Docs: Update change logs with recent changes. * feat: Adds e2e tests for noscript fix. * docs: Added comment on code block. * Remove nonexistent stylesheet from noscript tests --------- Co-authored-by: vipul0425 <[email protected]> Co-authored-by: DAreRodz <[email protected]>
1 parent a50d231 commit b769889

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

packages/e2e-tests/plugins/interactive-blocks/router-styles-blue/render.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ function () {
3333
);
3434
?>
3535
<p <?php echo $wrapper_attributes; ?>>Blue</p>
36+
37+
<noscript>
38+
<style>
39+
.noscript-style-test {
40+
color: rgb(0, 0, 255) !important;
41+
background-color: yellow !important;
42+
}
43+
</style>
44+
</noscript>

packages/e2e-tests/plugins/interactive-blocks/router-styles-red/render.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ function () {
3333
);
3434
?>
3535
<p <?php echo $wrapper_attributes; ?>>Red</p>
36+
37+
<noscript>
38+
<style>
39+
.noscript-style-test {
40+
color: rgb(255, 0, 0) !important;
41+
font-weight: bold !important;
42+
}
43+
</style>
44+
</noscript>

packages/e2e-tests/plugins/interactive-blocks/router-styles-wrapper/render.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,16 @@ function () {
101101

102102
<!-- Text hidden when media=print applies. -->
103103
<div class="hide-on-print" data-testid="hide-on-print">This should be visible when media is not "print".</div>
104+
105+
<!-- Element for testing noscript styles being ignored -->
106+
<div data-testid="noscript-style-test" class="noscript-style-test">This should not be affected by styles in noscript tags</div>
107+
108+
<!-- Noscript styles that should be ignored -->
109+
<noscript>
110+
<style>
111+
.noscript-style-test {
112+
color: rgb(255, 0, 0) !important;
113+
}
114+
</style>
115+
</noscript>
104116
</div>
105-
106-
107-

packages/interactivity-router/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
- Preserve `media` attribute on intial style sheets after client-side navigation. ([70668](https://github.com/WordPress/gutenberg/pull/70668))
1010

11+
- Ignores `<noscript>` elements while preparing DOM. ([70905](https://github.com/WordPress/gutenberg/pull/70905))
12+
13+
1114
## 2.26.0 (2025-06-25)
1215

1316
### New Features

packages/interactivity-router/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ const fetchPage = async ( url: string, { html }: { html: string } ) => {
138138
* server-rendered data.
139139
*/
140140
const preparePage: PreparePage = async ( url, dom, { vdom } = {} ) => {
141+
// Remove all noscript elements as they're irrelevant when request is served via router.
142+
// This prevents browsers from extracting styles from noscript tags.
143+
dom.querySelectorAll( 'noscript' ).forEach( ( el ) => el.remove() );
144+
141145
const regions = {};
142146
const regionsToAttach = {};
143147
dom.querySelectorAll( regionsSelector ).forEach( ( region ) => {

test/e2e/specs/interactivity/router-styles.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,34 @@ test.describe( 'Router styles', () => {
430430
// The "hide-on-print" element should remain visible.
431431
await expect( hideOnPrint ).toBeVisible();
432432
} );
433+
434+
test( 'should ignore styles inside noscript elements during navigation', async ( {
435+
page,
436+
} ) => {
437+
const csn = page.getByTestId( 'client-side navigation' );
438+
const noscriptStyleTest = page.getByTestId( 'noscript-style-test' );
439+
440+
// Initially the element should not have styling from noscript.
441+
await expect( noscriptStyleTest ).toHaveCSS( 'color', COLOR_WRAPPER );
442+
443+
await page.getByTestId( 'link red' ).click();
444+
445+
// This element disappears when a navigation starts.
446+
// It should be visible again after a successful navigation.
447+
await expect( csn ).toBeHidden();
448+
await expect( csn ).toBeVisible();
449+
450+
// After navigation, the element should still have the default color
451+
// and not be affected by styles in noscript elements
452+
await expect( noscriptStyleTest ).toHaveCSS( 'color', COLOR_WRAPPER );
453+
454+
await page.getByTestId( 'link all' ).click();
455+
456+
await expect( csn ).toBeHidden();
457+
await expect( csn ).toBeVisible();
458+
459+
// After navigating to the page with all blocks, the element should
460+
// still maintain its original styling and not be affected by noscript styles
461+
await expect( noscriptStyleTest ).toHaveCSS( 'color', COLOR_WRAPPER );
462+
} );
433463
} );

0 commit comments

Comments
 (0)