Introducing new and faster technique to escape variables values.

This commit is contained in:
Christopher Ramírez 2017-09-01 15:08:24 -06:00
parent f74046fb09
commit 76cb15e2ba
2 changed files with 35 additions and 44 deletions

View file

@ -49,21 +49,6 @@ class RenderTestCase(TestCase):
for test, expect in test_samples.items():
assert self.engine._unescape_entities(test) == expect
def test__encode_escape_chars(self):
test_samples = {
'<text:a>\n</text:a>': '<text:a><text:line-break/></text:a>',
'<text:h>\n</text:h>': '<text:h><text:line-break/></text:h>',
'<text:p>\n</text:p>': '<text:p><text:line-break/></text:p>',
'<text:p>Hello\n</text:p>': '<text:p>Hello<text:line-break/></text:p>',
'<text:p>Hello\nWorld\n!</text:p>': '<text:p>Hello<text:line-break/>World<text:line-break/>!</text:p>',
'<text:ruby-base>\n</text:ruby-base>': '<text:ruby-base><text:line-break/></text:ruby-base>',
'<text:meta>\u0009</text:meta>': '<text:meta><text:tab/></text:meta>',
'<text:meta-field>\n</text:meta-field>': '<text:meta-field><text:line-break/></text:meta-field>',
}
for test, expect in test_samples.items():
assert self.engine._encode_escape_chars(test) == expect
def _test_is_jinja_tag(self):
assert self._is_jinja_tag('{{ foo }}')==True
assert self._is_jinja_tag('{ foo }')==False
@ -80,22 +65,22 @@ class RenderTestCase(TestCase):
assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>'
class EncodeLFAndFWithinTextNamespace(TestCase):
"""Test encoding of line feed and tab chars within text: namespace"""
class EscapingVariablesValues(TestCase):
"""
Test encoding of line feed and tab variables valuess
"""
def test_encode_linefeed_char(self):
xml = '<text:span>This\nLF</text:span>'
espected = '<text:span>This<text:line-break/>LF</text:span>'
assert (Renderer._encode_escape_chars(xml) == espected)
xml = 'This\nLF'
expected = 'This<text:line-break/>LF'
assert (Renderer.get_escaped_var_value(xml) == expected)
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_encode_linefeed_char(self):
xml = 'This\tTab char'
expected = 'This<text:tab/>Tab char'
assert (Renderer.get_escaped_var_value(xml) == expected)
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)
def test_escape_xml_reserved_chars(self):
''' Should also escape minor and mayor signs '''
xml = '1 is > than 0 & -1 is <'
expected = '1 is &gt; than 0 &amp; -1 is &lt;'
assert (Renderer.get_escaped_var_value(xml) == expected)