Skip to content

Commit 5898dac

Browse files
committed
Reapply "iAPI Router: Fix CSS rule order in some constructed style sheets (WordPress#68923)"
This reverts commit a53bb20.
1 parent 2c71cfa commit 5898dac

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
88
*/
99

10+
add_action(
11+
'wp_enqueue_scripts',
12+
function () {
13+
wp_enqueue_style(
14+
'wrapper-styles-from-link',
15+
plugin_dir_url( __FILE__ ) . 'style-from-link.css',
16+
array()
17+
);
18+
}
19+
);
20+
1021
$wrapper_attributes = get_block_wrapper_attributes();
1122
?>
1223
<div <?php echo $wrapper_attributes; ?>>
@@ -38,6 +49,12 @@
3849
<p data-testid="all-from-inline" class="red-from-inline green-from-inline blue-from-inline">All</p>
3950
</fieldset>
4051

52+
<!-- This one should remain green after navigation. -->
53+
<fieldset>
54+
<legend>Rule order checker</legend>
55+
<p data-testid="order-checker" class="order-checker">I should remain green</p>
56+
</fieldset>
57+
4158
<!-- Links to pages with different blocks combination. -->
4259
<nav data-wp-interactive="test/router-styles">
4360
<?php foreach ( $attributes['links'] as $label => $link ) : ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.wp-block-test-router-styles-wrapper .order-checker {
2+
color: rgb(255, 0, 0);
3+
}
4+
5+
.wp-block-test-router-styles-wrapper .order-checker {
6+
color: rgb(0, 255, 0);
7+
}

packages/interactivity-router/src/assets/styles.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ const sheetFromLink = async (
4141
if ( elementSheet ) {
4242
return getCachedSheet( sheetId, () => {
4343
const sheet = new CSSStyleSheet();
44-
for ( const { cssText } of elementSheet.cssRules ) {
45-
sheet.insertRule( withAbsoluteUrls( cssText, sheetUrl ) );
44+
for ( let i = 0; i < elementSheet.cssRules.length; i++ ) {
45+
const { cssText } = elementSheet.cssRules[ i ];
46+
sheet.insertRule( withAbsoluteUrls( cssText, sheetUrl ), i );
4647
}
4748
return Promise.resolve( sheet );
4849
} );

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

+29
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,33 @@ test.describe( 'Router styles', () => {
229229
await expect( blue ).toHaveCSS( 'color', COLOR_BLUE );
230230
await expect( all ).toHaveCSS( 'color', COLOR_BLUE );
231231
} );
232+
233+
test( 'should preserve rule order from referenced style sheets', async ( {
234+
page,
235+
} ) => {
236+
const csn = page.getByTestId( 'client-side navigation' );
237+
const orderChecker = page.getByTestId( 'order-checker' );
238+
239+
await expect( orderChecker ).toHaveCSS( 'color', COLOR_GREEN );
240+
241+
await page.getByTestId( 'link red' ).click();
242+
243+
await expect( csn ).toBeVisible();
244+
await expect( orderChecker ).toHaveCSS( 'color', COLOR_GREEN );
245+
246+
await page.getByTestId( 'link green' ).click();
247+
248+
await expect( csn ).toBeVisible();
249+
await expect( orderChecker ).toHaveCSS( 'color', COLOR_GREEN );
250+
251+
await page.getByTestId( 'link blue' ).click();
252+
253+
await expect( csn ).toBeVisible();
254+
await expect( orderChecker ).toHaveCSS( 'color', COLOR_GREEN );
255+
256+
await page.getByTestId( 'link all' ).click();
257+
258+
await expect( csn ).toBeVisible();
259+
await expect( orderChecker ).toHaveCSS( 'color', COLOR_GREEN );
260+
} );
232261
} );

0 commit comments

Comments
 (0)