mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 17:19:13 +00:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89
Commit: 93712b24bf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
4547 changed files with 104 additions and 113 deletions
200
Libraries/LibWeb/DOM/EditingHostManager.cpp
Normal file
200
Libraries/LibWeb/DOM/EditingHostManager.cpp
Normal file
|
@ -0,0 +1,200 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/EditingHostManager.h>
|
||||
#include <LibWeb/DOM/Range.h>
|
||||
#include <LibWeb/DOM/Text.h>
|
||||
#include <LibWeb/Selection/Selection.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(EditingHostManager);
|
||||
|
||||
JS::NonnullGCPtr<EditingHostManager> EditingHostManager::create(JS::Realm& realm, JS::NonnullGCPtr<Document> document)
|
||||
{
|
||||
return realm.heap().allocate<EditingHostManager>(realm, document);
|
||||
}
|
||||
|
||||
EditingHostManager::EditingHostManager(JS::NonnullGCPtr<Document> document)
|
||||
: m_document(document)
|
||||
{
|
||||
}
|
||||
|
||||
void EditingHostManager::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_document);
|
||||
visitor.visit(m_active_contenteditable_element);
|
||||
}
|
||||
|
||||
void EditingHostManager::handle_insert(String const& data)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
|
||||
auto selection_range = selection->range();
|
||||
if (!selection_range) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto node = selection->anchor_node();
|
||||
if (!node || !node->is_editable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is<DOM::Text>(*node)) {
|
||||
auto& realm = node->realm();
|
||||
auto text = realm.heap().allocate<DOM::Text>(realm, node->document(), data);
|
||||
MUST(node->append_child(*text));
|
||||
MUST(selection->collapse(*text, 1));
|
||||
return;
|
||||
}
|
||||
|
||||
auto& text_node = static_cast<DOM::Text&>(*node);
|
||||
|
||||
MUST(selection_range->delete_contents());
|
||||
MUST(text_node.insert_data(selection->anchor_offset(), data));
|
||||
VERIFY(selection->is_collapsed());
|
||||
|
||||
auto utf16_data = MUST(AK::utf8_to_utf16(data));
|
||||
Utf16View const utf16_view { utf16_data };
|
||||
auto length = utf16_view.length_in_code_units();
|
||||
MUST(selection->collapse(*node, selection->anchor_offset() + length));
|
||||
|
||||
text_node.invalidate_style(DOM::StyleInvalidationReason::EditingInsertion);
|
||||
}
|
||||
|
||||
void EditingHostManager::select_all()
|
||||
{
|
||||
if (!m_active_contenteditable_element) {
|
||||
return;
|
||||
}
|
||||
auto selection = m_document->get_selection();
|
||||
if (!selection->anchor_node() || !selection->focus_node()) {
|
||||
return;
|
||||
}
|
||||
MUST(selection->set_base_and_extent(*selection->anchor_node(), 0, *selection->focus_node(), selection->focus_node()->length()));
|
||||
}
|
||||
|
||||
void EditingHostManager::set_selection_anchor(JS::NonnullGCPtr<DOM::Node> anchor_node, size_t anchor_offset)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
MUST(selection->collapse(*anchor_node, anchor_offset));
|
||||
m_document->reset_cursor_blink_cycle();
|
||||
}
|
||||
|
||||
void EditingHostManager::set_selection_focus(JS::NonnullGCPtr<DOM::Node> focus_node, size_t focus_offset)
|
||||
{
|
||||
if (!m_active_contenteditable_element || !m_active_contenteditable_element->is_ancestor_of(*focus_node))
|
||||
return;
|
||||
auto selection = m_document->get_selection();
|
||||
if (!selection->anchor_node())
|
||||
return;
|
||||
MUST(selection->set_base_and_extent(*selection->anchor_node(), selection->anchor_offset(), *focus_node, focus_offset));
|
||||
m_document->reset_cursor_blink_cycle();
|
||||
}
|
||||
|
||||
void EditingHostManager::move_cursor_to_start(CollapseSelection collapse)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
auto node = selection->anchor_node();
|
||||
if (!node || !is<DOM::Text>(*node))
|
||||
return;
|
||||
|
||||
if (collapse == CollapseSelection::Yes) {
|
||||
MUST(selection->collapse(node, 0));
|
||||
m_document->reset_cursor_blink_cycle();
|
||||
return;
|
||||
}
|
||||
MUST(selection->set_base_and_extent(*node, selection->anchor_offset(), *node, 0));
|
||||
}
|
||||
|
||||
void EditingHostManager::move_cursor_to_end(CollapseSelection collapse)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
auto node = selection->anchor_node();
|
||||
if (!node || !is<DOM::Text>(*node))
|
||||
return;
|
||||
|
||||
if (collapse == CollapseSelection::Yes) {
|
||||
m_document->reset_cursor_blink_cycle();
|
||||
MUST(selection->collapse(node, node->length()));
|
||||
return;
|
||||
}
|
||||
MUST(selection->set_base_and_extent(*node, selection->anchor_offset(), *node, node->length()));
|
||||
}
|
||||
|
||||
void EditingHostManager::increment_cursor_position_offset(CollapseSelection collapse)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
if (!selection)
|
||||
return;
|
||||
selection->move_offset_to_next_character(collapse == CollapseSelection::Yes);
|
||||
}
|
||||
|
||||
void EditingHostManager::decrement_cursor_position_offset(CollapseSelection collapse)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
if (!selection)
|
||||
return;
|
||||
selection->move_offset_to_previous_character(collapse == CollapseSelection::Yes);
|
||||
}
|
||||
|
||||
void EditingHostManager::increment_cursor_position_to_next_word(CollapseSelection collapse)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
if (!selection)
|
||||
return;
|
||||
selection->move_offset_to_next_character(collapse == CollapseSelection::Yes);
|
||||
}
|
||||
|
||||
void EditingHostManager::decrement_cursor_position_to_previous_word(CollapseSelection collapse)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
if (!selection)
|
||||
return;
|
||||
selection->move_offset_to_previous_word(collapse == CollapseSelection::Yes);
|
||||
}
|
||||
|
||||
void EditingHostManager::handle_delete(DeleteDirection direction)
|
||||
{
|
||||
auto selection = m_document->get_selection();
|
||||
auto selection_range = selection->range();
|
||||
if (!selection_range) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (selection->is_collapsed()) {
|
||||
auto node = selection->anchor_node();
|
||||
if (!node || !is<DOM::Text>(*node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& text_node = static_cast<DOM::Text&>(*node);
|
||||
if (direction == DeleteDirection::Backward) {
|
||||
if (selection->anchor_offset() > 0) {
|
||||
MUST(text_node.delete_data(selection->anchor_offset() - 1, 1));
|
||||
text_node.invalidate_style(DOM::StyleInvalidationReason::EditingInsertion);
|
||||
}
|
||||
} else {
|
||||
if (selection->anchor_offset() < text_node.data().bytes_as_string_view().length()) {
|
||||
MUST(text_node.delete_data(selection->anchor_offset(), 1));
|
||||
text_node.invalidate_style(DOM::StyleInvalidationReason::EditingInsertion);
|
||||
}
|
||||
}
|
||||
m_document->reset_cursor_blink_cycle();
|
||||
return;
|
||||
}
|
||||
|
||||
MUST(selection_range->delete_contents());
|
||||
}
|
||||
|
||||
void EditingHostManager::handle_return_key()
|
||||
{
|
||||
dbgln("FIXME: Implement EditingHostManager::handle_return_key()");
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue