script/dom/trustedtypes/
trustedscripturl.rs1use std::fmt;
6
7use dom_struct::dom_struct;
8use script_bindings::domstring::TracedDOMString;
9use script_bindings::reflector::{Reflector, reflect_dom_object};
10
11use crate::dom::bindings::codegen::Bindings::TrustedScriptURLBinding::TrustedScriptURLMethods;
12use crate::dom::bindings::codegen::UnionTypes::TrustedScriptURLOrUSVString;
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::globalscope::GlobalScope;
17use crate::dom::trustedtypes::trustedtypepolicy::TrustedType;
18use crate::dom::trustedtypes::trustedtypepolicyfactory::{
19 DEFAULT_SCRIPT_SINK_GROUP, TrustedTypePolicyFactory,
20};
21
22#[dom_struct]
23pub struct TrustedScriptURL {
24 reflector_: Reflector,
25
26 data: TracedDOMString,
27}
28
29impl TrustedScriptURL {
30 fn new_inherited(data: DOMString) -> Self {
31 Self {
32 reflector_: Reflector::new(),
33 data: data.traced(),
34 }
35 }
36
37 pub(crate) fn new(
38 cx: &mut js::context::JSContext,
39 data: DOMString,
40 global: &GlobalScope,
41 ) -> DomRoot<Self> {
42 reflect_dom_object(cx, Box::new(Self::new_inherited(data)), global)
43 }
44
45 pub(crate) fn get_trusted_type_compliant_string(
46 cx: &mut js::context::JSContext,
47 global: &GlobalScope,
48 value: TrustedScriptURLOrUSVString,
49 sink: &str,
50 ) -> Fallible<DOMString> {
51 match value {
52 TrustedScriptURLOrUSVString::USVString(value) => {
53 TrustedTypePolicyFactory::get_trusted_type_compliant_string(
54 cx,
55 TrustedType::TrustedScriptURL,
56 global,
57 value.as_ref().into(),
58 sink,
59 DEFAULT_SCRIPT_SINK_GROUP,
60 )
61 },
62 TrustedScriptURLOrUSVString::TrustedScriptURL(trusted_script_url) => {
63 Ok(trusted_script_url.data.clone().root())
64 },
65 }
66 }
67
68 pub(crate) fn data(&self) -> &TracedDOMString {
69 &self.data
70 }
71}
72
73impl fmt::Display for TrustedScriptURL {
74 #[inline]
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 f.write_str(&self.data.str())
77 }
78}
79
80impl TrustedScriptURLMethods<crate::DomTypeHolder> for TrustedScriptURL {
81 fn Stringifier(&self) -> DOMString {
83 self.data.clone().root()
84 }
85
86 fn ToJSON(&self) -> DOMString {
88 self.data.clone().root()
89 }
90}