I think treating featureless as "no next candidate found" makes a lot more sense (even though in practice it should be the same, since we currently only have elements with child / descendant combinators). This also cleans up a bit more the code, and stops earlier for pseudos, part, and slotted, which similarly can't find an ancestor once past the original one we return. Differential Revision: https://phabricator.services.mozilla.com/D240490
rust-selectors
CSS Selectors library for Rust. Includes parsing and serilization of selectors, as well as matching against a generic tree of elements. Pseudo-elements and most pseudo-classes are generic as well.
Warning: breaking changes are made to this library fairly frequently (13 times in 2016, for example). However you can use this crate without updating it that often, old versions stay available on crates.io and Cargo will only automatically update to versions that are numbered as compatible.
To see how to use this library with your own tree representation,
see Kuchiki’s src/select.rs.
(Note however that Kuchiki is not always up to date with the latest rust-selectors version,
so that code may need to be tweaked.)
If you don’t already have a tree data structure,
consider using Kuchiki itself.