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