예제 #1
0
def test_insert_bad_type():
    s = HTMLFragment(
        '<strong>Vincent:</strong> Royale with cheese. '
        '<!-- Quarter Pounder -->'
    )
    with pytest.raises(TypeError):
        s.insert(0, 42)
예제 #2
0
def test_insert_multi_chars():
    s = HTMLFragment(
        '<strong>Vincent:</strong> Royale with cheese. '
        '<!-- Quarter Pounder -->'
    )
    with pytest.raises(ValueError):
        s.insert(0, 'Jules')
예제 #3
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'
예제 #4
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'
예제 #5
0
def test_parent_element_on_slice():
    s = HTMLFragment('Whose <div>motorcycle is <b>this</b>?</div>')
    assert s[0:3].parents == frozenset()
    assert s[7:10].parent_tags == frozenset(['div'])
    assert s[21:22].parent_tags == frozenset(['div', 'b'])
    assert s[7:21].parent_tags == frozenset(['div', 'b'])
    assert s[0:50].parent_tags == frozenset(['div', 'b'])
예제 #6
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'
예제 #7
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'
예제 #8
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'
    )
예제 #9
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.'
    )
예제 #10
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'
예제 #11
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'
예제 #12
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)
예제 #13
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'
예제 #14
0
def test_step():
    s = HTMLFragment(
        '<strong>Vincent:</strong> Royale with cheese. '
        '<!-- Quarter Pounder -->'
    )
    with pytest.raises(IndexError):
        s[0:5:2]
    with pytest.raises(IndexError):
        s[0:5:2] = 'Jules'
    with pytest.raises(IndexError):
        del s[0:5:2]
예제 #15
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'
예제 #16
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] == ':'
예제 #17
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'
예제 #18
0
def test_set_unicode():
    s = HTMLFragment(
        '<strong>Vincent:</strong> Royale with cheese. '
        '<!-- Quarter Pounder -->'
    )
    s[0] = u'Ž'
    assert unicode(s) == (
        u'<strong>Žincent:</strong> Royale with cheese. '
        u'<!-- Quarter Pounder -->'
    )
    assert s[0] == u'Ž'
예제 #19
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] == ':'
예제 #20
0
def test_get_text_by_index():
    s = HTMLFragment('''
    The Wolf:
    <p>
        That's <strong>thirty</strong> minutes away.
        <!-- The Wolf -->
        I'll be there in <strong>ten</strong>.
    </p>
    ''')
    assert s[0] == 'T'
    assert s[23] == 'T'
    assert s[28] == 's'
    assert s[44] == ' '
    assert s[68] == 'I'
    assert s[85] == 't'
예제 #21
0
def test_negative_index():
    s = HTMLFragment(
        '<strong>Vincent:</strong> Royale with cheese. '
        '<!-- Quarter Pounder -->'
    )
    with pytest.raises(IndexError):
        s[-5]
    with pytest.raises(IndexError):
        s[0:-5]
    with pytest.raises(IndexError):
        s[-5] = 'a'
    with pytest.raises(IndexError):
        s[0:-5] = 'Jules'
    with pytest.raises(IndexError):
        del s[-5]
    with pytest.raises(IndexError):
        del s[0:-5]
예제 #22
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
예제 #23
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"
예제 #24
0
def test_insert_slice_to_empty_string():
    s = HTMLFragment('')
    s[0:0] = 'Quarter Pounder'
    assert unicode(s) == 'Quarter Pounder'
예제 #25
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"
예제 #26
0
def test_parent_element_on_item():
    s = HTMLFragment('Whose motorcycle is <strong>this</strong>?')
    assert s[0].parents == frozenset()
    assert s[21].parent_tags == frozenset(['strong'])
예제 #27
0
def test_get_text_from_nested_tags():
    s = HTMLFragment('Whose <div><i><b>motorcycle</b></i> is this?</div>')
    assert s[0:5] == 'Whose'
    assert s[6:16] == 'motorcycle'
    assert s[20:24] == 'this'
예제 #28
0
def test_html_entities_conversions():
    s1 = unicode(HTMLFragment('<b>Honey&nbsp;Bunny</b> &copy;'))
    s2 = u'<b>Honey\xa0Bunny</b> \xa9'
    assert s1 == s2
예제 #29
0
def test_unicode():
    s = u'<em>Řeřicha</em> stands for cress in <strong>Czech</strong>.'
    assert unicode(HTMLFragment(s)) == s
예제 #30
0
def test_del_slice_to_empty_string():
    s = HTMLFragment('')
    del s[0:5]
    assert unicode(s) == ''
예제 #31
0
def test_set_slice_to_empty_string():
    s = HTMLFragment('')
    s[0:5] = 'Fabienne'
    assert unicode(s) == 'Fabienne'
예제 #32
0
def test_get_slice_from_empty_string():
    s = HTMLFragment('')
    assert s[0:5] == ''