Module: check_mk
Branch: master
Commit: 850a50666162623a1221338663137079302204cb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=850a5066616262…
Author: Roland Halbig <rh(a)mathias-kettner.de>
Date: Thu Jan 5 13:20:16 2017 +0100
Refactoring htmllib: improved readability in render function.
Change-Id: I6841ac36212e3aeb77d7f12f5460a86c5424ee64
---
tests/web/test_htmllib_integration.py | 2 +-
web/htdocs/htmllib.py | 34 ++++++++++++++++++++--------------
2 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/tests/web/test_htmllib_integration.py
b/tests/web/test_htmllib_integration.py
index b1abb4c..ef4713d 100644
--- a/tests/web/test_htmllib_integration.py
+++ b/tests/web/test_htmllib_integration.py
@@ -285,7 +285,7 @@ def test_check_mk():
gentest(old, new, lambda x: x.popup_trigger("CONTENT", 0,
what="it", data=None, url_vars=[("lol", "true")],
style="height:50px;",
menu_content="test_content", cssclass="class", onclose=None))
gentest(old, new, lambda x: x.popup_trigger("CONTENT", 0,
what="it", data=None, url_vars=[("lol", "true")],
- style="height:50px;",
menu_content="test_content", cssclass="class",
onclose="close();"))
+ style="height:50px;",
menu_content="test_content '<div> TEST </div>",
cssclass="class", onclose="close();"))
# headers
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index d0ce0f4..cf7f980 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -480,16 +480,26 @@ class HTMLGenerator(Escaper, OutputFunnel):
'cellspacing', 'cellpadding', 'border',
'allowTransparency', 'frameborder'], key
for k, v in attrs.iteritems():
+
if v is None: continue
+ k = k.rstrip('_')
if not isinstance(v, list):
- yield ' %s=\"%s\"' % (k.rstrip('_'),
self._escape_attribute(v))
- elif k in ["class", "class_"]:
- yield ' %s=\"%s\"' % (k.rstrip('_'), '
'.join(a for a in (self._escape_attribute(vi) for vi in v) if a))
- elif k == "style" or k.startswith('on'):
- yield ' %s=\"%s;\"' % (k.rstrip('_'),
re.sub(';+', ';', '; '.join(a for a in (self._escape_attribute(vi)
for vi in v) if a)))
+ v = self._escape_attribute(v)
else:
- yield ' %s=\"%s\"' % (k.rstrip('_'),
'_'.join(a for a in (self._escape_attribute(vi) for vi in v) if a))
+ if k == "class":
+ sep = ' '
+ elif k == "style" or k.startswith('on'):
+ sep = '; '
+ else:
+ sep = '_'
+
+ v = sep.join([a for a in (self._escape_attribute(vi) for vi in v) if a])
+
+ if sep.startswith(';'):
+ v = re.sub(';+', ';', v)
+
+ yield ' %s=\"%s\"' %(k, v)
# applies attribute encoding to prevent code injections.
@@ -497,13 +507,10 @@ class HTMLGenerator(Escaper, OutputFunnel):
""" You have to replace attributes which are also python elements
such as
'class', 'id', 'for' or 'type' using a
trailing underscore (e.g. 'class_' or 'id_'). """
#self.indent_level += self.indent
- if not attrs:
- return HTML("%s<%s%s>" % (' ' * (self.indent_level -
self.indent),\
- tag_name, ' /' if close_tag else
''))
- else:
- return HTML("%s<%s%s%s>" % (' ' * (self.indent_level
- self.indent),\
- tag_name,
''.join(self._render_attributes(**attrs)),\
- ' /' if close_tag else ''))
+ indent = ' ' * (self.indent_level - self.indent)
+ return HTML("%s<%s%s%s>" % (indent, tag_name,\
+ '' if not attrs else
''.join(self._render_attributes(**attrs)),\
+ '' if not close_tag else ' /'))
def _render_closing_tag(self, tag_name):
@@ -519,7 +526,6 @@ class HTMLGenerator(Escaper, OutputFunnel):
else:
tag = tag.rstrip("\n")
-
if isinstance(tag_content, HTML):
tag += tag_content.lstrip(' ').rstrip('\n')
else: