Crate css [] [src]

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

extern crate css;
use ::css::Stylesheet;

let some_css = "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, "margin-left:10pt;top:20px");

// To serialize to a Vec<u8> of bytes instead
let mut bytes = stylesheet.to_bytes();

// To serialize to a file instead
stylesheet.to_file_path("/path/to/to/stylesheet.css").unwrap();

To parse a single CSS selector

extern crate css;
use ::css::parse_selector;

let selector = parse_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).

Reexports

pub extern crate cssparser;
pub extern crate ordermap;
pub extern crate smallvec;

Modules

domain

Contains definitions of objects used in Stylesheet.

selectors

Temporary fork of CSS selectors as the version we need is not yet available in https://crates.io.

servo_arc

Temporary fork of Servo Arc as the version we need to support CSS selectors is not yet available in https://crates.io.

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

CustomParseError

Represents all the things that can go wrong when parsing.

StylesheetError

Represents all the things that can go wrong when loading and saving stylesheets.

Functions

parse_css_selector

Parses a CSS selector