2020-12-01 23:35:47 +01:00
|
|
|
/*
|
2021-05-18 22:09:15 +02:00
|
|
|
* Copyright (c) 2020-2021, the SerenityOS developers.
|
2020-12-01 23:35:47 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-01 23:35:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/StringBuilder.h>
|
2021-05-18 22:09:15 +02:00
|
|
|
#include <AK/Utf8View.h>
|
2023-03-01 07:30:02 -05:00
|
|
|
#include <LibUnicode/Segmentation.h>
|
2021-02-09 23:02:04 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-12-01 23:35:47 +01:00
|
|
|
#include <LibWeb/DOM/Position.h>
|
2020-12-06 19:51:55 +01:00
|
|
|
#include <LibWeb/DOM/Range.h>
|
2020-12-01 23:35:47 +01:00
|
|
|
#include <LibWeb/DOM/Text.h>
|
2021-11-18 15:01:28 +01:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.h>
|
2023-02-25 11:04:29 +01:00
|
|
|
#include <LibWeb/Layout/Viewport.h>
|
2021-05-18 22:09:15 +02:00
|
|
|
#include <LibWeb/Page/EditEventHandler.h>
|
2020-12-01 23:35:47 +01:00
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
|
2023-09-26 19:34:21 +02:00
|
|
|
void EditEventHandler::handle_delete_character_after(JS::NonnullGCPtr<DOM::Position> cursor_position)
|
2021-05-18 22:09:15 +02:00
|
|
|
{
|
2023-09-26 19:34:21 +02:00
|
|
|
auto& node = verify_cast<DOM::Text>(*cursor_position->node());
|
2023-03-01 07:30:02 -05:00
|
|
|
auto& text = node.data();
|
|
|
|
|
|
2023-09-26 19:34:21 +02:00
|
|
|
auto next_grapheme_offset = Unicode::next_grapheme_segmentation_boundary(Utf8View { text }, cursor_position->offset());
|
2023-03-01 07:30:02 -05:00
|
|
|
if (!next_grapheme_offset.has_value()) {
|
2021-05-18 22:09:15 +02:00
|
|
|
// FIXME: Move to the next node and delete the first character there.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder builder;
|
2023-09-26 19:34:21 +02:00
|
|
|
builder.append(text.bytes_as_string_view().substring_view(0, cursor_position->offset()));
|
2023-09-07 21:36:05 +12:00
|
|
|
builder.append(text.bytes_as_string_view().substring_view(*next_grapheme_offset));
|
|
|
|
|
node.set_data(MUST(builder.to_string()));
|
2021-05-18 22:09:15 +02:00
|
|
|
|
2022-02-18 23:14:52 +01:00
|
|
|
// FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
|
|
|
|
|
// remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
|
|
|
|
|
// which really hurts performance.
|
2023-02-26 16:09:02 -07:00
|
|
|
m_browsing_context->active_document()->force_layout();
|
2022-02-18 23:14:52 +01:00
|
|
|
|
2023-02-26 16:09:02 -07:00
|
|
|
m_browsing_context->did_edit({});
|
2021-05-18 22:09:15 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 22:41:26 +01:00
|
|
|
// This method is quite convoluted but this is necessary to make editing feel intuitive.
|
2020-12-06 19:51:55 +01:00
|
|
|
void EditEventHandler::handle_delete(DOM::Range& range)
|
2020-12-01 23:36:12 +01:00
|
|
|
{
|
2021-06-24 19:53:42 +02:00
|
|
|
auto* start = verify_cast<DOM::Text>(range.start_container());
|
|
|
|
|
auto* end = verify_cast<DOM::Text>(range.end_container());
|
2020-12-03 18:46:56 +01:00
|
|
|
|
2020-12-03 22:41:26 +01:00
|
|
|
if (start == end) {
|
2020-12-03 18:46:56 +01:00
|
|
|
StringBuilder builder;
|
2023-09-07 21:36:05 +12:00
|
|
|
builder.append(start->data().bytes_as_string_view().substring_view(0, range.start_offset()));
|
|
|
|
|
builder.append(end->data().bytes_as_string_view().substring_view(range.end_offset()));
|
2020-12-03 18:46:56 +01:00
|
|
|
|
2023-09-07 21:36:05 +12:00
|
|
|
start->set_data(MUST(builder.to_string()));
|
2020-12-03 18:46:56 +01:00
|
|
|
} else {
|
2020-12-03 22:41:26 +01:00
|
|
|
// Remove all the nodes that are fully enclosed in the range.
|
|
|
|
|
HashTable<DOM::Node*> queued_for_deletion;
|
|
|
|
|
for (auto* node = start->next_in_pre_order(); node; node = node->next_in_pre_order()) {
|
|
|
|
|
if (node == end)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
queued_for_deletion.set(node);
|
|
|
|
|
}
|
|
|
|
|
for (auto* parent = start->parent(); parent; parent = parent->parent())
|
|
|
|
|
queued_for_deletion.remove(parent);
|
|
|
|
|
for (auto* parent = end->parent(); parent; parent = parent->parent())
|
|
|
|
|
queued_for_deletion.remove(parent);
|
|
|
|
|
for (auto* node : queued_for_deletion)
|
2021-04-06 19:34:49 +01:00
|
|
|
node->remove();
|
2020-12-03 22:41:26 +01:00
|
|
|
|
|
|
|
|
// Join the parent nodes of start and end.
|
|
|
|
|
DOM::Node *insert_after = start, *remove_from = end, *parent_of_end = end->parent();
|
|
|
|
|
while (remove_from) {
|
|
|
|
|
auto* next_sibling = remove_from->next_sibling();
|
2020-12-02 13:41:58 +01:00
|
|
|
|
2021-04-06 19:34:49 +01:00
|
|
|
remove_from->remove();
|
2020-12-03 22:41:26 +01:00
|
|
|
insert_after->parent()->insert_before(*remove_from, *insert_after);
|
2020-12-03 18:46:56 +01:00
|
|
|
|
2020-12-03 22:41:26 +01:00
|
|
|
insert_after = remove_from;
|
|
|
|
|
remove_from = next_sibling;
|
|
|
|
|
}
|
|
|
|
|
if (!parent_of_end->has_children()) {
|
|
|
|
|
if (parent_of_end->parent())
|
2021-04-06 19:34:49 +01:00
|
|
|
parent_of_end->remove();
|
2020-12-03 22:41:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Join the start and end nodes.
|
|
|
|
|
StringBuilder builder;
|
2023-09-07 21:36:05 +12:00
|
|
|
builder.append(start->data().bytes_as_string_view().substring_view(0, range.start_offset()));
|
|
|
|
|
builder.append(end->data().bytes_as_string_view().substring_view(range.end_offset()));
|
2020-12-03 22:41:26 +01:00
|
|
|
|
2023-09-07 21:36:05 +12:00
|
|
|
start->set_data(MUST(builder.to_string()));
|
2021-04-06 19:34:49 +01:00
|
|
|
end->remove();
|
2020-12-01 23:36:12 +01:00
|
|
|
}
|
2020-12-02 13:41:58 +01:00
|
|
|
|
2020-12-03 18:46:56 +01:00
|
|
|
// FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
|
|
|
|
|
// remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
|
|
|
|
|
// which really hurts performance.
|
2023-02-26 16:09:02 -07:00
|
|
|
m_browsing_context->active_document()->force_layout();
|
2021-01-04 20:48:27 +01:00
|
|
|
|
2023-02-26 16:09:02 -07:00
|
|
|
m_browsing_context->did_edit({});
|
2020-12-01 23:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-26 19:34:21 +02:00
|
|
|
void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, u32 code_point)
|
2020-12-01 23:35:47 +01:00
|
|
|
{
|
2023-09-26 19:34:21 +02:00
|
|
|
if (is<DOM::Text>(*position->node())) {
|
|
|
|
|
auto& node = verify_cast<DOM::Text>(*position->node());
|
2020-12-01 23:36:12 +01:00
|
|
|
|
2020-12-01 23:35:47 +01:00
|
|
|
StringBuilder builder;
|
2023-09-26 19:34:21 +02:00
|
|
|
builder.append(node.data().bytes_as_string_view().substring_view(0, position->offset()));
|
2020-12-01 23:35:47 +01:00
|
|
|
builder.append_code_point(code_point);
|
2023-09-26 19:34:21 +02:00
|
|
|
builder.append(node.data().bytes_as_string_view().substring_view(position->offset()));
|
2023-09-07 21:36:05 +12:00
|
|
|
node.set_data(MUST(builder.to_string()));
|
2020-12-01 23:35:47 +01:00
|
|
|
|
|
|
|
|
node.invalidate_style();
|
|
|
|
|
}
|
2020-12-02 15:00:55 +01:00
|
|
|
|
2020-12-03 18:46:56 +01:00
|
|
|
// FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
|
|
|
|
|
// remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
|
|
|
|
|
// which really hurts performance.
|
2023-02-26 16:09:02 -07:00
|
|
|
m_browsing_context->active_document()->force_layout();
|
2021-01-04 20:48:27 +01:00
|
|
|
|
2023-02-26 16:09:02 -07:00
|
|
|
m_browsing_context->did_edit({});
|
2020-12-01 23:35:47 +01:00
|
|
|
}
|
|
|
|
|
}
|