script/dom/
xmldocument.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::rc::Rc;
6
7use data_url::mime::Mime;
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use net_traits::request::InsecureRequestsPolicy;
11use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
12use script_traits::DocumentActivity;
13use servo_url::{MutableOrigin, ServoUrl};
14
15use crate::document_loader::DocumentLoader;
16use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
17    DocumentMethods, NamedPropertyValue,
18};
19use crate::dom::bindings::codegen::Bindings::XMLDocumentBinding::XMLDocumentMethods;
20use crate::dom::bindings::inheritance::Castable;
21use crate::dom::bindings::reflector::reflect_dom_object;
22use crate::dom::bindings::root::DomRoot;
23use crate::dom::bindings::str::DOMString;
24use crate::dom::customelementregistry::CustomElementReactionStack;
25use crate::dom::document::{Document, DocumentSource, HasBrowsingContext, IsHTMLDocument};
26use crate::dom::location::Location;
27use crate::dom::node::Node;
28use crate::dom::window::Window;
29use crate::script_runtime::CanGc;
30
31// https://dom.spec.whatwg.org/#xmldocument
32#[dom_struct]
33pub(crate) struct XMLDocument {
34    document: Document,
35}
36
37impl XMLDocument {
38    #[expect(clippy::too_many_arguments)]
39    fn new_inherited(
40        window: &Window,
41        has_browsing_context: HasBrowsingContext,
42        url: Option<ServoUrl>,
43        origin: MutableOrigin,
44        is_html_document: IsHTMLDocument,
45        content_type: Option<Mime>,
46        last_modified: Option<String>,
47        activity: DocumentActivity,
48        source: DocumentSource,
49        doc_loader: DocumentLoader,
50        inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
51        has_trustworthy_ancestor_origin: bool,
52        custom_element_reaction_stack: Rc<CustomElementReactionStack>,
53        can_gc: CanGc,
54    ) -> XMLDocument {
55        XMLDocument {
56            document: Document::new_inherited(
57                window,
58                has_browsing_context,
59                url,
60                None,
61                origin,
62                is_html_document,
63                content_type,
64                last_modified,
65                activity,
66                source,
67                doc_loader,
68                None,
69                None,
70                Default::default(),
71                false,
72                false,
73                inherited_insecure_requests_policy,
74                has_trustworthy_ancestor_origin,
75                custom_element_reaction_stack,
76                window.Document().creation_sandboxing_flag_set(),
77                can_gc,
78            ),
79        }
80    }
81
82    #[expect(clippy::too_many_arguments)]
83    pub(crate) fn new(
84        window: &Window,
85        has_browsing_context: HasBrowsingContext,
86        url: Option<ServoUrl>,
87        origin: MutableOrigin,
88        doctype: IsHTMLDocument,
89        content_type: Option<Mime>,
90        last_modified: Option<String>,
91        activity: DocumentActivity,
92        source: DocumentSource,
93        doc_loader: DocumentLoader,
94        inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
95        has_trustworthy_ancestor_origin: bool,
96        custom_element_reaction_stack: Rc<CustomElementReactionStack>,
97        can_gc: CanGc,
98    ) -> DomRoot<XMLDocument> {
99        let doc = reflect_dom_object(
100            Box::new(XMLDocument::new_inherited(
101                window,
102                has_browsing_context,
103                url,
104                origin,
105                doctype,
106                content_type,
107                last_modified,
108                activity,
109                source,
110                doc_loader,
111                inherited_insecure_requests_policy,
112                has_trustworthy_ancestor_origin,
113                custom_element_reaction_stack,
114                can_gc,
115            )),
116            window,
117            can_gc,
118        );
119        {
120            let node = doc.upcast::<Node>();
121            node.set_owner_doc(&doc.document);
122        }
123        doc
124    }
125}
126
127impl XMLDocumentMethods<crate::DomTypeHolder> for XMLDocument {
128    /// <https://html.spec.whatwg.org/multipage/#dom-document-location>
129    fn GetLocation(&self, cx: &mut JSContext) -> Option<DomRoot<Location>> {
130        self.upcast::<Document>().GetLocation(cx)
131    }
132
133    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:supported-property-names>
134    fn SupportedPropertyNames(&self) -> Vec<DOMString> {
135        self.upcast::<Document>().SupportedPropertyNames()
136    }
137
138    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:dom-document-nameditem-filter>
139    fn NamedGetter(&self, name: DOMString, can_gc: CanGc) -> Option<NamedPropertyValue> {
140        self.upcast::<Document>().NamedGetter(name, can_gc)
141    }
142}