Skip to content

Commit

Permalink
CSSOM bits for @supports: CSSConditionRule and CSSSupportsRule, with…
Browse files Browse the repository at this point in the history
… conditionText attribute
  • Loading branch information
Manishearth committed Jan 8, 2017
1 parent 1b0842e commit b4a83b6
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 5 deletions.
7 changes: 7 additions & 0 deletions components/script/dom/bindings/trace.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions components/script/dom/cssconditionrule.rs
@@ -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!()
}
}
}
24 changes: 21 additions & 3 deletions components/script/dom/cssmediarule.rs
Expand Up @@ -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>,
Expand All @@ -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),
}
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions components/script/dom/cssrule.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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!()
}
Expand All @@ -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)),
}
}

Expand Down
77 changes: 77 additions & 0 deletions components/script/dom/csssupportsrule.rs
@@ -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()
}
}
2 changes: 2 additions & 0 deletions components/script/dom/mod.rs
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions components/script/dom/webidls/CSSConditionRule.webidl
@@ -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 changes: 2 additions & 1 deletion components/script/dom/webidls/CSSMediaRule.webidl
Expand Up @@ -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;
};
4 changes: 4 additions & 0 deletions components/script/dom/webidls/CSSRule.webidl
Expand Up @@ -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 changes: 8 additions & 0 deletions components/script/dom/webidls/CSSSupportsRule.webidl
@@ -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 {
};
7 changes: 6 additions & 1 deletion components/style/parser.rs
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions components/url/lib.rs
Expand Up @@ -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)
}
}

0 comments on commit b4a83b6

Please sign in to comment.