StyleResolver.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <LibHTML/CSS/StyleResolver.h>
  2. #include <LibHTML/CSS/StyleSheet.h>
  3. #include <LibHTML/DOM/Document.h>
  4. #include <LibHTML/DOM/Element.h>
  5. #include <LibHTML/Dump.h>
  6. #include <LibHTML/Parser/CSSParser.h>
  7. #include <stdio.h>
  8. StyleResolver::StyleResolver(Document& document)
  9. : m_document(document)
  10. {
  11. }
  12. StyleResolver::~StyleResolver()
  13. {
  14. }
  15. static bool matches(const Selector::Component& component, const Element& element)
  16. {
  17. switch (component.type) {
  18. case Selector::Component::Type::Id:
  19. return component.value == element.attribute("id");
  20. case Selector::Component::Type::Class:
  21. return element.has_class(component.value);
  22. case Selector::Component::Type::TagName:
  23. return component.value == element.tag_name();
  24. default:
  25. ASSERT_NOT_REACHED();
  26. }
  27. }
  28. static bool matches(const Selector& selector, int component_index, const Element& element)
  29. {
  30. auto& component = selector.components()[component_index];
  31. if (!matches(component, element))
  32. return false;
  33. switch (component.relation) {
  34. case Selector::Component::Relation::None:
  35. return true;
  36. case Selector::Component::Relation::Descendant:
  37. ASSERT(component_index != 0);
  38. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  39. if (!ancestor->is_element())
  40. continue;
  41. if (matches(selector, component_index - 1, static_cast<const Element&>(*ancestor)))
  42. return true;
  43. }
  44. return false;
  45. case Selector::Component::Relation::ImmediateChild:
  46. ASSERT(component_index != 0);
  47. if (!element.parent() || !element.parent()->is_element())
  48. return false;
  49. return matches(selector, component_index - 1, static_cast<const Element&>(*element.parent()));
  50. case Selector::Component::Relation::AdjacentSibling:
  51. ASSERT(component_index != 0);
  52. if (auto* sibling = element.previous_element_sibling())
  53. return matches(selector, component_index - 1, *sibling);
  54. return false;
  55. case Selector::Component::Relation::GeneralSibling:
  56. ASSERT(component_index != 0);
  57. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  58. if (matches(selector, component_index - 1, *sibling))
  59. return true;
  60. }
  61. return false;
  62. }
  63. ASSERT_NOT_REACHED();
  64. }
  65. static bool matches(const Selector& selector, const Element& element)
  66. {
  67. ASSERT(!selector.components().is_empty());
  68. return matches(selector, selector.components().size() - 1, element);
  69. }
  70. static StyleSheet& default_stylesheet()
  71. {
  72. static StyleSheet* sheet;
  73. if (!sheet) {
  74. extern const char default_stylesheet_source[];
  75. String css = default_stylesheet_source;
  76. sheet = &parse_css(css).leak_ref();
  77. }
  78. return *sheet;
  79. }
  80. template<typename Callback>
  81. void StyleResolver::for_each_stylesheet(Callback callback) const
  82. {
  83. callback(default_stylesheet());
  84. for (auto& sheet : document().stylesheets()) {
  85. callback(sheet);
  86. }
  87. }
  88. NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
  89. {
  90. NonnullRefPtrVector<StyleRule> matching_rules;
  91. for_each_stylesheet([&](auto& sheet) {
  92. for (auto& rule : sheet.rules()) {
  93. for (auto& selector : rule.selectors()) {
  94. if (matches(selector, element)) {
  95. matching_rules.append(rule);
  96. break;
  97. }
  98. }
  99. }
  100. });
  101. #ifdef HTML_DEBUG
  102. dbgprintf("Rules matching Element{%p}\n", &element);
  103. for (auto& rule : matching_rules) {
  104. dump_rule(rule);
  105. }
  106. #endif
  107. return matching_rules;
  108. }
  109. bool StyleResolver::is_inherited_property(const StringView& name)
  110. {
  111. static HashTable<String> inherited_properties;
  112. if (inherited_properties.is_empty()) {
  113. inherited_properties.set("border-collapse");
  114. inherited_properties.set("border-spacing");
  115. inherited_properties.set("color");
  116. inherited_properties.set("font-family");
  117. inherited_properties.set("font-size");
  118. inherited_properties.set("font-style");
  119. inherited_properties.set("font-variant");
  120. inherited_properties.set("font-weight");
  121. inherited_properties.set("font");
  122. inherited_properties.set("letter-spacing");
  123. inherited_properties.set("line-height");
  124. inherited_properties.set("list-style-image");
  125. inherited_properties.set("list-style-position");
  126. inherited_properties.set("list-style-type");
  127. inherited_properties.set("list-style");
  128. inherited_properties.set("text-align");
  129. inherited_properties.set("text-indent");
  130. inherited_properties.set("text-transform");
  131. inherited_properties.set("visibility");
  132. inherited_properties.set("white-space");
  133. inherited_properties.set("word-spacing");
  134. // FIXME: This property is not supposed to be inherited, but we currently
  135. // rely on inheritance to propagate decorations into line boxes.
  136. inherited_properties.set("text-decoration");
  137. }
  138. return inherited_properties.contains(name);
  139. }
  140. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& element, const StyleProperties* parent_properties) const
  141. {
  142. auto style_properties = StyleProperties::create();
  143. if (parent_properties) {
  144. parent_properties->for_each_property([&](const StringView& name, auto& value) {
  145. if (is_inherited_property(name))
  146. style_properties->set_property(name, value);
  147. });
  148. }
  149. element.apply_presentational_hints(*style_properties);
  150. auto matching_rules = collect_matching_rules(element);
  151. for (auto& rule : matching_rules) {
  152. for (auto& property : rule.declaration().properties()) {
  153. style_properties->set_property(property.name, property.value);
  154. }
  155. }
  156. auto style_attribute = element.attribute("style");
  157. if (!style_attribute.is_null()) {
  158. if (auto declaration = parse_css_declaration(style_attribute)) {
  159. for (auto& property : declaration->properties()) {
  160. style_properties->set_property(property.name, property.value);
  161. }
  162. }
  163. }
  164. return style_properties;
  165. }