EditEventHandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibWeb/DOM/Position.h>
  28. #include <LibWeb/DOM/Range.h>
  29. #include <LibWeb/DOM/Text.h>
  30. #include <LibWeb/Layout/LayoutPosition.h>
  31. #include <LibWeb/Page/Frame.h>
  32. #include <LibWeb/DOM/Document.h>
  33. #include <LibWeb/Dump.h>
  34. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  35. #include "EditEventHandler.h"
  36. namespace Web {
  37. // This method is quite convoluted but this is necessary to make editing feel intuitive.
  38. void EditEventHandler::handle_delete(DOM::Range& range)
  39. {
  40. auto* start = downcast<DOM::Text>(range.start_container());
  41. auto* end = downcast<DOM::Text>(range.end_container());
  42. if (start == end) {
  43. StringBuilder builder;
  44. builder.append(start->data().substring_view(0, range.start_offset()));
  45. builder.append(end->data().substring_view(range.end_offset()));
  46. start->set_data(builder.to_string());
  47. } else {
  48. // Remove all the nodes that are fully enclosed in the range.
  49. HashTable<DOM::Node*> queued_for_deletion;
  50. for (auto* node = start->next_in_pre_order(); node; node = node->next_in_pre_order()) {
  51. if (node == end)
  52. break;
  53. queued_for_deletion.set(node);
  54. }
  55. for (auto* parent = start->parent(); parent; parent = parent->parent())
  56. queued_for_deletion.remove(parent);
  57. for (auto* parent = end->parent(); parent; parent = parent->parent())
  58. queued_for_deletion.remove(parent);
  59. for (auto* node : queued_for_deletion)
  60. node->parent()->remove_child(*node);
  61. // Join the parent nodes of start and end.
  62. DOM::Node *insert_after = start, *remove_from = end, *parent_of_end = end->parent();
  63. while (remove_from) {
  64. auto* next_sibling = remove_from->next_sibling();
  65. remove_from->parent()->remove_child(*remove_from);
  66. insert_after->parent()->insert_before(*remove_from, *insert_after);
  67. insert_after = remove_from;
  68. remove_from = next_sibling;
  69. }
  70. if (!parent_of_end->has_children()) {
  71. if (parent_of_end->parent())
  72. parent_of_end->parent()->remove_child(*parent_of_end);
  73. }
  74. // Join the start and end nodes.
  75. StringBuilder builder;
  76. builder.append(start->data().substring_view(0, range.start_offset()));
  77. builder.append(end->data().substring_view(range.end_offset()));
  78. start->set_data(builder.to_string());
  79. start->parent()->remove_child(*end);
  80. }
  81. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  82. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  83. // which really hurts performance.
  84. m_frame.document()->force_layout();
  85. m_frame.did_edit({});
  86. }
  87. void EditEventHandler::handle_insert(DOM::Position position, u32 code_point)
  88. {
  89. if (is<DOM::Text>(*position.node())) {
  90. auto& node = downcast<DOM::Text>(*position.node());
  91. StringBuilder builder;
  92. builder.append(node.data().substring_view(0, position.offset()));
  93. builder.append_code_point(code_point);
  94. builder.append(node.data().substring_view(position.offset()));
  95. node.set_data(builder.to_string());
  96. node.invalidate_style();
  97. }
  98. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  99. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  100. // which really hurts performance.
  101. m_frame.document()->force_layout();
  102. m_frame.did_edit({});
  103. }
  104. }