script/dom/
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 servo_url::ServoUrl;
8
9use crate::dom::bindings::codegen::Bindings::CSSStyleValueBinding::CSSStyleValueMethods;
10use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
11use crate::dom::bindings::root::DomRoot;
12use crate::dom::bindings::str::DOMString;
13use crate::dom::globalscope::GlobalScope;
14use crate::script_runtime::CanGc;
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        global: &GlobalScope,
32        value: String,
33        can_gc: CanGc,
34    ) -> DomRoot<CSSStyleValue> {
35        reflect_dom_object(
36            Box::new(CSSStyleValue::new_inherited(value)),
37            global,
38            can_gc,
39        )
40    }
41}
42
43impl CSSStyleValueMethods<crate::DomTypeHolder> for CSSStyleValue {
44    /// <https://drafts.css-houdini.org/css-typed-om-1/#CSSStyleValue-stringification-behavior>
45    fn Stringifier(&self) -> DOMString {
46        DOMString::from(&*self.value)
47    }
48}
49
50impl CSSStyleValue {
51    /// Parse the value as a `url()`.
52    /// TODO: This should really always be an absolute URL, but we currently
53    /// return relative URLs for computed values, so we pass in a base.
54    /// <https://github.com/servo/servo/issues/17625>
55    pub(crate) fn get_url(&self, base_url: ServoUrl) -> Option<ServoUrl> {
56        let mut input = ParserInput::new(&self.value);
57        let mut parser = Parser::new(&mut input);
58        parser
59            .expect_url()
60            .ok()
61            .and_then(|string| base_url.join(&string).ok())
62    }
63}