Skip to content

Commit 660d04c

Browse files
Merge pull request #772 from lindsay-stevens/pyxform-771
771: restore pre-py3.13 xml attribute escaping behaviour for \r\n\t
2 parents 5b36cca + 4031c3c commit 660d04c

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

pyxform/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
SPACE_TRANS_TABLE = str.maketrans({" ": "_"})
3030
XML_TEXT_SUBS = {"&": "&amp;", "<": "&lt;", ">": "&gt;"}
3131
XML_TEXT_TABLE = str.maketrans(XML_TEXT_SUBS)
32-
XML_ATTR_SUBS = {'"': "&quot;", "\r": "&#13;", "\n": "&#10;", "\t": "&#9;"}
33-
XML_ATTR_TABLE = str.maketrans(XML_ATTR_SUBS)
3432

3533

3634
class DetachableElement(Element):
@@ -85,8 +83,8 @@ def escape_text_for_xml(text: str, attribute: bool = False) -> str:
8583
chars = set(text)
8684
if any(c in chars for c in XML_TEXT_SUBS):
8785
text = text.translate(XML_TEXT_TABLE)
88-
if attribute and any(c in chars for c in XML_ATTR_SUBS):
89-
text = text.translate(XML_ATTR_TABLE)
86+
if attribute and '"' in chars:
87+
text = text.replace('"', "&quot;")
9088
return text
9189

9290

tests/xform_test_case/test_xml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class MinidomTextWriterMonkeyPatchTest(TestCase):
9090
maxDiff = None
9191

9292
def test_patch_lets_node_func_escape_only_necessary(self):
93-
"""Should find that pyxform escapes ["&<>\r\n\t] in attrs and [&<>] in text."""
93+
"""Should find that pyxform escapes ["&<>] in attrs and [&<>] in text."""
9494
replaceable_chars = "' \" & < > \r \n \t"
95-
expected = """<root attr="' &quot; &amp; &lt; &gt; &#13; &#10; &#9;">' " &amp; &lt; &gt; \r \n \t</root>"""
95+
expected = """<root attr="' &quot; &amp; &lt; &gt; \r \n \t">' " &amp; &lt; &gt; \r \n \t</root>"""
9696
observed = node("root", replaceable_chars, attr=replaceable_chars).toprettyxml(
9797
indent="", newl=""
9898
)

0 commit comments

Comments
 (0)