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