Element.cpp 9.7 KB

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