script/dom/
trustedhtml.rs1use std::fmt;
6
7use dom_struct::dom_struct;
8
9use crate::conversions::Convert;
10use crate::dom::bindings::codegen::Bindings::TrustedHTMLBinding::TrustedHTMLMethods;
11use crate::dom::bindings::codegen::UnionTypes::{
12 TrustedHTMLOrNullIsEmptyString, TrustedHTMLOrString,
13};
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::globalscope::GlobalScope;
19use crate::dom::trustedtypepolicy::TrustedType;
20use crate::dom::trustedtypepolicyfactory::{DEFAULT_SCRIPT_SINK_GROUP, TrustedTypePolicyFactory};
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub struct TrustedHTML {
25 reflector_: Reflector,
26
27 data: DOMString,
28}
29
30impl TrustedHTML {
31 fn new_inherited(data: DOMString) -> Self {
32 Self {
33 reflector_: Reflector::new(),
34 data,
35 }
36 }
37
38 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
39 pub(crate) fn new(data: DOMString, global: &GlobalScope, can_gc: CanGc) -> DomRoot<Self> {
40 reflect_dom_object(Box::new(Self::new_inherited(data)), global, can_gc)
41 }
42
43 pub(crate) fn get_trusted_script_compliant_string(
44 global: &GlobalScope,
45 value: TrustedHTMLOrString,
46 sink: &str,
47 can_gc: CanGc,
48 ) -> Fallible<DOMString> {
49 match value {
50 TrustedHTMLOrString::String(value) => {
51 TrustedTypePolicyFactory::get_trusted_type_compliant_string(
52 TrustedType::TrustedHTML,
53 global,
54 value,
55 sink,
56 DEFAULT_SCRIPT_SINK_GROUP,
57 can_gc,
58 )
59 },
60
61 TrustedHTMLOrString::TrustedHTML(trusted_html) => Ok(trusted_html.data.clone()),
62 }
63 }
64
65 pub(crate) fn data(&self) -> DOMString {
66 self.data.clone()
67 }
68}
69
70impl fmt::Display for TrustedHTML {
71 #[inline]
72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73 f.write_str(&self.data)
74 }
75}
76
77impl TrustedHTMLMethods<crate::DomTypeHolder> for TrustedHTML {
78 fn Stringifier(&self) -> DOMString {
80 self.data.clone()
81 }
82
83 fn ToJSON(&self) -> DOMString {
85 self.data.clone()
86 }
87}
88
89impl Convert<TrustedHTMLOrString> for TrustedHTMLOrNullIsEmptyString {
90 fn convert(self) -> TrustedHTMLOrString {
91 match self {
92 TrustedHTMLOrNullIsEmptyString::TrustedHTML(trusted_html) => {
93 TrustedHTMLOrString::TrustedHTML(trusted_html)
94 },
95 TrustedHTMLOrNullIsEmptyString::NullIsEmptyString(str) => {
96 TrustedHTMLOrString::String(str)
97 },
98 }
99 }
100}