Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improve handling of XML inside Android resources #46

Closed
wants to merge 5 commits into from

2 participants

@nijel

I'm not sure this is good approach, but at least it does not fail in simple cases like previous one (eg. string containing HTML with link).

@dwaynebailey
Owner

Thanks for the patch.

Could you add a test to test_aresource.py so that at least we know what we're fixing here and can prevent any future regressions.

Once we have a test, I'm happy for this to land.

@nijel

I've made some more fixes and added testcase for all these.

@dwaynebailey
Owner

Thanks for the fix.

Merged in 89c803a..b58d315

@mdentremont mdentremont referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 9, 2013
  1. @nijel

    Attempt to improve storing of markup in Android resources

    nijel authored nijel committed
    This seems to be quite tricky as we need to guess what was intended at
    this point.
  2. @nijel
  3. @nijel
  4. @nijel
  5. @nijel
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 10 deletions.
  1. +16 −10 translate/storage/aresource.py
  2. +3 −0  translate/storage/test_aresource.py
View
26 translate/storage/aresource.py
@@ -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."""
@@ -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)
@@ -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)
@@ -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"
View
3  translate/storage/test_aresource.py
@@ -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 + [
Something went wrong with that request. Please try again.