summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2012-04-20 20:10:01 +0200
committerSimon Sapin <simon.sapin@exyr.org>2012-04-20 20:10:01 +0200
commit7d8a55cb4b4ee95d84aa33368c9e79162fbc5cbd (patch)
tree85f8dba564fb6bf61ab032b364c7f4af11be6b20 /src
parent813e566f27daa19b63a59a6750bcb99bd7a1760c (diff)
downloadpython-lxml-7d8a55cb4b4ee95d84aa33368c9e79162fbc5cbd.tar.gz
Add a cssselect method to all elements, not just HtmlElement
translator defaults to 'xml' in _Element and 'html' in HtmlElement (Add it back again, but on a separate pull request.)
Diffstat (limited to 'src')
-rw-r--r--src/lxml/html/__init__.py3
-rw-r--r--src/lxml/lxml.etree.pyx14
2 files changed, 16 insertions, 1 deletions
diff --git a/src/lxml/html/__init__.py b/src/lxml/html/__init__.py
index 958304f6..1a1d7d28 100644
--- a/src/lxml/html/__init__.py
+++ b/src/lxml/html/__init__.py
@@ -682,7 +682,8 @@ class HtmlComment(etree.CommentBase, HtmlMixin):
class HtmlElement(etree.ElementBase, HtmlMixin):
- pass
+ # Override etree.ElementBase.cssselect, despite the MRO
+ cssselect = HtmlMixin.cssselect
class HtmlProcessingInstruction(etree.PIBase, HtmlMixin):
diff --git a/src/lxml/lxml.etree.pyx b/src/lxml/lxml.etree.pyx
index 344cc0f2..ec2fac18 100644
--- a/src/lxml/lxml.etree.pyx
+++ b/src/lxml/lxml.etree.pyx
@@ -1586,6 +1586,20 @@ cdef public class _Element [ type LxmlElementType, object LxmlElement ]:
smart_strings=smart_strings)
return evaluator(_path, **_variables)
+ def cssselect(self, expr, *, translator='xml'):
+ """
+ Run the CSS expression on this element and its children,
+ returning a list of the results.
+
+ Equivalent to lxml.cssselect.CSSSelect(expr)(self) -- note
+ that pre-compiling the expression can provide a substantial
+ speedup.
+ """
+ # Do the import here to make the dependency optional.
+ from lxml.cssselect import CSSSelector
+ return CSSSelector(expr, translator=translator)(self)
+
+
cdef extern from "etree_defs.h":
# macro call to 't->tp_new()' for fast instantiation