css-mediaquery 0.1.1

Parse CSS media queries and test them against a context (width, resolution, orientation, …) — a faithful port of the css-mediaquery npm package. Zero dependencies.
Documentation
//! End-to-end behavioral spec for `css-mediaquery`.
//!
//! Expected values are cross-checked against the `css-mediaquery` npm package.

use css_mediaquery::{matches, parse, Context, Modifier};

// ---------------------------------------------------------------------------
// parse() — AST
// ---------------------------------------------------------------------------

#[test]
fn parse_ast() {
    let q = parse("screen and (min-width: 600px), print and (orientation: landscape)");
    assert_eq!(q.len(), 2);
    assert!(!q[0].inverse);
    assert_eq!(q[0].media_type, "screen");
    assert_eq!(q[0].expressions[0].modifier, Some(Modifier::Min));
    assert_eq!(q[0].expressions[0].feature, "width");
    assert_eq!(q[0].expressions[0].value.as_deref(), Some("600px"));
    assert_eq!(q[1].media_type, "print");
    assert_eq!(q[1].expressions[0].modifier, None);
    assert_eq!(q[1].expressions[0].feature, "orientation");
}

#[test]
fn parse_defaults_type_to_all() {
    let q = parse("(min-width: 600px)");
    assert_eq!(q[0].media_type, "all");
    assert!(!q[0].inverse);
}

#[test]
fn parse_not_and_only() {
    assert!(parse("not screen and (min-width: 600px)")[0].inverse);
    assert!(!parse("only screen and (min-width: 600px)")[0].inverse);
    assert_eq!(parse("only screen")[0].media_type, "screen");
}

#[test]
fn parse_boolean_feature_has_no_value() {
    let q = parse("(color)");
    assert_eq!(q[0].expressions[0].feature, "color");
    assert_eq!(q[0].expressions[0].value, None);
}

// ---------------------------------------------------------------------------
// matches() — width / type
// ---------------------------------------------------------------------------

#[test]
fn matches_width_and_type() {
    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));
    assert!(matches("screen and (max-width: 700px)", &ctx));
    assert!(matches("(width: 640px)", &ctx)); // exact, no type
    assert!(!matches("print", &ctx)); // type mismatch
    assert!(matches("screen", &ctx)); // bare type
}

#[test]
fn comma_is_or() {
    let ctx = Context::new().media_type("screen").width(500.0);
    assert!(matches("(min-width: 800px), (max-width: 600px)", &ctx));
}

// ---------------------------------------------------------------------------
// matches() — units
// ---------------------------------------------------------------------------

#[test]
fn length_units_convert_like_css_mediaquery() {
    // 30em = 480px
    assert!(matches("(min-width: 30em)", &Context::new().width(481.0)));
    assert!(!matches("(min-width: 30em)", &Context::new().width(479.0)));
    // 1in = 96px
    assert!(matches("(min-width: 1in)", &Context::new().width(96.0)));
    // 2.54cm = 96px
    assert!(matches("(min-width: 2.54cm)", &Context::new().width(96.0)));
}

#[test]
fn resolution_units() {
    // 2dppx = 192dpi
    assert!(matches(
        "(min-resolution: 2dppx)",
        &Context::new().resolution(192.0)
    ));
    assert!(!matches(
        "(min-resolution: 2dppx)",
        &Context::new().resolution(96.0)
    ));
    assert!(matches(
        "(min-resolution: 100dpi)",
        &Context::new().resolution(150.0)
    ));
}

#[test]
fn aspect_ratio_is_decimal() {
    assert!(matches(
        "(min-aspect-ratio: 4/3)",
        &Context::new().aspect_ratio(16.0 / 9.0)
    ));
    assert!(!matches(
        "(min-aspect-ratio: 16/9)",
        &Context::new().aspect_ratio(4.0 / 3.0)
    ));
}

// ---------------------------------------------------------------------------
// matches() — string features & quirks
// ---------------------------------------------------------------------------

#[test]
fn orientation_must_be_explicit() {
    // orientation/aspect-ratio are NOT derived from width/height (css-mediaquery quirk)
    assert!(!matches(
        "(orientation: landscape)",
        &Context::new().width(800.0).height(600.0)
    ));
    assert!(matches(
        "(orientation: landscape)",
        &Context::new().orientation("landscape")
    ));
    assert!(matches(
        "(orientation: LANDSCAPE)",
        &Context::new().orientation("landscape")
    )); // case-insensitive
}

#[test]
fn missing_or_zero_values_do_not_match() {
    // no width in context → width expression is false
    assert!(!matches(
        "(min-width: 100px)",
        &Context::new().media_type("screen")
    ));
    // zero is falsy
    assert!(!matches("(min-width: 0px)", &Context::new().width(0.0)));
}

#[test]
fn not_logic_matches_css_mediaquery() {
    // "not screen and (min-width:600)" with a matching screen type quits early → false
    let ctx = Context::new().media_type("screen").width(500.0);
    assert!(!matches("not screen and (min-width: 600px)", &ctx));
    // with a non-matching type + not + a false expression → true (faithful quirk)
    let screen = Context::new().media_type("screen").width(500.0);
    assert!(matches("not print and (min-width: 600px)", &screen));
    // `not all` quits early on the always-matching type → always false
    assert!(!matches(
        "not all and (min-width: 800px)",
        &Context::new().width(500.0)
    ));
}

// Regression: head parsing must mirror css-mediaquery's RE_MEDIA_QUERY exactly.
#[test]
fn head_parsing_matches_reference() {
    // glued `not`/`only` prefix
    let q = parse("notscreen");
    assert!(q[0].inverse && q[0].media_type == "screen");
    let q = parse("onlyscreen");
    assert!(!q[0].inverse && q[0].media_type == "screen");
    // a lone keyword is itself the type
    assert_eq!(parse("only")[0].media_type, "only");
    assert!(!parse("only")[0].inverse);
    assert_eq!(parse("not")[0].media_type, "not");
    assert!(!parse("not")[0].inverse);
    // a stray `)` is excluded from the type
    assert_eq!(parse("screen) and (color)")[0].media_type, "screen");
    assert_eq!(parse(")")[0].media_type, "all");
    // match results align with the reference
    assert!(matches("onlyscreen", &Context::new().media_type("screen")));
    assert!(!matches("only", &Context::new().media_type("screen")));
}

// Regression: unit case-sensitivity + exponent parsing match the reference.
#[test]
fn unit_quirks_match_reference() {
    // uppercase units are treated as unitless (so `10IN` == 10px, not 960px)
    assert!(matches("(min-width: 10IN)", &Context::new().width(200.0)));
    assert!(!matches("(min-width: 10in)", &Context::new().width(200.0)));
    // exponent notation parses like parseFloat (`1e3px` == 1000px)
    assert!(matches("(max-width: 1e3px)", &Context::new().width(500.0)));
    assert!(!matches("(min-width: 1e3px)", &Context::new().width(500.0)));
}

#[test]
fn count_features() {
    assert!(matches("(min-color: 4)", &Context::new().color(8)));
    assert!(!matches("(min-color: 4)", &Context::new().color(2)));
    assert!(matches("(monochrome)", &Context::new().monochrome(1))); // boolean → defaults to 1
    assert!(!matches("(monochrome)", &Context::new().monochrome(2)));
}