Element.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <LibHTML/CSS/StyleResolver.h>
  27. #include <LibHTML/CSS/PropertyID.h>
  28. #include <LibHTML/CSS/Length.h>
  29. #include <LibHTML/DOM/Document.h>
  30. #include <LibHTML/DOM/Element.h>
  31. #include <LibHTML/Layout/LayoutBlock.h>
  32. #include <LibHTML/Layout/LayoutInline.h>
  33. #include <LibHTML/Layout/LayoutListItem.h>
  34. #include <LibHTML/Layout/LayoutTable.h>
  35. #include <LibHTML/Layout/LayoutTableCell.h>
  36. #include <LibHTML/Layout/LayoutTableRow.h>
  37. #include <LibHTML/Layout/LayoutTreeBuilder.h>
  38. Element::Element(Document& document, const String& tag_name)
  39. : ParentNode(document, NodeType::ELEMENT_NODE)
  40. , m_tag_name(tag_name)
  41. {
  42. }
  43. Element::~Element()
  44. {
  45. }
  46. Attribute* Element::find_attribute(const String& name)
  47. {
  48. for (auto& attribute : m_attributes) {
  49. if (attribute.name() == name)
  50. return &attribute;
  51. }
  52. return nullptr;
  53. }
  54. const Attribute* Element::find_attribute(const String& name) const
  55. {
  56. for (auto& attribute : m_attributes) {
  57. if (attribute.name() == name)
  58. return &attribute;
  59. }
  60. return nullptr;
  61. }
  62. String Element::attribute(const String& name) const
  63. {
  64. if (auto* attribute = find_attribute(name))
  65. return attribute->value();
  66. return {};
  67. }
  68. void Element::set_attribute(const String& name, const String& value)
  69. {
  70. if (auto* attribute = find_attribute(name))
  71. attribute->set_value(value);
  72. else
  73. m_attributes.empend(name, value);
  74. parse_attribute(name, value);
  75. }
  76. void Element::set_attributes(Vector<Attribute>&& attributes)
  77. {
  78. m_attributes = move(attributes);
  79. for (auto& attribute : m_attributes)
  80. parse_attribute(attribute.name(), attribute.value());
  81. }
  82. bool Element::has_class(const StringView& class_name) const
  83. {
  84. auto value = attribute("class");
  85. if (value.is_empty())
  86. return false;
  87. auto parts = value.split_view(' ');
  88. for (auto& part : parts) {
  89. if (part == class_name)
  90. return true;
  91. }
  92. return false;
  93. }
  94. RefPtr<LayoutNode> Element::create_layout_node(const StyleProperties* parent_style) const
  95. {
  96. auto style = document().style_resolver().resolve_style(*this, parent_style);
  97. const_cast<Element&>(*this).m_resolved_style = style;
  98. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  99. if (display == "none")
  100. return nullptr;
  101. if (display == "block")
  102. return adopt(*new LayoutBlock(this, move(style)));
  103. if (display == "inline")
  104. return adopt(*new LayoutInline(*this, move(style)));
  105. if (display == "list-item")
  106. return adopt(*new LayoutListItem(*this, move(style)));
  107. if (display == "table")
  108. return adopt(*new LayoutTable(*this, move(style)));
  109. if (display == "table-row")
  110. return adopt(*new LayoutTableRow(*this, move(style)));
  111. if (display == "table-cell")
  112. return adopt(*new LayoutTableCell(*this, move(style)));
  113. if (display == "inline-block")
  114. return adopt(*new LayoutBlock(this, move(style)));
  115. ASSERT_NOT_REACHED();
  116. }
  117. void Element::parse_attribute(const String&, const String&)
  118. {
  119. }
  120. enum class StyleDifference {
  121. None,
  122. NeedsRepaint,
  123. NeedsRelayout,
  124. };
  125. static StyleDifference compute_style_difference(const StyleProperties& old_style, const StyleProperties& new_style, const Document& document)
  126. {
  127. if (old_style == new_style)
  128. return StyleDifference::None;
  129. bool needs_repaint = false;
  130. bool needs_relayout = false;
  131. if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black))
  132. needs_repaint = true;
  133. else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black))
  134. needs_repaint = true;
  135. if (needs_relayout)
  136. return StyleDifference::NeedsRelayout;
  137. if (needs_repaint)
  138. return StyleDifference::NeedsRepaint;
  139. return StyleDifference::None;
  140. }
  141. void Element::recompute_style()
  142. {
  143. set_needs_style_update(false);
  144. ASSERT(parent());
  145. auto* parent_layout_node = parent()->layout_node();
  146. if (!parent_layout_node)
  147. return;
  148. ASSERT(parent_layout_node);
  149. auto style = document().style_resolver().resolve_style(*this, &parent_layout_node->style());
  150. m_resolved_style = style;
  151. if (!layout_node()) {
  152. if (style->string_or_fallback(CSS::PropertyID::Display, "inline") == "none")
  153. return;
  154. // We need a new layout tree here!
  155. LayoutTreeBuilder tree_builder;
  156. tree_builder.build(*this);
  157. return;
  158. }
  159. auto diff = compute_style_difference(layout_node()->style(), *style, document());
  160. if (diff == StyleDifference::None)
  161. return;
  162. layout_node()->set_style(*style);
  163. if (diff == StyleDifference::NeedsRelayout) {
  164. ASSERT_NOT_REACHED();
  165. }
  166. if (diff == StyleDifference::NeedsRepaint) {
  167. layout_node()->set_needs_display();
  168. }
  169. }
  170. NonnullRefPtr<StyleProperties> Element::computed_style()
  171. {
  172. auto properties = m_resolved_style->clone();
  173. if (layout_node() && layout_node()->has_style()) {
  174. CSS::PropertyID box_model_metrics[] = {
  175. CSS::PropertyID::MarginTop,
  176. CSS::PropertyID::MarginBottom,
  177. CSS::PropertyID::MarginLeft,
  178. CSS::PropertyID::MarginRight,
  179. CSS::PropertyID::PaddingTop,
  180. CSS::PropertyID::PaddingBottom,
  181. CSS::PropertyID::PaddingLeft,
  182. CSS::PropertyID::PaddingRight,
  183. CSS::PropertyID::BorderTopWidth,
  184. CSS::PropertyID::BorderBottomWidth,
  185. CSS::PropertyID::BorderLeftWidth,
  186. CSS::PropertyID::BorderRightWidth,
  187. };
  188. for (CSS::PropertyID id : box_model_metrics) {
  189. auto prop = layout_node()->style().property(id);
  190. if (prop)
  191. properties->set_property(id, prop.value());
  192. }
  193. }
  194. return properties;
  195. }