EditEventHandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/Text.h>
  29. #include <LibWeb/Layout/LayoutPosition.h>
  30. #include <LibWeb/Page/Frame.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/Dump.h>
  33. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  34. #include "EditEventHandler.h"
  35. namespace Web {
  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 = downcast<DOM::Text>(range.start().node());
  40. auto* end = downcast<DOM::Text>(range.end().node());
  41. if (start == end) {
  42. StringBuilder builder;
  43. builder.append(start->data().substring_view(0, range.start().offset()));
  44. builder.append(end->data().substring_view(range.end().offset()));
  45. start->set_data(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->parent()->remove_child(*node);
  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->parent()->remove_child(*remove_from);
  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->parent()->remove_child(*parent_of_end);
  72. }
  73. // Join the start and end nodes.
  74. StringBuilder builder;
  75. builder.append(start->data().substring_view(0, range.start().offset()));
  76. builder.append(end->data().substring_view(range.end().offset()));
  77. start->set_data(builder.to_string());
  78. start->parent()->remove_child(*end);
  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_frame.document()->force_layout();
  84. }
  85. void EditEventHandler::handle_insert(DOM::Position position, u32 code_point)
  86. {
  87. if (is<DOM::Text>(*position.node())) {
  88. auto& node = downcast<DOM::Text>(*position.node());
  89. StringBuilder builder;
  90. builder.append(node.data().substring_view(0, position.offset()));
  91. builder.append_code_point(code_point);
  92. builder.append(node.data().substring_view(position.offset()));
  93. node.set_data(builder.to_string());
  94. node.invalidate_style();
  95. }
  96. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  97. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  98. // which really hurts performance.
  99. m_frame.document()->force_layout();
  100. }
  101. }