File: test_defined.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 (92 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (4)
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
"""Test defined selectors."""
from .. import util


class TestDefined(util.TestCase):
    """Test defined selectors."""

    def test_defined_html(self):
        """Test defined HTML."""

        markup = """
        <!DOCTYPE html>
        <html>
        <head>
        </head>
        <body>
        <div id="0"></div>
        <div-custom id="1"></div-custom>
        <prefix:div id="2"></prefix:div>
        <prefix:div-custom id="3"></prefix:div-custom>
        </body>
        </html>
        """

        self.assert_selector(
            markup,
            'body :defined',
            ['0', '2', '3'],
            flags=util.HTML
        )

    @util.skip_no_lxml
    def test_defined_xhtml(self):
        """Test defined XHTML."""

        markup = """
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
            "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
        <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
        <head>
        </head>
        <body>
        <div id="0"></div>
        <div-custom id="1"></div-custom>
        <prefix:div id="2"></prefix:div>
        <!--
        lxml seems to strip away the prefix in versions less than 4.4.0.
        This was most likely because prefix with no namespace is not really valid.
        XML does allow colons in names, but encourages them to be used for namespaces.
        This is a quirk of LXML, but it appears to be fine in 4.4.0+.
        -->
        <prefix:div-custom id="3"></prefix:div-custom>
        </body>
        </html>
        """

        from lxml import etree

        self.assert_selector(
            markup,
            'body :defined',
            # We should get 3, but for LXML versions less than 4.4.0 we don't for reasons stated above.
            ['0', '2'] if etree.LXML_VERSION < (4, 4, 0, 0) else ['0', '1', '2'],
            flags=util.XHTML
        )

    def test_defined_xml(self):
        """Test defined HTML."""

        markup = """
        <?xml version="1.0" encoding="UTF-8"?>
        <html>
        <head>
        </head>
        <body>
        <div id="0"></div>
        <div-custom id="1"></div-custom>
        <prefix:div id="2"></prefix:div>
        <prefix:div-custom id="3"></prefix:div-custom>
        </body>
        </html>
        """

        # Defined is a browser thing.
        # XML doesn't care about defined and this will match nothing in XML.
        self.assert_selector(
            markup,
            'body :defined',
            [],
            flags=util.XML
        )