mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-08 10:44:56 +00:00
servo: Merge #14789 - Support @supports (from Manishearth:supports); r=SimonSapin
fixes #14786 cc @heycam @upsuper r? @SimonSapin Source-Repo: https://github.com/servo/servo Source-Revision: 50bba770d6073bba6e6e219dbc3687087a864a42
This commit is contained in:
parent
2ae1c9c70c
commit
59c175b443
@ -99,6 +99,7 @@ use style::media_queries::MediaList;
|
||||
use style::properties::PropertyDeclarationBlock;
|
||||
use style::selector_parser::{PseudoElement, Snapshot};
|
||||
use style::stylesheets::{CssRules, KeyframesRule, MediaRule, NamespaceRule, StyleRule, ImportRule};
|
||||
use style::stylesheets::SupportsRule;
|
||||
use style::values::specified::Length;
|
||||
use style::viewport::ViewportRule;
|
||||
use time::Duration;
|
||||
@ -531,6 +532,12 @@ unsafe impl JSTraceable for RwLock<ImportRule> {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl JSTraceable for RwLock<SupportsRule> {
|
||||
unsafe fn trace(&self, _trc: *mut JSTracer) {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl JSTraceable for RwLock<MediaRule> {
|
||||
unsafe fn trace(&self, _trc: *mut JSTracer) {
|
||||
// Do nothing.
|
||||
|
@ -2,11 +2,14 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use cssparser::serialize_identifier;
|
||||
use cssparser::{Parser, serialize_identifier};
|
||||
use dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
|
||||
use dom::bindings::error::Fallible;
|
||||
use dom::bindings::reflector::Reflector;
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::window::Window;
|
||||
use style::parser::ParserContext;
|
||||
use style::supports::{Declaration, parse_condition_or_declaration};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct CSS {
|
||||
@ -14,10 +17,31 @@ pub struct CSS {
|
||||
}
|
||||
|
||||
impl CSS {
|
||||
// http://dev.w3.org/csswg/cssom/#serialize-an-identifier
|
||||
/// http://dev.w3.org/csswg/cssom/#serialize-an-identifier
|
||||
pub fn Escape(_: &Window, ident: DOMString) -> Fallible<DOMString> {
|
||||
let mut escaped = String::new();
|
||||
serialize_identifier(&ident, &mut escaped).unwrap();
|
||||
Ok(DOMString::from(escaped))
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional/#dom-css-supports
|
||||
pub fn Supports(win: &Window, property: DOMString, value: DOMString) -> bool {
|
||||
let decl = Declaration { prop: property.into(), val: value.into() };
|
||||
let url = win.Document().url();
|
||||
let context = ParserContext::new_for_cssom(&url);
|
||||
decl.eval(&context)
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional/#dom-css-supports
|
||||
pub fn Supports_(win: &Window, condition: DOMString) -> bool {
|
||||
let mut input = Parser::new(&condition);
|
||||
let cond = parse_condition_or_declaration(&mut input);
|
||||
if let Ok(cond) = cond {
|
||||
let url = win.Document().url();
|
||||
let context = ParserContext::new_for_cssom(&url);
|
||||
cond.eval(&context)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
53
servo/components/script/dom/cssconditionrule.rs
Normal file
53
servo/components/script/dom/cssconditionrule.rs
Normal file
@ -0,0 +1,53 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::codegen::Bindings::CSSConditionRuleBinding::CSSConditionRuleMethods;
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::cssgroupingrule::CSSGroupingRule;
|
||||
use dom::cssmediarule::CSSMediaRule;
|
||||
use dom::cssstylesheet::CSSStyleSheet;
|
||||
use dom::csssupportsrule::CSSSupportsRule;
|
||||
use parking_lot::RwLock;
|
||||
use std::sync::Arc;
|
||||
use style::stylesheets::CssRules as StyleCssRules;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct CSSConditionRule {
|
||||
cssgroupingrule: CSSGroupingRule,
|
||||
}
|
||||
|
||||
impl CSSConditionRule {
|
||||
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet,
|
||||
rules: Arc<RwLock<StyleCssRules>>) -> CSSConditionRule {
|
||||
CSSConditionRule {
|
||||
cssgroupingrule: CSSGroupingRule::new_inherited(parent_stylesheet, rules),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl CSSConditionRuleMethods for CSSConditionRule {
|
||||
/// https://drafts.csswg.org/css-conditional-3/#dom-cssconditionrule-conditiontext
|
||||
fn ConditionText(&self) -> DOMString {
|
||||
if let Some(rule) = self.downcast::<CSSMediaRule>() {
|
||||
rule.get_condition_text()
|
||||
} else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
|
||||
rule.get_condition_text()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#dom-cssconditionrule-conditiontext
|
||||
fn SetConditionText(&self, text: DOMString) {
|
||||
if let Some(rule) = self.downcast::<CSSMediaRule>() {
|
||||
rule.set_condition_text(text)
|
||||
} else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
|
||||
rule.set_condition_text(text)
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
@ -2,24 +2,26 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use cssparser::Parser;
|
||||
use dom::bindings::codegen::Bindings::CSSMediaRuleBinding;
|
||||
use dom::bindings::codegen::Bindings::CSSMediaRuleBinding::CSSMediaRuleMethods;
|
||||
use dom::bindings::js::{MutNullableJS, Root};
|
||||
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::cssgroupingrule::CSSGroupingRule;
|
||||
use dom::cssconditionrule::CSSConditionRule;
|
||||
use dom::cssrule::SpecificCSSRule;
|
||||
use dom::cssstylesheet::CSSStyleSheet;
|
||||
use dom::medialist::MediaList;
|
||||
use dom::window::Window;
|
||||
use parking_lot::RwLock;
|
||||
use std::sync::Arc;
|
||||
use style::media_queries::parse_media_query_list;
|
||||
use style::stylesheets::MediaRule;
|
||||
use style_traits::ToCss;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct CSSMediaRule {
|
||||
cssrule: CSSGroupingRule,
|
||||
cssrule: CSSConditionRule,
|
||||
#[ignore_heap_size_of = "Arc"]
|
||||
mediarule: Arc<RwLock<MediaRule>>,
|
||||
medialist: MutNullableJS<MediaList>,
|
||||
@ -30,7 +32,7 @@ impl CSSMediaRule {
|
||||
-> CSSMediaRule {
|
||||
let list = mediarule.read().rules.clone();
|
||||
CSSMediaRule {
|
||||
cssrule: CSSGroupingRule::new_inherited(parent_stylesheet, list),
|
||||
cssrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
|
||||
mediarule: mediarule,
|
||||
medialist: MutNullableJS::new(None),
|
||||
}
|
||||
@ -48,6 +50,22 @@ impl CSSMediaRule {
|
||||
self.medialist.or_init(|| MediaList::new(self.global().as_window(),
|
||||
self.mediarule.read().media_queries.clone()))
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#the-cssmediarule-interface
|
||||
pub fn get_condition_text(&self) -> DOMString {
|
||||
let rule = self.mediarule.read();
|
||||
let list = rule.media_queries.read();
|
||||
list.to_css_string().into()
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#the-cssmediarule-interface
|
||||
pub fn set_condition_text(&self, text: DOMString) {
|
||||
let mut input = Parser::new(&text);
|
||||
let new_medialist = parse_media_query_list(&mut input);
|
||||
let rule = self.mediarule.read();
|
||||
let mut list = rule.media_queries.write();
|
||||
*list = new_medialist;
|
||||
}
|
||||
}
|
||||
|
||||
impl SpecificCSSRule for CSSMediaRule {
|
||||
|
@ -15,6 +15,7 @@ use dom::cssmediarule::CSSMediaRule;
|
||||
use dom::cssnamespacerule::CSSNamespaceRule;
|
||||
use dom::cssstylerule::CSSStyleRule;
|
||||
use dom::cssstylesheet::CSSStyleSheet;
|
||||
use dom::csssupportsrule::CSSSupportsRule;
|
||||
use dom::cssviewportrule::CSSViewportRule;
|
||||
use dom::window::Window;
|
||||
use std::cell::Cell;
|
||||
@ -59,6 +60,8 @@ impl CSSRule {
|
||||
rule as &SpecificCSSRule
|
||||
} else if let Some(rule) = self.downcast::<CSSImportRule>() {
|
||||
rule as &SpecificCSSRule
|
||||
} else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
|
||||
rule as &SpecificCSSRule
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
@ -77,6 +80,7 @@ impl CSSRule {
|
||||
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent_stylesheet, s)),
|
||||
StyleCssRule::Namespace(s) => Root::upcast(CSSNamespaceRule::new(window, parent_stylesheet, s)),
|
||||
StyleCssRule::Viewport(s) => Root::upcast(CSSViewportRule::new(window, parent_stylesheet, s)),
|
||||
StyleCssRule::Supports(s) => Root::upcast(CSSSupportsRule::new(window, parent_stylesheet, s)),
|
||||
}
|
||||
}
|
||||
|
||||
|
77
servo/components/script/dom/csssupportsrule.rs
Normal file
77
servo/components/script/dom/csssupportsrule.rs
Normal file
@ -0,0 +1,77 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use cssparser::Parser;
|
||||
use dom::bindings::codegen::Bindings::CSSSupportsRuleBinding;
|
||||
use dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
|
||||
use dom::bindings::js::Root;
|
||||
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||
use dom::bindings::str::DOMString;
|
||||
use dom::cssconditionrule::CSSConditionRule;
|
||||
use dom::cssrule::SpecificCSSRule;
|
||||
use dom::cssstylesheet::CSSStyleSheet;
|
||||
use dom::window::Window;
|
||||
use parking_lot::RwLock;
|
||||
use std::sync::Arc;
|
||||
use style::parser::ParserContext;
|
||||
use style::stylesheets::SupportsRule;
|
||||
use style::supports::SupportsCondition;
|
||||
use style_traits::ToCss;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct CSSSupportsRule {
|
||||
cssrule: CSSConditionRule,
|
||||
#[ignore_heap_size_of = "Arc"]
|
||||
supportsrule: Arc<RwLock<SupportsRule>>,
|
||||
}
|
||||
|
||||
impl CSSSupportsRule {
|
||||
fn new_inherited(parent_stylesheet: &CSSStyleSheet, supportsrule: Arc<RwLock<SupportsRule>>)
|
||||
-> CSSSupportsRule {
|
||||
let list = supportsrule.read().rules.clone();
|
||||
CSSSupportsRule {
|
||||
cssrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
|
||||
supportsrule: supportsrule,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unrooted_must_root)]
|
||||
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
|
||||
supportsrule: Arc<RwLock<SupportsRule>>) -> Root<CSSSupportsRule> {
|
||||
reflect_dom_object(box CSSSupportsRule::new_inherited(parent_stylesheet, supportsrule),
|
||||
window,
|
||||
CSSSupportsRuleBinding::Wrap)
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#the-csssupportsrule-interface
|
||||
pub fn get_condition_text(&self) -> DOMString {
|
||||
let rule = self.supportsrule.read();
|
||||
rule.condition.to_css_string().into()
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#the-csssupportsrule-interface
|
||||
pub fn set_condition_text(&self, text: DOMString) {
|
||||
let mut input = Parser::new(&text);
|
||||
let cond = SupportsCondition::parse(&mut input);
|
||||
if let Ok(cond) = cond {
|
||||
let url = self.global().as_window().Document().url();
|
||||
let context = ParserContext::new_for_cssom(&url);
|
||||
let enabled = cond.eval(&context);
|
||||
let mut rule = self.supportsrule.write();
|
||||
rule.condition = cond;
|
||||
rule.enabled = enabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SpecificCSSRule for CSSSupportsRule {
|
||||
fn ty(&self) -> u16 {
|
||||
use dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleConstants;
|
||||
CSSRuleConstants::SUPPORTS_RULE
|
||||
}
|
||||
|
||||
fn get_css(&self) -> DOMString {
|
||||
self.supportsrule.read().to_css_string().into()
|
||||
}
|
||||
}
|
@ -240,6 +240,7 @@ pub mod console;
|
||||
mod create;
|
||||
pub mod crypto;
|
||||
pub mod css;
|
||||
pub mod cssconditionrule;
|
||||
pub mod cssfontfacerule;
|
||||
pub mod cssgroupingrule;
|
||||
pub mod cssimportrule;
|
||||
@ -252,6 +253,7 @@ pub mod cssrulelist;
|
||||
pub mod cssstyledeclaration;
|
||||
pub mod cssstylerule;
|
||||
pub mod cssstylesheet;
|
||||
pub mod csssupportsrule;
|
||||
pub mod cssviewportrule;
|
||||
pub mod customevent;
|
||||
pub mod dedicatedworkerglobalscope;
|
||||
|
@ -11,3 +11,9 @@ interface CSS {
|
||||
[Throws]
|
||||
static DOMString escape(DOMString ident);
|
||||
};
|
||||
|
||||
// https://drafts.csswg.org/css-conditional-3/#the-css-interface
|
||||
partial interface CSS {
|
||||
static boolean supports(DOMString property, DOMString value);
|
||||
static boolean supports(DOMString conditionText);
|
||||
};
|
||||
|
@ -0,0 +1,9 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// https://drafts.csswg.org/css-conditional/#cssconditionrule
|
||||
[Abstract, Exposed=Window]
|
||||
interface CSSConditionRule : CSSGroupingRule {
|
||||
attribute DOMString conditionText;
|
||||
};
|
@ -3,7 +3,8 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// https://drafts.csswg.org/cssom/#the-cssmediarule-interface
|
||||
// https://drafts.csswg.org/css-conditional/#cssmediarule
|
||||
[Exposed=Window]
|
||||
interface CSSMediaRule : CSSGroupingRule {
|
||||
interface CSSMediaRule : CSSConditionRule {
|
||||
[SameObject, PutForwards=mediaText] readonly attribute MediaList media;
|
||||
};
|
||||
|
@ -31,3 +31,7 @@ partial interface CSSRule {
|
||||
const unsigned short VIEWPORT_RULE = 15;
|
||||
};
|
||||
|
||||
// https://drafts.csswg.org/css-conditional-3/#extentions-to-cssrule-interface
|
||||
partial interface CSSRule {
|
||||
const unsigned short SUPPORTS_RULE = 12;
|
||||
};
|
||||
|
@ -0,0 +1,8 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// https://drafts.csswg.org/css-conditional/#csssupportsrule
|
||||
[Exposed=Window]
|
||||
interface CSSSupportsRule : CSSConditionRule {
|
||||
};
|
@ -122,6 +122,7 @@ pub mod sequential;
|
||||
pub mod sink;
|
||||
pub mod str;
|
||||
pub mod stylesheets;
|
||||
pub mod supports;
|
||||
pub mod thread_state;
|
||||
pub mod timer;
|
||||
pub mod traversal;
|
||||
|
@ -11,7 +11,7 @@ use error_reporting::ParseErrorReporter;
|
||||
#[cfg(feature = "gecko")]
|
||||
use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
|
||||
use servo_url::ServoUrl;
|
||||
use stylesheets::Origin;
|
||||
use stylesheets::{MemoryHoleReporter, Origin};
|
||||
|
||||
/// Extra data that the style backend may need to parse stylesheets.
|
||||
#[cfg(not(feature = "gecko"))]
|
||||
@ -78,6 +78,11 @@ impl<'a> ParserContext<'a> {
|
||||
let extra_data = ParserContextExtraData::default();
|
||||
Self::new_with_extra_data(stylesheet_origin, base_url, error_reporter, extra_data)
|
||||
}
|
||||
|
||||
/// Create a parser context for on-the-fly parsing in CSSOM
|
||||
pub fn new_for_cssom(base_url: &'a ServoUrl) -> ParserContext<'a> {
|
||||
Self::new(Origin::User, base_url, Box::new(MemoryHoleReporter))
|
||||
}
|
||||
}
|
||||
|
||||
/// Defaults to a no-op.
|
||||
|
@ -28,6 +28,7 @@ use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use style_traits::ToCss;
|
||||
use stylist::FnvHashMap;
|
||||
use supports::SupportsCondition;
|
||||
use values::specified::url::SpecifiedUrl;
|
||||
use viewport::ViewportRule;
|
||||
|
||||
@ -215,6 +216,7 @@ pub enum CssRule {
|
||||
FontFace(Arc<RwLock<FontFaceRule>>),
|
||||
Viewport(Arc<RwLock<ViewportRule>>),
|
||||
Keyframes(Arc<RwLock<KeyframesRule>>),
|
||||
Supports(Arc<RwLock<SupportsRule>>),
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
@ -274,6 +276,7 @@ impl CssRule {
|
||||
CssRule::Keyframes(_) => CssRuleType::Keyframes,
|
||||
CssRule::Namespace(_) => CssRuleType::Namespace,
|
||||
CssRule::Viewport(_) => CssRuleType::Viewport,
|
||||
CssRule::Supports(_) => CssRuleType::Supports,
|
||||
}
|
||||
}
|
||||
|
||||
@ -290,9 +293,10 @@ impl CssRule {
|
||||
///
|
||||
/// Note that only some types of rules can contain rules. An empty slice is
|
||||
/// used for others.
|
||||
///
|
||||
/// This will not recurse down unsupported @supports rules
|
||||
pub fn with_nested_rules_and_mq<F, R>(&self, mut f: F) -> R
|
||||
where F: FnMut(&[CssRule], Option<&MediaList>) -> R
|
||||
{
|
||||
where F: FnMut(&[CssRule], Option<&MediaList>) -> R {
|
||||
match *self {
|
||||
CssRule::Import(ref lock) => {
|
||||
let rule = lock.read();
|
||||
@ -315,6 +319,16 @@ impl CssRule {
|
||||
let rules = &media_rule.rules.read().0;
|
||||
f(rules, Some(&mq))
|
||||
}
|
||||
CssRule::Supports(ref lock) => {
|
||||
let supports_rule = lock.read();
|
||||
let enabled = supports_rule.enabled;
|
||||
if enabled {
|
||||
let rules = &supports_rule.rules.read().0;
|
||||
f(rules, None)
|
||||
} else {
|
||||
f(&[], None)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -367,6 +381,7 @@ impl ToCss for CssRule {
|
||||
CssRule::Viewport(ref lock) => lock.read().to_css(dest),
|
||||
CssRule::Keyframes(ref lock) => lock.read().to_css(dest),
|
||||
CssRule::Media(ref lock) => lock.read().to_css(dest),
|
||||
CssRule::Supports(ref lock) => lock.read().to_css(dest),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -441,7 +456,12 @@ impl ToCss for KeyframesRule {
|
||||
try!(dest.write_str(&*self.name.to_string()));
|
||||
try!(dest.write_str(" { "));
|
||||
let iter = self.keyframes.iter();
|
||||
let mut first = true;
|
||||
for lock in iter {
|
||||
if !first {
|
||||
try!(dest.write_str(" "));
|
||||
}
|
||||
first = false;
|
||||
let keyframe = lock.read();
|
||||
try!(keyframe.to_css(dest));
|
||||
}
|
||||
@ -460,9 +480,34 @@ impl ToCss for MediaRule {
|
||||
// Serialization of MediaRule is not specced.
|
||||
// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
try!(dest.write_str("@media ("));
|
||||
try!(dest.write_str("@media "));
|
||||
try!(self.media_queries.read().to_css(dest));
|
||||
try!(dest.write_str(") {"));
|
||||
try!(dest.write_str(" {"));
|
||||
for rule in self.rules.read().0.iter() {
|
||||
try!(dest.write_str(" "));
|
||||
try!(rule.to_css(dest));
|
||||
}
|
||||
dest.write_str(" }")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
/// An @supports rule
|
||||
pub struct SupportsRule {
|
||||
/// The parsed condition
|
||||
pub condition: SupportsCondition,
|
||||
/// Child rules
|
||||
pub rules: Arc<RwLock<CssRules>>,
|
||||
/// The result of evaluating the condition
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
impl ToCss for SupportsRule {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
try!(dest.write_str("@supports "));
|
||||
try!(self.condition.to_css(dest));
|
||||
try!(dest.write_str(" {"));
|
||||
for rule in self.rules.read().0.iter() {
|
||||
try!(dest.write_str(" "));
|
||||
try!(rule.to_css(dest));
|
||||
@ -712,6 +757,7 @@ rule_filter! {
|
||||
effective_font_face_rules(FontFace => FontFaceRule),
|
||||
effective_viewport_rules(Viewport => ViewportRule),
|
||||
effective_keyframes_rules(Keyframes => KeyframesRule),
|
||||
effective_supports_rules(Supports => SupportsRule),
|
||||
}
|
||||
|
||||
/// The stylesheet loader is the abstraction used to trigger network requests
|
||||
@ -758,6 +804,8 @@ enum AtRulePrelude {
|
||||
FontFace,
|
||||
/// A @media rule prelude, with its media queries.
|
||||
Media(Arc<RwLock<MediaList>>),
|
||||
/// An @supports rule, with its conditional
|
||||
Supports(SupportsCondition),
|
||||
/// A @viewport rule prelude.
|
||||
Viewport,
|
||||
/// A @keyframes rule, with its animation name.
|
||||
@ -913,6 +961,10 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> {
|
||||
let media_queries = parse_media_query_list(input);
|
||||
Ok(AtRuleType::WithBlock(AtRulePrelude::Media(Arc::new(RwLock::new(media_queries)))))
|
||||
},
|
||||
"supports" => {
|
||||
let cond = SupportsCondition::parse(input)?;
|
||||
Ok(AtRuleType::WithBlock(AtRulePrelude::Supports(cond)))
|
||||
},
|
||||
"font-face" => {
|
||||
Ok(AtRuleType::WithBlock(AtRulePrelude::FontFace))
|
||||
},
|
||||
@ -949,6 +1001,14 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> {
|
||||
rules: self.parse_nested_rules(input),
|
||||
}))))
|
||||
}
|
||||
AtRulePrelude::Supports(cond) => {
|
||||
let enabled = cond.eval(self.context);
|
||||
Ok(CssRule::Supports(Arc::new(RwLock::new(SupportsRule {
|
||||
condition: cond,
|
||||
rules: self.parse_nested_rules(input),
|
||||
enabled: enabled,
|
||||
}))))
|
||||
}
|
||||
AtRulePrelude::Viewport => {
|
||||
Ok(CssRule::Viewport(Arc::new(RwLock::new(
|
||||
try!(ViewportRule::parse(input, self.context))))))
|
||||
|
@ -408,14 +408,15 @@ impl Stylist {
|
||||
|
||||
fn mq_eval_changed(rules: &[CssRule], before: &Device, after: &Device) -> bool {
|
||||
for rule in rules {
|
||||
if rule.with_nested_rules_and_mq(|rules, mq| {
|
||||
let changed = rule.with_nested_rules_and_mq(|rules, mq| {
|
||||
if let Some(mq) = mq {
|
||||
if mq.evaluate(before) != mq.evaluate(after) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
mq_eval_changed(rules, before, after)
|
||||
}) {
|
||||
});
|
||||
if changed {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
231
servo/components/style/supports.rs
Normal file
231
servo/components/style/supports.rs
Normal file
@ -0,0 +1,231 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! [@supports rules](https://drafts.csswg.org/css-conditional-3/#at-supports)
|
||||
|
||||
use cssparser::{parse_important, Parser, Token};
|
||||
use parser::ParserContext;
|
||||
use properties::{PropertyDeclaration, PropertyId};
|
||||
use std::fmt;
|
||||
use style_traits::ToCss;
|
||||
|
||||
#[derive(Debug)]
|
||||
/// An @supports condition
|
||||
///
|
||||
/// https://drafts.csswg.org/css-conditional-3/#at-supports
|
||||
pub enum SupportsCondition {
|
||||
/// `not (condition)`
|
||||
Not(Box<SupportsCondition>),
|
||||
/// `(condition)`
|
||||
Parenthesized(Box<SupportsCondition>),
|
||||
/// `(condition) and (condition) and (condition) ..`
|
||||
And(Vec<SupportsCondition>),
|
||||
/// `(condition) or (condition) or (condition) ..`
|
||||
Or(Vec<SupportsCondition>),
|
||||
/// `property-ident: value` (value can be any tokens)
|
||||
Declaration(Declaration),
|
||||
/// `(any tokens)` or `func(any tokens)`
|
||||
FutureSyntax(String),
|
||||
}
|
||||
|
||||
impl SupportsCondition {
|
||||
/// Parse a condition
|
||||
///
|
||||
/// https://drafts.csswg.org/css-conditional/#supports_condition
|
||||
pub fn parse(input: &mut Parser) -> Result<SupportsCondition, ()> {
|
||||
if let Ok(_) = input.try(|i| i.expect_ident_matching("not")) {
|
||||
let inner = SupportsCondition::parse_in_parens(input)?;
|
||||
return Ok(SupportsCondition::Not(Box::new(inner)));
|
||||
}
|
||||
|
||||
let in_parens = SupportsCondition::parse_in_parens(input)?;
|
||||
|
||||
let (keyword, wrapper) = match input.next() {
|
||||
Err(()) => {
|
||||
// End of input
|
||||
return Ok(in_parens)
|
||||
}
|
||||
Ok(Token::Ident(ident)) => {
|
||||
match_ignore_ascii_case! { ident,
|
||||
"and" => ("and", SupportsCondition::And as fn(_) -> _),
|
||||
"or" => ("or", SupportsCondition::Or as fn(_) -> _),
|
||||
_ => return Err(())
|
||||
}
|
||||
}
|
||||
_ => return Err(())
|
||||
};
|
||||
|
||||
let mut conditions = Vec::with_capacity(2);
|
||||
conditions.push(in_parens);
|
||||
loop {
|
||||
conditions.push(SupportsCondition::parse_in_parens(input)?);
|
||||
if input.try(|input| input.expect_ident_matching(keyword)).is_err() {
|
||||
// Did not find the expected keyword.
|
||||
// If we found some other token,
|
||||
// it will be rejected by `Parser::parse_entirely` somewhere up the stack.
|
||||
return Ok(wrapper(conditions))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// https://drafts.csswg.org/css-conditional-3/#supports_condition_in_parens
|
||||
fn parse_in_parens(input: &mut Parser) -> Result<SupportsCondition, ()> {
|
||||
// Whitespace is normally taken care of in `Parser::next`,
|
||||
// but we want to not include it in `pos` for the SupportsCondition::FutureSyntax cases.
|
||||
while input.try(Parser::expect_whitespace).is_ok() {}
|
||||
let pos = input.position();
|
||||
match input.next()? {
|
||||
Token::ParenthesisBlock => {
|
||||
input.parse_nested_block(|input| {
|
||||
// `input.try()` not needed here since the alternative uses `consume_all()`.
|
||||
parse_condition_or_declaration(input).or_else(|()| {
|
||||
consume_all(input);
|
||||
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
|
||||
})
|
||||
})
|
||||
}
|
||||
Token::Function(_) => {
|
||||
input.parse_nested_block(|i| Ok(consume_all(i))).unwrap();
|
||||
Ok(SupportsCondition::FutureSyntax(input.slice_from(pos).to_owned()))
|
||||
}
|
||||
_ => Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate a supports condition
|
||||
pub fn eval(&self, cx: &ParserContext) -> bool {
|
||||
match *self {
|
||||
SupportsCondition::Not(ref cond) => !cond.eval(cx),
|
||||
SupportsCondition::Parenthesized(ref cond) => cond.eval(cx),
|
||||
SupportsCondition::And(ref vec) => vec.iter().all(|c| c.eval(cx)),
|
||||
SupportsCondition::Or(ref vec) => vec.iter().any(|c| c.eval(cx)),
|
||||
SupportsCondition::Declaration(ref decl) => decl.eval(cx),
|
||||
SupportsCondition::FutureSyntax(_) => false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// supports_condition | declaration
|
||||
/// https://drafts.csswg.org/css-conditional/#dom-css-supports-conditiontext-conditiontext
|
||||
pub fn parse_condition_or_declaration(input: &mut Parser) -> Result<SupportsCondition, ()> {
|
||||
if let Ok(condition) = input.try(SupportsCondition::parse) {
|
||||
Ok(SupportsCondition::Parenthesized(Box::new(condition)))
|
||||
} else {
|
||||
Declaration::parse(input).map(SupportsCondition::Declaration)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToCss for SupportsCondition {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
||||
where W: fmt::Write {
|
||||
match *self {
|
||||
SupportsCondition::Not(ref cond) => {
|
||||
dest.write_str("not ")?;
|
||||
cond.to_css(dest)
|
||||
}
|
||||
SupportsCondition::Parenthesized(ref cond) => {
|
||||
dest.write_str("(")?;
|
||||
cond.to_css(dest)?;
|
||||
dest.write_str(")")
|
||||
}
|
||||
SupportsCondition::And(ref vec) => {
|
||||
let mut first = true;
|
||||
for cond in vec {
|
||||
if !first {
|
||||
dest.write_str(" and ")?;
|
||||
}
|
||||
first = false;
|
||||
cond.to_css(dest)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
SupportsCondition::Or(ref vec) => {
|
||||
let mut first = true;
|
||||
for cond in vec {
|
||||
if !first {
|
||||
dest.write_str(" or ")?;
|
||||
}
|
||||
first = false;
|
||||
cond.to_css(dest)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
SupportsCondition::Declaration(ref decl) => {
|
||||
dest.write_str("(")?;
|
||||
decl.to_css(dest)?;
|
||||
dest.write_str(")")
|
||||
}
|
||||
SupportsCondition::FutureSyntax(ref s) => dest.write_str(&s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// A possibly-invalid property declaration
|
||||
pub struct Declaration {
|
||||
/// The property name
|
||||
pub prop: String,
|
||||
/// The property value
|
||||
pub val: String,
|
||||
}
|
||||
|
||||
impl ToCss for Declaration {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
||||
where W: fmt::Write {
|
||||
dest.write_str(&self.prop)?;
|
||||
dest.write_str(":")?;
|
||||
// no space, the `val` already contains any possible spaces
|
||||
dest.write_str(&self.val)
|
||||
}
|
||||
}
|
||||
|
||||
/// Slurps up input till exhausted, return string from source position
|
||||
fn parse_anything(input: &mut Parser) -> String {
|
||||
let pos = input.position();
|
||||
consume_all(input);
|
||||
input.slice_from(pos).to_owned()
|
||||
}
|
||||
|
||||
/// consume input till done
|
||||
fn consume_all(input: &mut Parser) {
|
||||
while let Ok(_) = input.next() {}
|
||||
}
|
||||
|
||||
impl Declaration {
|
||||
/// Parse a declaration
|
||||
pub fn parse(input: &mut Parser) -> Result<Declaration, ()> {
|
||||
let prop = input.expect_ident()?.into_owned();
|
||||
input.expect_colon()?;
|
||||
let val = parse_anything(input);
|
||||
Ok(Declaration { prop: prop, val: val })
|
||||
}
|
||||
|
||||
/// Determine if a declaration parses
|
||||
///
|
||||
/// https://drafts.csswg.org/css-conditional-3/#support-definition
|
||||
pub fn eval(&self, cx: &ParserContext) -> bool {
|
||||
use properties::PropertyDeclarationParseResult::*;
|
||||
let id = if let Ok(id) = PropertyId::parse((&*self.prop).into()) {
|
||||
id
|
||||
} else {
|
||||
return false
|
||||
};
|
||||
let mut input = Parser::new(&self.val);
|
||||
let mut list = Vec::new();
|
||||
let res = PropertyDeclaration::parse(id, cx, &mut input,
|
||||
&mut list, /* in_keyframe */ false);
|
||||
let _ = input.try(parse_important);
|
||||
if !input.is_exhausted() {
|
||||
return false;
|
||||
}
|
||||
match res {
|
||||
UnknownProperty => false,
|
||||
ExperimentalProperty => false, // only happens for experimental props
|
||||
// that haven't been enabled
|
||||
InvalidValue => false,
|
||||
AnimationPropertyInKeyframeBlock => unreachable!(),
|
||||
ValidOrIgnoredDeclaration => true,
|
||||
}
|
||||
}
|
||||
}
|
@ -185,3 +185,9 @@ impl Index<Range<Position>> for ServoUrl {
|
||||
&self.0[range]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Url> for ServoUrl {
|
||||
fn from(url: Url) -> Self {
|
||||
ServoUrl::from_url(url)
|
||||
}
|
||||
}
|
||||
|
@ -276,6 +276,11 @@ class MachCommands(CommandBase):
|
||||
args += ["%s" % ' '.join(features + ["testing"])]
|
||||
else:
|
||||
args += ["testing"]
|
||||
|
||||
args += test_patterns
|
||||
|
||||
if nocapture:
|
||||
args += ["--", "--nocapture"]
|
||||
return call(args, env=env, cwd=self.servo_crate())
|
||||
|
||||
@Command('test-stylo',
|
||||
|
@ -1 +1 @@
|
||||
1.12.0
|
||||
1.13.0
|
||||
|
@ -39,6 +39,25 @@ macro_rules! assert_roundtrip_with_context {
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! assert_roundtrip {
|
||||
($fun:expr, $string:expr) => {
|
||||
assert_roundtrip!($fun, $string, $string);
|
||||
};
|
||||
($fun:expr,$input:expr, $output:expr) => {
|
||||
let mut parser = Parser::new($input);
|
||||
let parsed = $fun(&mut parser)
|
||||
.expect(&format!("Failed to parse {}", $input));
|
||||
let serialized = ToCss::to_css_string(&parsed);
|
||||
assert_eq!(serialized, $output);
|
||||
|
||||
let mut parser = Parser::new(&serialized);
|
||||
let re_parsed = $fun(&mut parser)
|
||||
.expect(&format!("Failed to parse serialization {}", $input));
|
||||
let re_serialized = ToCss::to_css_string(&re_parsed);
|
||||
assert_eq!(serialized, re_serialized);
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! parse_longhand {
|
||||
($name:ident, $s:expr) => {{
|
||||
let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
|
||||
@ -58,3 +77,4 @@ mod inherited_text;
|
||||
mod mask;
|
||||
mod position;
|
||||
mod selectors;
|
||||
mod supports;
|
||||
|
14
servo/tests/unit/style/parsing/supports.rs
Normal file
14
servo/tests/unit/style/parsing/supports.rs
Normal file
@ -0,0 +1,14 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use cssparser::Parser;
|
||||
use style::supports::SupportsCondition;
|
||||
use style_traits::ToCss;
|
||||
|
||||
#[test]
|
||||
fn test_supports_condition() {
|
||||
assert_roundtrip!(SupportsCondition::parse, "(margin: 1px)");
|
||||
assert_roundtrip!(SupportsCondition::parse, "not (--be: to be)");
|
||||
assert_roundtrip!(SupportsCondition::parse, "(color: blue) and future-extension(4)");
|
||||
}
|
Loading…
Reference in New Issue
Block a user