mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
CSSOM bits for @supports: CSSConditionRule and CSSSupportsRule, with conditionText attribute
This commit is contained in:
parent
1b0842e228
commit
b4a83b6cec
12 changed files with 199 additions and 5 deletions
|
@ -99,6 +99,7 @@ use style::media_queries::MediaList;
|
||||||
use style::properties::PropertyDeclarationBlock;
|
use style::properties::PropertyDeclarationBlock;
|
||||||
use style::selector_parser::{PseudoElement, Snapshot};
|
use style::selector_parser::{PseudoElement, Snapshot};
|
||||||
use style::stylesheets::{CssRules, KeyframesRule, MediaRule, NamespaceRule, StyleRule, ImportRule};
|
use style::stylesheets::{CssRules, KeyframesRule, MediaRule, NamespaceRule, StyleRule, ImportRule};
|
||||||
|
use style::stylesheets::SupportsRule;
|
||||||
use style::values::specified::Length;
|
use style::values::specified::Length;
|
||||||
use style::viewport::ViewportRule;
|
use style::viewport::ViewportRule;
|
||||||
use time::Duration;
|
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 impl JSTraceable for RwLock<MediaRule> {
|
||||||
unsafe fn trace(&self, _trc: *mut JSTracer) {
|
unsafe fn trace(&self, _trc: *mut JSTracer) {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
|
|
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
|
* 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/. */
|
* 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;
|
||||||
use dom::bindings::codegen::Bindings::CSSMediaRuleBinding::CSSMediaRuleMethods;
|
use dom::bindings::codegen::Bindings::CSSMediaRuleBinding::CSSMediaRuleMethods;
|
||||||
use dom::bindings::js::{MutNullableJS, Root};
|
use dom::bindings::js::{MutNullableJS, Root};
|
||||||
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
use dom::cssgroupingrule::CSSGroupingRule;
|
use dom::cssconditionrule::CSSConditionRule;
|
||||||
use dom::cssrule::SpecificCSSRule;
|
use dom::cssrule::SpecificCSSRule;
|
||||||
use dom::cssstylesheet::CSSStyleSheet;
|
use dom::cssstylesheet::CSSStyleSheet;
|
||||||
use dom::medialist::MediaList;
|
use dom::medialist::MediaList;
|
||||||
use dom::window::Window;
|
use dom::window::Window;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use style::media_queries::parse_media_query_list;
|
||||||
use style::stylesheets::MediaRule;
|
use style::stylesheets::MediaRule;
|
||||||
use style_traits::ToCss;
|
use style_traits::ToCss;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct CSSMediaRule {
|
pub struct CSSMediaRule {
|
||||||
cssrule: CSSGroupingRule,
|
cssrule: CSSConditionRule,
|
||||||
#[ignore_heap_size_of = "Arc"]
|
#[ignore_heap_size_of = "Arc"]
|
||||||
mediarule: Arc<RwLock<MediaRule>>,
|
mediarule: Arc<RwLock<MediaRule>>,
|
||||||
medialist: MutNullableJS<MediaList>,
|
medialist: MutNullableJS<MediaList>,
|
||||||
|
@ -30,7 +32,7 @@ impl CSSMediaRule {
|
||||||
-> CSSMediaRule {
|
-> CSSMediaRule {
|
||||||
let list = mediarule.read().rules.clone();
|
let list = mediarule.read().rules.clone();
|
||||||
CSSMediaRule {
|
CSSMediaRule {
|
||||||
cssrule: CSSGroupingRule::new_inherited(parent_stylesheet, list),
|
cssrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
|
||||||
mediarule: mediarule,
|
mediarule: mediarule,
|
||||||
medialist: MutNullableJS::new(None),
|
medialist: MutNullableJS::new(None),
|
||||||
}
|
}
|
||||||
|
@ -48,6 +50,22 @@ impl CSSMediaRule {
|
||||||
self.medialist.or_init(|| MediaList::new(self.global().as_window(),
|
self.medialist.or_init(|| MediaList::new(self.global().as_window(),
|
||||||
self.mediarule.read().media_queries.clone()))
|
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 {
|
impl SpecificCSSRule for CSSMediaRule {
|
||||||
|
|
|
@ -15,6 +15,7 @@ use dom::cssmediarule::CSSMediaRule;
|
||||||
use dom::cssnamespacerule::CSSNamespaceRule;
|
use dom::cssnamespacerule::CSSNamespaceRule;
|
||||||
use dom::cssstylerule::CSSStyleRule;
|
use dom::cssstylerule::CSSStyleRule;
|
||||||
use dom::cssstylesheet::CSSStyleSheet;
|
use dom::cssstylesheet::CSSStyleSheet;
|
||||||
|
use dom::csssupportsrule::CSSSupportsRule;
|
||||||
use dom::cssviewportrule::CSSViewportRule;
|
use dom::cssviewportrule::CSSViewportRule;
|
||||||
use dom::window::Window;
|
use dom::window::Window;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
@ -59,6 +60,8 @@ impl CSSRule {
|
||||||
rule as &SpecificCSSRule
|
rule as &SpecificCSSRule
|
||||||
} else if let Some(rule) = self.downcast::<CSSImportRule>() {
|
} else if let Some(rule) = self.downcast::<CSSImportRule>() {
|
||||||
rule as &SpecificCSSRule
|
rule as &SpecificCSSRule
|
||||||
|
} else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
|
||||||
|
rule as &SpecificCSSRule
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
@ -77,6 +80,7 @@ impl CSSRule {
|
||||||
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent_stylesheet, s)),
|
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent_stylesheet, s)),
|
||||||
StyleCssRule::Namespace(s) => Root::upcast(CSSNamespaceRule::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::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;
|
mod create;
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod css;
|
pub mod css;
|
||||||
|
pub mod cssconditionrule;
|
||||||
pub mod cssfontfacerule;
|
pub mod cssfontfacerule;
|
||||||
pub mod cssgroupingrule;
|
pub mod cssgroupingrule;
|
||||||
pub mod cssimportrule;
|
pub mod cssimportrule;
|
||||||
|
@ -252,6 +253,7 @@ pub mod cssrulelist;
|
||||||
pub mod cssstyledeclaration;
|
pub mod cssstyledeclaration;
|
||||||
pub mod cssstylerule;
|
pub mod cssstylerule;
|
||||||
pub mod cssstylesheet;
|
pub mod cssstylesheet;
|
||||||
|
pub mod csssupportsrule;
|
||||||
pub mod cssviewportrule;
|
pub mod cssviewportrule;
|
||||||
pub mod customevent;
|
pub mod customevent;
|
||||||
pub mod dedicatedworkerglobalscope;
|
pub mod dedicatedworkerglobalscope;
|
||||||
|
|
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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom/#the-cssmediarule-interface
|
// https://drafts.csswg.org/cssom/#the-cssmediarule-interface
|
||||||
|
// https://drafts.csswg.org/css-conditional/#cssmediarule
|
||||||
[Exposed=Window]
|
[Exposed=Window]
|
||||||
interface CSSMediaRule : CSSGroupingRule {
|
interface CSSMediaRule : CSSConditionRule {
|
||||||
[SameObject, PutForwards=mediaText] readonly attribute MediaList media;
|
[SameObject, PutForwards=mediaText] readonly attribute MediaList media;
|
||||||
};
|
};
|
||||||
|
|
|
@ -31,3 +31,7 @@ partial interface CSSRule {
|
||||||
const unsigned short VIEWPORT_RULE = 15;
|
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 {
|
||||||
|
};
|
|
@ -11,7 +11,7 @@ use error_reporting::ParseErrorReporter;
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
|
use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
|
||||||
use servo_url::ServoUrl;
|
use servo_url::ServoUrl;
|
||||||
use stylesheets::Origin;
|
use stylesheets::{MemoryHoleReporter, Origin};
|
||||||
|
|
||||||
/// Extra data that the style backend may need to parse stylesheets.
|
/// Extra data that the style backend may need to parse stylesheets.
|
||||||
#[cfg(not(feature = "gecko"))]
|
#[cfg(not(feature = "gecko"))]
|
||||||
|
@ -78,6 +78,11 @@ impl<'a> ParserContext<'a> {
|
||||||
let extra_data = ParserContextExtraData::default();
|
let extra_data = ParserContextExtraData::default();
|
||||||
Self::new_with_extra_data(stylesheet_origin, base_url, error_reporter, extra_data)
|
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.
|
/// Defaults to a no-op.
|
||||||
|
|
|
@ -185,3 +185,9 @@ impl Index<Range<Position>> for ServoUrl {
|
||||||
&self.0[range]
|
&self.0[range]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Url> for ServoUrl {
|
||||||
|
fn from(url: Url) -> Self {
|
||||||
|
ServoUrl::from_url(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue