HTMLElement.cpp 5.6 KB

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