Skip to content

Commit

Permalink
LibWeb: Make DOMImplementation.createHTMLDocument() create HTMLDocument
Browse files Browse the repository at this point in the history
Prior to this change, this API would actually create an XML Document(!)
  • Loading branch information
awesomekling committed Mar 11, 2024
1 parent b9bacb3 commit 99ca2cc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[object HTMLDocument]
true
[object HTMLDocument]
true
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<body>
<script src="../include.js"></script>
<script>
test(() => {
println(document);
println(document instanceof HTMLDocument);
let newDocument = document.implementation.createHTMLDocument();
println(newDocument);
println(newDocument instanceof HTMLDocument);
});
</script>
</body>
4 changes: 3 additions & 1 deletion Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/DOM/XMLDocument.h>
#include <LibWeb/HTML/HTMLDocument.h>
#include <LibWeb/HTML/Origin.h>
#include <LibWeb/Namespace.h>

Expand Down Expand Up @@ -91,10 +92,11 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_docume
JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(Optional<String> const& title) const
{
// 1. Let doc be a new document that is an HTML document.
auto html_document = Document::create(realm());
auto html_document = HTML::HTMLDocument::create(realm());

// 2. Set doc’s content type to "text/html".
html_document->set_content_type("text/html"_string);
html_document->set_document_type(DOM::Document::Type::HTML);

html_document->set_ready_for_post_load_tasks(true);

Expand Down
6 changes: 6 additions & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ JS::NonnullGCPtr<HTMLDocument> HTMLDocument::create(JS::Realm& realm, URL const&
return realm.heap().allocate<HTMLDocument>(realm, realm, url);
}

void HTMLDocument::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLDocumentPrototype>(realm, "HTMLDocument"_fly_string));
}

}
4 changes: 3 additions & 1 deletion Userland/Libraries/LibWeb/HTML/HTMLDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Web::HTML {
// https://github.com/whatwg/html/issues/4792
// https://github.com/whatwg/dom/issues/221
class HTMLDocument final : public DOM::Document {
JS_CELL(HTMLDocument, DOM::Document);
WEB_PLATFORM_OBJECT(HTMLDocument, DOM::Document);
JS_DECLARE_ALLOCATOR(HTMLDocument);

public:
Expand All @@ -25,6 +25,8 @@ class HTMLDocument final : public DOM::Document {
WebIDL::ExceptionOr<JS::NonnullGCPtr<HTMLDocument>> construct_impl(JS::Realm&);

private:
virtual void initialize(JS::Realm&) override;

HTMLDocument(JS::Realm&, URL const&);
};

Expand Down

0 comments on commit 99ca2cc

Please sign in to comment.