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