Skip to content
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 NUMBER_PATTERN regex for parsing SVG viewboxes #128

Merged
merged 2 commits into from
Aug 4, 2023
Merged
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
52 changes: 52 additions & 0 deletions tests/test_svg_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,58 @@ def test_view_box_re(self):
(".1,0,0,0", ViewBox(0.1, 0, 0, 0)),
("+.1,0,0,0", ViewBox(0.1, 0, 0, 0)),
("-.1,0,0,0", ViewBox(-0.1, 0, 0, 0)),
# https://github.com/wagtail/Willow/issues/127
# More combinations that include scientific notation
(
"-5.000000011410315e-06 0.0 750.00001 525.000007",
ViewBox(-5.000000011410315e-06, 0.0, 750.00001, 525.000007),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for -5.000000011410315e06 which should be valid as far as I can tell from https://www.w3.org/TR/SVG11/types.html#DataTypeNumber and https://www.w3.org/TR/SVG11/types.html#DataTypeInteger ?

Other than that, LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added a few more tests.

),
(
"0.0 -5.000000011410315e-06 750.00001 525.000007",
ViewBox(0.0, -5.000000011410315e-06, 750.00001, 525.000007),
),
(
"0.0 0.0 -5.000000011410315e-06 525.000007",
ViewBox(0.0, 0.0, -5.000000011410315e-06, 525.000007),
),
(
"0.0 0.0 750.00001 -5.000000011410315e-06",
ViewBox(0.0, 0.0, 750.00001, -5.000000011410315e-06),
),
(
"-5.000000011410315e-06 -5.000000011410315e-06 -5.000000011410315e-06 -5.000000011410315e-06",
ViewBox(
-5.000000011410315e-06,
-5.000000011410315e-06,
-5.000000011410315e-06,
-5.000000011410315e-06,
),
),
(
"-5.000000011410315e06 0.0 750.00001 525.000007",
ViewBox(-5.000000011410315e06, 0.0, 750.00001, 525.000007),
),
(
"0.0 -5.000000011410315e06 750.00001 525.000007",
ViewBox(0.0, -5.000000011410315e06, 750.00001, 525.000007),
),
(
"0.0 0.0 -5.000000011410315e06 525.000007",
ViewBox(0.0, 0.0, -5.000000011410315e06, 525.000007),
),
(
"0.0 0.0 750.00001 -5.000000011410315e06",
ViewBox(0.0, 0.0, 750.00001, -5.000000011410315e06),
),
(
"-5.000000011410315e06 -5.000000011410315e06 -5.000000011410315e06 -5.000000011410315e06",
ViewBox(
-5.000000011410315e06,
-5.000000011410315e06,
-5.000000011410315e06,
-5.000000011410315e06,
),
),
]
for value, expected in params:
with self.subTest(value=value, expected=expected):
Expand Down
3 changes: 2 additions & 1 deletion willow/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ class SvgWrapper:
UNIT_RE = re.compile(r"(?:em|ex|px|in|cm|mm|pt|pc|%)$")

# https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
# https://www.w3.org/TR/2013/WD-SVG2-20130409/types.html#DataTypeNumber
# This will exclude some inputs that Python will accept (e.g. "1.e9", "1."),
# but for integration with other tools, we should adhere to the spec
NUMBER_PATTERN = r"([+-]?(?:\d*\.)?\d+(?:[Ee]\d+)?)"
NUMBER_PATTERN = r"([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)"

# https://www.w3.org/Graphics/SVG/1.1/coords.html#ViewBoxAttribute
VIEW_BOX_RE = re.compile(
Expand Down