Repository navigation
Read static SVG dimensions from the root <svg> tag only - #534
Open
eastagile-tracker wants to merge 1 commit into
Open
eastagile-tracker wants to merge 1 commit into
eastagile-tracker wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR proposes reading static SVG dimensions from the root
<svg>tag only, soimageDimensionsindist/assets.jsonand thethe_theme_static_image()fallback stop picking upstroke-widthand 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()inconfig/webpack-static-image-dimensions-plugin.js(build time) andread_theme_static_image_dimensions_from_filesystem()ininc/Helpers/Formatting/Image.php(request-time fallback) both search the whole file for the firstwidth="…"/height="…". Two things go wrong.\bmatches right after a hyphen, sostroke-width="2"counts as a width. And when the root<svg>has only aviewBox, the first child element with awidth/height(a<rect>withrx, an<image>, a<use>) is taken as the image size. Icon sets drawn withstroke-widthon a viewBox-only root are common, so a 24x24 icon dropped intosrc/img/static/ends up as<img width="2" height="18">throughget_theme_static_image_markup(), which is the opposite of what the CLS helper is for.Reproduced on current
master(36ae9f3):The change
Both readers now take the root
<svg …>start tag first and look forwidth,heightandviewBoxonly inside it, requiring whitespace before the attribute name sostroke-widthno longer matches. The precedence is unchanged (width/height first, then viewBox, rounded), and a file with no<svg>tag returnsnullas 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:size.svg(width="120.4" height="40" viewBox="0 0 10 10")viewbox.svg(viewBox="0 0 64.6 32")nosize.svg/empty.svgstroke-width="2", child<rect width="18" height="18" rx="2"><rect width="10" height="10" rx="2"><image width="300" height="150">stroke-width="2" width="24" height="24"width="100%" height="100%" viewBox="0 0 300 100"+ child<rect>npm run build,npm run lint:cssandnpm run lint:jspass before and after, andcomposer cs(the PHP Quality job) is clean with the change. The shippedarrow.svgandexternal.svgkeep their 18x14 and 14x14 entries indist/assets.json. #529 adds tests for this function but does not touchImage.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.
If you'd rather not receive contributions like this, reply
no-more-prson 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 readswidth,height, orviewBoxfrom that snippet. Attribute matching requires leading whitespace (\swidth,\sheight) so names likestroke-widthare 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 yieldsnull. Build-timedist/assets.jsonimageDimensionsand runtimeget_theme_static_image_markup()CLS attributes should align for typical icon SVGs (e.g. 24×24 fromviewBoxrather than 2×18 fromstroke-widthor child rects).Reviewed by Cursor Bugbot for commit 666e871. Bugbot is set up for automated code reviews on this repo. Configure here.