Skip to main content

Crate css_mediaquery

Crate css_mediaquery 

Source
Expand description

§css-mediaquery — parse and match CSS media queries

Parse a CSS media query string into an AST and test it against a context (viewport width, resolution, orientation, …) — a headless matchMedia for servers and tooling. A faithful Rust port of the css-mediaquery npm package.

use css_mediaquery::{matches, Context};

let ctx = Context::new().media_type("screen").width(640.0);
assert!(matches("screen and (min-width: 600px)", &ctx));
assert!(!matches("screen and (min-width: 800px)", &ctx));

Lengths in the query are converted to pixels exactly as css-mediaquery does (1em = 1rem = 16px, 1in = 96px, …); resolutions to dpi (1dppx = 96dpi). A feature only matches when the context supplies a (non-zero) value for it — orientation and aspect-ratio are not derived from width/height.

Structs§

Context
The device/viewport context a query is tested against. Build it with the setters; any feature left unset never matches.
Expression
A single (feature: value) test within a query.
Query
One comma-separated media query: an optional not, a media type, and a list of feature expressions.

Enums§

Modifier
Whether a feature is compared with min-, max-, or exactly.

Functions§

matches
Test whether query matches context.
parse
Parse a media query string into a list of Query (one per comma).