1use dom_struct::dom_struct;
6use html5ever::{QualName, local_name, ns};
7use js::context::JSContext;
8use script_bindings::error::Error;
9use script_traits::DocumentActivity;
10
11use crate::document_loader::DocumentLoader;
12use crate::dom::bindings::codegen::Bindings::DOMImplementationBinding::DOMImplementationMethods;
13use crate::dom::bindings::codegen::Bindings::DocumentBinding::{
14 DocumentMethods, ElementCreationOptions,
15};
16use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
17use crate::dom::bindings::codegen::UnionTypes::StringOrElementCreationOptions;
18use crate::dom::bindings::domname::{is_valid_doctype_name, namespace_from_domstring};
19use crate::dom::bindings::error::Fallible;
20use crate::dom::bindings::inheritance::Castable;
21use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
22use crate::dom::bindings::root::{Dom, DomRoot};
23use crate::dom::bindings::str::DOMString;
24use crate::dom::document::{Document, DocumentSource, HasBrowsingContext, IsHTMLDocument};
25use crate::dom::documenttype::DocumentType;
26use crate::dom::element::{CustomElementCreationMode, ElementCreator};
27use crate::dom::node::Node;
28use crate::dom::text::Text;
29use crate::dom::types::Element;
30use crate::dom::xmldocument::XMLDocument;
31use crate::script_runtime::CanGc;
32
33#[dom_struct]
35pub(crate) struct DOMImplementation {
36 reflector_: Reflector,
37 document: Dom<Document>,
38}
39
40impl DOMImplementation {
41 fn new_inherited(document: &Document) -> DOMImplementation {
42 DOMImplementation {
43 reflector_: Reflector::new(),
44 document: Dom::from_ref(document),
45 }
46 }
47
48 pub(crate) fn new(document: &Document, can_gc: CanGc) -> DomRoot<DOMImplementation> {
49 let window = document.window();
50 reflect_dom_object(
51 Box::new(DOMImplementation::new_inherited(document)),
52 window,
53 can_gc,
54 )
55 }
56}
57
58impl DOMImplementationMethods<crate::DomTypeHolder> for DOMImplementation {
60 fn CreateDocumentType(
62 &self,
63 cx: &mut js::context::JSContext,
64 qualified_name: DOMString,
65 pubid: DOMString,
66 sysid: DOMString,
67 ) -> Fallible<DomRoot<DocumentType>> {
68 if !is_valid_doctype_name(&qualified_name) {
71 debug!("Not a valid doctype name");
72 return Err(Error::InvalidCharacter(None));
73 }
74
75 Ok(DocumentType::new(
76 cx,
77 qualified_name,
78 Some(pubid),
79 Some(sysid),
80 &self.document,
81 ))
82 }
83
84 fn CreateDocument(
86 &self,
87 cx: &mut JSContext,
88 maybe_namespace: Option<DOMString>,
89 qname: DOMString,
90 maybe_doctype: Option<&DocumentType>,
91 ) -> Fallible<DomRoot<XMLDocument>> {
92 let win = self.document.window();
93 let loader = DocumentLoader::new(&self.document.loader());
94 let namespace = namespace_from_domstring(maybe_namespace.to_owned());
95
96 let content_type = match namespace {
97 ns!(html) => "application/xhtml+xml",
98 ns!(svg) => "image/svg+xml",
99 _ => "application/xml",
100 }
101 .parse()
102 .unwrap();
103
104 let doc = XMLDocument::new(
106 win,
107 HasBrowsingContext::No,
108 None,
109 self.document.origin().clone(),
110 IsHTMLDocument::NonHTMLDocument,
111 Some(content_type),
112 None,
113 DocumentActivity::Inactive,
114 DocumentSource::NotFromParser,
115 loader,
116 Some(self.document.insecure_requests_policy()),
117 self.document.has_trustworthy_ancestor_or_current_origin(),
118 self.document.custom_element_reaction_stack(),
119 CanGc::from_cx(cx),
120 );
121
122 let maybe_elem = if qname.is_empty() {
126 None
127 } else {
128 let options =
129 StringOrElementCreationOptions::ElementCreationOptions(ElementCreationOptions {
130 is: None,
131 });
132 match doc
133 .upcast::<Document>()
134 .CreateElementNS(cx, maybe_namespace, qname, options)
135 {
136 Err(error) => return Err(error),
137 Ok(elem) => Some(elem),
138 }
139 };
140
141 {
142 let doc_node = doc.upcast::<Node>();
143
144 if let Some(doc_type) = maybe_doctype {
146 doc_node.AppendChild(cx, doc_type.upcast()).unwrap();
147 }
148
149 if let Some(ref elem) = maybe_elem {
151 doc_node.AppendChild(cx, elem.upcast()).unwrap();
152 }
153 }
154
155 Ok(doc)
160 }
161
162 fn CreateHTMLDocument(
164 &self,
165 cx: &mut JSContext,
166 title: Option<DOMString>,
167 ) -> DomRoot<Document> {
168 let win = self.document.window();
169 let loader = DocumentLoader::new(&self.document.loader());
170
171 let doc = Document::new(
174 win,
175 HasBrowsingContext::No,
176 None,
177 None,
178 self.document.origin().clone(),
180 IsHTMLDocument::HTMLDocument,
181 None,
182 None,
183 DocumentActivity::Inactive,
184 DocumentSource::NotFromParser,
185 loader,
186 None,
187 None,
188 Default::default(),
189 false,
190 self.document.allow_declarative_shadow_roots(),
191 Some(self.document.insecure_requests_policy()),
192 self.document.has_trustworthy_ancestor_or_current_origin(),
193 self.document.custom_element_reaction_stack(),
194 self.document.creation_sandboxing_flag_set(),
195 CanGc::from_cx(cx),
196 );
197
198 {
199 let doc_node = doc.upcast::<Node>();
201 let doc_type = DocumentType::new(cx, DOMString::from("html"), None, None, &doc);
202 doc_node.AppendChild(cx, doc_type.upcast()).unwrap();
203 }
204
205 {
206 let doc_node = doc.upcast::<Node>();
209 let doc_html = DomRoot::upcast::<Node>(Element::create(
210 cx,
211 QualName::new(None, ns!(html), local_name!("html")),
212 None,
213 &doc,
214 ElementCreator::ScriptCreated,
215 CustomElementCreationMode::Asynchronous,
216 None,
217 ));
218 doc_node
219 .AppendChild(cx, &doc_html)
220 .expect("Appending failed");
221
222 {
223 let doc_head = DomRoot::upcast::<Node>(Element::create(
226 cx,
227 QualName::new(None, ns!(html), local_name!("head")),
228 None,
229 &doc,
230 ElementCreator::ScriptCreated,
231 CustomElementCreationMode::Asynchronous,
232 None,
233 ));
234 doc_html.AppendChild(cx, &doc_head).unwrap();
235
236 if let Some(title_str) = title {
238 let doc_title = DomRoot::upcast::<Node>(Element::create(
241 cx,
242 QualName::new(None, ns!(html), local_name!("title")),
243 None,
244 &doc,
245 ElementCreator::ScriptCreated,
246 CustomElementCreationMode::Asynchronous,
247 None,
248 ));
249 doc_head.AppendChild(cx, &doc_title).unwrap();
250
251 let title_text = Text::new(cx, title_str, &doc);
254 doc_title.AppendChild(cx, title_text.upcast()).unwrap();
255 }
256 }
257
258 let doc_body = Element::create(
261 cx,
262 QualName::new(None, ns!(html), local_name!("body")),
263 None,
264 &doc,
265 ElementCreator::ScriptCreated,
266 CustomElementCreationMode::Asynchronous,
267 None,
268 );
269 doc_html.AppendChild(cx, doc_body.upcast()).unwrap();
270 }
271
272 doc
274 }
275
276 fn HasFeature(&self) -> bool {
278 true
279 }
280}