Skip to content

Propose visits for window.open() calls, not only target="_blank" links - #227

Open
officialasishkumar wants to merge 1 commit into
hotwired:mainfrom
officialasishkumar:fix/window-open-urls
Open

officialasishkumar wants to merge 1 commit into
hotwired:mainfrom
officialasishkumar:fix/window-open-urls

Conversation

@officialasishkumar

Copy link
Copy Markdown

Fixes #212

HotwireWebChromeClient.onCreateWindow() recovered the new window's url with requestFocusNodeHref(), which reads the href of the focused anchor. A window.open() call has no anchor, so no url was found and the tap silently did nothing. target="_blank" links kept working, which hid the problem until content opened links from JavaScript.

This hands the WebViewTransport a placeholder WebView and proposes a visit to the first location it's asked to load (shouldOverrideUrlLoading(), with onPageStarted() as a fallback). Because it observes the actual navigation instead of inferring it from DOM focus, it covers:

  • target="_blank" links, as before
  • window.open(url), at the top level and inside iframes
  • windows opened first and given a location later, e.g. const w = window.open(); w.location = url. The initial about:blank load is ignored.

The placeholder is never shown or attached. It's destroyed once its location is known, when the page calls close() on it, or when another window replaces it, so at most one is ever kept around. It's destroyed on the next main-loop pass because this can happen inside one of its own client callbacks.

onCreateWindow() now returns true when it handles the window, as the WebChromeClient contract requires after sending the transport message. It still returns false when there's no transport. Windows opened without a user gesture are still blocked by the WebView, because javaScriptCanOpenWindowsAutomatically stays at its default of false.

Added HotwireWebChromeClientTest, which covers proposing the visit from both callbacks, proposing only one visit per window, ignoring about:blank, and destroying the window in each case. 8 of the 9 tests fail without the change.

onCreateWindow() recovered the new window's url with requestFocusNodeHref(),
which reads the href of the focused anchor. A window.open() call has no
anchor, so no url was found and the tap silently did nothing.

Back the new window with a placeholder WebView instead, handed to the
WebViewTransport, and propose a visit to the first location it's asked to
load. This observes the actual navigation, so it covers target="_blank"
links and window.open() alike, including from iframes and windows that are
opened first and given a location afterwards. The window is never shown
and is destroyed once its location is known, when it's closed, or when
another window replaces it.

Fixes hotwired#212
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

onCreateWindow silently drops window.open URLs (only recovers target="_blank" anchors)

1 participant