|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import io |
| 4 | +import re |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import qrcode |
| 10 | +from qrcode.image import svg |
| 11 | +from qrcode.tests.consts import UNICODE_TEXT |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from qrcode.image.base import BaseImageWithDrawer |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "image_factory", |
| 19 | + [ |
| 20 | + svg.SvgFragmentImage, |
| 21 | + svg.SvgImage, |
| 22 | + svg.SvgFillImage, |
| 23 | + svg.SvgPathImage, |
| 24 | + svg.SvgPathFillImage, |
| 25 | + ], |
| 26 | +) |
| 27 | +def test_svg_no_width_height(image_factory: BaseImageWithDrawer) -> None: |
| 28 | + """Test that SVG output doesn't have width and height attributes.""" |
| 29 | + qr = qrcode.QRCode() |
| 30 | + qr.add_data(UNICODE_TEXT) |
| 31 | + |
| 32 | + # Create a svg with the specified factory and (optional) module drawer |
| 33 | + img = qr.make_image(image_factory=image_factory) |
| 34 | + svg_str = img.to_string().decode("utf-8") |
| 35 | + |
| 36 | + # Check that width and height attributes are not present in the SVG tag |
| 37 | + svg_tag_match = re.search(r"<svg[^>]*>", svg_str) |
| 38 | + assert svg_tag_match, "SVG tag not found" |
| 39 | + |
| 40 | + svg_tag = svg_tag_match.group(0) |
| 41 | + assert "width=" not in svg_tag, "width attribute should not be present" |
| 42 | + assert "height=" not in svg_tag, "height attribute should not be present" |
| 43 | + |
| 44 | + # Check that viewBox is present and uses pixels (no mm suffix) |
| 45 | + viewbox_match = re.search(r'viewBox="([^"]*)"', svg_tag) |
| 46 | + assert viewbox_match, "viewBox attribute not found" |
| 47 | + viewbox = viewbox_match.group(1) |
| 48 | + assert "mm" not in viewbox, "viewBox should use pixels, not mm" |
| 49 | + |
| 50 | + # Check that inner elements use pixels (no mm suffix) |
| 51 | + assert "mm" not in svg_str, "SVG elements should use pixels, not mm" |
0 commit comments