Open ViRb3 opened 4 years ago
I just noticed an alternative way to do this, using the :scope
psuedo-class. According to MDN, equivalent usage would be as follows:
var selected = context.querySelectorAll(':scope > img');
Unfortunately, Cascadia doesn't support this psuedo-class. Is there an existing way to achieve what I want?
No, Cascadia has no concept of scope at all. A selector either matches an element or it doesn't. There is no provision for matching an element relative to a given scope. In this sense it's like the Selectors "live profile"—but I copied :has
from jQuery.
In the context of goquery it would be really useful if one could do something like:
table.Find(":scope > tbody > tr > th")
To find only th
directly under the top-level table, and not find some nested table. But it seems this is currently not possible.
I made an goquery issue as well: https://github.com/PuerkitoBio/goquery/issues/414
I was thinking, :scope
in the context of cascadia could just be the node from which the selector starts?
Yes, that's what it would be. But Cascadia selectors don't know where they start. The basic interface of the Cascadia package is Matcher:
type Matcher interface {
Match(n *html.Node) bool
}
A Matcher can be called on to match a node from anywhere in the page, in no particular order. It has no concept of a "starting point".
But it would work for MatchAll
and Query
.
No, because those are implemented as wrappers around Match. Almost everything in Cascadia is built around the Match interface.
Support for scope would require such an extensive rewrite that the result wouldn't really be Cascadia any more. It would be an all-new CSS selector package.
Just so I understand this thread, support for div:has(> article)
will not and cannot be support in Cascadias current form?
Right.
Problem
Cascadia seems to fail with the
has
psudo-class and a direct child selectorFrom MDN
Cascadia test
Result
selector_test.go:651: error compiling "a:has(> img)": expected identifier, found > instead
Is there any chance to get this implemented? Thanks!