|
| 1 | +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | +from __future__ import division |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +import textwrap |
| 20 | + |
| 21 | +import six |
| 22 | +import tensorflow as tf |
| 23 | + |
| 24 | +from tensorboard import plugin_util |
| 25 | + |
| 26 | + |
| 27 | +class MarkdownToSafeHTMLTest(tf.test.TestCase): |
| 28 | + |
| 29 | + def _test(self, markdown_string, expected): |
| 30 | + actual = plugin_util.markdown_to_safe_html(markdown_string) |
| 31 | + self.assertEqual(expected, actual) |
| 32 | + |
| 33 | + def test_empty_input(self): |
| 34 | + self._test(u'', u'') |
| 35 | + |
| 36 | + def test_basic_formatting(self): |
| 37 | + self._test(u'# _Hello_, **world!**\n\n' |
| 38 | + 'Check out [my website](http://example.com)!', |
| 39 | + u'<h1><em>Hello</em>, <strong>world!</strong></h1>\n' |
| 40 | + '<p>Check out <a href="http://example.com">my website</a>!</p>') |
| 41 | + |
| 42 | + def test_table_formatting(self): |
| 43 | + self._test( |
| 44 | + textwrap.dedent( |
| 45 | + u"""\ |
| 46 | + Here is some data: |
| 47 | +
|
| 48 | + TensorBoard usage | Happiness |
| 49 | + ------------------|---------- |
| 50 | + 0.0 | 0.0 |
| 51 | + 0.5 | 0.5 |
| 52 | + 1.0 | 1.0 |
| 53 | +
|
| 54 | + Wouldn't you agree?"""), |
| 55 | + textwrap.dedent( |
| 56 | + u"""\ |
| 57 | + <p>Here is some data:</p> |
| 58 | + <table> |
| 59 | + <thead> |
| 60 | + <tr> |
| 61 | + <th>TensorBoard usage</th> |
| 62 | + <th>Happiness</th> |
| 63 | + </tr> |
| 64 | + </thead> |
| 65 | + <tbody> |
| 66 | + <tr> |
| 67 | + <td>0.0</td> |
| 68 | + <td>0.0</td> |
| 69 | + </tr> |
| 70 | + <tr> |
| 71 | + <td>0.5</td> |
| 72 | + <td>0.5</td> |
| 73 | + </tr> |
| 74 | + <tr> |
| 75 | + <td>1.0</td> |
| 76 | + <td>1.0</td> |
| 77 | + </tr> |
| 78 | + </tbody> |
| 79 | + </table> |
| 80 | + <p>Wouldn't you agree?</p>""")) |
| 81 | + |
| 82 | + def test_whitelisted_tags_and_attributes_allowed(self): |
| 83 | + s = (u'Check out <a href="http://example.com" title="do it">' |
| 84 | + 'my website</a>!') |
| 85 | + self._test(s, u'<p>%s</p>' % s) |
| 86 | + |
| 87 | + def test_arbitrary_tags_and_attributes_removed(self): |
| 88 | + self._test(u'We should bring back the <blink>blink tag</blink>; ' |
| 89 | + '<a name="bookmark" href="http://please-dont.com">' |
| 90 | + 'sign the petition!</a>', |
| 91 | + u'<p>We should bring back the ' |
| 92 | + '<blink>blink tag</blink>; ' |
| 93 | + '<a href="http://please-dont.com">sign the petition!</a></p>') |
| 94 | + |
| 95 | + def test_javascript_hrefs_sanitized(self): |
| 96 | + self._test(u'A <a href="javascript:void0">sketchy link</a> for you', |
| 97 | + u'<p>A <a>sketchy link</a> for you</p>') |
| 98 | + |
| 99 | + def test_byte_strings_interpreted_as_utf8(self): |
| 100 | + s = u'> Look\u2014some UTF-8!'.encode('utf-8') |
| 101 | + assert isinstance(s, six.binary_type), (type(s), six.binary_type) |
| 102 | + self._test(s, |
| 103 | + u'<blockquote>\n<p>Look\u2014some UTF-8!</p>\n</blockquote>') |
| 104 | + |
| 105 | + def test_unicode_strings_passed_through(self): |
| 106 | + s = u'> Look\u2014some UTF-8!' |
| 107 | + assert not isinstance(s, six.binary_type), (type(s), six.binary_type) |
| 108 | + self._test(s, |
| 109 | + u'<blockquote>\n<p>Look\u2014some UTF-8!</p>\n</blockquote>') |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + tf.test.main() |
0 commit comments