HTMLElement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/Document.h>
  28. #include <LibWeb/HTML/HTMLAnchorElement.h>
  29. #include <LibWeb/HTML/HTMLElement.h>
  30. #include <LibWeb/Layout/TextNode.h>
  31. namespace Web::HTML {
  32. HTMLElement::HTMLElement(DOM::Document& document, const QualifiedName& qualified_name)
  33. : Element(document, qualified_name)
  34. {
  35. }
  36. HTMLElement::~HTMLElement()
  37. {
  38. }
  39. HTMLElement::ContentEditableState HTMLElement::content_editable_state() const
  40. {
  41. auto contenteditable = attribute(HTML::AttributeNames::contenteditable);
  42. // "true" and the empty string map to the "true" state.
  43. if ((!contenteditable.is_null() && contenteditable.is_empty()) || contenteditable.equals_ignoring_case("true"))
  44. return ContentEditableState::True;
  45. // "false" maps to the "false" state.
  46. if (contenteditable.equals_ignoring_case("false"))
  47. return ContentEditableState::False;
  48. // "inherit", an invalid value, and a missing value all map to the "inherit" state.
  49. return ContentEditableState::Inherit;
  50. }
  51. bool HTMLElement::is_editable() const
  52. {
  53. switch (content_editable_state()) {
  54. case ContentEditableState::True:
  55. return true;
  56. case ContentEditableState::False:
  57. return false;
  58. case ContentEditableState::Inherit:
  59. return parent() && parent()->is_editable();
  60. default:
  61. ASSERT_NOT_REACHED();
  62. }
  63. }
  64. String HTMLElement::content_editable() const
  65. {
  66. switch (content_editable_state()) {
  67. case ContentEditableState::True:
  68. return "true";
  69. case ContentEditableState::False:
  70. return "false";
  71. case ContentEditableState::Inherit:
  72. return "inherit";
  73. default:
  74. ASSERT_NOT_REACHED();
  75. }
  76. }
  77. void HTMLElement::set_content_editable(const String& content_editable)
  78. {
  79. if (content_editable.equals_ignoring_case("inherit")) {
  80. remove_attribute(HTML::AttributeNames::contenteditable);
  81. return;
  82. }
  83. if (content_editable.equals_ignoring_case("true")) {
  84. set_attribute(HTML::AttributeNames::contenteditable, "true");
  85. return;
  86. }
  87. if (content_editable.equals_ignoring_case("false")) {
  88. set_attribute(HTML::AttributeNames::contenteditable, "false");
  89. return;
  90. }
  91. // FIXME: otherwise the attribute setter must throw a "SyntaxError" DOMException.
  92. }
  93. void HTMLElement::set_inner_text(StringView text)
  94. {
  95. remove_all_children();
  96. append_child(document().create_text_node(text));
  97. set_needs_style_update(true);
  98. document().schedule_style_update();
  99. document().invalidate_layout();
  100. }
  101. String HTMLElement::inner_text()
  102. {
  103. StringBuilder builder;
  104. // innerText for element being rendered takes visibility into account, so force a layout and then walk the layout tree.
  105. document().layout();
  106. if (!layout_node())
  107. return text_content();
  108. Function<void(const Layout::Node&)> recurse = [&](auto& node) {
  109. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  110. if (child->is_text())
  111. builder.append(downcast<Layout::TextNode>(*child).text_for_rendering());
  112. if (child->is_break())
  113. builder.append('\n');
  114. recurse(*child);
  115. }
  116. };
  117. recurse(*layout_node());
  118. return builder.to_string();
  119. }
  120. bool HTMLElement::cannot_navigate() const
  121. {
  122. // FIXME: Return true if element's node document is not fully active
  123. return !is<HTML::HTMLAnchorElement>(this) && !is_connected();
  124. }
  125. }