Skip to content

Commit

Permalink
Add a cssselect method to all elements, not just HtmlElement
Browse files Browse the repository at this point in the history
translator defaults to 'xml' in _Element and 'html' in HtmlElement
  • Loading branch information
SimonSapin committed Apr 20, 2012
1 parent 8c8418f commit 7820b8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lxml/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ class HtmlComment(etree.CommentBase, HtmlMixin):
pass

class HtmlElement(etree.ElementBase, HtmlMixin):
pass
# Override etree.ElementBase.cssselect, despite the MRO
cssselect = HtmlMixin.cssselect

class HtmlProcessingInstruction(etree.PIBase, HtmlMixin):
pass
Expand Down
14 changes: 14 additions & 0 deletions src/lxml/lxml.etree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,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
Expand Down

5 comments on commit 7820b8d

@scoder
Copy link

@scoder scoder commented on 7820b8d Apr 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, my bad. I should have checked. I had thought this method was already there and needed fixing, but it was only in lxml.html, not in lxml.etree. I didn't mean to add it to lxml.etree.

@SimonSapin
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that it should not be added there?

@scoder
Copy link

@scoder scoder commented on 7820b8d Apr 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm saying, yes. In any case, it's an addition to the API that is independent of the intention of this pull request.

Is there a real advantage of having it on all Elements, instead of just HtmlElements? I mean, it's convenient, ok, but it's not efficient and also won't work on all systems because it requires an external module to be installed. Making it a method feels too tightly integrated for an external module.

I'm really not sure, but I'm leaning towards considering the method on HtmlElements a legacy issue rather than a sparkling good idea. And if that's the case, we shouldn't add to it.

@SimonSapin
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I’ll make two independent pull requests so they can be judged separately.

@scoder
Copy link

@scoder scoder commented on 7820b8d Apr 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Please sign in to comment.