EditEventHandler.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // FIXME: Deleting nodes seems to mess up the layout tree. Not sure if this is not entirely
  39. // my fault or not my fault at all.
  40. dump_tree(*m_frame.document());
  41. if (range.start().node() != range.end().node()) {
  42. if (range.start().node()->parent() == range.end().node()->parent()) {
  43. // Remove all intermediate nodes.
  44. auto* current = range.start().node()->next_sibling();
  45. while (current != range.end().node()) {
  46. auto* next = current->next_sibling();
  47. current->parent()->remove_child(*current);
  48. current = next;
  49. }
  50. // Join remaining text together.
  51. StringBuilder builder;
  52. builder.append(downcast<DOM::Text>(range.start().node())->data().substring_view(0, range.start().offset()));
  53. builder.append(downcast<DOM::Text>(range.end().node())->data().substring_view(range.end().offset()));
  54. range.start().node()->parent()->remove_child(*range.end().node());
  55. downcast<DOM::Text>(range.start().node())->set_data(builder.to_string());
  56. range.start().node()->invalidate_style();
  57. } else {
  58. TODO();
  59. }
  60. } else {
  61. if (is<DOM::Text>(*range.start().node())) {
  62. m_frame.document()->layout_node()->set_selection({});
  63. auto& node = downcast<DOM::Text>(*range.start().node());
  64. StringBuilder builder;
  65. builder.append(node.data().substring_view(0, range.start().offset()));
  66. builder.append(node.data().substring_view(range.end().offset()));
  67. node.set_data(builder.to_string());
  68. node.invalidate_style();
  69. }
  70. }
  71. // FIXME: We need to remove stale layout nodes when nodes are removed from the DOM. Currently,
  72. // this is the only way to get these to disappear.
  73. m_frame.document()->force_layout();
  74. dump_tree(*m_frame.document());
  75. }
  76. void EditEventHandler::handle_insert(DOM::Position position, u32 code_point)
  77. {
  78. if (is<DOM::Text>(*position.node())) {
  79. auto& node = downcast<DOM::Text>(*position.node());
  80. StringBuilder builder;
  81. builder.append(node.data().substring_view(0, position.offset()));
  82. builder.append_code_point(code_point);
  83. builder.append(node.data().substring_view(position.offset()));
  84. node.set_data(builder.to_string());
  85. node.invalidate_style();
  86. }
  87. // FIXME: We need to remove stale layout nodes when nodes are removed from the DOM. Currently,
  88. // this is the only way to get these to disappear.
  89. m_frame.document()->force_layout();
  90. }
  91. }