Skip to content

Commit cedc978

Browse files
authored
Respect namespaces provided by the parser when root is not HTML element (#348)
* Respect namespaces provided by the parser when root is not HTML element * Fix spelling
1 parent d0e5402 commit cedc978

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎docs/src/dictionary/en-custom.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ GitHub
1717
Gitter
1818
Hashable
1919
JQuery
20-
MathML
2120
MERCHANTABILITY
21+
MathML
2222
MkDocs
2323
NONINFRINGEMENT
2424
Precompile
@@ -67,6 +67,7 @@ matcher
6767
matchers
6868
multiline
6969
namespace
70+
namespaced
7071
namespaces
7172
newline
7273
parser's

‎docs/src/markdown/about/changelog.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ icon: lucide/scroll-text
3636
all but the first top level element of a fragment parsed with `html.parser` never matched.
3737
- **FIX**: `:dir()` now accepts any identifier, as CSS requires, including escaped ones. Values other than `ltr` and
3838
`rtl` do not match anything.
39+
- **FIX**: Respect namespaces provided by the parser when the root is not an HTML element, such as an `svg` element
40+
extracted from an `html5lib` document. Previously every element in such a tree was treated as an HTML element,
41+
which affected namespace selectors, namespaced attributes, and HTML specific pseudo-classes.
3942
- **FIX**: Fix `:lang()` treating an element with an empty `lang` or `xml:lang` attribute as having the language of
4043
a tagged ancestor.
4144
- **FIX**: Cache forms without a default button when matching `:default` to avoid quadratic time on forms with

‎soupsieve/css_match.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ def __init__(
609609
self.scope = scope if scope is not doc else root
610610
self.has_html_namespace = self.has_html_ns(root)
611611

612+
# Parsers either record a namespace for every element or for none. The root
613+
# alone can't be expected to be HTML as a non-HTML element may have been detached.
614+
self.has_namespaces = bool(self.get_uri(root))
615+
612616
# A document can be both XML and HTML (XHTML)
613617
self.is_xml = self.is_xml_tree(doc)
614618
self.is_html = not self.is_xml or self.has_html_namespace
@@ -624,7 +628,7 @@ def reset(self) -> None: # pragma: no cover
624628
def supports_namespaces(self) -> bool:
625629
"""Check if namespaces are supported in the HTML type."""
626630

627-
return self.is_xml or self.has_html_namespace
631+
return self.is_xml or self.has_namespaces
628632

629633
def get_tag_ns(self, el: bs4.Tag | None) -> str:
630634
"""Get tag namespace."""

‎tests/test_level3/test_namespace.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test namespace selectors."""
22
from .. import util
3+
import soupsieve as sv
34

45

56
class TestNamespace(util.TestCase):
@@ -325,3 +326,34 @@ def test_attribute_namespace_xml(self):
325326
namespaces={"xlink": "http://www.w3.org/1999/xlink"},
326327
flags=util.XHTML
327328
)
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'])

‎tests/test_level4/test_dir.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,22 @@ def test_top_level_non_html_elements(self):
517517

518518
self.assertEqual([el['id'] for el in sv.select(':dir(ltr)[id]', soup)], ['1', '2'])
519519

520+
@util.requires_html5lib
521+
def test_non_html_root(self):
522+
"""Test that `dir` is ignored on a non-HTML element that is the root."""
523+
524+
markup = """
525+
<svg id="1" dir="rtl">
526+
<g id="2"></g>
527+
<foreignObject id="3"><p id="4" dir="rtl"><span id="5"></span></p></foreignObject>
528+
</svg>
529+
"""
530+
svg = self.soup(markup, 'html5lib').svg.extract()
531+
532+
self.assertTrue(sv.match(':dir(ltr)', svg))
533+
self.assertEqual([el['id'] for el in sv.select(':dir(ltr)', svg)], ['2', '3'])
534+
self.assertEqual([el['id'] for el in sv.select(':dir(rtl)', svg)], ['4', '5'])
535+
520536
def test_bdi_on_root(self):
521537
"""Test that `bdi` uses its text for direction when it is the root."""
522538

0 commit comments

Comments
 (0)