mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Auto merge of #14789 - Manishearth:supports, r=SimonSapin
Support @supports fixes #14786 cc @heycam @upsuper r? @SimonSapin <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14789) <!-- Reviewable:end -->
This commit is contained in:
commit
50bba770d6
98 changed files with 638 additions and 218 deletions
|
@ -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
components/script/dom/cssconditionrule.rs
Normal file
53
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
components/script/dom/csssupportsrule.rs
Normal file
77
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);
|
||||
};
|
||||
|
|
9
components/script/dom/webidls/CSSConditionRule.webidl
Normal file
9
components/script/dom/webidls/CSSConditionRule.webidl
Normal file
|
@ -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;
|
||||
};
|
||||
|
|
8
components/script/dom/webidls/CSSSupportsRule.webidl
Normal file
8
components/script/dom/webidls/CSSSupportsRule.webidl
Normal file
|
@ -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 {
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue