Element.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/CSS/Length.h>
  28. #include <LibWeb/CSS/Parser/CSSParser.h>
  29. #include <LibWeb/CSS/PropertyID.h>
  30. #include <LibWeb/CSS/StyleResolver.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/DOM/DocumentFragment.h>
  33. #include <LibWeb/DOM/Element.h>
  34. #include <LibWeb/DOM/Text.h>
  35. #include <LibWeb/Dump.h>
  36. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  37. #include <LibWeb/Layout/BlockBox.h>
  38. #include <LibWeb/Layout/InlineNode.h>
  39. #include <LibWeb/Layout/ListItemBox.h>
  40. #include <LibWeb/Layout/TableBox.h>
  41. #include <LibWeb/Layout/TableCellBox.h>
  42. #include <LibWeb/Layout/TableRowBox.h>
  43. #include <LibWeb/Layout/TableRowGroupBox.h>
  44. #include <LibWeb/Layout/TreeBuilder.h>
  45. namespace Web::DOM {
  46. Element::Element(Document& document, const QualifiedName& qualified_name)
  47. : ParentNode(document, NodeType::ELEMENT_NODE)
  48. , m_qualified_name(qualified_name)
  49. {
  50. }
  51. Element::~Element()
  52. {
  53. }
  54. Attribute* Element::find_attribute(const FlyString& name)
  55. {
  56. for (auto& attribute : m_attributes) {
  57. if (attribute.name() == name)
  58. return &attribute;
  59. }
  60. return nullptr;
  61. }
  62. const Attribute* Element::find_attribute(const FlyString& name) const
  63. {
  64. for (auto& attribute : m_attributes) {
  65. if (attribute.name() == name)
  66. return &attribute;
  67. }
  68. return nullptr;
  69. }
  70. String Element::attribute(const FlyString& name) const
  71. {
  72. if (auto* attribute = find_attribute(name))
  73. return attribute->value();
  74. return {};
  75. }
  76. void Element::set_attribute(const FlyString& name, const String& value)
  77. {
  78. if (auto* attribute = find_attribute(name))
  79. attribute->set_value(value);
  80. else
  81. m_attributes.empend(name, value);
  82. parse_attribute(name, value);
  83. }
  84. void Element::remove_attribute(const FlyString& name)
  85. {
  86. m_attributes.remove_first_matching([&](auto& attribute) { return attribute.name() == name; });
  87. }
  88. void Element::set_attributes(Vector<Attribute>&& attributes)
  89. {
  90. m_attributes = move(attributes);
  91. for (auto& attribute : m_attributes)
  92. parse_attribute(attribute.name(), attribute.value());
  93. }
  94. bool Element::has_class(const FlyString& class_name) const
  95. {
  96. for (auto& class_ : m_classes) {
  97. if (class_ == class_name)
  98. return true;
  99. }
  100. return false;
  101. }
  102. RefPtr<Layout::Node> Element::create_layout_node(const CSS::StyleProperties* parent_style)
  103. {
  104. auto style = document().style_resolver().resolve_style(*this, parent_style);
  105. const_cast<Element&>(*this).m_resolved_style = style;
  106. auto display = style->display();
  107. if (display == CSS::Display::None)
  108. return nullptr;
  109. if (local_name() == "noscript" && document().is_scripting_enabled())
  110. return nullptr;
  111. if (display == CSS::Display::Block)
  112. return adopt(*new Layout::BlockBox(document(), this, move(style)));
  113. if (display == CSS::Display::Inline) {
  114. if (style->float_().value_or(CSS::Float::None) != CSS::Float::None)
  115. return adopt(*new Layout::BlockBox(document(), this, move(style)));
  116. return adopt(*new Layout::InlineNode(document(), *this, move(style)));
  117. }
  118. if (display == CSS::Display::ListItem)
  119. return adopt(*new Layout::ListItemBox(document(), *this, move(style)));
  120. if (display == CSS::Display::Table)
  121. return adopt(*new Layout::TableBox(document(), *this, move(style)));
  122. if (display == CSS::Display::TableRow)
  123. return adopt(*new Layout::TableRowBox(document(), *this, move(style)));
  124. if (display == CSS::Display::TableCell)
  125. return adopt(*new Layout::TableCellBox(document(), *this, move(style)));
  126. if (display == CSS::Display::TableRowGroup || display == CSS::Display::TableHeaderGroup || display == CSS::Display::TableFooterGroup)
  127. return adopt(*new Layout::TableRowGroupBox(document(), *this, move(style)));
  128. if (display == CSS::Display::InlineBlock) {
  129. auto inline_block = adopt(*new Layout::BlockBox(document(), this, move(style)));
  130. inline_block->set_inline(true);
  131. return inline_block;
  132. }
  133. ASSERT_NOT_REACHED();
  134. }
  135. void Element::parse_attribute(const FlyString& name, const String& value)
  136. {
  137. if (name == HTML::AttributeNames::class_) {
  138. auto new_classes = value.split_view(' ');
  139. m_classes.clear();
  140. m_classes.ensure_capacity(new_classes.size());
  141. for (auto& new_class : new_classes) {
  142. m_classes.unchecked_append(new_class);
  143. }
  144. set_needs_style_update(true);
  145. } else if (name == HTML::AttributeNames::style) {
  146. m_inline_style = parse_css_declaration(CSS::ParsingContext(document()), value);
  147. set_needs_style_update(true);
  148. } else if (name == HTML::AttributeNames::id) {
  149. set_needs_style_update(true);
  150. }
  151. }
  152. enum class StyleDifference {
  153. None,
  154. NeedsRepaint,
  155. NeedsRelayout,
  156. };
  157. static StyleDifference compute_style_difference(const CSS::StyleProperties& old_style, const CSS::StyleProperties& new_style, const Document& document)
  158. {
  159. if (old_style == new_style)
  160. return StyleDifference::None;
  161. bool needs_repaint = false;
  162. bool needs_relayout = false;
  163. if (new_style.display() != old_style.display())
  164. needs_relayout = true;
  165. if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black))
  166. needs_repaint = true;
  167. else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black))
  168. needs_repaint = true;
  169. if (needs_relayout)
  170. return StyleDifference::NeedsRelayout;
  171. if (needs_repaint)
  172. return StyleDifference::NeedsRepaint;
  173. return StyleDifference::None;
  174. }
  175. void Element::recompute_style()
  176. {
  177. set_needs_style_update(false);
  178. ASSERT(parent());
  179. auto* parent_layout_node = parent()->layout_node();
  180. if (!parent_layout_node)
  181. return;
  182. ASSERT(parent_layout_node);
  183. auto style = document().style_resolver().resolve_style(*this, &parent_layout_node->specified_style());
  184. m_resolved_style = style;
  185. if (!layout_node()) {
  186. if (style->display() == CSS::Display::None)
  187. return;
  188. // We need a new layout tree here!
  189. Layout::TreeBuilder tree_builder;
  190. tree_builder.build(*this);
  191. return;
  192. }
  193. // Don't bother with style on widgets. NATIVE LOOK & FEEL BABY!
  194. if (layout_node()->is_widget())
  195. return;
  196. auto diff = compute_style_difference(layout_node()->specified_style(), *style, document());
  197. if (diff == StyleDifference::None)
  198. return;
  199. layout_node()->set_specified_style(*style);
  200. if (diff == StyleDifference::NeedsRelayout) {
  201. document().force_layout();
  202. return;
  203. }
  204. if (diff == StyleDifference::NeedsRepaint) {
  205. layout_node()->set_needs_display();
  206. }
  207. }
  208. NonnullRefPtr<CSS::StyleProperties> Element::computed_style()
  209. {
  210. auto properties = m_resolved_style->clone();
  211. if (layout_node() && layout_node()->has_style()) {
  212. CSS::PropertyID box_model_metrics[] = {
  213. CSS::PropertyID::MarginTop,
  214. CSS::PropertyID::MarginBottom,
  215. CSS::PropertyID::MarginLeft,
  216. CSS::PropertyID::MarginRight,
  217. CSS::PropertyID::PaddingTop,
  218. CSS::PropertyID::PaddingBottom,
  219. CSS::PropertyID::PaddingLeft,
  220. CSS::PropertyID::PaddingRight,
  221. CSS::PropertyID::BorderTopWidth,
  222. CSS::PropertyID::BorderBottomWidth,
  223. CSS::PropertyID::BorderLeftWidth,
  224. CSS::PropertyID::BorderRightWidth,
  225. };
  226. for (CSS::PropertyID id : box_model_metrics) {
  227. auto prop = layout_node()->specified_style().property(id);
  228. if (prop.has_value())
  229. properties->set_property(id, prop.value());
  230. }
  231. }
  232. return properties;
  233. }
  234. void Element::set_inner_html(StringView markup)
  235. {
  236. auto new_children = HTML::HTMLDocumentParser::parse_html_fragment(*this, markup);
  237. remove_all_children();
  238. while (!new_children.is_empty()) {
  239. append_child(new_children.take_first());
  240. }
  241. set_needs_style_update(true);
  242. document().invalidate_layout();
  243. }
  244. String Element::inner_html() const
  245. {
  246. StringBuilder builder;
  247. Function<void(const Node&)> recurse = [&](auto& node) {
  248. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  249. if (child->is_element()) {
  250. builder.append('<');
  251. builder.append(downcast<Element>(*child).local_name());
  252. builder.append('>');
  253. recurse(*child);
  254. builder.append("</");
  255. builder.append(downcast<Element>(*child).local_name());
  256. builder.append('>');
  257. }
  258. if (child->is_text()) {
  259. builder.append(downcast<Text>(*child).data());
  260. }
  261. }
  262. };
  263. recurse(*this);
  264. return builder.to_string();
  265. }
  266. bool Element::is_focused() const
  267. {
  268. return document().focused_element() == this;
  269. }
  270. }