Skip to main content

script/dom/css/
cssstylevalue.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use cssparser::{Parser, ParserInput};
6use dom_struct::dom_struct;
7use js::context::JSContext;
8use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
9use servo_url::ServoUrl;
10
11use crate::dom::bindings::codegen::Bindings::CSSStyleValueBinding::CSSStyleValueMethods;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::bindings::str::DOMString;
14use crate::dom::globalscope::GlobalScope;
15
16#[dom_struct]
17pub(crate) struct CSSStyleValue {
18    reflector: Reflector,
19    value: String,
20}
21
22impl CSSStyleValue {
23    fn new_inherited(value: String) -> CSSStyleValue {
24        CSSStyleValue {
25            reflector: Reflector::new(),
26            value,
27        }
28    }
29
30    pub(crate) fn new(
31        cx: &mut JSContext,
32        global: &GlobalScope,
33        value: String,
34    ) -> DomRoot<CSSStyleValue> {
35        reflect_dom_object_with_cx(Box::new(CSSStyleValue::new_inherited(value)), global, cx)
36    }
37}
38
39impl CSSStyleValueMethods<crate::DomTypeHolder> for CSSStyleValue {
40    /// <https://drafts.css-houdini.org/css-typed-om-1/#CSSStyleValue-stringification-behavior>
41    fn Stringifier(&self) -> DOMString {
42        DOMString::from(&*self.value)
43    }
44}
45
46impl CSSStyleValue {
47    /// Parse the value as a `url()`.
48    /// TODO: This should really always be an absolute URL, but we currently
49    /// return relative URLs for computed values, so we pass in a base.
50    /// <https://github.com/servo/servo/issues/17625>
51    pub(crate) fn get_url(&self, base_url: ServoUrl) -> Option<ServoUrl> {
52        let mut input = ParserInput::new(&self.value);
53        let mut parser = Parser::new(&mut input);
54        parser
55            .expect_url()
56            .ok()
57            .and_then(|string| base_url.join(&string).ok())
58    }
59}