Skip to content

Commit

Permalink
Correctly escape xml characters in extensions. fixes #277
Browse files Browse the repository at this point in the history
  • Loading branch information
RDMurray committed Jun 10, 2024
1 parent 017d996 commit 4220c9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions gpxpy/gpxfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime as mod_datetime
import re as mod_re
import copy as mod_copy
import xml.sax.saxutils as mod_saxutils

from . import utils as mod_utils

Expand Down Expand Up @@ -400,8 +401,7 @@ def _ETree_to_xml(self, node: Any, nsmap: Dict[str, str]={}, prettyprint: bool=T
result.append(f' {attrib}="{value}"')
result.append('>')
if node.text is not None:
result.append(node.text.strip())

result.append(mod_saxutils.escape(node.text).strip())

# Build subelement nodes
for child in node:
Expand All @@ -412,7 +412,7 @@ def _ETree_to_xml(self, node: Any, nsmap: Dict[str, str]={}, prettyprint: bool=T
# Add tail and close tag
tail = node.tail
if tail is not None:
tail = tail.strip()
tail = mod_saxutils.escape(tail.strip())
else:
tail = ''
if len(node) > 0:
Expand Down
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2649,6 +2649,18 @@ def test_xml_chars_encode_decode(self) -> None:

self.assertTrue('<name>Test&lt;a&gt;jkljkl&lt;/gpx&gt;</name>' in gpx_2.to_xml())

def test_xml_chars_encode_decode_extensions(self) -> None:
gpx = mod_gpxpy.gpx.GPX()
ext = mod_etree.Element('test')
ext.text = "Test<a>jkljkl</gpx>"
ext.tail = "<&tail>"
gpx.extensions.append(ext)
print(gpx.to_xml())
gpx_2 = mod_gpxpy.parse(gpx.to_xml())
self.assertTrue('<test>Test&lt;a&gt;jkljkl&lt;/gpx&gt;</test>' in gpx_2.to_xml())
self.assertTrue('&lt;&amp;tail&gt;' in gpx_2.to_xml())


def test_10_to_11_conversion(self) -> None:
"""
This test checks that reparsing from 1.0 to 1.1 and from 1.1 to 1.0
Expand Down

0 comments on commit 4220c9a

Please sign in to comment.