File: test_attribute.py

package info (click to toggle)
soupsieve 2.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,048 kB
  • sloc: python: 8,428; sh: 8; makefile: 5; javascript: 2
file content (112 lines) | stat: -rw-r--r-- 3,422 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""Test attribute selectors."""
from .. import util


class TestAttribute(util.TestCase):
    """Test attribute selectors."""

    MARKUP = """
    <div id="div">
    <p id="0" class="herearesomewords">Some text <span id="1"> in a paragraph</span>.</p>
    <a id="2" href="http://google.com">Link</a>
    <span id="3">Direct child</span>
    <pre id="pre">
    <span id="4">Child 1</span>
    <span id="5">Child 2</span>
    <span id="6">Child 3</span>
    </pre>
    </div>
    """

    def test_attribute_begins(self):
        """Test attribute whose value begins with the specified value."""

        self.assert_selector(
            self.MARKUP,
            "[class^=here]",
            ["0"],
            flags=util.HTML
        )

    def test_attribute_end(self):
        """Test attribute whose value ends with the specified value."""

        self.assert_selector(
            self.MARKUP,
            "[class$=words]",
            ["0"],
            flags=util.HTML
        )

    def test_attribute_contains(self):
        """Test attribute whose value contains the specified value."""

        markup = """
        <div id="div">
        <p id="0" class="somewordshere">Some text <span id="1"> in a paragraph</span>.</p>
        <a id="2" href="http://google.com">Link</a>
        <span id="3" class="herewords">Direct child</span>
        <pre id="pre" class="wordshere">
        <span id="4">Child 1</span>
        <span id="5">Child 2</span>
        <span id="6">Child 3</span>
        </pre>
        </div>
        """

        self.assert_selector(
            markup,
            "[class*=words]",
            ["0", "3", "pre"],
            flags=util.HTML
        )

    def test_attribute_contains_with_newlines(self):
        """Test attribute `*=` will match with new lines."""

        self.assert_selector(
            "<p><span id='1' title='foo bar'>foo1</span><span id='2' title='foo\nbar'>foo1</span></p>",
            "span[title*='bar']",
            ["1", "2"],
            flags=util.HTML
        )

    def test_attribute_starts_with_newlines(self):
        """Test attribute `^=` will match with new lines."""

        self.assert_selector(
            "<p><span id='1' title='foo bar'>foo1</span><span id='2' title='foo\nbar'>foo1</span></p>",
            "span[title^='foo']",
            ["1", "2"],
            flags=util.HTML
        )

    def test_attribute_ends_with_newlines(self):
        """Test attribute `$=` will match with new lines."""

        self.assert_selector(
            "<p><span id='1' title='foo bar'>foo1</span><span id='2' title='foo\nbar'>foo1</span></p>",
            "span[title$='bar']",
            ["1", "2"],
            flags=util.HTML
        )

    def test_attribute_dash_list_with_newlines(self):
        """Test attribute `|=` will match with new lines."""

        self.assert_selector(
            "<p><span id='1' title='fo-o bar'>foo1</span><span id='2' title='fo-o\nbar'>foo1</span></p>",
            "span[title|='fo']",
            ["1", "2"],
            flags=util.HTML
        )

    def test_attribute_space_list_with_newlines(self):
        """Test attribute `~=` will match with new lines."""

        self.assert_selector(
            "<p><span id='1' title='foo bar baz'>foo1</span><span id='2' title='foo\nbar baz'>foo1</span></p>",
            "span[title~='baz']",
            ["1", "2"],
            flags=util.HTML
        )