Skip to content

Commit

Permalink
Fix NUMBER_PATTERN regex for parsing SVG viewboxes (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jams2 authored and zerolab committed Aug 4, 2023
1 parent bb120f9 commit 2ccd97c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
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),
),
(
"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

0 comments on commit 2ccd97c

Please sign in to comment.