HTMLElement.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. , m_dataset(DOMStringMap::create(*this))
  24. {
  25. }
  26. HTMLElement::~HTMLElement()
  27. {
  28. }
  29. HTMLElement::ContentEditableState HTMLElement::content_editable_state() const
  30. {
  31. auto contenteditable = attribute(HTML::AttributeNames::contenteditable);
  32. // "true", an empty string or a missing value map to the "true" state.
  33. if ((!contenteditable.is_null() && contenteditable.is_empty()) || contenteditable.equals_ignoring_case("true"))
  34. return ContentEditableState::True;
  35. // "false" maps to the "false" state.
  36. if (contenteditable.equals_ignoring_case("false"))
  37. return ContentEditableState::False;
  38. // Having no such attribute or an invalid value maps to the "inherit" state.
  39. return ContentEditableState::Inherit;
  40. }
  41. bool HTMLElement::is_editable() const
  42. {
  43. switch (content_editable_state()) {
  44. case ContentEditableState::True:
  45. return true;
  46. case ContentEditableState::False:
  47. return false;
  48. case ContentEditableState::Inherit:
  49. return parent() && parent()->is_editable();
  50. default:
  51. VERIFY_NOT_REACHED();
  52. }
  53. }
  54. String HTMLElement::content_editable() const
  55. {
  56. switch (content_editable_state()) {
  57. case ContentEditableState::True:
  58. return "true";
  59. case ContentEditableState::False:
  60. return "false";
  61. case ContentEditableState::Inherit:
  62. return "inherit";
  63. default:
  64. VERIFY_NOT_REACHED();
  65. }
  66. }
  67. // https://html.spec.whatwg.org/multipage/interaction.html#contenteditable
  68. DOM::ExceptionOr<void> HTMLElement::set_content_editable(const String& content_editable)
  69. {
  70. if (content_editable.equals_ignoring_case("inherit")) {
  71. remove_attribute(HTML::AttributeNames::contenteditable);
  72. return {};
  73. }
  74. if (content_editable.equals_ignoring_case("true")) {
  75. set_attribute(HTML::AttributeNames::contenteditable, "true");
  76. return {};
  77. }
  78. if (content_editable.equals_ignoring_case("false")) {
  79. set_attribute(HTML::AttributeNames::contenteditable, "false");
  80. return {};
  81. }
  82. return DOM::SyntaxError::create("Invalid contentEditable value, must be 'true', 'false', or 'inherit'");
  83. }
  84. void HTMLElement::set_inner_text(StringView text)
  85. {
  86. remove_all_children();
  87. append_child(document().create_text_node(text));
  88. set_needs_style_update(true);
  89. document().invalidate_layout();
  90. }
  91. String HTMLElement::inner_text()
  92. {
  93. StringBuilder builder;
  94. // innerText for element being rendered takes visibility into account, so force a layout and then walk the layout tree.
  95. document().update_layout();
  96. if (!layout_node())
  97. return text_content();
  98. Function<void(const Layout::Node&)> recurse = [&](auto& node) {
  99. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  100. if (is<Layout::TextNode>(child))
  101. builder.append(verify_cast<Layout::TextNode>(*child).text_for_rendering());
  102. if (is<Layout::BreakNode>(child))
  103. builder.append('\n');
  104. recurse(*child);
  105. }
  106. };
  107. recurse(*layout_node());
  108. return builder.to_string();
  109. }
  110. unsigned HTMLElement::offset_top() const
  111. {
  112. if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
  113. return 0;
  114. auto position = layout_node()->box_type_agnostic_position();
  115. auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
  116. return position.y() - parent_position.y();
  117. }
  118. unsigned HTMLElement::offset_left() const
  119. {
  120. if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node())
  121. return 0;
  122. auto position = layout_node()->box_type_agnostic_position();
  123. auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
  124. return position.x() - parent_position.x();
  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. }