Open frisi opened 5 years ago
@dangra @kmike as you did the last commits to this repos i'm asking you. can you shed some light on this? if not - who shall i ask? thank you!
@frisi , I'm really not sure how version 0.9.1 did it, but to me, something like > .child
alone, i.e. starting with >
is not a valid CSS3 selector.
This is one example where CSS3 falls short when working in specific subtrees of the document, and where XPath can be used directly.
Maybe there's something for your use-case in CSS4, around :scope
elements, but I'm not sure I understand how it's supposed to work. See relative selectors.
Certain contexts may accept relative selectors, which are a shorthand for selectors that represent elements relative to a :scope element (i.e. an element that matches :scope). In a relative selector, “:scope ” (the :scope pseudo-class followed by a space) is implied at the beginning of each complex selector that does not already contain the :scope pseudo-class. This allows the selector to begin syntactically with a combinator. However, it must be absolutized before matching.
thanks for your detailed explanation @redapple.
IIUC element.cssselect('> a')
is not made/meant to work on subtrees.
i replaced the relative css selects with xpath (which - for me at least - is a lot less readable ;-):
broken = tree.cssselect('div.parent')[0].cssselect('> .child')
works = tree.cssselect('div.parent')[0].xpath('./*[contains(concat(" ", @class, " "), " child ")]')
is there a place in the documentation to add a note on this limitation? if not, i'd say this issue can be closed.
Are there really no better workarounds than dropping the entire selector to XPath manually? Perhaps there is some esoteric XPath way to take an existing expression and turn it into something that means "the same but only direct children of the scope element"?
in version 1.0.3 i get an exception when using cssselect on an element to select it's direct children
element > element
(see https://www.w3schools.com/cssref/sel_element_gt.asp)in version 0.9.1 the following worked w/o raising an exception, however it leads to an unexpected result since the second
div.child
is no direct child ofdiv.parent
>
only works when parent selector is given in the selectorA) is it a regression, that
element.cssselect('> .child')
raises an exception on recent versions?B) is there a way to select a direct child given the parent element?