EditEventHandler.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. void EditEventHandler::handle_delete(DOM::Range range)
  37. {
  38. auto* start = downcast<DOM::Text>(range.start().node());
  39. auto* end = downcast<DOM::Text>(range.end().node());
  40. // Remove all the nodes that are fully enclosed in the range.
  41. HashTable<DOM::Node*> queued_for_deletion;
  42. for (auto* node = start->next_in_pre_order(); node; node = node->next_in_pre_order()) {
  43. if (node == end)
  44. break;
  45. queued_for_deletion.set(node);
  46. }
  47. for (auto* parent = start->parent(); parent; parent = parent->parent())
  48. queued_for_deletion.remove(parent);
  49. for (auto* parent = end->parent(); parent; parent = parent->parent())
  50. queued_for_deletion.remove(parent);
  51. for (auto* node : queued_for_deletion)
  52. node->parent()->remove_child(*node);
  53. if (start == end || start->next_sibling() == end) {
  54. // If the start and end text nodes are now immediate siblings, merge the remainders into one.
  55. StringBuilder builder;
  56. builder.append(start->data().substring_view(0, range.start().offset()));
  57. builder.append(end->data().substring_view(range.end().offset()));
  58. start->set_data(builder.to_string());
  59. start->invalidate_style();
  60. if (start != end)
  61. start->parent()->remove_child(*end);
  62. } else {
  63. // Otherwise, remove parts from both nodes.
  64. start->set_data(start->data().substring_view(0, range.start().offset()));
  65. start->invalidate_style();
  66. end->set_data(end->data().substring_view(range.end().offset()));
  67. end->invalidate_style();
  68. }
  69. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  70. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  71. // which really hurts performance.
  72. m_frame.document()->force_layout();
  73. }
  74. void EditEventHandler::handle_insert(DOM::Position position, u32 code_point)
  75. {
  76. if (is<DOM::Text>(*position.node())) {
  77. auto& node = downcast<DOM::Text>(*position.node());
  78. StringBuilder builder;
  79. builder.append(node.data().substring_view(0, position.offset()));
  80. builder.append_code_point(code_point);
  81. builder.append(node.data().substring_view(position.offset()));
  82. node.set_data(builder.to_string());
  83. node.invalidate_style();
  84. }
  85. // FIXME: When nodes are removed from the DOM, the associated layout nodes become stale and still
  86. // remain in the layout tree. This has to be fixed, this just causes everything to be recomputed
  87. // which really hurts performance.
  88. m_frame.document()->force_layout();
  89. }
  90. }