Skip to content

Truly independent web browser

LibWeb: Reveal and position focus after text fragments

Sam Atkinsa6f93a225c6a
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index d419a5f8e4c..4d6a9785b7e 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -4680,22 +4680,17 @@ void Document::scroll_to_the_fragment(bool allow_text_directive_scroll)
auto range = scroll_target.get<GC::Ptr<Range>>();
VERIFY(range);
- // 2. Set target to be the first common ancestor of target's start node and end node.
- GC::Ptr<Node> target_node = range->common_ancestor_container();
-
- // 3. While target is non-null and is not an element, set target to target's parent.
- while (target_node && !is<Element>(*target_node))
- target_node = target_node->parent();
- if (target_node)
- target = static_cast<Element*>(target_node.ptr());
-
// INTEROP: The first common ancestor can be far larger than the matched passage. Other
// engines use the start container's nearest element so that :target, reveal,
// and focus effects stay attached to the beginning that is scrolled into view.
// https://github.com/WICG/scroll-to-text-fragment/issues/259
- target_node = range->start_container();
+ // The draft also does not define how to cross a shadow root when finding that
+ // element. Follow the shadow host so a range beginning in a direct Text child
+ // of a ShadowRoot still has a target.
+ // https://github.com/WICG/scroll-to-text-fragment/issues/190
+ GC::Ptr<Node> target_node = range->start_container();
while (target_node && !is<Element>(*target_node))
- target_node = target_node->parent();
+ target_node = target_node->parent_or_shadow_host();
if (target_node)
target = static_cast<Element*>(target_node.ptr());
}
@@ -4728,7 +4723,7 @@ void Document::scroll_to_the_fragment(bool allow_text_directive_scroll)
GC::Ptr<Node> ancestor = target;
// 3. While ancestor has a parent node within the flat tree:
- while (ancestor && ancestor->parent()) {
+ while (ancestor && ancestor->flat_tree_parent()) {
// 1. If ancestor has a hidden attribute in the Hidden Until Found state, then
// append (ancestor, "until-found") to ancestorsToReveal.
if (auto* element = as_if<Element>(ancestor.ptr())) {
@@ -4742,7 +4737,7 @@ void Document::scroll_to_the_fragment(bool allow_text_directive_scroll)
// 2. If ancestor is slotted into the second slot of a details element which
// does not have an open attribute, then append (ancestor's parent node,
// "details") to ancestorsToReveal.
- auto* parent_element = as_if<Element>(ancestor->parent());
+ auto* parent_element = as_if<Element>(ancestor->flat_tree_parent());
if (parent_element
&& parent_element->namespace_uri() == Namespace::HTML
&& parent_element->local_name() == HTML::TagNames::details
@@ -4764,7 +4759,7 @@ void Document::scroll_to_the_fragment(bool allow_text_directive_scroll)
}
// 3. Set ancestor to the parent node of ancestor within the flat tree.
- ancestor = ancestor->parent();
+ ancestor = ancestor->flat_tree_parent();
}
// 4. For each (ancestorToReveal, revealType) of ancestorsToReveal:
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 7edbf913403..ab710c269f5 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -2822,7 +2822,9 @@ EventResult EventHandler::focus_next_element()
return EventResult::Dropped;
};
- auto node = m_navigable->active_document()->focused_area();
+ auto node = m_navigable->active_document()->sequential_focus_navigation_starting_point();
+ if (!node)
+ node = m_navigable->active_document()->focused_area();
if (!node)
return set_focus_to_first_focusable_element();
@@ -2857,7 +2859,9 @@ EventResult EventHandler::focus_previous_element()
return EventResult::Dropped;
};
- auto node = m_navigable->active_document()->focused_area();
+ auto node = m_navigable->active_document()->sequential_focus_navigation_starting_point();
+ if (!node)
+ node = m_navigable->active_document()->focused_area();
if (!node)
return set_focus_to_last_focusable_element();
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 4fcfd2bc34d..4455f25ad0a 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -532,7 +532,9 @@ Text/input/wpt-import/content-security-policy/unsafe-eval/eval-scripts-setTimeou
; scroll-to-text-fragment tests all rely on loading/fetching other pages.
Text/input/scroll-to-text-fragment/history.html
+Text/input/scroll-to-text-fragment/searchable-text-visibility.html
Text/input/wpt-import/scroll-to-text-fragment/find-range-from-text-directive.html
+Text/input/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.html
Text/input/wpt-import/scroll-to-text-fragment/force-load-at-top.html
Text/input/wpt-import/scroll-to-text-fragment/iframe-target.html
Text/input/wpt-import/scroll-to-text-fragment/iframes.sub.html
diff --git a/Tests/LibWeb/Text/data/scroll-to-text-fragment-hidden-content-target.html b/Tests/LibWeb/Text/data/scroll-to-text-fragment-hidden-content-target.html
new file mode 100644
index 00000000000..e21d58b89b1
--- /dev/null
+++ b/Tests/LibWeb/Text/data/scroll-to-text-fragment-hidden-content-target.html
@@ -0,0 +1,57 @@
+<!doctype html>
+<meta charset="utf-8">
+<style>
+ body { margin: 0; }
+ .spacer { height: 120vh; }
+ #scroller { height: 100px; overflow: auto; }
+ #inner-spacer { height: 400px; }
+</style>
+<script>
+ const mode = new URL(location.href).searchParams.get("mode");
+ if (mode === "hidden-shadow") {
+ document.write(`
+ <div id="scroller">
+ <div id="inner-spacer"></div>
+ <details id="details">
+ <summary>Summary</summary>
+ <div hidden="until-found" id="hidden"><span id="host"></span></div>
+ </details>
+ </div>
+ `);
+ const shadow = document.querySelector("#host").attachShadow({ mode: "open" });
+ shadow.innerHTML = "<span>hidden inline</span> <span>passage</span>";
+ } else if (mode === "bare-shadow") {
+ document.write(`
+ <div class="spacer"></div>
+ <div id="host"></div>
+ `);
+ const shadow = document.querySelector("#host").attachShadow({ mode: "open" });
+ shadow.append("bare shadow passage");
+ } else {
+ document.write(`
+ <div hidden="until-found" id="hidden" style="visibility: hidden">invisible passage</div>
+ <div class="spacer"></div>
+ <div id="fallback">element fallback</div>
+ `);
+ }
+
+ window.addEventListener("load", () => {
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ if (mode === "hidden-shadow") {
+ internals.signalTestIsDone(
+ `Hidden ancestor revealed: ${!document.querySelector("#hidden").hasAttribute("hidden")}\n`
+ + `Details ancestor opened: ${document.querySelector("#details").open}\n`
+ + `Outer shadow ancestor scrolled: ${document.querySelector("#scroller").scrollTop > 0}\n`);
+ } else if (mode === "bare-shadow") {
+ internals.signalTestIsDone(`Bare shadow text scrolled: ${scrollY > 0}\n`);
+ } else {
+ const fallbackRect = document.querySelector("#fallback").getBoundingClientRect();
+ internals.signalTestIsDone(
+ `Visibility-hidden content remained hidden: ${document.querySelector("#hidden").getAttribute("hidden") === "until-found"}\n`
+ + `Element fragment fallback used: ${fallbackRect.top >= 0 && fallbackRect.top < innerHeight}\n`);
+ }
+ });
+ });
+ });
+</script>
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-bare-shadow-text.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-bare-shadow-text.txt
new file mode 100644
index 00000000000..ef4b0f548fe
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-bare-shadow-text.txt
@@ -0,0 +1 @@
+Bare shadow text scrolled: true
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-hidden-shadow-content.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-hidden-shadow-content.txt
new file mode 100644
index 00000000000..b90a75ec122
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-hidden-shadow-content.txt
@@ -0,0 +1,3 @@
+Hidden ancestor revealed: true
+Details ancestor opened: true
+Outer shadow ancestor scrolled: true
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-visibility-hidden.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-visibility-hidden.txt
new file mode 100644
index 00000000000..80a52b7fc7a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-visibility-hidden.txt
@@ -0,0 +1,2 @@
+Visibility-hidden content remained hidden: true
+Element fragment fallback used: true
diff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment/searchable-text-visibility.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment/searchable-text-visibility.txt
new file mode 100644
index 00000000000..aef375821df
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment/searchable-text-visibility.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass A rendered hidden element below a visibility-hidden ancestor remains searchable
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.txt
new file mode 100644
index 00000000000..7420600971a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Text fragment with suffix should only reveal the matching details element
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/sequential-focus.txt b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/sequential-focus.txt
index e4c8a067629..2835a1be7e6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/sequential-focus.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/scroll-to-text-fragment/sequential-focus.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Text Fragment focus behavior
\ No newline at end of file
+1 Pass
+Pass Text Fragment focus behavior
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-bare-shadow-text.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-bare-shadow-text.html
new file mode 100644
index 00000000000..badeb15198f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-bare-shadow-text.html
@@ -0,0 +1,11 @@
+<!doctype html>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#invoking-text-directives">
+<script src="include.js"></script>
+<script>
+ asyncTest(async () => {
+ const targetURL = new URL("../data/scroll-to-text-fragment-hidden-content-target.html", location.href);
+ targetURL.searchParams.set("mode", "bare-shadow");
+ targetURL.hash = ":~:text=bare%20shadow%20passage";
+ location.href = targetURL;
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-bare-shadow-text.html.headers b/Tests/LibWeb/Text/input/scroll-to-text-fragment-bare-shadow-text.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-bare-shadow-text.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-hidden-shadow-content.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-hidden-shadow-content.html
new file mode 100644
index 00000000000..1c9f23cc2ef
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-hidden-shadow-content.html
@@ -0,0 +1,12 @@
+<!doctype html>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document">
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#invoking-text-directives">
+<script src="include.js"></script>
+<script>
+ asyncTest(async () => {
+ const targetURL = new URL("../data/scroll-to-text-fragment-hidden-content-target.html", location.href);
+ targetURL.searchParams.set("mode", "hidden-shadow");
+ targetURL.hash = ":~:text=hidden%20inline%20passage";
+ location.href = targetURL;
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-visibility-hidden.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-visibility-hidden.html
new file mode 100644
index 00000000000..5dc804829b8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-visibility-hidden.html
@@ -0,0 +1,12 @@
+<!doctype html>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document">
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#invoking-text-directives">
+<script src="include.js"></script>
+<script>
+ asyncTest(async () => {
+ const targetURL = new URL("../data/scroll-to-text-fragment-hidden-content-target.html", location.href);
+ targetURL.searchParams.set("mode", "visibility-hidden");
+ targetURL.hash = "fallback:~:text=invisible%20passage";
+ location.href = targetURL;
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment/searchable-text-visibility.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment/searchable-text-visibility.html
new file mode 100644
index 00000000000..b56eade1093
--- /dev/null
+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment/searchable-text-visibility.html
@@ -0,0 +1,42 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Text fragments honor computed text visibility</title>
+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#finding-ranges-in-a-document">
+<script src="../wpt-import/resources/testharness.js"></script>
+<script src="../wpt-import/resources/testharnessreport.js"></script>
+
+<style>
+ body {
+ height: 4000px;
+ margin: 0;
+ }
+
+ #spacer {
+ height: 3000px;
+ }
+
+ #rendered-hidden-ancestor {
+ display: block;
+ visibility: hidden;
+ }
+
+ #target {
+ visibility: visible;
+ }
+</style>
+
+<div id="spacer"></div>
+<div id="rendered-hidden-ancestor" hidden>
+ <span id="target">searchable visibility target</span>
+</div>
+
+<script>
+promise_test(async () => {
+ location.href = "#:~:text=searchable%20visibility%20target";
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+
+ const targetRect = target.getBoundingClientRect();
+ assert_true(scrollY > 0, "the text fragment scrolled the document");
+ assert_true(targetRect.top >= 0 && targetRect.top <= innerHeight, "the target is visible in the viewport");
+}, "A rendered hidden element below a visibility-hidden ancestor remains searchable");
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.html b/Tests/LibWeb/Text/input/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.html
new file mode 100644
index 00000000000..1d53754de39
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/scroll-to-text-fragment/find-range-from-text-directive-no-reveal.html
@@ -0,0 +1,36 @@
+<!doctype html>
+<title>Finding the text directive range must not reveal other hidden elements</title>
+<meta charset=utf-8>
+<link rel="help" href="https://bugzil.la/1970909">
+<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
+<link rel="author" title="Jan-Niklas Jaeschke" href="mailto:[email protected]">
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<body>
+
+<div id="hidden1" hidden="until-found">abc</div>
+<details id="details1">
+ <summary>First Section</summary>
+ <div>abc</div>
+</details>
+<details id="details2">
+ <summary>Second Section</summary>
+ <div>abc def</div>
+</details>
+
+<script>
+// Test that navigating to a text fragment does not open hidden=until-found or
+// closed <details> elements that contain an invalid match for the text fragment.
+promise_test(async t => {
+
+ window.location.hash = ':~:text=abc,-def';
+
+ await new Promise(resolve => details2.addEventListener('toggle', resolve));
+
+ assert_equals(hidden1.hidden, 'until-found', 'hidden=until-found element should remain hidden');
+ assert_false(details1.open, 'closed <details> element should remain closed');
+ assert_true(details2.open, '<details> element containing the match should be opened');
+}, 'Text fragment with suffix should only reveal the matching details element');
+</script>
+</body>
+</html>