Example #1
0
def replace(html, replacements=None):
    """Performs replacements on given HTML string."""
    if not replacements:
        return html  # no replacements
    html = HTMLFragment(html)

    for r in replacements:
        r.replace(html)

    return unicode(html)
Example #2
0
def test_pass_document_get_document():
    s = """
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Fabienne</title>
        <link rel="stylesheet" href="style.css">
        <script src="script.js"></script>
        <style>p { color: red; }</style>
      </head>
      <body class="zed dead">
        <!-- page content -->
        It's a chopper, baby.
      </body>
    </html>
    """
    s1 = strip_whitespace(unicode(HTMLFragment(s)))
    s2 = strip_whitespace(s)
    assert s1 == s2
Example #3
0
def test_set_slice_to_empty_string():
    s = HTMLFragment("")
    s[0:5] = "Fabienne"
    assert unicode(s) == "Fabienne"
Example #4
0
def test_del_slice():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    del s[0:10]
    assert unicode(s) == ("<strong></strong>oyale with cheese. " "<!-- Quarter Pounder -->")
    assert s[1] == "y"
Example #5
0
def test_pass_nested_tags_get_nested_tags():
    s = "Whose <strong><b>motorcycle</b></strong> is this?"
    assert unicode(HTMLFragment(s)) == s
Example #6
0
def test_set_long_slice():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    s[0:7] = "Vincent to Jules"
    assert unicode(s) == ("<strong>Vincent to Jules:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    assert s[11] == "J" and s[16] == ":"
Example #7
0
def test_pass_wrapped_fragment_get_wrapped_fragment():
    s = "<p>Whose motorcycle is <strong>this</strong>?</p>"
    assert unicode(HTMLFragment(s)) == s
Example #8
0
def test_insert_no_chars():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    s.insert(0, "")
    assert unicode(s) == ("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    assert s[0] == "V"
Example #9
0
def test_insert_text_by_index():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    s.insert(7, "s")
    assert unicode(s) == ("<strong>Vincents:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    assert s[7] == "s"
Example #10
0
def test_insert_slice():
    s = HTMLFragment("<strong>Vincent:</strong> <b>Royale</b> with cheese.")
    s[14:14] = " Quarter Pounder"
    assert unicode(s) == ("<strong>Vincent:</strong> " "<b>Royale Quarter Pounder</b> with cheese.")
    assert s[16] == "Q"
Example #11
0
def test_unicode():
    s = "<em>Řeřicha</em> stands for cress in <strong>Czech</strong>."
    assert unicode(HTMLFragment(s)) == s
Example #12
0
def test_set_short_slice_between_tags():
    s = HTMLFragment("<strong>Vincent:</strong> <b>Royale with</b> cheese.")
    s[5:21] = "Jules Jules"
    assert unicode(s) == ("<strong>VinceJul</strong>e<b>s Jules</b>cheese.")
    assert s[5] == "J"
Example #13
0
def test_set_long_slice_between_tags_no_constraint():
    s = HTMLFragment("<strong>Vincent:</strong> <b>Royale with</b> cheese.")
    s[5:] = "Jules Jules Jules Jules Jules"
    assert unicode(s) == ("<strong>VinceJul</strong>e<b>s Jules Jul</b>es Jules Jules")
    assert s[5] == "J"
Example #14
0
def test_set_long_slice_with_offset():
    s = HTMLFragment("<strong>Vincent:</strong> <b>Royale with</b> cheese.")
    s[16:20] = "without"
    assert unicode(s) == ("<strong>Vincent:</strong> <b>Royale without</b> cheese.")
    assert s[20] == "o"
Example #15
0
def test_set_short_slice():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    s[0:7] = "Jules"
    assert unicode(s) == ("<strong>Jules:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    assert s[4] == "s" and s[5] == ":"
Example #16
0
def test_set_unicode():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    s[0] = "Ž"
    assert unicode(s) == ("<strong>Žincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    assert s[0] == "Ž"
Example #17
0
def test_del_slice_to_empty_string():
    s = HTMLFragment("")
    del s[0:5]
    assert unicode(s) == ""
Example #18
0
def test_set_text_by_index():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    s[21] = "C"
    assert unicode(s) == ("<strong>Vincent:</strong> Royale with Cheese. " "<!-- Quarter Pounder -->")
    assert s[21] == "C"
Example #19
0
def test_insert_slice_to_end():
    s = HTMLFragment("<strong>Vincent:</strong> <b>Royale</b> with cheese.")
    s[50:] = "Quarter Pounder"
    assert unicode(s) == ("<strong>Vincent:</strong> " "<b>Royale</b> with cheese.Quarter Pounder")
Example #20
0
def test_pass_text_get_text():
    s = "Whose motorcycle is this?"
    assert unicode(HTMLFragment(s)) == s
Example #21
0
def test_html_entities_conversions():
    s1 = unicode(HTMLFragment("<b>Honey&nbsp;Bunny</b> &copy;"))
    s2 = "<b>Honey\xa0Bunny</b> \xa9"
    assert s1 == s2
Example #22
0
def test_insert_slice_to_beginning():
    s = HTMLFragment("<strong>Vincent:</strong> <b>Royale</b> with cheese.")
    s[0:0] = "Quarter Pounder"
    assert unicode(s) == ("Quarter Pounder<strong>Vincent:</strong> " "<b>Royale</b> with cheese.")
Example #23
0
def test_del_text_by_index():
    s = HTMLFragment("<strong>Vincent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    del s[3]
    assert unicode(s) == ("<strong>Vinent:</strong> Royale with cheese. " "<!-- Quarter Pounder -->")
    assert s[3] == "e"
Example #24
0
def test_insert_slice_to_empty_string():
    s = HTMLFragment("")
    s[0:0] = "Quarter Pounder"
    assert unicode(s) == "Quarter Pounder"