LibWeb: Use correct boundaries in Selection::collapse_to_start/end()

We were using the anchor_node() as the boundary point node when
collapsing a selection, but the spec tells us to use the start and end
boundary point nodes.
This commit is contained in:
Jelle Raaijmakers 2024-12-20 11:34:49 +01:00 committed by Jelle Raaijmakers
parent eb11c35640
commit 2052792663
Notes: github-actions[bot] 2024-12-21 18:18:09 +00:00
3 changed files with 35 additions and 4 deletions

View file

@ -239,8 +239,8 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_start()
auto new_range = DOM::Range::create(*m_document);
// 3. Set the start both its start and end to the start of this's range
TRY(new_range->set_start(*anchor_node(), m_range->start_offset()));
TRY(new_range->set_end(*anchor_node(), m_range->start_offset()));
TRY(new_range->set_start(*m_range->start_container(), m_range->start_offset()));
TRY(new_range->set_end(*m_range->start_container(), m_range->start_offset()));
// 4. Then set this's range to the newly-created range.
set_range(new_range);
@ -259,8 +259,8 @@ WebIDL::ExceptionOr<void> Selection::collapse_to_end()
auto new_range = DOM::Range::create(*m_document);
// 3. Set the start both its start and end to the start of this's range
TRY(new_range->set_start(*anchor_node(), m_range->end_offset()));
TRY(new_range->set_end(*anchor_node(), m_range->end_offset()));
TRY(new_range->set_start(*m_range->end_container(), m_range->end_offset()));
TRY(new_range->set_end(*m_range->end_container(), m_range->end_offset()));
// 4. Then set this's range to the newly-created range.
set_range(new_range);

View file

@ -0,0 +1,2 @@
[object HTMLDivElement] 0 [object HTMLUListElement] 3
[object HTMLUListElement] 3 [object HTMLUListElement] 3

View file

@ -0,0 +1,29 @@
<script src="include.js"></script>
<div>
Well
<ul>
<li>Hello</li>
<li>Friends</li>
<li>!</li>
</ul>
</div>
<script>
test(() => {
const anchor = document.querySelector('div');
const range = document.createRange();
range.setStart(anchor, 0);
range.setEnd(anchor.childNodes[1], 3);
getSelection().addRange(range);
const printRange = () => {
let activeRange = getSelection().getRangeAt(0);
println(`${activeRange.startContainer} ${activeRange.startOffset} ${activeRange.endContainer} ${activeRange.endOffset}`);
};
printRange();
getSelection().collapseToEnd();
printRange();
})
</script>