LibWeb: Add spec comments to DOMImplementation

This commit is contained in:
Shannon Booth 2023-08-12 21:30:16 +12:00 committed by Andreas Kling
parent d3d67857b2
commit d8ae02ef59
Notes: sideshowbarker 2024-07-17 11:30:54 +09:00
2 changed files with 35 additions and 5 deletions

View file

@ -46,72 +46,98 @@ void DOMImplementation::visit_edges(Cell::Visitor& visitor)
// https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_document(DeprecatedString const& namespace_, DeprecatedString const& qualified_name, JS::GCPtr<DocumentType> 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> 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. documents origin is thiss associated documents origin.
xml_document->set_origin(document().origin());
if (namespace_ == Namespace::HTML)
// 7. documents 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<Document> 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 docs 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<DocumentType>(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<Text>(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. docs origin is thiss associated documents origin.
html_document->set_origin(document().origin());
// 9. Return doc.
return html_document;
}
// https://dom.spec.whatwg.org/#dom-domimplementation-createdocumenttype
WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentType>> 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);

View file

@ -25,7 +25,11 @@ public:
WebIDL::ExceptionOr<JS::NonnullGCPtr<DocumentType>> 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&);