diff --git a/src/components/script/dom/domimplementation.rs b/src/components/script/dom/domimplementation.rs index 71f63f52066..aaf4d427b9f 100644 --- a/src/components/script/dom/domimplementation.rs +++ b/src/components/script/dom/domimplementation.rs @@ -8,7 +8,7 @@ use dom::bindings::js::JS; use dom::bindings::utils::{Reflector, Reflectable, reflect_dom_object}; use dom::bindings::error::{Fallible, InvalidCharacter, NamespaceError}; use dom::bindings::utils::{QName, Name, InvalidXMLName, xml_name_type}; -use dom::document::{Document, HTMLDocument}; +use dom::document::{Document, HTMLDocument, NonHTMLDocument}; use dom::documenttype::DocumentType; use dom::htmlbodyelement::HTMLBodyElement; use dom::htmlheadelement::HTMLHeadElement; @@ -63,6 +63,42 @@ impl DOMImplementation { } } + // http://dom.spec.whatwg.org/#dom-domimplementation-createdocument + pub fn CreateDocument(&self, namespace: Option, qname: DOMString, + maybe_doctype: Option>) -> Fallible> { + // Step 1. + let doc = Document::new(&self.owner, None, NonHTMLDocument, None); + let mut doc_node: JS = NodeCast::from(&doc); + + // Step 2-3. + let maybe_elem = if qname.is_empty() { + None + } else { + match doc.get().CreateElementNS(&doc, namespace, qname) { + Err(error) => return Err(error), + Ok(elem) => Some(elem) + } + }; + + // Step 4. + match maybe_doctype { + None => (), + Some(ref doctype) => assert!(doc_node.AppendChild(&mut NodeCast::from(doctype)).is_ok()) + } + + // Step 5. + match maybe_elem { + None => (), + Some(ref elem) => assert!(doc_node.AppendChild(&mut NodeCast::from(elem)).is_ok()) + } + + // Step 6. + // FIXME: https://github.com/mozilla/servo/issues/1522 + + // Step 7. + Ok(doc) + } + // http://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument pub fn CreateHTMLDocument(&self, title: Option) -> JS { // Step 1-2. diff --git a/src/components/script/dom/webidls/DOMImplementation.webidl b/src/components/script/dom/webidls/DOMImplementation.webidl index aeb6049ff72..643f0d64a76 100644 --- a/src/components/script/dom/webidls/DOMImplementation.webidl +++ b/src/components/script/dom/webidls/DOMImplementation.webidl @@ -17,10 +17,10 @@ interface DOMImplementation { [Creator, Throws] DocumentType createDocumentType(DOMString qualifiedName, DOMString publicId, DOMString systemId); - /*[Throws] + [Creator, Throws] Document createDocument(DOMString? namespace, [TreatNullAs=EmptyString] DOMString qualifiedName, - optional DocumentType? doctype = null);*/ + optional DocumentType? doctype = null); [Creator] Document createHTMLDocument(optional DOMString title); }; diff --git a/src/test/content/test_document_implementation.html b/src/test/content/test_document_implementation.html index 99c9aea32ef..7ac34da077d 100644 --- a/src/test/content/test_document_implementation.html +++ b/src/test/content/test_document_implementation.html @@ -51,6 +51,30 @@ is(htmldoc.body.childNodes.length, 0, "test3-19, createHTMLDocument"); } + // test4: createDocument + { + var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null); + is_not(doc, null, "test4-0, createDocument"); + is_a(doc, Document, "test4-1, createDocument"); + is(doc.childNodes.length, 1, "test4-2, createDocument"); + is(doc.doctype, null, "test4-3, createDocument"); + is_a(doc.documentElement, HTMLHtmlElement, "test4-4, createDocument"); + + var doctype = document.implementation.createDocumentType("html", null, null); + doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', doctype); + is(doc.childNodes.length, 2, "test4-5, createDocument"); + is(doc.doctype, doctype, "test4-6, createDocument"); + is_a(doc.documentElement, HTMLHtmlElement, "test4-7, createDocument"); + + doctype = document.implementation.createDocumentType( + 'svg:svg', '-//W3C//DTD SVG 1.1//EN', + 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'); + doc = document.implementation.createDocument('http://www.w3.org/2000/svg', 'svg:svg', doctype); + is(doc.childNodes.length, 2, "test4-8, createDocument"); + is(doc.doctype, doctype, "test4-9, createDocument"); + is_a(doc.documentElement, Element, "test4-10, createDocument"); + } + finish();