Re:Htmlq: like jq, but for html
by vlunkr on Tuesday September 07, 2021 @11:21AM
(#28445938)
Well, the big one is selecting a parent from the child.
[ Parent
| on HN ]
Re:Htmlq: like jq, but for html
by androceium on Tuesday September 07, 2021 @02:49PM
(#28448418)
You could do this with the :has() CSS psuedo-class[0], though inverted (select a parent that _has_ the child matching a selector).
Looks like that psuedo-class has not been implemented in the kuchiki library that htmlq uses though.
You can do it either way in XPath thanks to how you can use a path expression and/or predicates almost everywhere in a query
# Find all elements li and select the parent element for each
//li/..
# Find all element nodes with a child element named li
//*[li]
# Non-abbreviated queries
/descendant::li/parent::*
/descendant::*[child::li]
# CSS using :has
:has(> li)