File size: 2,758 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Test scope selectors."""
from .. import util
import soupsieve as sv


class TestScope(util.TestCase):
    """Test scope selectors."""

    MARKUP = """
    <html id="root">
    <head>
    </head>
    <body>
    <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>
    </body>
    </html>
    """

    def test_scope_is_root(self):
        """Test scope is the root when the a specific element is not the target of the select call."""

        # Scope is root when applied to a document node
        self.assert_selector(
            self.MARKUP,
            ":scope",
            ["root"],
            flags=util.HTML
        )

        self.assert_selector(
            self.MARKUP,
            ":scope > body > div",
            ["div"],
            flags=util.HTML
        )

    def test_scope_cannot_select_target(self):
        """Test that scope, the element which scope is called on, cannot be selected."""

        for parser in util.available_parsers(
                'html.parser', 'lxml', 'html5lib', 'xml'):
            soup = self.soup(self.MARKUP, parser)
            el = soup.html

            # Scope is the element we are applying the select to, and that element is never returned
            self.assertTrue(len(sv.select(':scope', el, flags=sv.DEBUG)) == 0)

    def test_scope_is_select_target(self):
        """Test that scope is the element which scope is called on."""

        for parser in util.available_parsers(
                'html.parser', 'lxml', 'html5lib', 'xml'):
            soup = self.soup(self.MARKUP, parser)
            el = soup.html

            # Scope here means the current element under select
            ids = [el.attrs['id'] for el in sv.select(':scope div', el, flags=sv.DEBUG)]
            self.assertEqual(sorted(ids), sorted(['div']))

            el = soup.body
            ids = [el.attrs['id'] for el in sv.select(':scope div', el, flags=sv.DEBUG)]
            self.assertEqual(sorted(ids), sorted(['div']))

            # `div` is the current element under select, and it has no `div` elements.
            el = soup.div
            ids = [el.attrs['id'] for el in sv.select(':scope div', el, flags=sv.DEBUG)]
            self.assertEqual(sorted(ids), sorted([]))

            # `div` does have an element with the class `.wordshere`
            ids = [el.attrs['id'] for el in sv.select(':scope .wordshere', el, flags=sv.DEBUG)]
            self.assertEqual(sorted(ids), sorted(['pre']))