1. Olivier Lauzanne
  2. pyquery
  3. Issues
Issue #9 invalid

cssselectpatch breaks union queries in lxml.cssselect

Antti Kaihola
created an issue

Something strange is going on in {{{cssselectpatch.py}}}. I can see that it monkey-patches {{{lxml.cssselect}}}. But after importing {{{cssselectpatch}}}, {{{lxml.cssselect}}} generates invalid XPath queries for when using the comma operator. See below for a test session which exhibits the incorrect behavior.

First test lxml CSS selectors without using the patch from pyquery:

{{{

!python

from lxml.cssselect import parse, css_to_xpath css_to_xpath('p, p a') u'descendant-or-self::p | descendant-or-self::p/descendant::a' parse('p, p a') Or([Element[p], CombinedSelector[Element[p] <followed> Element[a]]]) parse('p, p a').xpath() XPathExprOr[p | p/descendant::a] parse('p, p a').xpath().items [XPathExpr[p], XPathExpr[p/descendant::a]] }}}

The second item of the 'or' expression is always a combination of {{{p}}} with a descendant {{{a}}}.

Now import patches from pyquery and do the same test again:

{{{

!python

from pyquery import cssselectpatch css_to_xpath('p, p a') u'descendant-or-self::p | descendant-or-self::a' }}}

Now the second part of the resulting XPath query is missing the {{{p}}} step and the descendant operator.

Let's try using the lower-level parser:

{{{

!python

parse('p, p a') Or([JQueryElement[p], CombinedSelector[JQueryElement[p] <followed> JQueryElement[a]]]) }}}

Here the correct steps seem to be present. But when the steps are combined into an XPath expression, the {{{p}}} step and the descendant operator vanish:

{{{

!python

parse('p, p a').xpath() AdvancedXPathExprOr[p | a] }}}

However, they still seem to be present in the expression objects:

{{{

!python

parse('p, p a').xpath().items [AdvancedXPathExpr[p], AdvancedXPathExpr[p/descendant::a]] }}}

This is as far as I got, but beyond this the cause for this behavior is a mystery for me.

Comments (3)

  1. Log in to comment