Truly independent web browser
LibWeb: Search across a <br> as across a space
Aliaksandr Kalenik8c9113b08ece
diff --git a/Libraries/LibWeb/Layout/SearchableText.cpp b/Libraries/LibWeb/Layout/SearchableText.cppindex b3bb3fa66fa..33a8515af96 100644--- a/Libraries/LibWeb/Layout/SearchableText.cpp+++ b/Libraries/LibWeb/Layout/SearchableText.cpp@@ -12,6 +12,7 @@#include <LibWeb/DOM/ShadowRoot.h>#include <LibWeb/DOM/Text.h>#include <LibWeb/HTML/AttributeNames.h>+#include <LibWeb/HTML/HTMLBRElement.h>#include <LibWeb/HTML/HTMLDetailsElement.h>#include <LibWeb/HTML/HTMLElement.h>#include <LibWeb/HTML/TagNames.h>@@ -315,6 +316,18 @@ SearchableText SearchableText::collect(Viewport& viewport)if (auto* element = as_if<DOM::Element>(node); element && has_block_level_display(*element))break;+ // AD-HOC: A line break separates the words around it as collapsible white space does, and collapses with+ // the white space next to it. The space it stands for covers no DOM text, at the end of the text+ // before it.+ if (is<HTML::HTMLBRElement>(node) && node.has_layout_box() && !pending_space_dom_node && !segments.is_empty()) {+ if (auto last_text = segments.last().dom_node.ptr()) {+ pending_space_dom_node = last_text.ptr();+ pending_space_dom_offset = expected_dom_offset;+ pending_space_dom_end_offset = expected_dom_offset;+ pending_space_searchability = segments.last().searchability;+ }+ }+// 2. If curNode is search invisible:if (auto* element = as_if<DOM::Element>(node); element && is_search_invisible(*element)) {// 1. Set curNode to the next node, in shadow-including tree order, that isn't a shadow-includingdiff --git a/Libraries/LibWeb/Rust/src/layout/text_queries.rs b/Libraries/LibWeb/Rust/src/layout/text_queries.rsindex 3ca070db536..11edd405078 100644--- a/Libraries/LibWeb/Rust/src/layout/text_queries.rs+++ b/Libraries/LibWeb/Rust/src/layout/text_queries.rs@@ -5,7 +5,7 @@*/use super::LayoutNodeArena;-use super::node_data::{GENERATED_FOR_FIRST_LETTER, NodeSlotId};+use super::node_data::{GENERATED_FOR_FIRST_LETTER, NodeKind, NodeSlotId};use super::node_facts::{kind_is_box, kind_is_text, node_style_view};use super::rendered_text::{FfiTextSourceRange, RenderedTextBoundary, ensure_text_content};use crate::css::css_enums::{visibility, white_space_collapse};@@ -70,6 +70,11 @@ impl MappedText {});}+ /// Where the text appended last ends.+ fn end(&self) -> Option<TextPosition> {+ self.runs.last().map(|run| run.end)+ }+fn position(&self, offset: usize, boundary: RenderedTextBoundary) -> Option<TextPosition> {if offset > self.text.len() || self.runs.is_empty() {return None;@@ -269,6 +274,16 @@ impl SearchTextBuilder {self.pending_space = None;}+ /// A line break separates the words around it as collapsible white space does, and collapses with the white space+ /// next to it. The space it stands for covers no DOM text, at the end of the text before it.+ fn break_line(&mut self) {+ if self.pending_space.is_none()+ && let Some(end) = self.current.end()+ {+ self.pending_space = Some((end, end));+ }+ }+fn append(&mut self, node: NodeSlotId, text: &[u16], collapse: bool) {for (offset, &unit) in text.iter().enumerate() {let start = TextPosition { node, offset };@@ -299,6 +314,7 @@ fn text_node_identity(arena: &LayoutNodeArena, node: NodeSlotId) -> u32 {enum SearchNode {Skip,Break,+ LineBreak,Text(StyleNodeID),}@@ -318,6 +334,9 @@ fn search_node(arena: &LayoutNodeArena, node: NodeSlotId) -> SearchNode {if node_style_view(data).is_some_and(|style| style.display().is_none()) {return SearchNode::Skip;}+ if data.kind.get() == NodeKind::BreakNode {+ return SearchNode::LineBreak;+ }let pseudo = data.generated_for.get();if kind_is_box(data.kind.get()) || (pseudo != 0 && pseudo != GENERATED_FOR_FIRST_LETTER) {return SearchNode::Break;@@ -361,6 +380,7 @@ fn ensure_searchable_text(match search_node(arena, node) {SearchNode::Skip => {}SearchNode::Break => builder.flush(),+ SearchNode::LineBreak => builder.break_line(),SearchNode::Text(text) => {if !is_searchable(text) {continue;@@ -461,7 +481,6 @@ pub extern "C" fn layout_text_word_range(#[cfg(test)]mod tests {use super::*;- use crate::layout::node_data::NodeKind;use crate::layout::rendered_text::{RenderedTextEdit, TextContent};fn node(diff --git a/Tests/LibWeb/Text/expected/HTML/window-find-across-line-break.txt b/Tests/LibWeb/Text/expected/HTML/window-find-across-line-break.txtnew file mode 100644index 00000000000..11b7e647da0--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/window-find-across-line-break.txt@@ -0,0 +1,6 @@+'alpha beta': true+'gamma delta': true+'epsilon zeta': true+'alphabeta': false+'alpha ' ends in its text node: true+' eta': falsediff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-across-line-break-and-white-space.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-across-line-break-and-white-space.txtnew file mode 100644index 00000000000..9e5c767f4ae--- /dev/null+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-across-line-break-and-white-space.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass A line break followed by white space separates words\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/scroll-to-text-fragment-across-line-break.txt b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-across-line-break.txtnew file mode 100644index 00000000000..d3dcd5b2b2a--- /dev/null+++ b/Tests/LibWeb/Text/expected/scroll-to-text-fragment-across-line-break.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass A line break with no white space next to it separates words\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/window-find-across-line-break.html b/Tests/LibWeb/Text/input/HTML/window-find-across-line-break.htmlnew file mode 100644index 00000000000..bcee14d1830--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/window-find-across-line-break.html@@ -0,0 +1,29 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<p id="no-whitespace">alpha<br>beta</p>+<p id="whitespace-after">gamma<br>+<b>delta</b></p>+<p id="whitespace-around">epsilon <br> zeta</p>+<p id="block-start"><br>eta</p>+<script>+ test(() => {+ function find(query) {+ window.getSelection().removeAllRanges();+ return window.find(query);+ }++ // A line break separates words as a space does, whether or not white space sits next to it.+ println("'alpha beta': " + find("alpha beta"));+ println("'gamma delta': " + find("gamma delta"));+ println("'epsilon zeta': " + find("epsilon zeta"));+ println("'alphabeta': " + find("alphabeta"));++ // A match that ends on the line break ends where the text before it ends.+ find("alpha ");+ let range = window.getSelection().getRangeAt(0);+ println("'alpha ' ends in its text node: " + (range.endContainer.nodeType === Node.TEXT_NODE && range.endOffset === 5));++ // A line break at the start of a block adds no space.+ println("' eta': " + find(" eta"));+ });+</script>diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-across-line-break-and-white-space.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-across-line-break-and-white-space.htmlnew file mode 100644index 00000000000..afb91dbd8b3--- /dev/null+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-across-line-break-and-white-space.html@@ -0,0 +1,23 @@+<!doctype html>+<meta charset="utf-8">+<title>Text fragment matching across a line break followed by white space</title>+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#find-a-range-from-a-node-list">+<script src="wpt-import/resources/testharness.js"></script>+<script src="wpt-import/resources/testharnessreport.js"></script>++<style>+ #target { margin-top: 2000px; }+</style>++<p id="target">gamma<br>+<b>delta</b></p>++<script>+promise_test(async () => {+ await new Promise(resolve => addEventListener("load", resolve, { once: true }));++ location.href = "#:~:text=gamma%20delta";+ await new Promise(resolve => requestAnimationFrame(resolve));+ assert_greater_than(scrollY, 0, "the words on both sides of the line break matched");+}, "A line break followed by white space separates words");+</script>diff --git a/Tests/LibWeb/Text/input/scroll-to-text-fragment-across-line-break.html b/Tests/LibWeb/Text/input/scroll-to-text-fragment-across-line-break.htmlnew file mode 100644index 00000000000..920bd5aff89--- /dev/null+++ b/Tests/LibWeb/Text/input/scroll-to-text-fragment-across-line-break.html@@ -0,0 +1,22 @@+<!doctype html>+<meta charset="utf-8">+<title>Text fragment matching across a line break</title>+<link rel="help" href="https://wicg.github.io/scroll-to-text-fragment/#find-a-range-from-a-node-list">+<script src="wpt-import/resources/testharness.js"></script>+<script src="wpt-import/resources/testharnessreport.js"></script>++<style>+ #target { margin-top: 2000px; }+</style>++<p id="target">alpha<br>beta</p>++<script>+promise_test(async () => {+ await new Promise(resolve => addEventListener("load", resolve, { once: true }));++ location.href = "#:~:text=alpha%20beta";+ await new Promise(resolve => requestAnimationFrame(resolve));+ assert_greater_than(scrollY, 0, "the words on both sides of the line break matched");+}, "A line break with no white space next to it separates words");+</script>