Make WordPress Core

Changeset 55736


Ignore:
Timestamp:
05/09/2023 12:15:44 PM (3 years ago)
Author:
audrasjb
Message:

Script Loader: Optimize performance of _wp_normalize_relative_css_links() by more than 2x.

  • Replace preg_match_all() and its secondary str_replace() call with preg_replace_callback().
  • Fix case where paths beginning with http and https (but not http: and https:) were erroneously not counted as relative.
  • Improve code style and readability by consolidating conditions and returning once.
  • Use str_starts_with() consistently instead of strpos().

Follow-up to [52036], [52695], and [52754].

Props westonruter, adamsilverstein, azaozz.
Merges [55658] and [55669] to the 6.2 branch.
Fixes #58069.
See #54243.

Location:
branches/6.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.2

  • branches/6.2/src/wp-includes/script-loader.php

    r55730 r55736  
    29422942 */
    29432943function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
    2944         $has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results );
    2945         if ( $has_src_results ) {
    2946                 // Loop through the URLs to find relative ones.
    2947                 foreach ( $src_results[1] as $src_index => $src_result ) {
    2948                         // Skip if this is an absolute URL.
    2949                         if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) {
    2950                                 continue;
     2944        return preg_replace_callback(
     2945                '#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#',
     2946                static function ( $matches ) use ( $stylesheet_url ) {
     2947                        list( , $prefix, $url ) = $matches;
     2948
     2949                        // Short-circuit if the URL does not require normalization.
     2950                        if (
     2951                                str_starts_with( $url, 'http:' ) ||
     2952                                str_starts_with( $url, 'https:' ) ||
     2953                                str_starts_with( $url, '//' ) ||
     2954                                str_starts_with( $url, '#' ) ||
     2955                                str_starts_with( $url, 'data:' )
     2956                        ) {
     2957                                return $matches[0];
    29512958                        }
    29522959
    2953                         // Skip if the URL is an HTML ID.
    2954                         if ( str_starts_with( $src_result, '#' ) ) {
    2955                                 continue;
    2956                         }
    2957 
    2958                         // Skip if the URL is a data URI.
    2959                         if ( str_starts_with( $src_result, 'data:' ) ) {
    2960                                 continue;
    2961                         }
    2962 
    29632960                        // Build the absolute URL.
    2964                         $absolute_url = dirname( $stylesheet_url ) . '/' . $src_result;
     2961                        $absolute_url = dirname( $stylesheet_url ) . '/' . $url;
    29652962                        $absolute_url = str_replace( '/./', '/', $absolute_url );
     2963
    29662964                        // Convert to URL related to the site root.
    2967                         $relative_url = wp_make_link_relative( $absolute_url );
    2968 
    2969                         // Replace the URL in the CSS.
    2970                         $css = str_replace(
    2971                                 $src_results[0][ $src_index ],
    2972                                 str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ),
    2973                                 $css
    2974                         );
    2975                 }
    2976         }
    2977 
    2978         return $css;
     2965                        $url = wp_make_link_relative( $absolute_url );
     2966
     2967                        return $prefix . $url;
     2968                },
     2969                $css
     2970        );
    29792971}
    29802972
  • branches/6.2/tests/phpunit/tests/dependencies/styles.php

    r55368 r55736  
    197197         * @ticket 54243
    198198         * @ticket 54922
     199         * @ticket 58069
    199200         *
    200201         * @covers ::_wp_normalize_relative_css_links
     
    240241                                'css'      => 'img {mask-image: url(\'data:image/svg+xml;utf8,<svg></svg>\');}',
    241242                                'expected' => 'img {mask-image: url(\'data:image/svg+xml;utf8,<svg></svg>\');}',
     243                        ),
     244                        'URLs with path beginning with http'           => array(
     245                                'css'      => 'p {background:url( "http-is-awesome.png" );}',
     246                                'expected' => 'p {background:url( "/wp-content/themes/test/http-is-awesome.png" );}',
     247                        ),
     248                        'URLs with path beginning with https'          => array(
     249                                'css'      => 'p {background:url( "https-is-more-awesome.png" );}',
     250                                'expected' => 'p {background:url( "/wp-content/themes/test/https-is-more-awesome.png" );}',
    242251                        ),
    243252                );
Note: See TracChangeset for help on using the changeset viewer.