|
| 1 | +"""Test ampersand selectors.""" |
| 2 | +from .. import util |
| 3 | +import soupsieve as sv |
| 4 | + |
| 5 | + |
| 6 | +class TestAmp(util.TestCase): |
| 7 | + """Test scope selectors.""" |
| 8 | + |
| 9 | + MARKUP = """ |
| 10 | + <html id="root"> |
| 11 | + <head> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <div id="div"> |
| 15 | + <p id="0" class="somewordshere">Some text <span id="1"> in a paragraph</span>.</p> |
| 16 | + <a id="2" href="http://google.com">Link</a> |
| 17 | + <span id="3" class="herewords">Direct child</span> |
| 18 | + <pre id="pre" class="wordshere"> |
| 19 | + <span id="4">Child 1</span> |
| 20 | + <span id="5">Child 2</span> |
| 21 | + <span id="6">Child 3</span> |
| 22 | + </pre> |
| 23 | + </div> |
| 24 | + </body> |
| 25 | + </html> |
| 26 | + """ |
| 27 | + |
| 28 | + def test_amp_is_root(self): |
| 29 | + """Test ampersand is the root when the a specific element is not the target of the select call.""" |
| 30 | + |
| 31 | + # Scope is root when applied to a document node |
| 32 | + self.assert_selector( |
| 33 | + self.MARKUP, |
| 34 | + "&", |
| 35 | + ["root"], |
| 36 | + flags=util.HTML |
| 37 | + ) |
| 38 | + |
| 39 | + self.assert_selector( |
| 40 | + self.MARKUP, |
| 41 | + "& > body > div", |
| 42 | + ["div"], |
| 43 | + flags=util.HTML |
| 44 | + ) |
| 45 | + |
| 46 | + def test_amp_cannot_select_target(self): |
| 47 | + """Test that ampersand, the element which scope is called on, cannot be selected.""" |
| 48 | + |
| 49 | + for parser in util.available_parsers( |
| 50 | + 'html.parser', 'lxml', 'html5lib', 'xml'): |
| 51 | + soup = self.soup(self.MARKUP, parser) |
| 52 | + el = soup.html |
| 53 | + |
| 54 | + # Scope is the element we are applying the select to, and that element is never returned |
| 55 | + self.assertTrue(len(sv.select('&', el, flags=sv.DEBUG)) == 0) |
| 56 | + |
| 57 | + def test_amp_is_select_target(self): |
| 58 | + """Test that ampersand is the element which scope is called on.""" |
| 59 | + |
| 60 | + for parser in util.available_parsers( |
| 61 | + 'html.parser', 'lxml', 'html5lib', 'xml'): |
| 62 | + soup = self.soup(self.MARKUP, parser) |
| 63 | + el = soup.html |
| 64 | + |
| 65 | + # Scope here means the current element under select |
| 66 | + ids = [el.attrs['id'] for el in sv.select('& div', el, flags=sv.DEBUG)] |
| 67 | + self.assertEqual(sorted(ids), sorted(['div'])) |
| 68 | + |
| 69 | + el = soup.body |
| 70 | + ids = [el.attrs['id'] for el in sv.select('& div', el, flags=sv.DEBUG)] |
| 71 | + self.assertEqual(sorted(ids), sorted(['div'])) |
| 72 | + |
| 73 | + # `div` is the current element under select, and it has no `div` elements. |
| 74 | + el = soup.div |
| 75 | + ids = [el.attrs['id'] for el in sv.select('& div', el, flags=sv.DEBUG)] |
| 76 | + self.assertEqual(sorted(ids), sorted([])) |
| 77 | + |
| 78 | + # `div` does have an element with the class `.wordshere` |
| 79 | + ids = [el.attrs['id'] for el in sv.select('& .wordshere', el, flags=sv.DEBUG)] |
| 80 | + self.assertEqual(sorted(ids), sorted(['pre'])) |
0 commit comments