script/dom/
trustedscripturl.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 std::fmt;
6
7use dom_struct::dom_struct;
8
9use crate::dom::bindings::codegen::Bindings::TrustedScriptURLBinding::TrustedScriptURLMethods;
10use crate::dom::bindings::codegen::UnionTypes::TrustedScriptURLOrUSVString;
11use crate::dom::bindings::error::Fallible;
12use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::globalscope::GlobalScope;
16use crate::dom::trustedtypepolicy::TrustedType;
17use crate::dom::trustedtypepolicyfactory::{DEFAULT_SCRIPT_SINK_GROUP, TrustedTypePolicyFactory};
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21pub struct TrustedScriptURL {
22    reflector_: Reflector,
23
24    data: DOMString,
25}
26
27impl TrustedScriptURL {
28    fn new_inherited(data: DOMString) -> Self {
29        Self {
30            reflector_: Reflector::new(),
31            data,
32        }
33    }
34
35    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
36    pub(crate) fn new(data: DOMString, global: &GlobalScope, can_gc: CanGc) -> DomRoot<Self> {
37        reflect_dom_object(Box::new(Self::new_inherited(data)), global, can_gc)
38    }
39
40    pub(crate) fn get_trusted_script_url_compliant_string(
41        global: &GlobalScope,
42        value: TrustedScriptURLOrUSVString,
43        containing_class: &str,
44        field: &str,
45        can_gc: CanGc,
46    ) -> Fallible<DOMString> {
47        match value {
48            TrustedScriptURLOrUSVString::USVString(value) => {
49                let sink = format!("{} {}", containing_class, field);
50                TrustedTypePolicyFactory::get_trusted_type_compliant_string(
51                    TrustedType::TrustedScriptURL,
52                    global,
53                    value.as_ref().into(),
54                    &sink,
55                    DEFAULT_SCRIPT_SINK_GROUP,
56                    can_gc,
57                )
58            },
59            TrustedScriptURLOrUSVString::TrustedScriptURL(trusted_script_url) => {
60                Ok(trusted_script_url.data.clone())
61            },
62        }
63    }
64
65    pub(crate) fn data(&self) -> DOMString {
66        self.data.clone()
67    }
68}
69
70impl fmt::Display for TrustedScriptURL {
71    #[inline]
72    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73        f.write_str(&self.data)
74    }
75}
76
77impl TrustedScriptURLMethods<crate::DomTypeHolder> for TrustedScriptURL {
78    /// <https://www.w3.org/TR/trusted-types/#trustedscripturl-stringification-behavior>
79    fn Stringifier(&self) -> DOMString {
80        self.data.clone()
81    }
82
83    /// <https://www.w3.org/TR/trusted-types/#dom-trustedscripturl-tojson>
84    fn ToJSON(&self) -> DOMString {
85        self.data.clone()
86    }
87}