Skip to main content

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