From 62bea2803101adb2f1fc08afc51455951a986c35 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 2 Jan 2017 00:11:27 -0800 Subject: [PATCH] Add CSS.supports() --- components/script/dom/css.rs | 28 ++++++++++++++++++++++-- components/script/dom/webidls/CSS.webidl | 6 +++++ components/style/supports.rs | 4 ++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/components/script/dom/css.rs b/components/script/dom/css.rs index 2e04aed50bc3..bf264f143228 100644 --- a/components/script/dom/css.rs +++ b/components/script/dom/css.rs @@ -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 { 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 + } + } } diff --git a/components/script/dom/webidls/CSS.webidl b/components/script/dom/webidls/CSS.webidl index 438c6814ccb2..5f4aa4f0befc 100644 --- a/components/script/dom/webidls/CSS.webidl +++ b/components/script/dom/webidls/CSS.webidl @@ -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); +}; diff --git a/components/style/supports.rs b/components/style/supports.rs index a373ad58b0be..bbbfec9f4fc5 100644 --- a/components/style/supports.rs +++ b/components/style/supports.rs @@ -161,9 +161,9 @@ impl ToCss for SupportsCondition { /// A possibly-invalid property declaration pub struct Declaration { /// The property name - prop: String, + pub prop: String, /// The property value - val: String, + pub val: String, } impl ToCss for Declaration {