Skip to content

Truly independent web browser

LibWeb: Record navigables again when focus moves between them

Aliaksandr Kalenikc74377fb5397
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index cb0a03094ae..b2b28fd2fc8 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -222,9 +222,18 @@ void Page::focused_navigable_changed_in_another_page(HTML::CrossProcessId id)
void Page::update_focused_navigable(GC::Ptr<HTML::Navigable> navigable)
{
invalidate_compositor_keyboard_scroll_state();
+ GC::Ptr<HTML::Navigable> previously_focused_navigable = m_focused_navigable.ptr();
m_focused_navigable = navigable;
for (auto const& root : local_roots())
root->set_needs_repaint();
+
+ // A navigable below the top-level traversable paints its focus ring, caret and selection as focused only while it
+ // is the focused navigable, so the two navigables focus moved between record again.
+ for (auto navigable_whose_focus_changed : { previously_focused_navigable, navigable }) {
+ auto* local_navigable = navigable_whose_focus_changed ? as_if<HTML::LocalNavigable>(*navigable_whose_focus_changed) : nullptr;
+ if (auto document = local_navigable ? local_navigable->active_document() : nullptr)
+ Painting::request_document_repaint(*document, InvalidateDisplayList::PaintCommands);
+ }
}
void Page::navigable_document_destroyed(Badge<DOM::Document>, HTML::LocalNavigable& navigable)
diff --git a/Tests/LibWeb/Ref/expected/iframe-selection-repaints-after-focus-leaves-ref.html b/Tests/LibWeb/Ref/expected/iframe-selection-repaints-after-focus-leaves-ref.html
new file mode 100644
index 00000000000..3379cbe61c2
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/iframe-selection-repaints-after-focus-leaves-ref.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<style>
+ body { margin: 0; }
+ iframe { display: block; border: 0; width: 200px; height: 40px; }
+ #outside { width: 20px; height: 20px; outline: none; }
+</style>
+<iframe id="frame" srcdoc="<!DOCTYPE html><style>body { margin: 0; font: 20px monospace; }</style><span id=text>selected</span>"></iframe>
+<div id="outside" tabindex="-1"></div>
+<script>
+ const nextFrame = () => new Promise(resolve => requestAnimationFrame(resolve));
+ frame.addEventListener("load", async () => {
+ outside.focus();
+ frame.contentWindow.getSelection().selectAllChildren(frame.contentDocument.getElementById("text"));
+ await nextFrame();
+ await nextFrame();
+ document.documentElement.classList.remove("reftest-wait");
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/iframe-selection-repaints-after-focus-leaves.html b/Tests/LibWeb/Ref/input/iframe-selection-repaints-after-focus-leaves.html
new file mode 100644
index 00000000000..e4c581f5ff6
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/iframe-selection-repaints-after-focus-leaves.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/iframe-selection-repaints-after-focus-leaves-ref.html">
+<style>
+ body { margin: 0; }
+ iframe { display: block; border: 0; width: 200px; height: 40px; }
+ #outside { width: 20px; height: 20px; outline: none; }
+</style>
+<iframe id="frame" srcdoc="<!DOCTYPE html><style>body { margin: 0; font: 20px monospace; }</style><span id=text>selected</span>"></iframe>
+<div id="outside" tabindex="-1"></div>
+<script>
+ // A selection paints in the inactive selection color once focus leaves its navigable, which no row of the
+ // iframe's document notices.
+ const nextFrame = () => new Promise(resolve => requestAnimationFrame(resolve));
+ frame.addEventListener("load", async () => {
+ frame.contentWindow.focus();
+ frame.contentWindow.getSelection().selectAllChildren(frame.contentDocument.getElementById("text"));
+ await nextFrame();
+ await nextFrame();
+ outside.focus();
+ await nextFrame();
+ await nextFrame();
+ document.documentElement.classList.remove("reftest-wait");
+ });
+</script>
+</html>