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_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 = []
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([]))
"""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 = []
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([]))
# `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']))
# 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([]))
# `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']))
<p id="1"><code id="2"></code><img src="./image.png" id="3"></p>
<pre id="4"></pre>
<p><span class="some-class" id="5"></span><span id="some-id"></span></p>
<pre class="ignore" id="6">
</pre>
"""
soup = self.soup(markup, 'html.parser')
ids = []
for el in sv.select('span[id]', soup, limit=1):
ids.append(el.attrs['id'])
self.assertEqual(sorted(['5']), sorted(ids))
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([]))
# `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)
<p id="1"><code id="2"></code><img src="./image.png" id="3"></p>
<pre id="4"></pre>
<p><span class="some-class" id="5"></span><span id="some-id"></span></p>
<pre class="ignore" id="6">
</pre>
"""
soup = bs4.BeautifulSoup(markup, 'html5lib')
ids = []
for el in sv.select('span[id]', soup):
ids.append(el.attrs['id'])
self.assertEqual(sorted(['5', 'some-id']), sorted(ids))
ids = []
for el in sv.select('span[id]', soup, limit=1):
ids.append(el.attrs['id'])
self.assertEqual(sorted(['5']), sorted(ids))
self.assertEqual(
sv.select('span[id]', soup, limit=1)[0].attrs['id'],
sv.select_one('span[id]', soup).attrs['id']
)
self.assertEqual(None, sv.select_one('h1', soup))
soup = bs4.BeautifulSoup(markup, 'html5lib')
ids = []
for el in sv.select('span[id]', soup):
ids.append(el.attrs['id'])
self.assertEqual(sorted(['5', 'some-id']), sorted(ids))
ids = []
for el in sv.select('span[id]', soup, limit=1):
ids.append(el.attrs['id'])
self.assertEqual(sorted(['5']), sorted(ids))
self.assertEqual(
sv.select('span[id]', soup, limit=1)[0].attrs['id'],
sv.select_one('span[id]', soup).attrs['id']
)
self.assertEqual(None, sv.select_one('h1', soup))
ids = []
for el in sv.iselect('span[id]', soup):
ids.append(el.attrs['id'])
self.assertEqual(sorted(['5', 'some-id']), sorted(ids))
span = sv.select('span[id]', soup)[0]
ids = []
for el in sv.select('span[id]:not(#some-id)', span.parent):
ids.append(el.attrs['id'])