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_with_cx;
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                Default::default(),
72                false,
73                false,
74                inherited_insecure_requests_policy,
75                has_trustworthy_ancestor_origin,
76                custom_element_reaction_stack,
77                window.Document().creation_sandboxing_flag_set(),
78                timeline,
79                window.pipeline_id(),
80                image_cache,
81            ),
82        }
83    }
84
85    #[expect(clippy::too_many_arguments)]
86    pub(crate) fn new(
87        cx: &mut JSContext,
88        window: &Window,
89        has_browsing_context: HasBrowsingContext,
90        url: Option<ServoUrl>,
91        origin: MutableOrigin,
92        doctype: IsHTMLDocument,
93        content_type: Option<Mime>,
94        last_modified: Option<String>,
95        activity: DocumentActivity,
96        doc_loader: DocumentLoader,
97        inherited_insecure_requests_policy: Option<InsecureRequestsPolicy>,
98        has_trustworthy_ancestor_origin: bool,
99        custom_element_reaction_stack: Rc<CustomElementReactionStack>,
100        image_cache: Arc<dyn ImageCache>,
101    ) -> DomRoot<XMLDocument> {
102        let timeline = DocumentTimeline::new(cx, window);
103        let doc = reflect_dom_object_with_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            cx,
122        );
123        {
124            let node = doc.upcast::<Node>();
125            node.set_owner_doc(&doc.document);
126        }
127        doc
128    }
129}
130
131impl XMLDocumentMethods<crate::DomTypeHolder> for XMLDocument {
132    /// <https://html.spec.whatwg.org/multipage/#dom-document-location>
133    fn GetLocation(&self, cx: &mut JSContext) -> Option<DomRoot<Location>> {
134        self.upcast::<Document>().GetLocation(cx)
135    }
136
137    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:supported-property-names>
138    fn SupportedPropertyNames(&self, no_gc: &NoGC) -> Vec<DOMString> {
139        self.upcast::<Document>().SupportedPropertyNames(no_gc)
140    }
141
142    /// <https://html.spec.whatwg.org/multipage/#dom-tree-accessors:dom-document-nameditem-filter>
143    fn NamedGetter(
144        &self,
145        cx: &mut js::context::JSContext,
146        name: DOMString,
147    ) -> Option<NamedPropertyValue> {
148        self.upcast::<Document>().NamedGetter(cx, name)
149    }
150}