Skip to content

Commit

Permalink
LibWeb: Fix CloseWatcher constructor for detached iframes
Browse files Browse the repository at this point in the history
This fixes the last subtest in /close-watcher/frame-removal.html :)
  • Loading branch information
beuss-git authored and trflynn89 committed Oct 15, 2024
1 parent 14b2e58 commit 63d9ed9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PASS
25 changes: 25 additions & 0 deletions Tests/LibWeb/Text/input/HTML/CloseWatcher-detached-iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script src="../include.js"></script>
<script>
promiseTest(async () => {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);

await new Promise(resolve => iframe.onload = resolve);

const iframeCloseWatcher = iframe.contentWindow.CloseWatcher;
const iframeDOMException = iframe.contentWindow.DOMException;

iframe.remove();

try {
new iframeCloseWatcher();
println("FAIL");
} catch (error) {
if (error instanceof iframeDOMException && error.name === "InvalidStateError") {
println("PASS");
} else {
println(`FAIL: CloseWatcher construction threw unexpected error: ${error.name}`);
}
}
});
</script>
10 changes: 8 additions & 2 deletions Userland/Libraries/LibWeb/HTML/CloseWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <LibWeb/HTML/CloseWatcher.h>
#include <LibWeb/HTML/CloseWatcherManager.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/HTML/Window.h>

namespace Web::HTML {
Expand Down Expand Up @@ -41,9 +42,14 @@ JS::NonnullGCPtr<CloseWatcher> CloseWatcher::establish(HTML::Window& window)
// https://html.spec.whatwg.org/multipage/interaction.html#dom-closewatcher
WebIDL::ExceptionOr<JS::NonnullGCPtr<CloseWatcher>> CloseWatcher::construct_impl(JS::Realm& realm, CloseWatcherOptions const& options)
{
// 1. If this's relevant global object's associated Document is not fully active, then return an "InvalidStateError" DOMException.
// FIXME: Not in spec explicitly, but this should account for detached iframes too. See /close-watcher/frame-removal.html WPT.
auto& window = verify_cast<HTML::Window>(realm.global_object());

// NOTE: Not in spec explicitly, but this should account for detached iframes too. See /close-watcher/frame-removal.html WPT.
auto navigable = window.navigable();
if (navigable && navigable->has_been_destroyed())
return WebIDL::InvalidStateError::create(realm, "The iframe has been detached"_string);

// 1. If this's relevant global object's associated Document is not fully active, then return an "InvalidStateError" DOMException.
if (!window.associated_document().is_fully_active())
return WebIDL::InvalidStateError::create(realm, "The document is not fully active."_string);

Expand Down

0 comments on commit 63d9ed9

Please sign in to comment.