HTMLElement.cpp 5.4 KB

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