[FIX] translate: fix xml_translate to use an HTML parser as a fallback
When terms are translated in the website, their values are sent as HTML (with
text entities escaped). The function `xml_translate` is used to sanitize those
values, and sometimes crashes because of non-closed entities like `<br>`. Fix
it by using an HTML parser as a fallback.
This is a partial revert of 6c8e53a087.
This commit is contained in:
@@ -50,13 +50,6 @@ class TranslationToolsTestCase(unittest.TestCase):
|
||||
self.assertEquals(result, source)
|
||||
self.assertItemsEqual(terms, [source])
|
||||
|
||||
def test_translate_xml_text_entity_unescaped(self):
|
||||
""" Test xml_translate() on plain text with HTML entities. """
|
||||
terms = []
|
||||
source = "Blah blah blah"
|
||||
with self.assertRaises(XMLSyntaxError):
|
||||
xml_translate(terms.append, source)
|
||||
|
||||
def test_translate_xml_text_entity(self):
|
||||
""" Test xml_translate() on plain text with HTML escaped entities. """
|
||||
terms = []
|
||||
|
||||
@@ -271,10 +271,11 @@ def xml_translate(callback, value):
|
||||
trans.process(root)
|
||||
return trans.get_done()
|
||||
except etree.ParseError:
|
||||
# fallback in case it is a translated term...
|
||||
root = etree.fromstring("<div>%s</div>" % encode(value))
|
||||
trans.process(root)
|
||||
return trans.get_done()[5:-6] # remove tags <div> and </div>
|
||||
# fallback for translated terms: use an HTML parser and wrap the term
|
||||
wrapped = "<div>%s</div>" % encode(value)
|
||||
root = etree.fromstring(wrapped, etree.HTMLParser(encoding='utf-8'))
|
||||
trans.process(root[0][0]) # html > body > div
|
||||
return trans.get_done()[5:-6] # remove tags <div> and </div>
|
||||
|
||||
def html_translate(callback, value):
|
||||
""" Translate an HTML value (string), using `callback` for translating text
|
||||
|
||||
Reference in New Issue
Block a user