Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/Selection/Selection.cpp (C++)

Code before:

void Selection::move_offset_to_previous_word(bool collapse_selection)
{
    auto anchor_node = this->anchor_node();
    if (!anchor_node || !is<DOM::Text>(*anchor_node)) {
        return;
    }

    auto& text_node = static_cast<DOM::Text&>(*anchor_node);
    while (true) {
        auto focus_offset = this->focus_offset();
        if (auto offset = text_node.word_segmenter().previous_boundary(focus_offset); offset.has_value()) {
            auto word = text_node.data().substring_view(*offset, focus_offset - *offset);
            if (collapse_selection) {
                MUST(collapse(anchor_node, *offset));
                m_document->reset_cursor_blink_cycle();
            } else {
                MUST(set_base_and_extent(*anchor_node, anchor_offset(), *anchor_node, *offset));
            }
            if (Unicode::Segmenter::should_continue_beyond_word(word))
                continue;
        }
        break;
    }
}

void Selection::move_offset_to_next_line(bool collapse_selection)
{
    auto* text_node = as_if<DOM::Text>(anchor_node().ptr());
    if (!text_node)
        return;

    auto new_offset = increment_cursor_position_to_next_line_in_node(*text_node, focus_offset());
    if (!new_offset.has_value())
        return;

    if (collapse_selection) {
        MUST(collapse(text_node, *new_offset));
        m_document->reset_cursor_blink_cycle();
    } else {
        MUST(set_base_and_extent(*text_node, anchor_offset(), *text_node, *new_offset));
    }
}

void Selection::move_offset_to_previous_line(bool collapse_selection)
{
    auto* text_node = as_if<DOM::Text>(anchor_node().ptr());
    if (!text_node)
        return;

    auto new_offset = compute_cursor_position_on_previous_line(*text_node, focus_offset());

Review comment (suggestion):
I think this should be `focus_node()` in both methods; I've got some relevant changes for that in #5952

Code after:
void Selection::move_offset_to_previous_word(bool collapse_selection)
{
    auto* text_node = as_if<DOM::Text>(anchor_node().ptr());
    if (!text_node)
        return;

    while (true) {
        auto focus_offset = this->focus_offset();
        if (auto offset = text_node->word_segmenter().previous_boundary(focus_offset); offset.has_value()) {
            auto word = text_node->data().substring_view(*offset, focus_offset - *offset);
            if (collapse_selection) {
                MUST(collapse(text_node, *offset));
                m_document->reset_cursor_blink_cycle();
            } else {
                MUST(set_base_and_extent(*text_node, anchor_offset(), *text_node, *offset));
            }
            if (Unicode::Segmenter::should_continue_beyond_word(word))
                continue;
        }
        break;
    }
}

void Selection::move_offset_to_next_line(bool collapse_selection)
{
    auto* text_node = as_if<DOM::Text>(anchor_node().ptr());
    if (!text_node)
        return;

    auto new_offset = compute_cursor_position_on_next_line(*text_node, focus_offset());
    if (!new_offset.has_value())
        return;

    if (collapse_selection) {
        MUST(collapse(text_node, *new_offset));
        m_document->reset_cursor_blink_cycle();
    } else {
        MUST(set_base_and_extent(*text_node, anchor_offset(), *text_node, *new_offset));
    }
}

void Selection::move_offset_to_previous_line(bool collapse_selection)
{
    auto* text_node = as_if<DOM::Text>(anchor_node().ptr());
    if (!text_node)
        return;

    auto new_offset = compute_cursor_position_on_previous_line(*text_node, focus_offset());
    if (!new_offset.has_value())
        return;