Skip to content

Commit

Permalink
LibWeb: Update Range::set_base_and_extent() to latest spec
Browse files Browse the repository at this point in the history
This allows it to work with content inside shadow roots.
  • Loading branch information
tcl3 committed Jul 2, 2024
1 parent 90e57a4 commit 487089b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
After calling selection.setBaseAndExtent():
Selection range count: 1
Selection start offset: 0, end offset: 1
21 changes: 21 additions & 0 deletions Tests/LibWeb/Text/input/set-selection-inside-shadow-root.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script src="include.js"></script>
<div id="root"></div>
<script>
function printSelectionDetails(selection) {
println(`Selection range count: ${selection.rangeCount}`);
if (selection.rangeCount > 0)
println(`Selection start offset: ${selection.getRangeAt(0).startOffset}, end offset: ${selection.getRangeAt(0).endOffset}`);
}

test(() => {
const rootElement = document.getElementById("root");
const shadowRoot = rootElement.attachShadow({ mode: "open" });
shadowRoot.innerHTML = "This is some text";
const selection = window.getSelection();
selection.setBaseAndExtent(shadowRoot, 0, shadowRoot, 1);
println("After calling selection.setBaseAndExtent():");
printSelectionDetails(selection);

shadowRoot.innerHTML = "";
});
</script>
7 changes: 2 additions & 5 deletions Userland/Libraries/LibWeb/Selection/Selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,8 @@ WebIDL::ExceptionOr<void> Selection::set_base_and_extent(JS::NonnullGCPtr<DOM::N
if (focus_offset > focus_node->length())
return WebIDL::IndexSizeError::create(realm(), "Focus offset points outside of the focus node"_fly_string);

// 2. If the roots of anchorNode or focusNode are not the document associated with this, abort these steps.
if (&anchor_node->root() != m_document.ptr())
return {};

if (&focus_node->root() != m_document.ptr())
// 2. If document associated with this is not a shadow-including inclusive ancestor of anchorNode or focusNode, abort these steps.
if (!(m_document->is_shadow_including_inclusive_ancestor_of(anchor_node) || m_document->is_shadow_including_inclusive_ancestor_of(focus_node)))
return {};

// 3. Let anchor be the boundary point (anchorNode, anchorOffset) and let focus be the boundary point (focusNode, focusOffset).
Expand Down

0 comments on commit 487089b

Please sign in to comment.