Skip to content
Open
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
15 changes: 12 additions & 3 deletions config/webpack-static-image-dimensions-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <svg> tag sizes the image: child elements (<rect width>) and
// prefixed attributes (stroke-width) must not be read as its dimensions.
const rootMatch = contents.match(/<svg\b[^>]*>/)

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 {
Expand All @@ -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 {
Expand Down
14 changes: 11 additions & 3 deletions inc/Helpers/Formatting/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <svg> tag sizes the image: child elements (<rect width>) and
// prefixed attributes (stroke-width) must not be read as its dimensions.
if ( ! preg_match( '/<svg\b[^>]*>/', $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] ) ),
Expand Down