Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pyxform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
SPACE_TRANS_TABLE = str.maketrans({" ": "_"})
XML_TEXT_SUBS = {"&": "&amp;", "<": "&lt;", ">": "&gt;"}
XML_TEXT_TABLE = str.maketrans(XML_TEXT_SUBS)
XML_ATTR_SUBS = {'"': "&quot;", "\r": "&#13;", "\n": "&#10;", "\t": "&#9;"}
XML_ATTR_TABLE = str.maketrans(XML_ATTR_SUBS)


class DetachableElement(Element):
Expand Down Expand Up @@ -85,8 +83,8 @@ def escape_text_for_xml(text: str, attribute: bool = False) -> str:
chars = set(text)
if any(c in chars for c in XML_TEXT_SUBS):
text = text.translate(XML_TEXT_TABLE)
if attribute and any(c in chars for c in XML_ATTR_SUBS):
text = text.translate(XML_ATTR_TABLE)
if attribute and '"' in chars:
text = text.replace('"', "&quot;")
return text


Expand Down
4 changes: 2 additions & 2 deletions tests/xform_test_case/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class MinidomTextWriterMonkeyPatchTest(TestCase):
maxDiff = None

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