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_with_cx;
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;
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        source: DocumentSource,
51        doc_loader: DocumentLoader,
52        inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
53        has_trustworthy_ancestor_origin: bool,
54        custom_element_reaction_stack: Rc<CustomElementReactionStack>,
55        timeline: &DocumentTimeline,
56        image_cache: Arc<dyn ImageCache>,
57    ) -> XMLDocument {
58        XMLDocument {
59            document: Document::new_inherited(
60                window,
61                has_browsing_context,
62                url,
63                None,
64                origin,
65                is_html_document,
66                content_type,
67                last_modified,
68                activity,
69                source,
70                doc_loader,
71                None,
72                None,
73                Default::default(),
74                false,
75                false,
76                inherited_insecure_requests_policy,
77                has_trustworthy_ancestor_origin,
78                custom_element_reaction_stack,
79                window.Document().creation_sandboxing_flag_set(),
80                timeline,
81                window.pipeline_id(),
82                image_cache,
83            ),
84        }
85    }
86
87    #[expect(clippy::too_many_arguments)]
88    pub(crate) fn new(
89        cx: &mut JSContext,
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    ) -> DomRoot<XMLDocument> {
105        let timeline = DocumentTimeline::new(cx, window);
106        let doc = reflect_dom_object_with_cx(
107            Box::new(XMLDocument::new_inherited(
108                window,
109                has_browsing_context,
110                url,
111                origin,
112                doctype,
113                content_type,
114                last_modified,
115                activity,
116                source,
117                doc_loader,
118                inherited_insecure_requests_policy,
119                has_trustworthy_ancestor_origin,
120                custom_element_reaction_stack,
121                &timeline,
122                image_cache,
123            )),
124            window,
125            cx,
126        );
127        {
128            let node = doc.upcast::<Node>();
129            node.set_owner_doc(&doc.document);
130        }
131        doc
132    }
133}
134
135impl XMLDocumentMethods<crate::DomTypeHolder> for XMLDocument {
136    /// <https://html.spec.whatwg.org/multipage/#dom-document-location>
137    fn GetLocation(&self, cx: &mut JSContext) -> Option<DomRoot<Location>> {
138        self.upcast::<Document>().GetLocation(cx)
139    }
140
141    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:supported-property-names>
142    fn SupportedPropertyNames(&self, no_gc: &NoGC) -> Vec<DOMString> {
143        self.upcast::<Document>().SupportedPropertyNames(no_gc)
144    }
145
146    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:dom-document-nameditem-filter>
147    fn NamedGetter(
148        &self,
149        cx: &mut js::context::JSContext,
150        name: DOMString,
151    ) -> Option<NamedPropertyValue> {
152        self.upcast::<Document>().NamedGetter(cx, name)
153    }
154}