Skip to main content

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