|
1 | 1 | """Test namespace selectors.""" |
2 | 2 | from .. import util |
| 3 | +import soupsieve as sv |
3 | 4 |
|
4 | 5 |
|
5 | 6 | class TestNamespace(util.TestCase): |
@@ -325,3 +326,34 @@ def test_attribute_namespace_xml(self): |
325 | 326 | namespaces={"xlink": "http://www.w3.org/1999/xlink"}, |
326 | 327 | flags=util.XHTML |
327 | 328 | ) |
| 329 | + |
| 330 | + @util.requires_html5lib |
| 331 | + def test_non_html_root(self): |
| 332 | + """Test that the namespaces a parser provides are used when the root is not an HTML element.""" |
| 333 | + |
| 334 | + namespaces = { |
| 335 | + "svg": "http://www.w3.org/2000/svg", |
| 336 | + "html": "http://www.w3.org/1999/xhtml", |
| 337 | + "xlink": "http://www.w3.org/1999/xlink" |
| 338 | + } |
| 339 | + markup = """ |
| 340 | + <svg id="1" xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 341 | + <a id="2" xlink:href="target"></a> |
| 342 | + <foreignObject id="3"><a id="4" href="target"></a></foreignObject> |
| 343 | + </svg> |
| 344 | + """ |
| 345 | + svg = self.soup(markup, 'html5lib').svg.extract() |
| 346 | + |
| 347 | + for selector, expected in (('svg|a', ['2']), ('html|a', ['4']), ('[xlink|href]', ['2'])): |
| 348 | + self.assertEqual([el['id'] for el in sv.select(selector, svg, namespaces=namespaces)], expected) |
| 349 | + |
| 350 | + def test_non_html_root_no_namespaces(self): |
| 351 | + """Test that everything is treated as HTML when a parser does not provide namespaces.""" |
| 352 | + |
| 353 | + namespaces = {"svg": "http://www.w3.org/2000/svg", "html": "http://www.w3.org/1999/xhtml"} |
| 354 | + markup = """<svg id="1"><a id="2"></a><foreignObject id="3"><a id="4"></a></foreignObject></svg>""" |
| 355 | + |
| 356 | + for parser in util.available_parsers('html.parser', 'lxml'): |
| 357 | + svg = self.soup(markup, parser).svg.extract() |
| 358 | + self.assertEqual(sv.select('svg|a', svg, namespaces=namespaces), []) |
| 359 | + self.assertEqual([el['id'] for el in sv.select('html|a', svg, namespaces=namespaces)], ['2', '4']) |
0 commit comments