mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Add CSS.supports()
This commit is contained in:
parent
56731dc6b5
commit
62bea28031
3 changed files with 34 additions and 4 deletions
|
@ -2,11 +2,14 @@
|
||||||
* 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::serialize_identifier;
|
use cssparser::{Parser, serialize_identifier};
|
||||||
|
use dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
|
||||||
use dom::bindings::error::Fallible;
|
use dom::bindings::error::Fallible;
|
||||||
use dom::bindings::reflector::Reflector;
|
use dom::bindings::reflector::Reflector;
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
use dom::window::Window;
|
use dom::window::Window;
|
||||||
|
use style::parser::ParserContext;
|
||||||
|
use style::supports::{Declaration, parse_condition_or_declaration};
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct CSS {
|
pub struct CSS {
|
||||||
|
@ -14,10 +17,31 @@ pub struct CSS {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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> {
|
pub fn Escape(_: &Window, ident: DOMString) -> Fallible<DOMString> {
|
||||||
let mut escaped = String::new();
|
let mut escaped = String::new();
|
||||||
serialize_identifier(&ident, &mut escaped).unwrap();
|
serialize_identifier(&ident, &mut escaped).unwrap();
|
||||||
Ok(DOMString::from(escaped))
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,3 +11,9 @@ interface CSS {
|
||||||
[Throws]
|
[Throws]
|
||||||
static DOMString escape(DOMString ident);
|
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);
|
||||||
|
};
|
||||||
|
|
|
@ -161,9 +161,9 @@ impl ToCss for SupportsCondition {
|
||||||
/// A possibly-invalid property declaration
|
/// A possibly-invalid property declaration
|
||||||
pub struct Declaration {
|
pub struct Declaration {
|
||||||
/// The property name
|
/// The property name
|
||||||
prop: String,
|
pub prop: String,
|
||||||
/// The property value
|
/// The property value
|
||||||
val: String,
|
pub val: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToCss for Declaration {
|
impl ToCss for Declaration {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue