Skip to main content

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