File size: 4,668 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 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
"""Test root selectors."""
from .. import util
import soupsieve as sv
class TestRoot(util.TestCase):
"""Test root 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>
"""
MARKUP_IFRAME = """
<html id="root">
<head>
</head>
<body>
<div id="div">
</div>
<iframe src="https://something.com">
<html id="root2">
<head>
</head>
<body>
<div id="div2">
</div>
</body>
</html>
</iframe>
<div id="other-div"></div>
</body>
</html>
"""
def test_root(self):
"""Test root."""
# Root in HTML is `<html>`
self.assert_selector(
self.MARKUP,
":root",
["root"],
flags=util.HTML
)
def test_root_iframe(self):
"""Test root."""
# Root in HTML is `<html>`
self.assert_selector(
self.MARKUP_IFRAME,
":root",
["root", "root2"],
flags=util.PYHTML
)
def test_root_complex(self):
"""Test root within a complex selector."""
self.assert_selector(
self.MARKUP,
":root > body > div",
["div"],
flags=util.HTML
)
def test_no_iframe(self):
"""Test that we don't count `iframe` as root."""
self.assert_selector(
self.MARKUP_IFRAME,
":root div",
["div", "div2", "other-div"],
flags=util.PYHTML
)
self.assert_selector(
self.MARKUP_IFRAME,
":root > body > div",
["div", "div2", "other-div"],
flags=util.PYHTML
)
def test_iframe(self):
"""
Test that we only count `iframe` as root since the scoped element is the root.
Not all the parsers treat `iframe` content the same. `html5lib` for instance
will escape the content in the `iframe`, so we are just going to test the builtin
Python parser.
"""
soup = self.soup(self.MARKUP_IFRAME, 'html.parser')
ids = [el['id'] for el in sv.select(':root div', soup.iframe.html)]
self.assertEqual(sorted(ids), sorted(['div2']))
ids = [el['id'] for el in sv.select(':root > body > div', soup.iframe.html)]
self.assertEqual(sorted(ids), sorted(['div2']))
def test_no_root_double_tag(self):
"""Test when there is no root due to double root tags."""
markup = """
<div id="1"></div>
<div id="2"></div>
"""
soup = self.soup(markup, 'html.parser')
self.assertEqual(soup.select(':root'), [])
def test_no_root_text(self):
"""Test when there is no root due to HTML text."""
markup = """
text
<div id="1"></div>
"""
soup = self.soup(markup, 'html.parser')
self.assertEqual(soup.select(':root'), [])
def test_no_root_cdata(self):
"""Test when there is no root due to CDATA and tag."""
markup = """
<![CDATA[test]]>
<div id="1"></div>
"""
soup = self.soup(markup, 'html.parser')
self.assertEqual(soup.select(':root'), [])
def test_root_whitespace(self):
"""Test when there is root and white space."""
markup = """
<div id="1"></div>
"""
soup = self.soup(markup, 'html.parser')
ids = [el['id'] for el in soup.select(':root')]
self.assertEqual(sorted(ids), sorted(['1']))
def test_root_preprocess(self):
"""Test when there is root and pre-processing statement."""
markup = """
<?php ?>
<div id="1"></div>
"""
soup = self.soup(markup, 'html.parser')
ids = [el['id'] for el in soup.select(':root')]
self.assertEqual(sorted(ids), sorted(['1']))
def test_root_doctype(self):
"""Test when there is root and doc type."""
markup = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<div id="1"></div>
"""
soup = self.soup(markup, 'html.parser')
ids = [el['id'] for el in soup.select(':root')]
self.assertEqual(sorted(ids), sorted(['1']))
|