Changeset 60920
- Timestamp:
- 10/10/2025 04:17:22 AM (5 weeks ago)
- Location:
- trunk
- Files:
-
- 3 edited
-
src/wp-includes/class-wp-styles.php (modified) (1 diff)
-
src/wp-includes/script-loader.php (modified) (1 diff)
-
tests/phpunit/tests/dependencies/styles.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-styles.php
r60719 r60920 334 334 $output = $this->get_data( $handle, 'after' ); 335 335 336 if ( empty( $output ) ) {336 if ( empty( $output ) || ! is_array( $output ) ) { 337 337 return false; 338 338 } 339 339 340 340 if ( ! $this->do_concat ) { 341 342 // Obtain the original `src` for a stylesheet possibly inlined by wp_maybe_inline_styles(). 343 $inlined_src = $this->get_data( $handle, 'inlined_src' ); 344 345 // If there's only one `after` inline style, and that inline style had been inlined, then use the $inlined_src 346 // as the sourceURL. Otherwise, if there is more than one inline `after` style associated with the handle, 347 // then resort to using the handle to construct the sourceURL since there isn't a single source. 348 if ( count( $output ) === 1 && is_string( $inlined_src ) && strlen( $inlined_src ) > 0 ) { 349 $source_url = esc_url_raw( $inlined_src ); 350 } else { 351 $source_url = rawurlencode( "{$handle}-inline-css" ); 352 } 353 341 354 $output[] = sprintf( 342 355 '/*# sourceURL=%s */', 343 rawurlencode( "{$handle}-inline-css" )356 $source_url 344 357 ); 345 358 } -
trunk/src/wp-includes/script-loader.php
r60719 r60920 3117 3117 $style['css'] = _wp_normalize_relative_css_links( $style['css'], $style['src'] ); 3118 3118 3119 // Keep track of the original `src` for the style that was inlined so that the `sourceURL` comment can be added. 3120 $wp_styles->add_data( $style['handle'], 'inlined_src', $style['src'] ); 3121 3119 3122 // Set `src` to `false` and add styles inline. 3120 3123 $wp_styles->registered[ $style['handle'] ]->src = false; -
trunk/tests/phpunit/tests/dependencies/styles.php
r60719 r60920 525 525 /** 526 526 * @ticket 58394 527 * @ticket 63887 527 528 * 528 529 * @covers ::wp_maybe_inline_styles 529 */ 530 public function test_wp_maybe_inline_styles() { 531 wp_register_style( 'test-handle', '/' . WPINC . '/css/classic-themes.css' ); 532 wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' ); 533 534 wp_enqueue_style( 'test-handle' ); 530 * @covers ::wp_add_inline_style 531 * @covers ::wp_print_styles 532 * @covers WP_Styles::do_items 533 * @covers WP_Styles::do_item 534 * @covers WP_Styles::print_inline_style 535 * 536 * @dataProvider data_provider_test_wp_maybe_inline_styles 537 */ 538 public function test_wp_maybe_inline_styles( ?string $additional_inline_style, ?int $styles_inline_size_limit ) { 539 $rel_path = 'css/classic-themes.css'; 540 $src_url = includes_url( $rel_path ); 541 $src_path = ABSPATH . WPINC . '/' . $rel_path; 542 $css = file_get_contents( $src_path ); 543 $handle = 'test-handle'; 544 545 if ( isset( $styles_inline_size_limit ) ) { 546 add_filter( 547 'styles_inline_size_limit', 548 static function () use ( $styles_inline_size_limit ): int { 549 return $styles_inline_size_limit; 550 } 551 ); 552 } 553 wp_register_style( $handle, $src_url, array(), null ); 554 wp_style_add_data( $handle, 'path', $src_path ); 555 if ( isset( $additional_inline_style ) ) { 556 wp_add_inline_style( $handle, $additional_inline_style ); 557 } 558 wp_enqueue_style( $handle ); 535 559 536 560 wp_maybe_inline_styles(); 537 561 538 $this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should be reset to false' ); 539 540 $css = file_get_contents( ABSPATH . WPINC . '/css/classic-themes.css' ); 541 $this->assertSameSets( $GLOBALS['wp_styles']->registered['test-handle']->extra['after'], array( $css ), 'Source of style should set to after property' ); 562 $expected_after = array(); 563 if ( ! isset( $styles_inline_size_limit ) || strlen( $css ) <= $styles_inline_size_limit ) { 564 $expected_after[] = $css; 565 $this->assertFalse( wp_styles()->registered[ $handle ]->src, 'Source of style should be reset to false' ); 566 $this->assertArrayHasKey( 'inlined_src', wp_styles()->registered[ $handle ]->extra ); 567 $this->assertSame( $src_url, wp_styles()->registered[ $handle ]->extra['inlined_src'] ); 568 } else { 569 $this->assertArrayNotHasKey( 'inlined_src', wp_styles()->registered[ $handle ]->extra ); 570 } 571 572 if ( isset( $additional_inline_style ) ) { 573 $expected_after[] = $additional_inline_style; 574 } 575 576 $after = wp_styles()->get_data( $handle, 'after' ); 577 if ( false === $after ) { 578 $after = array(); 579 } 580 $this->assertSameSets( $after, $expected_after, 'Source of style should set to after property' ); 581 582 $printed_styles = get_echo( 'wp_print_styles', array( $handle ) ); 583 $processor = new WP_HTML_Tag_Processor( $printed_styles ); 584 585 if ( isset( $styles_inline_size_limit ) && strlen( $css ) > $styles_inline_size_limit ) { 586 $this->assertTrue( $processor->next_tag() ); 587 $this->assertSame( 'LINK', $processor->get_tag() ); 588 $this->assertSame( 'stylesheet', $processor->get_attribute( 'rel' ) ); 589 $this->assertSame( $src_url, $processor->get_attribute( 'href' ) ); 590 } 591 592 if ( count( $expected_after ) > 0 ) { 593 $this->assertTrue( $processor->next_tag() ); 594 $this->assertSame( 'STYLE', $processor->get_tag() ); 595 $this->assertSame( $handle . '-inline-css', $processor->get_attribute( 'id' ) ); 596 $this->assertSame( 'text/css', $processor->get_attribute( 'type' ) ); 597 598 $expected_inline_styles = $expected_after; 599 if ( isset( $additional_inline_style ) ) { 600 $source_url = $handle . '-inline-css'; 601 } else { 602 $source_url = $src_url; 603 } 604 $expected_inline_styles[] = "/*# sourceURL=$source_url */"; 605 606 $expected_text = "\n" . implode( "\n", $expected_inline_styles ) . "\n"; 607 $this->assertSame( $expected_text, $processor->get_modifiable_text() ); 608 } 609 610 $this->assertFalse( $processor->next_tag() ); 611 } 612 613 /** 614 * Data provider for test_wp_maybe_inline_styles. 615 * 616 * @see self::test_wp_maybe_inline_styles() 617 * @return array<string, array{additional_inline_style: string|null, styles_inline_size_limit: int|null}> 618 */ 619 public static function data_provider_test_wp_maybe_inline_styles(): array { 620 return array( 621 'regular_limit_without_additional_inline_styles' => array( 622 'additional_inline_style' => null, 623 'styles_inline_size_limit' => null, 624 ), 625 'regular_limit_with_additional_inline_style' => array( 626 'additional_inline_style' => '/* additional inline style */', 627 'styles_inline_size_limit' => null, 628 ), 629 'zero_limit_without_additional_inline_style' => array( 630 'additional_inline_style' => null, 631 'styles_inline_size_limit' => 0, 632 ), 633 'zero_limit_with_additional_inline_style' => array( 634 'additional_inline_style' => '/* additional inline style */', 635 'styles_inline_size_limit' => 0, 636 ), 637 ); 542 638 } 543 639
Note: See TracChangeset
for help on using the changeset viewer.