Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Test match parent closest."""
markup = """
<article id="article">
<div id="div-01">Here is div-01
<div id="div-02">Here is div-02
<div id="div-04">Here is div-04</div>
<div id="div-03">Here is div-03</div>
</div>
<div id="div-05">Here is div-05</div>
</div>
</article>
"""
soup = self.soup(markup, 'html.parser')
el = sv.select_one('#div-03', soup)
self.assertTrue(sv.closest('#div-02', el).attrs['id'] == 'div-02')
<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')
self.assertEqual(None, sv.select_one('h1', soup))
"""Test closest match complex parent."""
markup = """
<article id="article">
<div id="div-01">Here is div-01
<div id="div-02">Here is div-02
<div id="div-04">Here is div-04</div>
<div id="div-03">Here is div-03</div>
</div>
<div id="div-05">Here is div-05</div>
</div>
</article>
"""
soup = self.soup(markup, 'html.parser')
el = sv.select_one('#div-03', soup)
self.assertTrue(sv.closest('article > div', el).attrs['id'] == 'div-01')
self.assertTrue(sv.closest(':not(div)', el).attrs['id'] == 'article')
<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')
self.assertEqual(
sv.select('span[id]', soup, limit=1)[0].attrs['id'],
sv.select_one('span[id]', soup).attrs['id']
)
"""Test closest."""
markup = """
<article id="article">
<div id="div-01">Here is div-01
<div id="div-02">Here is div-02
<div id="div-04">Here is div-04</div>
<div id="div-03">Here is div-03</div>
</div>
<div id="div-05">Here is div-05</div>
</div>
</article>
"""
soup = bs4.BeautifulSoup(markup, 'html5lib')
el = sv.select_one('#div-03', soup)
self.assertTrue(sv.closest('#div-02', el).attrs['id'] == 'div-02')
self.assertTrue(sv.closest('div div', el).attrs['id'] == 'div-03')
self.assertTrue(sv.closest('article > div', el).attrs['id'] == 'div-01')
self.assertTrue(sv.closest(':not(div)', el).attrs['id'] == 'article')
self.assertTrue(sv.closest('div #div-05', el) is None)
self.assertTrue(sv.closest('a', el) is None)
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'])
self.assertEqual(sorted(['5']), sorted(ids))
"""Test closest match self."""
markup = """
<article id="article">
<div id="div-01">Here is div-01
<div id="div-02">Here is div-02
<div id="div-04">Here is div-04</div>
<div id="div-03">Here is div-03</div>
</div>
<div id="div-05">Here is div-05</div>
</div>
</article>
"""
soup = self.soup(markup, 'html.parser')
el = sv.select_one('#div-03', soup)
self.assertTrue(sv.closest('div div', el).attrs['id'] == 'div-03')
"""Test that closest only matches parents or self."""
markup = """
<article id="article">
<div id="div-01">Here is div-01
<div id="div-02">Here is div-02
<div id="div-04">Here is div-04</div>
<div id="div-03">Here is div-03</div>
</div>
<div id="div-05">Here is div-05</div>
</div>
</article>
"""
soup = self.soup(markup, 'html.parser')
el = sv.select_one('#div-03', soup)
self.assertTrue(sv.closest('div #div-05', el) is None)
self.assertTrue(sv.closest('a', el) is None)