Author: pekka.klarck
Date: Mon Apr 6 02:16:24 2009
New Revision: 1682
Modified:
trunk/src/robot/utils/htmlutils.py
trunk/utest/utils/test_htmlutils.py
Log:
cleanup and better handling of non-strings
Modified: trunk/src/robot/utils/htmlutils.py
==============================================================================
--- trunk/src/robot/utils/htmlutils.py (original)
+++ trunk/src/robot/utils/htmlutils.py Mon Apr 6 02:16:24 2009
@@ -16,10 +16,9 @@
import re
import os.path
-from robottypes import is_str
+from robottypes import is_str, unic
+
-_table_line_re = re.compile('^\s*\| (.* |)\|\s*$')
-_table_line_splitter = re.compile(' \|(?= )')
_hr_re = re.compile('^-{3,} *$')
_bold_re = re.compile('''
( # prefix (group 1)
@@ -50,7 +49,7 @@
def html_escape(text, formatting=False):
if not is_str(text):
- return text
+ text = unic(text)
for name, value in [('&', '&'), ('<', '<'), ('>', '>')]:
text = text.replace(name, value)
@@ -60,8 +59,8 @@
hr = None
for line in text.splitlines():
- if formatting and _table_line_re.search(line) is not None:
- if hr is not None:
+ if formatting and table.is_table_row(line):
+ if hr:
ret.append(hr)
hr = None
table.add_row(line)
@@ -76,14 +75,14 @@
hr = '<hr />\n'
else:
line = _format_line(line, formatting)
- if hr is not None:
+ if hr:
line = hr + line
hr = None
ret.append(line)
if table.is_started():
ret.append(table.end())
- if hr is not None:
+ if hr:
ret.append(hr)
return '<br />\n'.join(ret)
@@ -100,12 +99,18 @@
class _Table:
+ _is_line = re.compile('^\s*\| (.* |)\|\s*$')
+ _line_splitter = re.compile(' \|(?= )')
+
def __init__(self):
self._rows = []
+ def is_table_row(self, row):
+ return self._is_line.match(row) is not None
+
def add_row(self, text):
text = text.strip()[1:-1] # remove outer whitespace and pipes
- cells = [ cell.strip() for cell in
_table_line_splitter.split(text) ]
+ cells = [ cell.strip() for cell in self._line_splitter.split(text)
]
self._rows.append(cells)
def end(self):
@@ -118,7 +123,7 @@
def _format(self, rows):
maxlen = max([ len(row) for row in rows ])
- table = [ '<table border="1" class="doc">' ]
+ table = ['<table border="1" class="doc">']
for row in rows:
row += [''] * (maxlen - len(row)) # fix ragged tables
table.append('<tr>')
Modified: trunk/utest/utils/test_htmlutils.py
==============================================================================
--- trunk/utest/utils/test_htmlutils.py (original)
+++ trunk/utest/utils/test_htmlutils.py Mon Apr 6 02:16:24 2009
@@ -11,8 +11,18 @@
class TestHtmlEscape(unittest.TestCase):
def test_no_changes(self):
- for inp in [ '', 'nothing to change' ]:
+ for inp in ['', 'nothing to change']:
assert_equals(html_escape(inp), inp)
+
+ def test_non_strings(self):
+ for inp in [1, None, True]:
+ assert_equals(html_escape(inp), str(inp))
+
+ def test_non_string_with_str_needing_escaping(self):
+ class NonString:
+ def __str__(self):
+ return '<hello>'
+ assert_equals(html_escape(NonString()), '<hello>')
def test_new_lines_and_paragraphs(self):
for inp in [ 'Text on first line.\nText on second line.'
@@ -60,17 +70,17 @@
('hello http://link world',
'hello <a href="http://link">http://link</a> world'),
('multi\nhttp://link\nline',
- 'multi<br />\n<a href="http://link">http://link</a><br
/>\nline'),
+ 'multi<br />\n<a href="http://link">http://link</a><br
/>\n'
+ 'line'),
('http://link, ftp://link2.',
- '<a href="http://link">http://link</a>, ' \
- + '<a href="ftp://link2">ftp://link2</a>.'),
+ '<a href="http://link">http://link</a>, '
+ '<a href="ftp://link2">ftp://link2</a>.'),
('x (http://y, z)',
'x (<a href="http://y">http://y</a>, z)'),
('Hello http://one, ftp://kaksi/; "gopher://3.0"',
- 'Hello <a href="http://one">http://one</a>, ' \
- + '<a href="ftp://kaksi/">ftp://kaksi/</a>; ' \
- + '"<a href="gopher://3.0">gopher://3.0</a>"')
- ]:
+ 'Hello <a href="http://one">http://one</a>, '
+ '<a href="ftp://kaksi/">ftp://kaksi/</a>; '
+ '"<a href="gopher://3.0">gopher://3.0</a>"') ]:
assert_equals(html_escape(inp, True), exp)
assert_equals(html_escape(inp, False), exp)