Improve the xhtml_to_poezio_colors sanitization

add some tests, and remove the test_datetime_tuple because
of python time module misbehavior with timezones set manually.
(potentially due to http://bugs.python.org/issue6478)
This commit is contained in:
mathieui 2014-12-08 17:01:44 +01:00
parent 4b8749ca88
commit e273a32ec7
No known key found for this signature in database
GPG key ID: C59F84CEEFD616E3
3 changed files with 12 additions and 15 deletions

View file

@ -183,7 +183,8 @@ whitespace_re = re.compile(r'\s+')
xhtml_attr_re = re.compile(r'\x19-?\d[^}]*}|\x19[buaio]')
xhtml_data_re = re.compile(r'data:image/([a-z]+);base64,(.+)')
poezio_colors_trim = re.compile(r'\x19o(\x19\d+}|\x19\d|\x19[buaio])*\x19o')
poezio_color_double = re.compile(r'(?:\x19\d+}|\x19\d)+(\x19\d|\x19\d+})')
poezio_format_trim = re.compile(r'(\x19\d+}|\x19\d|\x19[buaio]|\x19o)+\x19o')
xhtml_simple_attr_re = re.compile(r'\x19\d')
@ -304,7 +305,8 @@ class XHTMLHandler(sax.ContentHandler):
@property
def result(self):
return re.sub(poezio_colors_trim, '\x19o', ''.join(self.builder).strip())
sanitized = re.sub(poezio_color_double, r'\1', ''.join(self.builder).strip())
return re.sub(poezio_format_trim, '\x19o', sanitized)
def append_formatting(self, formatting):
self.formatting.append(formatting)

View file

@ -14,19 +14,6 @@ from common import (datetime_tuple, get_utc_time, get_local_time, shell_split,
find_argument_quoted, find_argument_unquoted,
parse_str_to_secs, parse_secs_to_str, safeJID)
def test_datetime_tuple():
time.timezone = 0
time.altzone = 0
assert datetime_tuple('20130226T06:23:12') == datetime.datetime(2013, 2, 26, 6, 23, 12)
assert datetime_tuple('2013-02-26T06:23:12+02:00') == datetime.datetime(2013, 2, 26, 4, 23, 12)
time.timezone = -3600
time.altzone = -3600
assert datetime_tuple('20130226T07:23:12') == datetime.datetime(2013, 2, 26, 8, 23, 12)
assert datetime_tuple('2013-02-26T07:23:12+02:00') == datetime.datetime(2013, 2, 26, 6, 23, 12)
def test_utc_time():
delta = timedelta(seconds=-3600)
d = datetime.datetime.now()

View file

@ -38,6 +38,14 @@ def test_xhtml_to_poezio_colors():
xhtml = start + b'<a href="http://perdu.com">http://perdu.com</a>' + end
assert xhtml_to_poezio_colors(xhtml) == '\x19uhttp://perdu.com\x19o'
xhtml = b'<div style="font-weight:bold">Allo <div style="color:red">test <div style="color: blue">test2</div></div></div>'
assert xhtml_to_poezio_colors(xhtml, force=True) == '\x19bAllo \x19196}test \x1921}test2\x19o'
xhtml = (b'<div style="color:blue"><div style="color:yellow">'
b'<div style="color:blue">Allo <div style="color:red">'
b'test <div style="color: blue">test2</div></div></div></div></div>')
assert xhtml_to_poezio_colors(xhtml, force=True) == '\x1921}Allo \x19196}test \x1921}test2\x19o'
with pytest.raises(xml.sax._exceptions.SAXParseException):
xhtml_to_poezio_colors(b'<p>Invalid xml')