"""Test root selectors."""
from .. import util
import soupsieve as sv
class TestRoot(util.TestCase):
"""Test root selectors."""
MARKUP = """
Some text in a paragraph.
Link
Direct child
Child 1
Child 2
Child 3
"""
MARKUP_IFRAME = """
"""
def test_root(self):
"""Test root."""
# Root in HTML is ``
self.assert_selector(
self.MARKUP,
":root",
["root"],
flags=util.HTML
)
def test_root_iframe(self):
"""Test root."""
# Root in HTML is ``
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 = """
"""
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
"""
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 = """
"""
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 = """
"""
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 = """
"""
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 = """
"""
soup = self.soup(markup, 'html.parser')
ids = [el['id'] for el in soup.select(':root')]
self.assertEqual(sorted(ids), sorted(['1']))