Module: check_mk
Branch: master
Commit: 5b45692f5329aad5ead02d8be5c7f4634f4ed0bb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=5b45692f5329aa…
Author: Óscar Nájera <on(a)mathias-kettner.de>
Date: Tue Oct 9 14:50:58 2018 +0200
use id_ and type_ as function arguments
_render_attributes elimintates the trailing underscores in keywords, and
can thus be used.
html_escape function from python language is used
Change-Id: I6b654652aaef44b75cd275d5b51584f26141b401
---
cmk/gui/htmllib.py | 21 +++++++++++++--------
cmk/gui/wato/__init__.py | 4 ++--
tests/unit/cmk/gui/test_htmllib_HTMLGenerator.py | 2 +-
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/cmk/gui/htmllib.py b/cmk/gui/htmllib.py
index ea08ab2..c5a4619 100644
--- a/cmk/gui/htmllib.py
+++ b/cmk/gui/htmllib.py
@@ -69,6 +69,13 @@ import pprint
from contextlib import contextmanager
+try:
+ # First try python3
+ from html import escape as html_escape
+except ImportError:
+ # Default to python2
+ from cgi import escape as html_escape
+
# Monkey patch in order to make the HTML class below json-serializable without changing
the default json calls.
def _default(self, obj):
return getattr(obj.__class__, "to_json", _default.default)(obj)
@@ -120,13 +127,11 @@ class Escaper(object):
return "%s" % value # This is HTML code which must not be escaped
elif attr_type not in [str, unicode]: # also possible: type Exception!
value = "%s" % value # Note: this allows Unicode. value might not
have type str now
- return value.replace("&", "&")\
- .replace('"', """)\
- .replace("<", "<")\
- .replace(">", ">")
+ return html_escape(value, quote=True)
def unescape_attributes(self, value):
+ # In python3 use html.unescape
return value.replace("&", "&")\
.replace(""", "\"")\
.replace("<", "<")\
@@ -2332,7 +2337,7 @@ class html(HTMLGenerator):
if label:
self.label(label, for_=id_)
- self.write_html(self.render_input(varname, type_=args.get("type",
"text"), **attributes))
+ self.write_html(self.render_input(varname, type_=args.get("type_",
"text"), **attributes))
if error:
self.close_x()
@@ -2377,7 +2382,7 @@ class html(HTMLGenerator):
def password_input(self, varname, default_value = "", size=12, **args):
- self.text_input(varname, default_value, type="password", size = size,
**args)
+ self.text_input(varname, default_value, type_="password", size = size,
**args)
def text_area(self, varname, deflt="", rows=4, cols=30, attrs=None,
try_max_width=False):
@@ -2636,7 +2641,7 @@ class html(HTMLGenerator):
if icon:
self.img(class_=["treeangle", "title"],
src="images/icon_%s.png" % icon)
else:
- self.img(id=img_id, class_=["treeangle", "nform",
"open" if isopen else "closed"],
+ self.img(id_=img_id, class_=["treeangle", "nform",
"open" if isopen else "closed"],
src="images/%s_closed.png" % tree_img,
align="absbottom")
self.write_text(title)
self.close_td()
@@ -2645,7 +2650,7 @@ class html(HTMLGenerator):
self.open_div(class_="foldable")
if not icon:
- self.img(id="treeimg.%s.%s" % (treename, id_),
+ self.img(id_="treeimg.%s.%s" % (treename, id_),
class_=["treeangle", "open" if isopen else
"closed"],
src="images/%s_closed.png" % tree_img,
align="absbottom", onclick=onclick)
if isinstance(title, HTML): # custom HTML code
diff --git a/cmk/gui/wato/__init__.py b/cmk/gui/wato/__init__.py
index 5044f4a..2f59e0f 100644
--- a/cmk/gui/wato/__init__.py
+++ b/cmk/gui/wato/__init__.py
@@ -3035,9 +3035,9 @@ class ModeDiagHost(WatoMode):
html.open_td(class_="icons")
html.open_div()
- html.img("images/icon_reload.png", class_="icon",
id="%s_img" % ident)
+ html.img("images/icon_reload.png", class_="icon",
id_="%s_img" % ident)
html.open_a(href="")
- html.img("images/icon_reload.png", class_=["icon",
"retry"], id="%s_retry" % ident, title=_('Retry this test'))
+ html.img("images/icon_reload.png", class_=["icon",
"retry"], id_="%s_retry" % ident, title=_('Retry this test'))
html.close_a()
html.close_div()
html.close_td()
diff --git a/tests/unit/cmk/gui/test_htmllib_HTMLGenerator.py
b/tests/unit/cmk/gui/test_htmllib_HTMLGenerator.py
index 02fdb21..1a792b0 100644
--- a/tests/unit/cmk/gui/test_htmllib_HTMLGenerator.py
+++ b/tests/unit/cmk/gui/test_htmllib_HTMLGenerator.py
@@ -51,7 +51,7 @@ def test_HTMLGenerator(register_builtin_html):
def test_multiclass_call(register_builtin_html):
- html.div('', class_="1", css="3", cssclass =
"4", **{"class": "2"})
+ html.div('', class_="1", css="3", cssclass="4",
**{"class": "2"})
written_text = "".join(html.response.flush_output())
assert tools.compare_html(written_text, "<div class=\"1 3 4
2\"></div>")