Expand description
§lewp-css
Forked version and continued version of css
A Rust library crate for parsing, manipulating and serializing CSS stylesheets. Makes use of existing CSS parser logic from Servo. Includes forks of code from Servo because these are unpublished on https://crates.io. One use of this library is to minify CSS, as the serialized form it produces is minimal. Another use is provide a crate that others can use for auto-prefixing CSS and to eliminate unused CSS. The values of property declarations are currently stored as a string. Parsing property declarations is a monster job (in effect there are bespoke rules for every property). If you feel like helping…
§Usages
§Loading and Saving Stylesheets
use ::lewp_css::Stylesheet;
let some_css = "input { margin-left: 10pt; top: 20px; }".to_owned();
let stylesheet = Stylesheet::parse(&some_css).expect("CSS was invalid");
// Alternatively, load from a file using Stylesheet::from_file_path("/path/to/stylesheet.css").unwrap();
let mut destination = String::new();
// Don't write source-map and source-url comments if any are present in the stylesheet
let include_source_urls = false;
stylesheet.to_css(&mut destination, include_source_urls).expect("Failed to write to destination");
assert_eq!(&destination, "input{margin-left: 10pt;top: 20px}");
// To serialize to a Vec<u8> of bytes instead
let mut bytes = stylesheet.to_bytes(false);
// To serialize to a file instead
//stylesheet.to_file_path("/path/to/to/stylesheet.css", false).unwrap();
§To parse a single CSS selector
use ::lewp_css::parse_css_selector;
let selector = parse_css_selector("P.myclass").unwrap();
§To match CSS selectors to HTML
Use the html5ever_ext
crate. (The function domain::selectors::matches()
can do matching but needs a lot of HTML logic to do so).
Re-exports§
pub extern crate cssparser;
pub extern crate ordermap;
pub extern crate servo_arc;
pub extern crate smallvec;
Modules§
- domain
- Contains definitions of objects used in Stylesheet.
Structs§
- Stylesheet
- Represents an entire CSS stylesheet. The values of property declarations are currently stored as a string. Parsing property declarations is a monster job. If you feel like helping…
Enums§
- Custom
Parse Error - Represents all the things that can go wrong when parsing.
- Stylesheet
Error - Represents all the things that can go wrong when loading and saving stylesheets.
Functions§
- parse_
css_ selector - Parses a CSS selector