HTMLElement.cpp 4.5 KB

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