Introducing new tests for _encode_escape_chars.
This commit is contained in:
Christopher Ramírez 2017-06-12 12:54:40 -06:00
parent b62e9e1db3
commit 3d2cedeec8
2 changed files with 20 additions and 9 deletions

View file

@ -431,7 +431,7 @@ class Renderer(object):
""" """
Replace line feed and/or tabs within text:span entities. Replace line feed and/or tabs within text:span entities.
""" """
find_pattern = r'(?is)<text:([\S]+?)>([^>]*?([\n|\t])[^<]*?)</text:\1>' find_pattern = r'(?is)<text:([\S]+?).*?>([^>]*?([\n\t])[^<]*?)</text:\1>'
for m in re.findall(find_pattern, xml_text): for m in re.findall(find_pattern, xml_text):
replacement = m[1].replace('\n', '<text:line-break/>') replacement = m[1].replace('\n', '<text:line-break/>')
replacement = replacement.replace('\t', '<text:tab/>') replacement = replacement.replace('\t', '<text:tab/>')

View file

@ -79,12 +79,23 @@ class RenderTestCase(TestCase):
def test_create_text_span_node(self): def test_create_text_span_node(self):
assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>' assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>'
def test_newline(self):
xml_text = '<text:span text:style-name="T5">Teststra&#223;e 5\n1234 L&#252;nen</text:span>'
result = Renderer._encode_escape_chars(xml_text)
assert result == '<text:span text:style-name="T5">Teststra&#223;e 5<text:line-break/>1234 L&#252;nen</text:span>'
def test_evil_newline(self): class EncodeLFAndFWithinTextNamespace(TestCase):
xml_text = '<text:span>\n</text:span><do_not_touch>\n</do_not_touch>' """Test encoding of line feed and tab chars within text: namespace"""
result = Renderer._encode_escape_chars(xml_text) def test_encode_linefeed_char(self):
assert result == '<text:span><text:line-break/></text:span><do_not_touch>\n</do_not_touch>' xml = '<text:span>This\nLF</text:span>'
espected = '<text:span>This<text:line-break/>LF</text:span>'
assert (Renderer._encode_escape_chars(xml) == espected)
def test_encode_tab_char(self):
xml = '<text:span>This\tTab</text:span>'
espected = '<text:span>This<text:tab/>Tab</text:span>'
assert (Renderer._encode_escape_chars(xml) == espected)
def test_escape_elem_with_attributes(self):
"""A bug in _encode_escape_chars was preventing it from escaping
LF and tabs inside text elements with tag attributes. See:
https://github.com/christopher-ramirez/secretary/issues/39"""
xml = '<text:span attr="value">This\nLF</text:span>'
espected = '<text:span attr="value">This<text:line-break/>LF</text:span>'
assert (Renderer._encode_escape_chars(xml) == espected)