diff --git a/config/webpack-static-image-dimensions-plugin.js b/config/webpack-static-image-dimensions-plugin.js index ed83ee88..ef8eef27 100644 --- a/config/webpack-static-image-dimensions-plugin.js +++ b/config/webpack-static-image-dimensions-plugin.js @@ -22,8 +22,17 @@ function getSvgDimensionsFromContents(contents) { return null } - const widthMatch = contents.match(/\bwidth="(\d+(?:\.\d+)?)"/) - const heightMatch = contents.match(/\bheight="(\d+(?:\.\d+)?)"/) + // Only the root tag sizes the image: child elements () and + // prefixed attributes (stroke-width) must not be read as its dimensions. + const rootMatch = contents.match(/]*>/) + + if (!rootMatch) { + return null + } + + const root = rootMatch[0] + const widthMatch = root.match(/\swidth="(\d+(?:\.\d+)?)"/) + const heightMatch = root.match(/\sheight="(\d+(?:\.\d+)?)"/) if (widthMatch && heightMatch) { return { @@ -32,7 +41,7 @@ function getSvgDimensionsFromContents(contents) { } } - const viewBoxMatch = contents.match(/\bviewBox="[\d.\s-]+?\s+([\d.]+)\s+([\d.]+)"/) + const viewBoxMatch = root.match(/\sviewBox="[\d.\s-]+?\s+([\d.]+)\s+([\d.]+)"/) if (viewBoxMatch) { return { diff --git a/inc/Helpers/Formatting/Image.php b/inc/Helpers/Formatting/Image.php index 7c5903cf..41bc3523 100644 --- a/inc/Helpers/Formatting/Image.php +++ b/inc/Helpers/Formatting/Image.php @@ -228,15 +228,23 @@ function read_theme_static_image_dimensions_from_filesystem( string $relative_th return null; } - if ( preg_match( '/\bwidth="(\d+(?:\.\d+)?)"/', $contents, $width_match ) - && preg_match( '/\bheight="(\d+(?:\.\d+)?)"/', $contents, $height_match ) ) { + // Only the root tag sizes the image: child elements () and + // prefixed attributes (stroke-width) must not be read as its dimensions. + if ( ! preg_match( '/]*>/', $contents, $root_match ) ) { + return null; + } + + $root = $root_match[0]; + + if ( preg_match( '/\swidth="(\d+(?:\.\d+)?)"/', $root, $width_match ) + && preg_match( '/\sheight="(\d+(?:\.\d+)?)"/', $root, $height_match ) ) { return [ 'width' => (int) round( (float) $width_match[1] ), 'height' => (int) round( (float) $height_match[1] ), ]; } - if ( preg_match( '/\bviewBox="[\d.\s-]+?\s+([\d.]+)\s+([\d.]+)"/', $contents, $view_box_match ) ) { + if ( preg_match( '/\sviewBox="[\d.\s-]+?\s+([\d.]+)\s+([\d.]+)"/', $root, $view_box_match ) ) { return [ 'width' => max( 1, (int) round( (float) $view_box_match[1] ) ), 'height' => max( 1, (int) round( (float) $view_box_match[2] ) ),