summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfacelessuser <faceless.shop@gmail.com>2023-02-10 11:18:35 -0700
committerfacelessuser <faceless.shop@gmail.com>2023-02-10 11:18:35 -0700
commit9107a904bf30afe47f32006bc9f808a8ed7a5fca (patch)
tree5522adc33224574d7265dc2bbdd71ce7ee71b91b
parentcbce97624b05a399cd5805c5938b342a8e10fe9b (diff)
Remove unnecessary test and don't pass namespace on precompiled select
-rw-r--r--bs4/css.py16
-rw-r--r--bs4/tests/test_css.py29
2 files changed, 20 insertions, 25 deletions
diff --git a/bs4/css.py b/bs4/css.py
index b237051..ac89759 100644
--- a/bs4/css.py
+++ b/bs4/css.py
@@ -55,9 +55,9 @@ class CSS(object):
)
return self.api.escape(ident)
- def _ns(self, ns):
+ def _ns(self, ns, select):
"""Normalize a dictionary of namespaces."""
- if ns is None:
+ if not isinstance(select, self.api.SoupSieve) and ns is None:
ns = self.tag._namespaces
return ns
@@ -99,7 +99,7 @@ class CSS(object):
"""
return self.api.select_one(
- select, self.tag, self._ns(namespaces), flags, **kwargs
+ select, self.tag, self._ns(namespaces, select), flags, **kwargs
)
def select(self, select, namespaces=None, limit=0, flags=0, **kwargs):
@@ -133,7 +133,7 @@ class CSS(object):
return self._rs(
self.api.select(
- select, self.tag, self._ns(namespaces), limit, flags,
+ select, self.tag, self._ns(namespaces, select), limit, flags,
**kwargs
)
)
@@ -165,7 +165,7 @@ class CSS(object):
:rtype: types.GeneratorType
"""
return self.api.iselect(
- select, self.tag, self._ns(namespaces), limit, flags, **kwargs
+ select, self.tag, self._ns(namespaces, select), limit, flags, **kwargs
)
def closest(self, select, namespaces=None, flags=0, **kwargs):
@@ -193,7 +193,7 @@ class CSS(object):
"""
return self.api.closest(
- select, self.tag, self._ns(namespaces), flags, **kwargs
+ select, self.tag, self._ns(namespaces, select), flags, **kwargs
)
def match(self, select, namespaces=None, flags=0, **kwargs):
@@ -220,7 +220,7 @@ class CSS(object):
:rtype: bool
"""
return self.api.match(
- select, self.tag, self._ns(namespaces), flags, **kwargs
+ select, self.tag, self._ns(namespaces, select), flags, **kwargs
)
def filter(self, select, namespaces=None, flags=0, **kwargs):
@@ -248,6 +248,6 @@ class CSS(object):
"""
return self._rs(
self.api.filter(
- select, self.tag, self._ns(namespaces), flags, **kwargs
+ select, self.tag, self._ns(namespaces, select), flags, **kwargs
)
)
diff --git a/bs4/tests/test_css.py b/bs4/tests/test_css.py
index cf73831..c9ade48 100644
--- a/bs4/tests/test_css.py
+++ b/bs4/tests/test_css.py
@@ -15,6 +15,7 @@ from . import (
if SOUP_SIEVE_PRESENT:
from soupsieve import SelectorSyntaxError
+ from soupsieve import compile as sv_compile
@pytest.mark.skipif(not SOUP_SIEVE_PRESENT, reason="Soup Sieve not installed")
@@ -94,6 +95,17 @@ class TestCSSSelectors(SoupTest):
for selector, expected_ids in tests:
self.assert_selects(selector, expected_ids)
+ def test_precompiled(self):
+ sel = sv_compile('div')
+
+ els = self.soup.select(sel)
+ assert len(els) == 4
+ for div in els:
+ assert div.name == 'div'
+
+ el = self.soup.select_one(sel)
+ assert 'main' == el['id']
+
def test_one_tag_one(self):
els = self.soup.select('title')
assert len(els) == 1
@@ -474,20 +486,3 @@ class TestCSSSelectors(SoupTest):
assert m(".foo#bar") == '\\.foo\\#bar'
assert m("()[]{}") == '\\(\\)\\[\\]\\{\\}'
assert m(".foo") == self.soup.css.escape(".foo")
-
- def test_api_replacement(self):
- # You can pass in another object to act as a drop-in
- # replacement for the soupsieve module.
- class Mock():
- attribute = "value"
- pass
- mock_soupsieve = Mock()
- mock_soupsieve.escape = MagicMock()
-
- # If an unknown method turns out to be present in Soup Sieve,
- # we may still be able to call it.
- css = CSS(self.soup, api=mock_soupsieve)
- css.escape("identifier")
- mock_soupsieve.escape.assert_called_with(
- "selector", self.soup, 1, flags=0
- )