Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of XML inside Android resources #46

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 16 additions & 10 deletions translate/storage/aresource.py
Expand Up @@ -31,7 +31,6 @@
EOF = None
WHITESPACE = ' \n\t' # Whitespace that we collapse
MULTIWHITESPACE = re.compile('[ \n\t]{2}')
OPEN_TAG_TO_ESCAPE = re.compile('<(?!/?\S*>)')

class AndroidResourceUnit(base.TranslationUnit):
"""A single term in the Android resource file."""
Expand Down Expand Up @@ -229,13 +228,20 @@ def getsource(self, lang=None):

def settarget(self, target):
if '<' in target:
# Handle text with markup
target = self.escape(target).replace('&', '&amp;')
target = OPEN_TAG_TO_ESCAPE.sub('&lt;', target)
# Parse new XML
newstring = etree.fromstring('<string>%s</string>' % target)
# Handle text with possible markup
target = target.replace('&', '&amp;')
try:
# Try as XML
newstring = etree.fromstring('<string>%s</string>' % target)
except:
# Fallback to string with XML escaping
target = target.replace('<', '&lt;')
newstring = etree.fromstring('<string>%s</string>' % target)
# Update text
self.xmlelement.text = newstring.text
if newstring.text is None:
self.xmlelement.text = ''
else:
self.xmlelement.text = newstring.text
# Remove old elements
for x in self.xmlelement.iterchildren():
self.xmlelement.remove(x)
Expand All @@ -249,10 +255,10 @@ def settarget(self, target):

def gettarget(self, lang=None):
# Grab inner text
target = (self.xmlelement.text or u'')
target = self.unescape(self.xmlelement.text or u'')
# Include markup as well
target += u''.join([data.forceunicode(etree.tostring(child, encoding='utf-8')) for child in self.xmlelement.iterchildren()])
return self.unescape(target)
return target

target = property(gettarget, settarget)

Expand Down Expand Up @@ -305,7 +311,7 @@ def __eq__(self, other):
class AndroidResourceFile(lisa.LISAfile):
"""Class representing a Android resource file store."""
UnitClass = AndroidResourceUnit
Name = _("Android Resource")
Name = _("Android String Resource")
Mimetypes = ["application/xml"]
Extensions = ["xml"]
rootNode = "resources"
Expand Down
3 changes: 3 additions & 0 deletions translate/storage/test_aresource.py
Expand Up @@ -19,6 +19,9 @@ class TestPropUnit(test_monolingual.TestMonolingualUnit):
(' leading space', '<string name="Test String">" leading space"</string>\n\n'),
('>xml&entities', '<string name="Test String">&gt;xml&amp;entities</string>\n\n'),
('some <b>html code</b> here', '<string name="Test String">some <b>html code</b> here</string>\n\n'),
('<<< arrow', '<string name="Test String">&lt;&lt;&lt; arrow</string>\n\n'),
('<a href="http://example.net">link</a>', '<string name="Test String"><a href="http://example.net">link</a></string>\n\n'),
('<a href="http://example.net">link</a> and text', '<string name="Test String"><a href="http://example.net">link</a> and text</string>\n\n'),
]

parse_test_data = escape_data + [
Expand Down