Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
markup,
":scope > body > div",
["div"],
flags=util.HTML
)
for parser in ('html.parser', 'lxml', 'html5lib', 'xml'):
soup = bs4.BeautifulSoup(textwrap.dedent(markup.replace('\r\n', '\n')), 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)
# Scope here means the current element under select
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted(['div']))
el = soup.body
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted(['div']))
# `div` is the current element under select, and it has no `div` elements.
el = soup.div
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted([]))
def test_dir_on_input_root(self):
"""Test input direction when input is the root."""
markup = """<input dir="auto" type="text" id="1">"""
# Input is root
for parser in util.available_parsers('html.parser', 'lxml', 'html5lib'):
soup = self.soup(markup, parser)
fragment = soup.input.extract()
self.assertTrue(sv.match(":root:dir(ltr)", fragment, flags=sv.DEBUG))
flags=util.HTML
)
self.assert_selector(
markup,
"p:nth-child(even)",
['1', '7', '9'],
flags=util.HTML
)
for parser in ('html.parser', 'lxml', 'html5lib'):
# Paragraph is the root. There is no document.
markup = """<p id="1">text</p>"""
soup = bs4.BeautifulSoup(markup, parser)
fragment = soup.p.extract()
self.assertTrue(sv.match("p:nth-child(1)", fragment, flags=sv.DEBUG))
<span id="5"></span>
<span id="6"></span>
<p id="7"></p>
<p id="8"></p>
<p id="9"></p>
<p id="10"></p>
<span id="11"></span>
"""
for parser in util.available_parsers('html.parser', 'lxml', 'html5lib'):
# Paragraph is the root. There is no document.
markup = """<p id="1">text</p>"""
soup = self.soup(markup, parser)
fragment = soup.p.extract()
self.assertTrue(sv.match("p:nth-child(1)", fragment, flags=sv.DEBUG))
el = soup.body
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted(['div']))
# `div` is the current element under select, and it has no `div` elements.
el = soup.div
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted([]))
# `div` does have an element with the class `.wordshere`
ids = []
for el in sv.select(':scope .wordshere', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted(['pre']))
def test_invalid_type_input_select(self):
"""Test bad input into the select API."""
flags = sv.DEBUG
with self.assertRaises(TypeError):
sv.select('div', "not a tag", flags=flags)
el = soup.body
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted(['div']))
# `div` is the current element under select, and it has no `div` elements.
el = soup.div
ids = []
for el in sv.select(':scope div', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted([]))
# `div` does have an element with the class `.wordshere`
ids = []
for el in sv.select(':scope .wordshere', el, flags=sv.DEBUG):
ids.append(el.attrs['id'])
self.assertEqual(sorted(ids), sorted(['pre']))
def test_invalid_type_input(self):
"""Test bad input into the API."""
flags = sv.DEBUG
if self.quirks:
flags = sv._QUIRKS
with self.assertRaises(TypeError):
sv.match('div', "not a tag", flags=flags)
with self.assertRaises(TypeError):
sv.select('div', "not a tag", flags=flags)
with self.assertRaises(TypeError):
sv.filter('div', "not a tag", flags=flags)
with self.assertRaises(TypeError):
sv.comments('div', "not a tag", flags=flags)
def test_recompile(self):
"""If you feed through the same object, it should pass through unless you change parameters."""
p1 = sv.compile('p[id]')
p2 = sv.compile(p1)
self.assertTrue(p1 is p2)
with pytest.raises(ValueError):
sv.compile(p1, flags=sv.DEBUG)
with pytest.raises(ValueError):
sv.compile(p1, namespaces={"": ""})
with pytest.raises(ValueError):
sv.compile(p1, custom={":--header": 'h1, h2, h3, h4, h5, h6'})