Пример #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] == ''