EditEventHandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  31. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  32. // which really hurts performance.
  33. m_browsing_context->active_document()->force_layout();
  34. m_browsing_context->did_edit({});
  35. }
  36. // This method is quite convoluted but this is necessary to make editing feel intuitive.
  37. void EditEventHandler::handle_delete(DOM::Range& range)
  38. {
  39. auto* start = verify_cast<DOM::Text>(range.start_container());
  40. auto* end = verify_cast<DOM::Text>(range.end_container());
  41. if (start == end) {
  42. StringBuilder builder;
  43. builder.append(start->data().bytes_as_string_view().substring_view(0, range.start_offset()));
  44. builder.append(end->data().bytes_as_string_view().substring_view(range.end_offset()));
  45. start->set_data(MUST(builder.to_string()));
  46. } else {
  47. // Remove all the nodes that are fully enclosed in the range.
  48. HashTable<DOM::Node*> queued_for_deletion;
  49. for (auto* node = start->next_in_pre_order(); node; node = node->next_in_pre_order()) {
  50. if (node == end)
  51. break;
  52. queued_for_deletion.set(node);
  53. }
  54. for (auto* parent = start->parent(); parent; parent = parent->parent())
  55. queued_for_deletion.remove(parent);
  56. for (auto* parent = end->parent(); parent; parent = parent->parent())
  57. queued_for_deletion.remove(parent);
  58. for (auto* node : queued_for_deletion)
  59. node->remove();
  60. // Join the parent nodes of start and end.
  61. DOM::Node *insert_after = start, *remove_from = end, *parent_of_end = end->parent();
  62. while (remove_from) {
  63. auto* next_sibling = remove_from->next_sibling();
  64. remove_from->remove();
  65. insert_after->parent()->insert_before(*remove_from, *insert_after);
  66. insert_after = remove_from;
  67. remove_from = next_sibling;
  68. }
  69. if (!parent_of_end->has_children()) {
  70. if (parent_of_end->parent())
  71. parent_of_end->remove();
  72. }
  73. // Join the start and end nodes.
  74. StringBuilder builder;
  75. builder.append(start->data().bytes_as_string_view().substring_view(0, range.start_offset()));
  76. builder.append(end->data().bytes_as_string_view().substring_view(range.end_offset()));
  77. start->set_data(MUST(builder.to_string()));
  78. end->remove();
  79. }
  80. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  81. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  82. // which really hurts performance.
  83. m_browsing_context->active_document()->force_layout();
  84. m_browsing_context->did_edit({});
  85. }
  86. void EditEventHandler::handle_insert(JS::NonnullGCPtr<DOM::Position> position, u32 code_point)
  87. {
  88. if (is<DOM::Text>(*position->node())) {
  89. auto& node = verify_cast<DOM::Text>(*position->node());
  90. StringBuilder builder;
  91. builder.append(node.data().bytes_as_string_view().substring_view(0, position->offset()));
  92. builder.append_code_point(code_point);
  93. builder.append(node.data().bytes_as_string_view().substring_view(position->offset()));
  94. node.set_data(MUST(builder.to_string()));
  95. node.invalidate_style();
  96. }
  97. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  98. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  99. // which really hurts performance.
  100. m_browsing_context->active_document()->force_layout();
  101. m_browsing_context->did_edit({});
  102. }
  103. }