Skip to content

Commit c20036f

Browse files
added test coverage and escaped title in cube lists
1 parent 6d877c0 commit c20036f

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

lib/iris/experimental/representation.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,10 @@
2323
from __future__ import (absolute_import, division, print_function)
2424
from six.moves import (filter, input, map, range, zip) # noqa
2525

26+
from html import escape
2627
import re
2728

2829

29-
def escape(s):
30-
"""
31-
Replace special characters "&", "<" and ">" to HTML-safe sequences.
32-
Code taken from:
33-
https://github.com/python/cpython/blob/3.7/Lib/html/__init__.py
34-
"""
35-
s = s.replace("&", "&amp;") # Must be done first!
36-
s = s.replace("<", "&lt;")
37-
s = s.replace(">", "&gt;")
38-
return s
39-
4030

4131
class CubeRepresentation(object):
4232
"""
@@ -413,6 +403,7 @@ def make_content(self):
413403
for i, cube in enumerate(self.cubelist):
414404
title = '{i}: {summary}'.format(i=i,
415405
summary=cube.summary(shorten=True))
406+
title = escape(title)
416407
content = cube._repr_html_()
417408
html.append(self._accordian_panel.format(uid=self.cubelist_id,
418409
title=title,

lib/iris/tests/unit/experimental/representation/test_CubeListRepresentation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from __future__ import (absolute_import, division, print_function)
2020
from six.moves import (filter, input, map, range, zip) # noqa
21+
from html import escape, unescape
2122

2223
# Import iris.tests first so that some things can be initialised before
2324
# importing anything else.
@@ -46,6 +47,7 @@ def setUp(self):
4647
stock.lat_lon_cube()])
4748
self.representer = CubeListRepresentation(self.cubes)
4849
self.content = self.representer.make_content()
50+
self.cubes[0].rename('name & <html>')
4951

5052
def test_repr_len(self):
5153
self.assertEqual(len(self.cubes), len(self.content))
@@ -62,8 +64,10 @@ def test__cube_name_summary_consistency(self):
6264
# Get the cube name out of the repr html...
6365
cube_name = first_contents_line.split('>0: ')[1].split('/')[0]
6466
# ... and prettify it (to be the same as in the following cube repr).
65-
pretty_cube_name = cube_name.strip().replace('_', ' ').title()
66-
self.assertIn(pretty_cube_name, single_cube_html)
67+
unescaped_name = unescape(cube_name)
68+
pretty_cube_name = unescaped_name.strip().replace('_', ' ').title()
69+
pretty_escaped_name = escape(pretty_cube_name)
70+
self.assertIn(pretty_escaped_name, single_cube_html)
6771

6872

6973
@tests.skip_data

lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from __future__ import (absolute_import, division, print_function)
2020
from six.moves import (filter, input, map, range, zip) # noqa
21+
from html import escape
2122

2223
# Import iris.tests first so that some things can be initialised before
2324
# importing anything else.
@@ -81,13 +82,13 @@ class Test__summary_content(tests.IrisTest):
8182
def setUp(self):
8283
self.cube = stock.lat_lon_cube()
8384
# Check we're not tripped up by names containing spaces.
84-
self.cube.rename('Electron density')
85+
self.cube.rename('Electron density (&<html>)')
8586
self.cube.units = '1e11 e/m^3'
8687
self.representer = CubeRepresentation(self.cube)
8788

8889
def test_name(self):
8990
# Check the cube name is being set and formatted correctly.
90-
expected = self.cube.name().replace('_', ' ').title()
91+
expected = escape(self.cube.name().replace('_', ' ').title())
9192
result = self.representer.name
9293
self.assertEqual(expected, result)
9394

0 commit comments

Comments
 (0)