Skip to content

Commit 5b786db

Browse files
committed
Avoid svg: prefix in output by directly using the tag instead of QName for SVG namespace handling.
Fixes #317 Fixes #353
1 parent ca92eed commit 5b786db

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

qrcode/image/styles/moduledrawers/svg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class SvgQRModuleDrawer(BaseSvgQRModuleDrawer):
5353

5454
def initialize(self, *args, **kwargs) -> None:
5555
super().initialize(*args, **kwargs)
56-
self.tag_qname = ET.QName(self.img._SVG_namespace, self.tag)
56+
# Use tag directly instead of QName with namespace to avoid svg: prefix in output
57+
self.tag_qname = self.tag
5758

5859
def drawrect(self, box, is_active: bool):
5960
if not is_active:

qrcode/tests/regression/__init__.py

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import io
2+
3+
import pytest
4+
5+
import qrcode
6+
from qrcode.image import svg
7+
from qrcode.tests.consts import UNICODE_TEXT
8+
9+
10+
@pytest.mark.parametrize(
11+
"image_factory",
12+
[
13+
svg.SvgFillImage,
14+
svg.SvgFragmentImage,
15+
svg.SvgImage,
16+
svg.SvgPathFillImage,
17+
# svg.SvgPathImage, # Result does not contain <rect elements
18+
],
19+
)
20+
def test_svg_no_namespace_prefix(image_factory: svg.SvgFragmentImage):
21+
"""
22+
Test that SVG output doesn't contain <svg:rect> elements.
23+
24+
This regression test ensures that rect elements in SVG output are rendered as
25+
<rect> without the svg: namespace prefix, which can cause rendering issues in
26+
browsers, when loaded in HTML.
27+
28+
https://github.com/lincolnloop/python-qrcode/issues/353
29+
https://github.com/lincolnloop/python-qrcode/issues/317
30+
"""
31+
# Create a QR code
32+
qr = qrcode.QRCode()
33+
qr.add_data(UNICODE_TEXT)
34+
35+
img = qr.make_image(image_factory=image_factory)
36+
f = io.BytesIO()
37+
img.save(f)
38+
svg_content = f.getvalue().decode("utf-8")
39+
40+
# Check that there are no <svg:rect> elements in the output
41+
assert "<svg:rect" not in svg_content
42+
43+
# Check that there are <rect> elements in the output
44+
assert "<rect" in svg_content

0 commit comments

Comments
 (0)