/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
impl MediaType { /// The `screen` media type. pubfn screen() -> Self {
MediaType(CustomIdent(atom!("screen")))
}
/// The `print` media type. pubfn print() -> Self {
MediaType(CustomIdent(atom!("print")))
}
fn parse(name: &str) -> Result<Self, ()> { // From https://drafts.csswg.org/mediaqueries/#mq-syntax: // // The <media-type> production does not include the keywords only, not, and, or, and layer. // // Here we also perform the to-ascii-lowercase part of the serialization // algorithm: https://drafts.csswg.org/cssom/#serializing-media-queries
match_ignore_ascii_case! { name, "not" | "or" | "and" | "only" | "layer" => Err(()),
_ => Ok(MediaType(CustomIdent(Atom::from(string_as_ascii_lowercase(name))))),
}
}
}
/// A [media query][mq]. /// /// [mq]: https://drafts.csswg.org/mediaqueries/ #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToShmem)] pubstruct MediaQuery { /// The qualifier for this query. pub qualifier: Option<Qualifier>, /// The media type for this query, that can be known, unknown, or "all". pub media_type: MediaQueryType, /// The condition that this media query contains. This cannot have `or` /// in the first level. pub condition: Option<QueryCondition>,
}
matchself.media_type {
MediaQueryType::All => { // We need to print "all" if there's a qualifier, or there's // just an empty list of expressions. // // Otherwise, we'd serialize media queries like "(min-width: // 40px)" in "all (min-width: 40px)", which is unexpected. ifself.qualifier.is_some() || self.condition.is_none() {
dest.write_str("all")?;
}
},
MediaQueryType::Concrete(MediaType(ref desc)) => desc.to_css(dest)?,
}
let condition = matchself.condition {
Some(ref c) => c,
None => return Ok(()),
};
ifself.media_type != MediaQueryType::All || self.qualifier.is_some() {
dest.write_str(" and ")?;
}
condition.to_css(dest)
}
}
impl MediaQuery { /// Return a media query that never matches, used for when we fail to parse /// a given media query. pubfn never_matching() -> Self { Self {
qualifier: Some(Qualifier::Not),
media_type: MediaQueryType::All,
condition: None,
}
}
/// Returns whether this media query depends on the viewport. pubfn is_viewport_dependent(&self) -> bool { self.condition.as_ref().map_or(false, |c| { return c
.cumulative_flags()
.contains(FeatureFlags::VIEWPORT_DEPENDENT);
})
}
/// Parse a media query given css input. /// /// Returns an error if any of the expressions is unknown. pubfn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> { let (qualifier, explicit_media_type) = input
.try_parse(|input| -> Result<_, ()> { let qualifier = input.try_parse(Qualifier::parse).ok(); let ident = input.expect_ident().map_err(|_| ())?; let media_type = MediaQueryType::parse(&ident)?;
Ok((qualifier, Some(media_type)))
})
.unwrap_or_default();
/// <http://dev.w3.org/csswg/mediaqueries-3/#media0> #[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)] pubenum MediaQueryType { /// A media type that matches every device.
All, /// A specific media type.
Concrete(MediaType),
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.