HTMLElement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibJS/Interpreter.h>
  8. #include <LibJS/Parser.h>
  9. #include <LibWeb/DOM/DOMException.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/EventListener.h>
  12. #include <LibWeb/DOM/ExceptionOr.h>
  13. #include <LibWeb/HTML/EventHandler.h>
  14. #include <LibWeb/HTML/HTMLAnchorElement.h>
  15. #include <LibWeb/HTML/HTMLBodyElement.h>
  16. #include <LibWeb/HTML/HTMLElement.h>
  17. #include <LibWeb/Layout/BreakNode.h>
  18. #include <LibWeb/Layout/TextNode.h>
  19. #include <LibWeb/UIEvents/EventNames.h>
  20. namespace Web::HTML {
  21. HTMLElement::HTMLElement(DOM::Document& document, QualifiedName qualified_name)
  22. : Element(document, move(qualified_name))
  23. {
  24. }
  25. HTMLElement::~HTMLElement()
  26. {
  27. }
  28. HTMLElement::ContentEditableState HTMLElement::content_editable_state() const
  29. {
  30. auto contenteditable = attribute(HTML::AttributeNames::contenteditable);
  31. // "true", an empty string or a missing value map to the "true" state.
  32. if ((!contenteditable.is_null() && contenteditable.is_empty()) || contenteditable.equals_ignoring_case("true"))
  33. return ContentEditableState::True;
  34. // "false" maps to the "false" state.
  35. if (contenteditable.equals_ignoring_case("false"))
  36. return ContentEditableState::False;
  37. // Having no such attribute or an invalid value maps to the "inherit" state.
  38. return ContentEditableState::Inherit;
  39. }
  40. bool HTMLElement::is_editable() const
  41. {
  42. switch (content_editable_state()) {
  43. case ContentEditableState::True:
  44. return true;
  45. case ContentEditableState::False:
  46. return false;
  47. case ContentEditableState::Inherit:
  48. return parent() && parent()->is_editable();
  49. default:
  50. VERIFY_NOT_REACHED();
  51. }
  52. }
  53. String HTMLElement::content_editable() const
  54. {
  55. switch (content_editable_state()) {
  56. case ContentEditableState::True:
  57. return "true";
  58. case ContentEditableState::False:
  59. return "false";
  60. case ContentEditableState::Inherit:
  61. return "inherit";
  62. default:
  63. VERIFY_NOT_REACHED();
  64. }
  65. }
  66. // https://html.spec.whatwg.org/multipage/interaction.html#contenteditable
  67. DOM::ExceptionOr<void> HTMLElement::set_content_editable(const String& content_editable)
  68. {
  69. if (content_editable.equals_ignoring_case("inherit")) {
  70. remove_attribute(HTML::AttributeNames::contenteditable);
  71. return {};
  72. }
  73. if (content_editable.equals_ignoring_case("true")) {
  74. set_attribute(HTML::AttributeNames::contenteditable, "true");
  75. return {};
  76. }
  77. if (content_editable.equals_ignoring_case("false")) {
  78. set_attribute(HTML::AttributeNames::contenteditable, "false");
  79. return {};
  80. }
  81. return DOM::SyntaxError::create("Invalid contentEditable value, must be 'true', 'false', or 'inherit'");
  82. }
  83. void HTMLElement::set_inner_text(StringView text)
  84. {
  85. remove_all_children();
  86. append_child(document().create_text_node(text));
  87. set_needs_style_update(true);
  88. document().invalidate_layout();
  89. }
  90. String HTMLElement::inner_text()
  91. {
  92. StringBuilder builder;
  93. // innerText for element being rendered takes visibility into account, so force a layout and then walk the layout tree.
  94. document().update_layout();
  95. if (!layout_node())
  96. return text_content();
  97. Function<void(const Layout::Node&)> recurse = [&](auto& node) {
  98. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  99. if (is<Layout::TextNode>(child))
  100. builder.append(verify_cast<Layout::TextNode>(*child).text_for_rendering());
  101. if (is<Layout::BreakNode>(child))
  102. builder.append('\n');
  103. recurse(*child);
  104. }
  105. };
  106. recurse(*layout_node());
  107. return builder.to_string();
  108. }
  109. unsigned HTMLElement::offset_top() const
  110. {
  111. if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
  112. return 0;
  113. auto position = layout_node()->box_type_agnostic_position();
  114. auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
  115. return position.y() - parent_position.y();
  116. }
  117. unsigned HTMLElement::offset_left() const
  118. {
  119. if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
  120. return 0;
  121. auto position = layout_node()->box_type_agnostic_position();
  122. auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
  123. return position.x() - parent_position.x();
  124. }
  125. bool HTMLElement::cannot_navigate() const
  126. {
  127. // FIXME: Return true if element's node document is not fully active
  128. return !is<HTML::HTMLAnchorElement>(this) && !is_connected();
  129. }
  130. void HTMLElement::parse_attribute(const FlyString& name, const String& value)
  131. {
  132. Element::parse_attribute(name, value);
  133. #undef __ENUMERATE
  134. #define __ENUMERATE(attribute_name, event_name) \
  135. if (name == HTML::AttributeNames::attribute_name) { \
  136. set_event_handler_attribute(event_name, EventHandler { value }); \
  137. }
  138. ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE)
  139. #undef __ENUMERATE
  140. }
  141. }