StyleResolver.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  51. ASSERT_NOT_REACHED();
  52. }
  53. static bool matches(const Selector& selector, const Element& element)
  54. {
  55. ASSERT(!selector.components().is_empty());
  56. return matches(selector, selector.components().size() - 1, element);
  57. }
  58. static StyleSheet& default_stylesheet()
  59. {
  60. static StyleSheet* sheet;
  61. if (!sheet) {
  62. extern const char default_stylesheet_source[];
  63. String css = default_stylesheet_source;
  64. sheet = &parse_css(css).leak_ref();
  65. }
  66. return *sheet;
  67. }
  68. template<typename Callback>
  69. void StyleResolver::for_each_stylesheet(Callback callback) const
  70. {
  71. callback(default_stylesheet());
  72. for (auto& sheet : document().stylesheets()) {
  73. callback(sheet);
  74. }
  75. }
  76. NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
  77. {
  78. NonnullRefPtrVector<StyleRule> matching_rules;
  79. for_each_stylesheet([&](auto& sheet) {
  80. for (auto& rule : sheet.rules()) {
  81. for (auto& selector : rule.selectors()) {
  82. if (matches(selector, element)) {
  83. matching_rules.append(rule);
  84. break;
  85. }
  86. }
  87. }
  88. });
  89. #ifdef HTML_DEBUG
  90. dbgprintf("Rules matching Element{%p}\n", &element);
  91. for (auto& rule : matching_rules) {
  92. dump_rule(rule);
  93. }
  94. #endif
  95. return matching_rules;
  96. }
  97. bool StyleResolver::is_inherited_property(const StringView& name)
  98. {
  99. static HashTable<String> inherited_properties;
  100. if (inherited_properties.is_empty()) {
  101. inherited_properties.set("border-collapse");
  102. inherited_properties.set("border-spacing");
  103. inherited_properties.set("color");
  104. inherited_properties.set("font-family");
  105. inherited_properties.set("font-size");
  106. inherited_properties.set("font-style");
  107. inherited_properties.set("font-variant");
  108. inherited_properties.set("font-weight");
  109. inherited_properties.set("font");
  110. inherited_properties.set("letter-spacing");
  111. inherited_properties.set("line-height");
  112. inherited_properties.set("list-style-image");
  113. inherited_properties.set("list-style-position");
  114. inherited_properties.set("list-style-type");
  115. inherited_properties.set("list-style");
  116. inherited_properties.set("text-align");
  117. inherited_properties.set("text-indent");
  118. inherited_properties.set("text-transform");
  119. inherited_properties.set("visibility");
  120. inherited_properties.set("white-space");
  121. inherited_properties.set("word-spacing");
  122. // FIXME: This property is not supposed to be inherited, but we currently
  123. // rely on inheritance to propagate decorations into line boxes.
  124. inherited_properties.set("text-decoration");
  125. }
  126. return inherited_properties.contains(name);
  127. }
  128. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& element, const StyleProperties* parent_properties) const
  129. {
  130. auto style_properties = StyleProperties::create();
  131. if (parent_properties) {
  132. parent_properties->for_each_property([&](const StringView& name, auto& value) {
  133. if (is_inherited_property(name))
  134. style_properties->set_property(name, value);
  135. });
  136. }
  137. element.apply_presentational_hints(*style_properties);
  138. auto matching_rules = collect_matching_rules(element);
  139. for (auto& rule : matching_rules) {
  140. for (auto& property : rule.declaration().properties()) {
  141. style_properties->set_property(property.name, property.value);
  142. }
  143. }
  144. auto style_attribute = element.attribute("style");
  145. if (!style_attribute.is_null()) {
  146. if (auto declaration = parse_css_declaration(style_attribute)) {
  147. for (auto& property : declaration->properties()) {
  148. style_properties->set_property(property.name, property.value);
  149. }
  150. }
  151. }
  152. return style_properties;
  153. }