Browse Source

LibWeb: Make factory methods of DOM::Document fallible

Kenneth Myhra 2 years ago
parent
commit
0d9076c9f5

+ 2 - 2
Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp

@@ -49,7 +49,7 @@ void DOMImplementation::visit_edges(Cell::Visitor& visitor)
 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.
-    auto xml_document = Document::create(realm());
+    auto xml_document = TRY(Document::create(realm()));
 
     xml_document->set_ready_for_post_load_tasks(true);
 
@@ -79,7 +79,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> DOMImplementation::create_docume
 // https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
 JS::NonnullGCPtr<Document> DOMImplementation::create_html_document(DeprecatedString const& title) const
 {
-    auto html_document = Document::create(realm());
+    auto html_document = Document::create(realm()).release_value_but_fixme_should_propagate_errors();
 
     html_document->set_content_type("text/html");
     html_document->set_ready_for_post_load_tasks(true);

+ 6 - 6
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -126,7 +126,7 @@ static JS::NonnullGCPtr<HTML::BrowsingContext> obtain_a_browsing_context_to_use_
 }
 
 // https://html.spec.whatwg.org/multipage/browsing-the-web.html#initialise-the-document-object
-JS::NonnullGCPtr<Document> Document::create_and_initialize(Type type, DeprecatedString content_type, HTML::NavigationParams navigation_params)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> Document::create_and_initialize(Type type, DeprecatedString content_type, HTML::NavigationParams navigation_params)
 {
     // 1. Let browsingContext be the result of the obtaining a browsing context to use for a navigation response
     //    given navigationParams's browsing context, navigationParams's final sandboxing flag set,
@@ -230,7 +230,7 @@ JS::NonnullGCPtr<Document> Document::create_and_initialize(Type type, Deprecated
     //    FIXME: and cross-origin opener policy is navigationParams's cross-origin opener policy,
     //    FIXME: load timing info is loadTimingInfo,
     //    and navigation id is navigationParams's id.
-    auto document = Document::create(window->realm());
+    auto document = TRY(Document::create(window->realm()));
     document->m_type = type;
     document->m_content_type = move(content_type);
     document->set_origin(navigation_params.origin);
@@ -283,14 +283,14 @@ JS::NonnullGCPtr<Document> Document::create_and_initialize(Type type, Deprecated
     return document;
 }
 
-JS::NonnullGCPtr<Document> Document::construct_impl(JS::Realm& realm)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> Document::construct_impl(JS::Realm& realm)
 {
     return Document::create(realm);
 }
 
-JS::NonnullGCPtr<Document> Document::create(JS::Realm& realm, AK::URL const& url)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> Document::create(JS::Realm& realm, AK::URL const& url)
 {
-    return realm.heap().allocate<Document>(realm, realm, url).release_allocated_value_but_fixme_should_propagate_errors();
+    return MUST_OR_THROW_OOM(realm.heap().allocate<Document>(realm, realm, url));
 }
 
 Document::Document(JS::Realm& realm, const AK::URL& url)
@@ -2330,7 +2330,7 @@ JS::NonnullGCPtr<DOM::Document> Document::appropriate_template_contents_owner_do
         // 1. If doc does not yet have an associated inert template document, then:
         if (!m_associated_inert_template_document) {
             // 1. Let new doc be a new Document (whose browsing context is null). This is "a Document created by this algorithm" for the purposes of the step above.
-            auto new_document = DOM::Document::create(realm());
+            auto new_document = DOM::Document::create(realm()).release_value_but_fixme_should_propagate_errors();
             new_document->m_created_for_appropriate_template_contents = true;
 
             // 2. If doc is an HTML document, mark new doc as an HTML document also.

+ 3 - 3
Userland/Libraries/LibWeb/DOM/Document.h

@@ -82,10 +82,10 @@ public:
         HTML
     };
 
-    static JS::NonnullGCPtr<Document> create_and_initialize(Type, DeprecatedString content_type, HTML::NavigationParams);
+    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create_and_initialize(Type, DeprecatedString content_type, HTML::NavigationParams);
 
-    static JS::NonnullGCPtr<Document> create(JS::Realm&, AK::URL const& url = "about:blank"sv);
-    static JS::NonnullGCPtr<Document> construct_impl(JS::Realm&);
+    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create(JS::Realm&, AK::URL const& url = "about:blank"sv);
+    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> construct_impl(JS::Realm&);
     virtual ~Document() override;
 
     // https://w3c.github.io/selection-api/#dom-document-getselection

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Node.cpp

@@ -730,7 +730,7 @@ JS::NonnullGCPtr<Node> Node::clone_node(Document* document, bool clone_children)
     else if (is<Document>(this)) {
         // Document
         auto document_ = verify_cast<Document>(this);
-        auto document_copy = Document::create(this->realm(), document_->url());
+        auto document_copy = Document::create(this->realm(), document_->url()).release_value_but_fixme_should_propagate_errors();
 
         // Set copy’s encoding, content type, URL, origin, type, and mode to those of node.
         document_copy->set_encoding(document_->encoding());

+ 1 - 1
Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp

@@ -168,7 +168,7 @@ JS::NonnullGCPtr<BrowsingContext> BrowsingContext::create_a_new_browsing_context
     //     load timing info is loadTimingInfo,
     //     FIXME: navigation id is null,
     //     and which is ready for post-load tasks.
-    auto document = DOM::Document::create(window->realm());
+    auto document = DOM::Document::create(window->realm()).release_value_but_fixme_should_propagate_errors();
 
     // Non-standard
     document->set_window({}, *window);

+ 1 - 1
Userland/Libraries/LibWeb/HTML/DOMParser.cpp

@@ -37,7 +37,7 @@ JS::ThrowCompletionOr<void> DOMParser::initialize(JS::Realm& realm)
 JS::NonnullGCPtr<DOM::Document> DOMParser::parse_from_string(DeprecatedString const& string, Bindings::DOMParserSupportedType type)
 {
     // 1. Let document be a new Document, whose content type is type and url is this's relevant global object's associated Document's URL.
-    auto document = DOM::Document::create(realm(), verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document().url());
+    auto document = DOM::Document::create(realm(), verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document().url()).release_value_but_fixme_should_propagate_errors();
     document->set_content_type(Bindings::idl_enum_to_deprecated_string(type));
 
     // 2. Switch on type:

+ 1 - 1
Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp

@@ -3448,7 +3448,7 @@ DOM::Document& HTMLParser::document()
 Vector<JS::Handle<DOM::Node>> HTMLParser::parse_html_fragment(DOM::Element& context_element, StringView markup)
 {
     // 1. Create a new Document node, and mark it as being an HTML document.
-    auto temp_document = DOM::Document::create(context_element.realm());
+    auto temp_document = DOM::Document::create(context_element.realm()).release_value_but_fixme_should_propagate_errors();
     temp_document->set_document_type(DOM::Document::Type::HTML);
 
     // 2. If the node document of the context element is in quirks mode, then let the Document be in quirks mode.

+ 2 - 9
Userland/Libraries/LibWeb/Loader/FrameLoader.cpp

@@ -313,10 +313,7 @@ void FrameLoader::load_html(StringView html, const AK::URL& url)
         .reserved_environment = {},
         .browsing_context = browsing_context(),
     };
-    auto document = DOM::Document::create_and_initialize(
-        DOM::Document::Type::HTML,
-        "text/html",
-        move(navigation_params));
+    auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", move(navigation_params)).release_value_but_fixme_should_propagate_errors();
     browsing_context().set_active_document(document);
 
     auto parser = HTML::HTMLParser::create(document, html, "utf-8");
@@ -413,11 +410,7 @@ void FrameLoader::resource_did_load()
         .reserved_environment = {},
         .browsing_context = browsing_context(),
     };
-    auto document = DOM::Document::create_and_initialize(
-        DOM::Document::Type::HTML,
-        "text/html",
-        move(navigation_params));
-
+    auto document = DOM::Document::create_and_initialize(DOM::Document::Type::HTML, "text/html", move(navigation_params)).release_value_but_fixme_should_propagate_errors();
     document->set_url(url);
     document->set_encoding(resource()->encoding());
     document->set_content_type(resource()->mime_type());