@@ -956,40 +956,69 @@ public function test_wp_hoist_late_printed_styles(): void {
956956 switch_theme ( 'default ' );
957957
958958 // Enqueue a style
959- wp_enqueue_style ( 'test-style ' , 'http://example.com/style.css ' );
959+ wp_enqueue_style ( 'early ' , 'http://example.com/style.css ' );
960+ wp_add_inline_style ( 'early ' , '/* EARLY */ ' );
960961
961962 wp_hoist_late_printed_styles ();
962963
963964 // Ensure late styles are printed.
964965 add_filter ( 'print_late_styles ' , '__return_false ' , 1000 );
965966 $ this ->assertTrue ( apply_filters ( 'print_late_styles ' , true ), 'Expected late style printing to be forced. ' );
966967
967- // Simulate wp_head
968- ob_start ();
969- do_action ( 'wp_head ' );
970- $ head_output = ob_get_clean ();
968+ // Simulate wp_head.
969+ $ head_output = get_echo ( 'wp_head ' );
971970
972- $ this ->assertMatchesRegularExpression ( '/<!--wp_late_styles_placeholder:[a-f0-9-]+-->/ ' , $ head_output , 'Expect the placeholder to be present ' );
973- $ this ->assertStringContainsString ( 'test-style ' , $ head_output , 'Expect the enqueued stylesheet to be present ' );
971+ $ placeholder_pattern = '/<!--wp_late_styles_placeholder:[a-f0-9-]+-->/ ' ;
974972
975- // Enqueue a late style (after wp_head)
976- wp_enqueue_style ( 'test-late-style ' , ' http://example.com/late-style.css ' );
973+ $ this -> assertMatchesRegularExpression ( $ placeholder_pattern , $ head_output , ' Expected the placeholder to be present ' );
974+ $ this -> assertStringContainsString ( 'early ' , $ head_output , ' Expected the early-enqueued stylesheet to be present. ' );
977975
978- // Simulate footer scripts
979- ob_start ();
980- do_action ( 'wp_print_footer_scripts ' );
981- $ footer_output = ob_get_clean ();
976+ // Enqueue a late style (after wp_head).
977+ wp_enqueue_style ( 'late ' , 'http://example.com/late-style.css ' , array (), null );
978+ wp_add_inline_style ( 'late ' , '/* EARLY */ ' );
979+
980+ // Simulate footer scripts.
981+ $ footer_output = get_echo ( 'wp_footer ' );
982982
983- // Create a complete HTML buffer
984- $ buffer = '<html><head> ' . $ head_output . '</head><body>Content ' . $ footer_output . '</body></html> ' ;
983+ // Create a simulated output buffer.
984+ $ buffer = '<html><head> ' . $ head_output . '</head><body><main> Content</main> ' . $ footer_output . '</body></html> ' ;
985985
986- // Apply the output buffer filter
986+ // Apply the output buffer filter.
987987 $ filtered_buffer = apply_filters ( 'wp_template_enhancement_output_buffer ' , $ buffer );
988988
989- preg_match ( '/<head>(.+?)<\/head>/s ' , $ filtered_buffer , $ head_matches );
990- $ this ->assertNotEmpty ( $ head_matches , 'Expect the late enqueued styles to be present in the header ' );
991- $ this ->assertStringContainsString ( 'test-late-style ' , $ head_matches [1 ], 'Expect the late enqueued styles to be present in the header ' );
992- $ this ->assertStringNotContainsString ( '<!--late_styles: ' , $ filtered_buffer , 'Expect the placeholder to be gone ' );
989+ $ this ->assertDoesNotMatchRegularExpression ( $ placeholder_pattern , $ filtered_buffer , 'Expected the placeholder to be removed. ' );
990+ $ found_styles = array (
991+ 'HEAD ' => array (),
992+ 'BODY ' => array (),
993+ );
994+ $ processor = WP_HTML_Processor::create_full_parser ( $ filtered_buffer );
995+ while ( $ processor ->next_tag () ) {
996+ $ group = in_array ( 'HEAD ' , $ processor ->get_breadcrumbs (), true ) ? 'HEAD ' : 'BODY ' ;
997+ if (
998+ 'LINK ' === $ processor ->get_tag () &&
999+ $ processor ->get_attribute ( 'rel ' ) === 'stylesheet '
1000+ ) {
1001+ $ found_styles [ $ group ][] = $ processor ->get_attribute ( 'id ' );
1002+ } elseif ( 'STYLE ' === $ processor ->get_tag () ) {
1003+ $ found_styles [ $ group ][] = $ processor ->get_attribute ( 'id ' );
1004+ }
1005+ }
1006+
1007+ $ expected = array (
1008+ 'early-css ' ,
1009+ 'early-inline-css ' ,
1010+ 'late-css ' ,
1011+ 'late-inline-css ' ,
1012+ );
1013+ foreach ( $ expected as $ style_id ) {
1014+ $ this ->assertContains ( $ style_id , $ found_styles ['HEAD ' ], 'Expected stylesheet with ID to be in the HEAD. ' );
1015+ }
1016+ $ this ->assertSame (
1017+ $ expected ,
1018+ array_values ( array_intersect ( $ found_styles ['HEAD ' ], $ expected ) ),
1019+ 'Expected styles to be printed in the same order. '
1020+ );
1021+ $ this ->assertCount ( 0 , $ found_styles ['BODY ' ], 'Expected no styles to be present in the footer. ' );
9931022 }
9941023
9951024 public function assertTemplateHierarchy ( $ url , array $ expected , $ message = '' ) {
0 commit comments