Text.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/TextPrototype.h>
  8. #include <LibWeb/DOM/Range.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/Scripting/Environments.h>
  11. #include <LibWeb/HTML/Window.h>
  12. #include <LibWeb/Layout/TextNode.h>
  13. namespace Web::DOM {
  14. JS_DEFINE_ALLOCATOR(Text);
  15. Text::Text(Document& document, String const& data)
  16. : CharacterData(document, NodeType::TEXT_NODE, data)
  17. {
  18. }
  19. Text::Text(Document& document, NodeType type, String const& data)
  20. : CharacterData(document, type, data)
  21. {
  22. }
  23. void Text::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(Text);
  27. }
  28. void Text::visit_edges(Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. SlottableMixin::visit_edges(visitor);
  32. visitor.visit(m_owner);
  33. }
  34. // https://dom.spec.whatwg.org/#dom-text-text
  35. WebIDL::ExceptionOr<JS::NonnullGCPtr<Text>> Text::construct_impl(JS::Realm& realm, String const& data)
  36. {
  37. // The new Text(data) constructor steps are to set this’s data to data and this’s node document to current global object’s associated Document.
  38. auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
  39. return realm.heap().allocate<Text>(realm, window.associated_document(), data);
  40. }
  41. EditableTextNodeOwner* Text::editable_text_node_owner()
  42. {
  43. if (!m_owner)
  44. return nullptr;
  45. EditableTextNodeOwner* owner = dynamic_cast<EditableTextNodeOwner*>(m_owner.ptr());
  46. VERIFY(owner);
  47. return owner;
  48. }
  49. // https://dom.spec.whatwg.org/#dom-text-splittext
  50. // https://dom.spec.whatwg.org/#concept-text-split
  51. WebIDL::ExceptionOr<JS::NonnullGCPtr<Text>> Text::split_text(size_t offset)
  52. {
  53. // 1. Let length be node’s length.
  54. auto length = this->length();
  55. // 2. If offset is greater than length, then throw an "IndexSizeError" DOMException.
  56. if (offset > length)
  57. return WebIDL::IndexSizeError::create(realm(), "Split offset is greater than length"_fly_string);
  58. // 3. Let count be length minus offset.
  59. auto count = length - offset;
  60. // 4. Let new data be the result of substringing data with node node, offset offset, and count count.
  61. auto new_data = TRY(substring_data(offset, count));
  62. // 5. Let new node be a new Text node, with the same node document as node. Set new node’s data to new data.
  63. auto new_node = heap().allocate<Text>(realm(), document(), new_data);
  64. // 6. Let parent be node’s parent.
  65. JS::GCPtr<Node> parent = this->parent();
  66. // 7. If parent is not null, then:
  67. if (parent) {
  68. // 1. Insert new node into parent before node’s next sibling.
  69. parent->insert_before(*new_node, next_sibling());
  70. // 2. For each live range whose start node is node and start offset is greater than offset, set its start node to new node and decrease its start offset by offset.
  71. for (auto& range : Range::live_ranges()) {
  72. if (range->start_container() == this && range->start_offset() > offset)
  73. TRY(range->set_start(*new_node, range->start_offset() - offset));
  74. }
  75. // 3. For each live range whose end node is node and end offset is greater than offset, set its end node to new node and decrease its end offset by offset.
  76. for (auto& range : Range::live_ranges()) {
  77. if (range->end_container() == this && range->end_offset() > offset)
  78. TRY(range->set_end(*new_node, range->end_offset() - offset));
  79. }
  80. // 4. For each live range whose start node is parent and start offset is equal to the index of node plus 1, increase its start offset by 1.
  81. for (auto& range : Range::live_ranges()) {
  82. if (range->start_container() == this && range->start_offset() == index() + 1)
  83. TRY(range->set_start(*range->start_container(), range->start_offset() + 1));
  84. }
  85. // 5. For each live range whose end node is parent and end offset is equal to the index of node plus 1, increase its end offset by 1.
  86. for (auto& range : Range::live_ranges()) {
  87. if (range->end_container() == parent.ptr() && range->end_offset() == index() + 1) {
  88. TRY(range->set_end(*range->end_container(), range->end_offset() + 1));
  89. }
  90. }
  91. }
  92. // 8. Replace data with node node, offset offset, count count, and data the empty string.
  93. TRY(replace_data(offset, count, String {}));
  94. // 9. Return new node.
  95. return new_node;
  96. }
  97. // https://dom.spec.whatwg.org/#dom-text-wholetext
  98. String Text::whole_text()
  99. {
  100. // https://dom.spec.whatwg.org/#contiguous-text-nodes
  101. // The contiguous Text nodes of a node node are node, node’s previous sibling Text node, if any, and its contiguous
  102. // Text nodes, and node’s next sibling Text node, if any, and its contiguous Text nodes, avoiding any duplicates.
  103. Vector<Text*> nodes;
  104. nodes.append(this);
  105. auto* current_node = previous_sibling();
  106. while (current_node && (current_node->is_text() || current_node->is_cdata_section())) {
  107. nodes.append(static_cast<Text*>(current_node));
  108. current_node = current_node->previous_sibling();
  109. }
  110. // Reverse nodes so they are in tree order
  111. nodes.reverse();
  112. current_node = next_sibling();
  113. while (current_node && (current_node->is_text() || current_node->is_cdata_section())) {
  114. nodes.append(static_cast<Text*>(current_node));
  115. current_node = current_node->next_sibling();
  116. }
  117. StringBuilder builder;
  118. for (auto const& text_node : nodes)
  119. builder.append(text_node->data());
  120. return MUST(builder.to_string());
  121. }
  122. }