diff options
author | facelessuser <faceless.shop@gmail.com> | 2023-02-10 11:34:48 -0700 |
---|---|---|
committer | facelessuser <faceless.shop@gmail.com> | 2023-02-10 11:34:48 -0700 |
commit | a01835fcf30959eb286850b6c5fd1dd7ef52bd4c (patch) | |
tree | 8e1fd92268e54d34740928072888e7a87ecc5daf /bs4 | |
parent | 9107a904bf30afe47f32006bc9f808a8ed7a5fca (diff) |
Add compile method
Diffstat (limited to 'bs4')
-rw-r--r-- | bs4/css.py | 21 | ||||
-rw-r--r-- | bs4/tests/test_css.py | 3 |
2 files changed, 22 insertions, 2 deletions
@@ -73,6 +73,27 @@ class CSS(object): from bs4.element import ResultSet return ResultSet(None, results) + def compile(self, select, namespaces=None, flags=0, **kwargs): + """Pre-compile a selector and return the compiled object. + + :param selector: A CSS selector. + + :param namespaces: A dictionary mapping namespace prefixes + used in the CSS selector to namespace URIs. By default, + Beautiful Soup will use the prefixes it encountered while + parsing the document. + + :param flags: Flags to be passed into Soup Sieve's + soupsieve.select_one() method. + + :param kwargs: Keyword arguments to be passed into SoupSieve's + soupsieve.select_one() method. + + :return: A precompiled selector object. + :rtype: soupsieve.SoupSieve + """ + return self.api.compile(select, self._ns(namespaces, select), flags, **kwargs) + def select_one(self, select, namespaces=None, flags=0, **kwargs): """Perform a CSS selection operation on the current Tag and return the first result. diff --git a/bs4/tests/test_css.py b/bs4/tests/test_css.py index c9ade48..359dbcd 100644 --- a/bs4/tests/test_css.py +++ b/bs4/tests/test_css.py @@ -15,7 +15,6 @@ 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") @@ -96,7 +95,7 @@ class TestCSSSelectors(SoupTest): self.assert_selects(selector, expected_ids) def test_precompiled(self): - sel = sv_compile('div') + sel = self.soup.css.compile('div') els = self.soup.select(sel) assert len(els) == 4 |