BarrierLab

media-query-parse

The CSS Media Queries Level 4 grammar in Rust, including range syntax; parses, does not evaluate.

Parses a media query, or a comma-separated list of them, into an AST and reports whether it is syntactically valid. It does not decide whether a query matches a device or viewport. Norm: CSS Syntax Level 3 (tokenization, via css-tokenize) and Media Queries Level 4 (grammar, including the <mf-range> comparison syntax such as (400px <= width <= 700px)).

Status

On crates.io. Used by html-conform (the media datatype) and style-cascade, which evaluates the parsed queries against a viewport it is given.

Measured against the media-query cases of web-platform-tests (test_media_queries.html and match-media-parsing.html, vendored through xtask/vendor-wpt-mediaqueries.sh): every extracted case passes, tests/wpt_known_failures.txt is empty.

Structure

media query text

css-tokenize

parse_media_query

parse_media_query_list

MediaQuery AST

ParseError

Public surface

EntryPurpose
parse_media_queryone query → Result<MediaQuery, ParseError>
parse_media_query_listcomma-separated list → one Result per entry, so one invalid entry does not hide the others
MediaQuery, MediaQueryList, MediaType, MediaModifiertype queries (not/only) and condition queries
MediaCondition, MediaInParens, MediaFeature, MfName, MfValue, MfRange, MfComparisonconditions, plain, boolean and range features
GeneralEnclosedthe forward-compatible fallback block, kept as raw tokens
ParseErrorwhat the grammar expected and what it found

The public API is documented (#![deny(missing_docs)]) and #[non_exhaustive] where the grammar may grow.

Dependencies

Downward: css-tokenize (L0). Upward: html-conform (L1) and style-cascade (L2). media_query_parse::tokenizer is a hidden re-export of css-tokenize, kept for old code.

Limits

  • No evaluation. Whether a query matches is not decided here; the host or style-cascade does that with the viewport it knows.
  • Grammar, not vocabulary. The parser checks structure; it is not a list of the media features a browser knows.
  • <general-enclosed> is kept, not interpreted. Unknown but well-formed blocks parse, as the forward-compatible grammar demands.

Edit this page on GitHub