Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize _wp_normalize_relative_css_links() #4297

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2939,41 +2939,34 @@ static function( $a, $b ) {
* @return string The CSS with URLs made relative to the WordPress installation.
*/
function _wp_normalize_relative_css_links( $css, $stylesheet_url ) {
$has_src_results = preg_match_all( '#url\s*\(\s*[\'"]?\s*([^\'"\)]+)#', $css, $src_results );
if ( $has_src_results ) {
// Loop through the URLs to find relative ones.
foreach ( $src_results[1] as $src_index => $src_result ) {
// Skip if this is an absolute URL.
if ( 0 === strpos( $src_result, 'http' ) || 0 === strpos( $src_result, '//' ) ) {
continue;
}

// Skip if the URL is an HTML ID.
if ( str_starts_with( $src_result, '#' ) ) {
continue;
}

// Skip if the URL is a data URI.
if ( str_starts_with( $src_result, 'data:' ) ) {
continue;
return preg_replace_callback(
'#(url\s*\(\s*[\'"]?\s*)([^\'"\)]+)#',
static function ( $matches ) use ( $stylesheet_url ) {
list( , $prefix, $url ) = $matches;

if ( ! (
str_starts_with( $url, 'http:' )
||
str_starts_with( $url, 'https:' )
||
str_starts_with( $url, '//' )
||
str_starts_with( $url, '#' )
||
str_starts_with( $url, 'data:' )
) ) {
// Build the absolute URL.
$absolute_url = dirname( $stylesheet_url ) . '/' . $url;
$absolute_url = str_replace( '/./', '/', $absolute_url );

// Convert to URL related to the site root.
$url = wp_make_link_relative( $absolute_url );
}

// Build the absolute URL.
$absolute_url = dirname( $stylesheet_url ) . '/' . $src_result;
$absolute_url = str_replace( '/./', '/', $absolute_url );
// Convert to URL related to the site root.
$relative_url = wp_make_link_relative( $absolute_url );

// Replace the URL in the CSS.
$css = str_replace(
$src_results[0][ $src_index ],
str_replace( $src_result, $relative_url, $src_results[0][ $src_index ] ),
$css
);
}
}

return $css;
return $prefix . $url;
},
$css
);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/tests/dependencies/styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ public function data_normalize_relative_css_links() {
'css' => 'img {mask-image: url(\'data:image/svg+xml;utf8,<svg></svg>\');}',
'expected' => 'img {mask-image: url(\'data:image/svg+xml;utf8,<svg></svg>\');}',
),
'URLs with path beginning with http' => array(
'css' => 'p {background:url( "http-is-awesome.png" );}',
'expected' => 'p {background:url( "/wp-content/themes/test/http-is-awesome.png" );}',
),
'URLs with path beginning with https' => array(
'css' => 'p {background:url( "https-is-more-awesome.png" );}',
'expected' => 'p {background:url( "/wp-content/themes/test/https-is-more-awesome.png" );}',
),
);
}

Expand Down