Exemplo n.º 1
0
 def test_null_bytes_stripped(self):
     # If this function is mistakenly called with UTF-16 or UTF-32 encoded text,
     # there will probably be a bunch of null bytes. Ensure these are stripped.
     s = "un_der_score".encode("utf-32-le")
     # UTF-32 encoding of ASCII will have 3 null bytes per char. 36 = 3 * 12.
     self.assertEqual(
         plugin_util.safe_html(s),
         "un_der_score",
     )
Exemplo n.º 2
0
 def test_arbitrary_tags_and_attributes_removed(self):
     self.assertEqual(
         plugin_util.safe_html(
             "We should bring back the <blink>blink tag</blink>; "
             '<a name="bookmark" href="http://please-dont.com">'
             "sign the petition!</a>"),
         "We should bring back the "
         "&lt;blink&gt;blink tag&lt;/blink&gt;; "
         '<a href="http://please-dont.com">sign the petition!</a>',
     )
Exemplo n.º 3
0
def text_array_to_html(text_arr, enable_markdown):
    """Take a numpy.ndarray containing strings, and convert it into html.

    If the ndarray contains a single scalar string, that string is converted to
    html via our sanitized markdown parser. If it contains an array of strings,
    the strings are individually converted to html and then composed into a table
    using make_table. If the array contains dimensionality greater than 2,
    all but two of the dimensions are removed, and a warning message is prefixed
    to the table.

    Args:
      text_arr: A numpy.ndarray containing strings.
      enable_markdown: boolean, whether to enable Markdown

    Returns:
      The array converted to html.
    """
    if not text_arr.shape:
        # It is a scalar. No need to put it in a table.
        if enable_markdown:
            return plugin_util.markdown_to_safe_html(text_arr.item())
        else:
            return plugin_util.safe_html(text_arr.item())
    warning = ""
    if len(text_arr.shape) > 2:
        warning = plugin_util.markdown_to_safe_html(WARNING_TEMPLATE %
                                                    len(text_arr.shape))
        text_arr = reduce_to_2d(text_arr)
    if enable_markdown:
        table = plugin_util.markdowns_to_safe_html(
            text_arr.reshape(-1),
            lambda xs: make_table(np.array(xs).reshape(text_arr.shape)),
        )
    else:
        # Convert utf-8 bytes to str. The built-in np.char.decode doesn't work on
        # object arrays, and converting to an numpy chararray is lossy.
        decode = lambda bs: bs.decode("utf-8") if isinstance(bs, bytes) else bs
        text_arr_str = np.array([decode(bs) for bs in text_arr.reshape(-1)
                                 ]).reshape(text_arr.shape)
        table = plugin_util.safe_html(make_table(text_arr_str))
    return warning + table
Exemplo n.º 4
0
 def test_unicode_strings_passed_through(self):
     s = "Look\u2014some UTF-8!"
     assert not isinstance(s, bytes), (type(s), bytes)
     self.assertEqual(plugin_util.safe_html(s), "Look\u2014some UTF-8!")
Exemplo n.º 5
0
 def test_byte_strings_interpreted_as_utf8(self):
     s = "Look\u2014some UTF-8!".encode("utf-8")
     assert isinstance(s, bytes), (type(s), bytes)
     self.assertEqual(plugin_util.safe_html(s), "Look\u2014some UTF-8!")
Exemplo n.º 6
0
 def test_javascript_hrefs_sanitized(self):
     self.assertEqual(
         plugin_util.safe_html(
             'A <a href="javascript:void0">sketchy link</a> for you'),
         "A <a>sketchy link</a> for you",
     )
Exemplo n.º 7
0
 def test_whitelisted_tags_and_attributes_allowed(self):
     s = ('Check out <a href="http://example.com" title="do it">'
          "my website</a>!")
     self.assertEqual(plugin_util.safe_html(s), "%s" % s)
Exemplo n.º 8
0
 def test_empty_input(self):
     self.assertEqual(plugin_util.safe_html(""), "")