-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nesting ampersand (#269)
* Add support for nesting ampersand When used outside the context of nesting, ampersand is treated as the scoping root. * Update copyright and document stuff
- Loading branch information
1 parent
dc71495
commit c811bdf
Showing
12 changed files
with
138 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Test CSS introduced by Nesting level 1.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""Test ampersand selectors.""" | ||
from .. import util | ||
import soupsieve as sv | ||
|
||
|
||
class TestAmp(util.TestCase): | ||
"""Test scope 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> | ||
""" | ||
|
||
def test_amp_is_root(self): | ||
"""Test ampersand is the root when the a specific element is not the target of the select call.""" | ||
|
||
# Scope is root when applied to a document node | ||
self.assert_selector( | ||
self.MARKUP, | ||
"&", | ||
["root"], | ||
flags=util.HTML | ||
) | ||
|
||
self.assert_selector( | ||
self.MARKUP, | ||
"& > body > div", | ||
["div"], | ||
flags=util.HTML | ||
) | ||
|
||
def test_amp_cannot_select_target(self): | ||
"""Test that ampersand, 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('&', el, flags=sv.DEBUG)) == 0) | ||
|
||
def test_amp_is_select_target(self): | ||
"""Test that ampersand 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 = [el.attrs['id'] for el in sv.select('& div', el, flags=sv.DEBUG)] | ||
self.assertEqual(sorted(ids), sorted(['div'])) | ||
|
||
el = soup.body | ||
ids = [el.attrs['id'] for el in sv.select('& div', el, flags=sv.DEBUG)] | ||
self.assertEqual(sorted(ids), sorted(['div'])) | ||
|
||
# `div` is the current element under select, and it has no `div` elements. | ||
el = soup.div | ||
ids = [el.attrs['id'] for el in sv.select('& div', el, flags=sv.DEBUG)] | ||
self.assertEqual(sorted(ids), sorted([])) | ||
|
||
# `div` does have an element with the class `.wordshere` | ||
ids = [el.attrs['id'] for el in sv.select('& .wordshere', el, flags=sv.DEBUG)] | ||
self.assertEqual(sorted(ids), sorted(['pre'])) |