mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
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:
parent
eb11c35640
commit
2052792663
Notes:
github-actions[bot]
2024-12-21 18:18:09 +00:00
Author: https://github.com/gmta Commit: https://github.com/LadybirdBrowser/ladybird/commit/2052792663f Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2991
3 changed files with 35 additions and 4 deletions
|
@ -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);
|
||||
|
|
2
Tests/LibWeb/Text/expected/selection-collapseToEnd.txt
Normal file
2
Tests/LibWeb/Text/expected/selection-collapseToEnd.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
[object HTMLDivElement] 0 [object HTMLUListElement] 3
|
||||
[object HTMLUListElement] 3 [object HTMLUListElement] 3
|
29
Tests/LibWeb/Text/input/selection-collapseToEnd.html
Normal file
29
Tests/LibWeb/Text/input/selection-collapseToEnd.html
Normal 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>
|
Loading…
Add table
Reference in a new issue