Make WordPress Core


Ignore:
Timestamp:
01/21/2026 04:42:31 PM (10 months ago)
Author:
jonsurrell
Message:

Script Loader: Fix script module fetchpriority calculation when dependent with higher priority is not enqueued.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/10651

Follow-up to [60931], [60704].

Reviewed by jonsurrell.
Merges [61401] to the 6.9 branch.

Props westonruter, jonsurrell, youknowriad.
Fixes #64429. See #61734.

Location:
branches/6.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.9

  • branches/6.9/tests/phpunit/tests/dependencies/scripts.php

    r61260 r61506  
    14681468                        $result,
    14691469                        'Expected "high" indicates that the cached `$stored_results` entry for D was used instead of recalculating.'
     1470                );
     1471        }
     1472
     1473        /**
     1474         * Tests expected priority is used when a dependent is registered but not enqueued.
     1475         *
     1476         * @ticket 64429
     1477         *
     1478         * @covers WP_Scripts::print_scripts
     1479         * @covers WP_Scripts::get_highest_fetchpriority_with_dependents
     1480         */
     1481        public function test_priority_of_dependency_for_non_enqueued_dependent() {
     1482                $wp_scripts = wp_scripts();
     1483                wp_default_scripts( $wp_scripts );
     1484
     1485                $wp_scripts->add( 'not-enqueued', 'https://example.com/not-enqueued.js', array( 'comment-reply' ), null, array( 'priority' => 'high' ) );
     1486                $wp_scripts->enqueue( 'comment-reply' );
     1487
     1488                $actual = $this->normalize_markup_for_snapshot( get_echo( array( $wp_scripts, 'print_scripts' ) ) );
     1489                $this->assertEqualHTML(
     1490                        '<script type="text/javascript" src="/wp-includes/js/comment-reply.js" id="comment-reply-js" async="async" data-wp-strategy="async" fetchpriority="low"></script>',
     1491                        $actual,
     1492                        '<body>',
     1493                        "Snapshot:\n$actual"
    14701494                );
    14711495        }
    … …  
    40944118                $this->assertStringNotContainsStringIgnoringCase( 'sourceURL=', $translations_script_data );
    40954119        }
     4120
     4121        /**
     4122         * Normalizes markup for snapshot.
     4123         *
     4124         * @param string $markup Markup.
     4125         * @return string Normalized markup.
     4126         */
     4127        private function normalize_markup_for_snapshot( string $markup ): string {
     4128                $processor = new WP_HTML_Tag_Processor( $markup );
     4129                $clean_url = static function ( string $url ): string {
     4130                        $url = preg_replace( '#^https?://[^/]+#', '', $url );
     4131                        return remove_query_arg( 'ver', $url );
     4132                };
     4133                while ( $processor->next_tag() ) {
     4134                        if ( 'LINK' === $processor->get_tag() && is_string( $processor->get_attribute( 'href' ) ) ) {
     4135                                $processor->set_attribute( 'href', $clean_url( $processor->get_attribute( 'href' ) ) );
     4136                        } elseif ( 'SCRIPT' === $processor->get_tag() && is_string( $processor->get_attribute( 'src' ) ) ) {
     4137                                $processor->set_attribute( 'src', $clean_url( $processor->get_attribute( 'src' ) ) );
     4138                        }
     4139                }
     4140                return $processor->get_updated_html();
     4141        }
    40964142}
Note: See TracChangeset for help on using the changeset viewer.