EditEventHandler.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Utf8View.h>
  8. #include <LibUnicode/Segmentation.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Position.h>
  11. #include <LibWeb/DOM/Range.h>
  12. #include <LibWeb/DOM/Text.h>
  13. #include <LibWeb/HTML/BrowsingContext.h>
  14. #include <LibWeb/Layout/Viewport.h>
  15. #include <LibWeb/Page/EditEventHandler.h>
  16. namespace Web {
  17. void EditEventHandler::handle_delete_character_after(JS::NonnullGCPtr<DOM::Position> cursor_position)
  18. {
  19. auto& node = verify_cast<DOM::Text>(*cursor_position->node());
  20. auto& text = node.data();
  21. auto next_grapheme_offset = Unicode::next_grapheme_segmentation_boundary(Utf8View { text }, cursor_position->offset());
  22. if (!next_grapheme_offset.has_value()) {
  23. // FIXME: Move to the next node and delete the first character there.
  24. return;
  25. }
  26. StringBuilder builder;
  27. builder.append(text.bytes_as_string_view().substring_view(0, cursor_position->offset()));
  28. builder.append(text.bytes_as_string_view().substring_view(*next_grapheme_offset));
  29. node.set_data(MUST(builder.to_string()));
  30. m_browsing_context->did_edit({});
  31. }
  32. // This method is quite convoluted but this is necessary to make editing feel intuitive.
  33. void EditEventHandler::handle_delete(DOM::Range& range)
  34. {
  35. auto* start = verify_cast<DOM::Text>(range.start_container());
  36. auto* end = verify_cast<DOM::Text>(range.end_container());
  37. if (start == end) {
  38. StringBuilder builder;
  39. builder.append(start->data().bytes_as_string_view().substring_view(0, range.start_offset()));
  40. builder.append(end->data().bytes_as_string_view().substring_view(range.end_offset()));
  41. start->set_data(MUST(builder.to_string()));
  42. } else {
  43. // Remove all the nodes that are fully enclosed in the range.
  44. HashTable<DOM::Node*> queued_for_deletion;
  45. for (auto* node = start->next_in_pre_order(); node; node = node->next_in_pre_order()) {
  46. if (node == end)
  47. break;
  48. queued_for_deletion.set(node);
  49. }
  50. for (auto* parent = start->parent(); parent; parent = parent->parent())
  51. queued_for_deletion.remove(parent);
  52. for (auto* parent = end->parent(); parent; parent = parent->parent())
  53. queued_for_deletion.remove(parent);
  54. for (auto* node : queued_for_deletion)
  55. node->remove();
  56. // Join the parent nodes of start and end.
  57. DOM::Node *insert_after = start, *remove_from = end, *parent_of_end = end->parent();
  58. while (remove_from) {
  59. auto* next_sibling = remove_from->next_sibling();
  60. remove_from->remove();
  61. insert_after->parent()->insert_before(*remove_from, *insert_after);
  62. insert_after = remove_from;
  63. remove_from = next_sibling;
  64. }
  65. if (!parent_of_end->has_children()) {
  66. if (parent_of_end->parent())
  67. parent_of_end->remove();
  68. }
  69. // Join the start and end nodes.
  70. StringBuilder builder;
  71. builder.append(start->data().bytes_as_string_view().substring_view(0, range.start_offset()));
  72. builder.append(end->data().bytes_as_string_view().substring_view(range.end_offset()));
  73. start->set_data(MUST(builder.to_string()));
  74. end->remove();
  75. }
  76. m_browsing_context->did_edit({});
  77. }
  78. void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, u32 code_point)
  79. {
  80. StringBuilder builder;
  81. builder.append_code_point(code_point);
  82. handle_insert(position, MUST(builder.to_string()));
  83. }
  84. void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, String data)
  85. {
  86. if (is<DOM::Text>(*position->node())) {
  87. auto& node = verify_cast<DOM::Text>(*position->node());
  88. StringBuilder builder;
  89. builder.append(node.data().bytes_as_string_view().substring_view(0, position->offset()));
  90. builder.append(data);
  91. builder.append(node.data().bytes_as_string_view().substring_view(position->offset()));
  92. // Cut string by max length
  93. // FIXME: Cut by UTF-16 code units instead of raw bytes
  94. if (auto max_length = node.max_length(); max_length.has_value() && builder.string_view().length() > *max_length) {
  95. node.set_data(MUST(String::from_utf8(builder.string_view().substring_view(0, *max_length))));
  96. } else {
  97. node.set_data(MUST(builder.to_string()));
  98. }
  99. node.invalidate_style();
  100. } else {
  101. auto& node = *position->node();
  102. auto& realm = node.realm();
  103. auto text = realm.heap().allocate<DOM::Text>(realm, node.document(), data);
  104. MUST(node.append_child(*text));
  105. position->set_node(text);
  106. position->set_offset(1);
  107. }
  108. m_browsing_context->did_edit({});
  109. }
  110. }