Skip to content

Commit

Permalink
LibWeb: Exclude inert elements from find in page queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tcl3 committed Feb 9, 2025
1 parent c12bbc4 commit 8db9b3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Libraries/LibWeb/Layout/Viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ void Viewport::update_text_blocks()
return TraversalDecision::Continue;
}

if (layout_node.is_text_node()) {
auto const& text_node = as<Layout::TextNode>(layout_node);
auto& dom_node = const_cast<DOM::Text&>(text_node.dom_node());
if (text_positions.is_empty()) {
text_positions.empend(dom_node);
} else {
text_positions.empend(dom_node, current_start_position);
if (auto* text_node = as_if<Layout::TextNode>(layout_node)) {
// https://html.spec.whatwg.org/multipage/interaction.html#inert-subtrees
// When a node is inert:
// - The user agent should ignore the node for the purposes of find-in-page.
if (auto& dom_node = const_cast<DOM::Text&>(text_node->dom_node()); !dom_node.is_inert()) {
if (text_positions.is_empty()) {
text_positions.empend(dom_node);
} else {
text_positions.empend(dom_node, current_start_position);
}

auto const& current_node_text = text_node->text_for_rendering();
current_start_position += current_node_text.bytes_as_string_view().length();
builder.append(move(current_node_text));
}

auto const& current_node_text = text_node.text_for_rendering();
current_start_position += current_node_text.bytes_as_string_view().length();
builder.append(move(current_node_text));
}

return TraversalDecision::Continue;
Expand Down
2 changes: 2 additions & 0 deletions Tests/LibWeb/Text/expected/HTML/Window-find-inert.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
window.find("inert") initial result: true
window.find("inert") second call: false
12 changes: 12 additions & 0 deletions Tests/LibWeb/Text/input/HTML/Window-find-inert.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<div>not inert</div>
<div inert>inert</div>
<script>
test(() => {
let initialResult = window.find("inert");
let secondCallResult = window.find("inert");
println(`window.find("inert") initial result: ${initialResult}`);
println(`window.find("inert") second call: ${secondCallResult}`);
});
</script>

0 comments on commit 8db9b3a

Please sign in to comment.