-
Notifications
You must be signed in to change notification settings - Fork 35
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
Fix bug around SVG dimensions #43
Conversation
…maintain backwards compat. Add a new filter that can be used to default to using the height and width attributes first instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Added a note about the hook docs but feel free to correct me if OSP use the WP coding standards.
Co-authored-by: Peter Wilson <[email protected]>
I'd prefer to use the WP coding standards but I had forgotten that in order to generate docs using https://github.com/matzeeable/wp-hookdoc, you need to follow the JSDoc standard (unless there's a workaround here I'm not aware of) |
Thanks Darin, the only alternative I am aware of is to use https://github.com/WordPress/phpdoc-parser. That's not so much a workaround as different library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Description of the Change
In PR #19, a bug was fixed around how we calculate the image dimensions of an SVG. Prior to that fix, we would always default to using the
viewBox
dimensions and not thewidth
andheight
attributes, even though the latter were looked at first. The fix in #19 ensured we properly considered those attributes but this led to potential bugs when an SVG had different dimensions in theheight
andwidth
attributes as compared to theviewBox
attributes.For instance, if an SVG had a
viewBox
of0 0 32 32
and a width of200
and height of200
, even though we look at height and width first, because of the bug, those values were never used and instead, 32x32 would be used as the SVG dimensions (and stored as image meta). With the bug fix introduced in #19, we now properly use theheight
andwidth
attributes first but in this example, we still have 32x32 stored, so we end up with two different image dimensions. This can cause the SVG to use improper dimensions and be smaller or bigger than previous.In addition, while researching this I found that this bug was introduced in #11, in an attempt to fix issues when the
height
orwidth
attributes were percentages. That bug was brought back by the changes in #19.This PR attempts to fix all of these issues:
viewBox
attribute first. My personal opinion here is that theheight
andwidth
attributes are probably better to use as a default but that will unfortunately lead to backwards compatibility breaks now. I've introduced a new filter (safe_svg_use_width_height_attributes
) that can be used to change that behavior for sites that want to reverse the default order here:Usage
height
orwidth
attribute ends in a percent sign and if so, we don't use that valueAlternate Designs
We don't necessarily need to provide a filter here to change the default order of attribute usage but I figured it would be nice to allow users that option. We could just permanently swap the order and provide no way to change it.
We could also keep the order as-is and change the newly added filter to swap to using
viewBox
first. This would cause the same issues that were reported but our response to those users could be to use the new filter to fix that. I do personally think usingheight
andwidth
first makes the most sense but I don't like knowingly breaking user's sites.Possible Drawbacks
None I'm aware of
Verification Process
Upload and output an SVG that has different values in the
height
andwidth
attributes as compared to theviewBox
attribute. With the filter in the default state, the resulting image tag should use theviewBox
dimensions. Change the filter:and now the resulting image tag should use the other attribute values
Checklist:
Changelog Entry