From 619df0bc2c6e2e0f0fcdf689b230d935472d174f Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 12 Jan 2025 17:17:21 +0000 Subject: [PATCH] LibWeb: Don't crash when upgrading custom element with a bad constructor Previously, a crash would occur when attempting to throw an error in this case because the method used to create the exception tried to get the current realm from the execution context stack, which is empty. The realm is now passed explicitly when constructing the error, avoiding the crash. --- Libraries/LibWeb/DOM/ElementFactory.cpp | 4 +- .../parser-fallsback-to-unknown-element.txt | 9 ++ .../parser-fallsback-to-unknown-element.html | 91 +++++++++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.html diff --git a/Libraries/LibWeb/DOM/ElementFactory.cpp b/Libraries/LibWeb/DOM/ElementFactory.cpp index ef07a0120b6..4f73431e445 100644 --- a/Libraries/LibWeb/DOM/ElementFactory.cpp +++ b/Libraries/LibWeb/DOM/ElementFactory.cpp @@ -568,8 +568,6 @@ WebIDL::ExceptionOr> create_element(Document& document, FlyStri // 1. If the synchronous custom elements flag is set, then run these steps while catching any exceptions: if (synchronous_custom_elements_flag) { auto synchronously_upgrade_custom_element = [&]() -> JS::ThrowCompletionOr> { - auto& vm = document.vm(); - // 1. Let C be definition’s constructor. auto& constructor = definition->constructor(); @@ -578,7 +576,7 @@ WebIDL::ExceptionOr> create_element(Document& document, FlyStri // NOTE: IDL does not currently convert the object for us, so we will have to do it here. if (!result.has_value() || !result->is_object() || !is(result->as_object())) - return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "HTMLElement"sv); + return JS::throw_completion(JS::TypeError::create(realm, "Custom element constructor must return an object that implements HTMLElement"_string)); GC::Ref element = verify_cast(result->as_object()); diff --git a/Tests/LibWeb/Text/expected/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.txt b/Tests/LibWeb/Text/expected/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.txt new file mode 100644 index 00000000000..6ec4029fe7f --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.txt @@ -0,0 +1,9 @@ +Harness status: OK + +Found 4 tests + +4 Pass +Pass HTML parser must create a fallback HTMLUnknownElement when a custom element constructor returns a Text node +Pass HTML parser must create a fallback HTMLUnknownElement when a custom element constructor returns non-Element object +Pass HTML parser must create a fallback HTMLUnknownElement when a custom element constructor does not call super() +Pass HTML parser must create a fallback HTMLUnknownElement when a custom element constructor throws an exception \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.html b/Tests/LibWeb/Text/input/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.html new file mode 100644 index 00000000000..b19f54a6dec --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/custom-elements/parser/parser-fallsback-to-unknown-element.html @@ -0,0 +1,91 @@ + + + +Custom Elements: Changes to the HTML parser + + + + + + + + +
+ + + + + + + +