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

Code before:
    }

    if (is<DOM::Text>(*range.start_container()) && range.start_container()->layout_node())
        builder.append(static_cast<DOM::Text const&>(*range.start_container()).data().substring_view(range.start_offset()));

    range.for_each_contained([&](GC::Ref<DOM::Node> node) {
        if (is<DOM::Text>(*node) && node->layout_node())
            builder.append(static_cast<DOM::Text const&>(*node).data());
        return IterationDecision::Continue;
    });

    if (is<DOM::Text>(*range.end_container()) && range.end_container()->layout_node())
        builder.append(static_cast<DOM::Text const&>(*range.end_container()).data().substring_view(0, range.end_offset()));

    return MUST(builder.to_string());
}

String Navigable::selected_text() const
{
    auto document = active_document();
    if (!document)
        return String {};

    auto selection = document->get_selection();
    if (auto form_text = selection->try_selected_text_from_form_control(); form_text.has_value())
        return form_text->to_well_formed_utf8();

    auto range = selection->range();
    if (!range)
        return String {};
    return visible_text_in_range(*range);
}

void Navigable::select_all()
{
    auto document = active_document();
    if (!document)
        return;

    auto selection = document->get_selection();
    if (!selection)
        return;

    if (auto target = document->active_input_events_target()) {
        target->select_all();
    } else if (auto* body = document->body()) {
        (void)selection->select_all_children(*body);
    }
}

void Navigable::paste(Utf16String const& text)

Review comment (suggestion):
I missed this earlier, but this should probably just be `to_utf8()` as strings in the web generally allow lonely surrogates (WTF-8) including in Ranges

Code after:
        return static_cast<DOM::Text const&>(*range.start_container()).data().substring_view(range.start_offset(), range.end_offset() - range.start_offset()).to_utf8_but_should_be_ported_to_utf16();
    }

    if (is<DOM::Text>(*range.start_container()) && range.start_container()->layout_node())
        builder.append(static_cast<DOM::Text const&>(*range.start_container()).data().substring_view(range.start_offset()));

    range.for_each_contained([&](GC::Ref<DOM::Node> node) {
        if (is<DOM::Text>(*node) && node->layout_node())
            builder.append(static_cast<DOM::Text const&>(*node).data());
        return IterationDecision::Continue;
    });

    if (is<DOM::Text>(*range.end_container()) && range.end_container()->layout_node())
        builder.append(static_cast<DOM::Text const&>(*range.end_container()).data().substring_view(0, range.end_offset()));

    return MUST(builder.to_string());
}

String Navigable::selected_text() const
{
    auto document = active_document();
    if (!document)
        return String {};

    auto const* input_element = as_if<HTML::HTMLInputElement>(document->active_element());
    if (input_element && input_element->type_state() == HTML::HTMLInputElement::TypeAttributeState::Password) {
        // Apparently nobody wants bullet characters. We leave the clipboard alone here like other browsers.
        return String {};
    }
    auto selection = document->get_selection();
    if (auto form_text = selection->try_form_control_selected_text_for_stringifier(); form_text.has_value())
        return form_text->to_utf8();

    auto range = selection->range();
    if (!range)
        return String {};
    return visible_text_in_range(*range);
}

void Navigable::select_all()
{
    auto document = active_document();
    if (!document)
        return;

    auto selection = document->get_selection();
    if (!selection)
        return;

    if (auto target = document->active_input_events_target()) {
        target->select_all();