kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust
MIT License
470 stars 54 forks source link

Compiling selectors with cssparser::Parser #55

Closed twilco closed 5 years ago

twilco commented 5 years ago

Hi there!

I'm using Kuchiki to write a toy browser engine. I need to implement the cssparser::QualifiedRuleParser trait to parse CSS stylesheets into kuchiki::Selectors and declarations belonging to those selectors (e.g. margin: auto), similar to what Servo does here.

However, the KuchikiParser is private, so I can't pass it as the first argument to selectors::parser::SelectorList::parse.

There is kuchiki::Selectors::compile(&str), but I don't have a &str in parse_prelude and parse_block. Instead, I have a cssparser::Parser, which can go right into the second argument of selectors::parser::SelectorList::parse.


My issue can be solved in one of two ways:

  1. Expose the KuchikiParser as part of the public API.
  2. Add a new kuchiki::Selectors::compile method that takes a cssparser::Parser as input instead of a &str.

It's possible I might be better off using the html5ever, selectors, and cssparser crates directly, but I wanted to explore these options, too. Do either of these fit with your vision for the Kuchiki API?

SimonSapin commented 5 years ago

cssparser is a private dependency of kuchiki, by design. This means that updating to an incompatible version of cssparser (for example from 0.25.x to 0.26.x) is not a breaking change. Both of your proposed solutions would make it a public dependency, which I’d rather not do.

Two alternative options are:

twilco commented 5 years ago

cssparser is a private dependency of kuchiki, by design...Both of your proposed solutions would make it a public dependency, which I’d rather not do.

Fair enough, I thought this might be the case.

Thanks for your detailed alternatives — I'll extract the string from the parser for now. Supporting JavaScript, while a very, very distant goal, is something I'd like to do eventually. I'll press forward and see if Kuchiki will work for me. Thanks for your help, and for creating this crate!