diff --git a/tests/test_svg_image.py b/tests/test_svg_image.py index 84b8360..b296575 100644 --- a/tests/test_svg_image.py +++ b/tests/test_svg_image.py @@ -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): diff --git a/willow/svg.py b/willow/svg.py index cf42a3b..0e69bba 100644 --- a/willow/svg.py +++ b/willow/svg.py @@ -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(