Element.cpp 8.9 KB

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