From d8ae02ef593744902991ed32ea6c585f7ec18710 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 12 Aug 2023 21:30:16 +1200 Subject: [PATCH] LibWeb: Add spec comments to DOMImplementation --- .../LibWeb/DOM/DOMImplementation.cpp | 34 ++++++++++++++++--- .../Libraries/LibWeb/DOM/DOMImplementation.h | 6 +++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp b/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp index 4af9e169634..e4963ff822f 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp +++ b/Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp @@ -46,72 +46,98 @@ void DOMImplementation::visit_edges(Cell::Visitor& visitor) // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WebIDL::ExceptionOr> DOMImplementation::create_document(DeprecatedString const& namespace_, DeprecatedString const& qualified_name, JS::GCPtr doctype) const { - // FIXME: This should specifically be an XML document. + // FIXME: 1. Let document be a new XMLDocument auto xml_document = Document::create(realm()); xml_document->set_ready_for_post_load_tasks(true); + // 2. Let element be null. JS::GCPtr element; + // 3. If qualifiedName is not the empty string, then set element to the result of running the internal createElementNS steps, given document, namespace, qualifiedName, and an empty dictionary. if (!qualified_name.is_empty()) element = TRY(xml_document->create_element_ns(namespace_, qualified_name, ElementCreationOptions {})); + // 4. If doctype is non-null, append doctype to document. if (doctype) TRY(xml_document->append_child(*doctype)); + // 5. If element is non-null, append element to document. if (element) TRY(xml_document->append_child(*element)); + // 6. document’s origin is this’s associated document’s origin. xml_document->set_origin(document().origin()); - if (namespace_ == Namespace::HTML) + // 7. document’s content type is determined by namespace: + if (namespace_ == Namespace::HTML) { + // -> HTML namespace xml_document->set_content_type("application/xhtml+xml"); - else if (namespace_ == Namespace::SVG) + } else if (namespace_ == Namespace::SVG) { + // -> SVG namespace xml_document->set_content_type("image/svg+xml"); - else + } else { + // -> Any other namespace xml_document->set_content_type("application/xml"); + } + // 8. Return document. return xml_document; } // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument JS::NonnullGCPtr DOMImplementation::create_html_document(DeprecatedString const& title) const { + // 1. Let doc be a new document that is an HTML document. auto html_document = Document::create(realm()); + // 2. Set doc’s content type to "text/html". html_document->set_content_type("text/html"); + html_document->set_ready_for_post_load_tasks(true); + // 3. Append a new doctype, with "html" as its name and with its node document set to doc, to doc. auto doctype = heap().allocate(realm(), html_document); doctype->set_name("html"); MUST(html_document->append_child(*doctype)); + // 4. Append the result of creating an element given doc, html, and the HTML namespace, to doc. auto html_element = create_element(html_document, HTML::TagNames::html, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); MUST(html_document->append_child(html_element)); + // 5. Append the result of creating an element given doc, head, and the HTML namespace, to the html element created earlier. auto head_element = create_element(html_document, HTML::TagNames::head, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); MUST(html_element->append_child(head_element)); + // 6. If title is given: if (!title.is_null()) { + // 1. Append the result of creating an element given doc, title, and the HTML namespace, to the head element created earlier. auto title_element = create_element(html_document, HTML::TagNames::title, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); MUST(head_element->append_child(title_element)); + // 2. Append a new Text node, with its data set to title (which could be the empty string) and its node document set to doc, to the title element created earlier. auto text_node = heap().allocate(realm(), html_document, title); MUST(title_element->append_child(*text_node)); } + // 7. Append the result of creating an element given doc, body, and the HTML namespace, to the html element created earlier. auto body_element = create_element(html_document, HTML::TagNames::body, Namespace::HTML).release_value_but_fixme_should_propagate_errors(); MUST(html_element->append_child(body_element)); + // 8. doc’s origin is this’s associated document’s origin. html_document->set_origin(document().origin()); + // 9. Return doc. return html_document; } // https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype WebIDL::ExceptionOr> DOMImplementation::create_document_type(DeprecatedString const& qualified_name, DeprecatedString const& public_id, DeprecatedString const& system_id) { + // 1. Validate qualifiedName. TRY(Document::validate_qualified_name(realm(), qualified_name)); + + // 2. Return a new doctype, with qualifiedName as its name, publicId as its public ID, and systemId as its system ID, and with its node document set to the associated document of this. auto document_type = DocumentType::create(document()); document_type->set_name(qualified_name); document_type->set_public_id(public_id); diff --git a/Userland/Libraries/LibWeb/DOM/DOMImplementation.h b/Userland/Libraries/LibWeb/DOM/DOMImplementation.h index 410bf623aa7..d6bcb43249e 100644 --- a/Userland/Libraries/LibWeb/DOM/DOMImplementation.h +++ b/Userland/Libraries/LibWeb/DOM/DOMImplementation.h @@ -25,7 +25,11 @@ public: WebIDL::ExceptionOr> create_document_type(DeprecatedString const& qualified_name, DeprecatedString const& public_id, DeprecatedString const& system_id); // https://dom.spec.whatwg.org/#dom-domimplementation-hasfeature - bool has_feature() const { return true; } + bool has_feature() const + { + // The hasFeature() method steps are to return true. + return true; + } private: explicit DOMImplementation(Document&);