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