Skip to content

Read static SVG dimensions from the root <svg> tag only - #534

Open
eastagile-tracker wants to merge 1 commit into
BeAPI:masterfrom
eastagile-tracker:agile-board/svg-root-dimensions
Open

eastagile-tracker wants to merge 1 commit into
BeAPI:masterfrom
eastagile-tracker:agile-board/svg-root-dimensions

Conversation

@eastagile-tracker

@eastagile-tracker eastagile-tracker commented Dec 3, 2026 •

Copy link
Copy Markdown

This PR proposes reading static SVG dimensions from the root <svg> tag only, so imageDimensions in dist/assets.json and the the_theme_static_image() fallback stop picking up stroke-width and child-element sizes. We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/1486. You can sign in with your GitHub ID to claim ownership of the project.

What was wrong

getSvgDimensionsFromContents() in config/webpack-static-image-dimensions-plugin.js (build time) and read_theme_static_image_dimensions_from_filesystem() in inc/Helpers/Formatting/Image.php (request-time fallback) both search the whole file for the first width="…" / height="…". Two things go wrong. \b matches right after a hyphen, so stroke-width="2" counts as a width. And when the root <svg> has only a viewBox, the first child element with a width/height (a <rect> with rx, an <image>, a <use>) is taken as the image size. Icon sets drawn with stroke-width on a viewBox-only root are common, so a 24x24 icon dropped into src/img/static/ ends up as <img width="2" height="18"> through get_theme_static_image_markup(), which is the opposite of what the CLS helper is for.

Reproduced on current master (36ae9f3):

printf '%s\n' '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M8 12h8"/></svg>' > src/img/static/repro-stroke.svg
npm run build
node -e 'console.log(require("./dist/assets.json").imageDimensions["dist/images/repro-stroke.svg"])'
# master:       { width: 2, height: 18 }
# this branch:  { width: 24, height: 24 }

The change

Both readers now take the root <svg …> start tag first and look for width, height and viewBox only inside it, requiring whitespace before the attribute name so stroke-width no longer matches. The precedence is unchanged (width/height first, then viewBox, rounded), and a file with no <svg> tag returns null as an empty one already did. The JS and PHP versions stay line-for-line equivalent.

Verification

I ran both functions, before and after, against the same fixtures. The first four are the fixtures from #529's ImageTest::test_filesystem_dimensions_svg, and they give the values that test expects on both sides:

fixture master this branch
size.svg (width="120.4" height="40" viewBox="0 0 10 10") 120x40 120x40
viewbox.svg (viewBox="0 0 64.6 32") 65x32 65x32
nosize.svg / empty.svg null null
viewBox 24x24, root stroke-width="2", child <rect width="18" height="18" rx="2"> 2x18 24x24
viewBox 48x24, child <rect width="10" height="10" rx="2"> 10x10 48x24
viewBox 600x200, child <image width="300" height="150"> 300x150 600x200
root stroke-width="2" width="24" height="24" 2x24 24x24
root width="100%" height="100%" viewBox="0 0 300 100" + child <rect> 10x10 300x100
XML prolog + comment, attributes on separate lines 180x60 180x60

npm run build, npm run lint:css and npm run lint:js pass before and after, and composer cs (the PHP Quality job) is clean with the change. The shipped arrow.svg and external.svg keep their 18x14 and 14x14 entries in dist/assets.json. #529 adds tests for this function but does not touch Image.php, so the two should merge without conflict.

How this was managed

The work is tracked as https://eastagiletracker.com/projects/1486/stories/1592937 on https://eastagiletracker.com/projects/1486, a board imported from this repository's issues and pull requests (532 stories) and used to manage this fix.

board

If you'd rather not receive contributions like this, reply no-more-prs on this pull request and we won't open any further ones on your repositories.


Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com


Note

Low Risk
Localized SVG parsing fix for static image dimension metadata; no auth, security, or broad API surface changes.

Overview
Static SVG sizing is corrected in both the webpack build plugin (getSvgDimensionsFromContents) and the PHP filesystem fallback (read_theme_static_image_dimensions_from_filesystem).

Instead of scanning the whole file for the first width/height, each implementation now extracts the opening root <svg …> tag and only reads width, height, or viewBox from that snippet. Attribute matching requires leading whitespace (\swidth, \sheight) so names like stroke-width are ignored, and nested shapes (<rect>, <image>, etc.) no longer override a viewBox-only icon.

Precedence is unchanged (explicit width/height on the root, then viewBox); missing or invalid root <svg> still yields null. Build-time dist/assets.json imageDimensions and runtime get_theme_static_image_markup() CLS attributes should align for typical icon SVGs (e.g. 24×24 from viewBox rather than 2×18 from stroke-width or child rects).

Reviewed by Cursor Bugbot for commit 666e871. Bugbot is set up for automated code reviews on this repo. Configure here.

The width/height/viewBox lookups for static SVGs (build-time plugin and
the PHP filesystem fallback) scanned the whole file, and \b matches
after a hyphen. A viewBox-only icon with stroke-width="2" and a <rect>
child was recorded as 2x18 instead of 24x24, so the img helpers output
wrong width/height attributes.

Restrict the lookups to the root <svg> start tag and require whitespace
before the attribute name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant