StyleResolver.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <LibHTML/CSS/SelectorEngine.h>
  2. #include <LibHTML/CSS/StyleResolver.h>
  3. #include <LibHTML/CSS/StyleSheet.h>
  4. #include <LibHTML/DOM/Document.h>
  5. #include <LibHTML/DOM/Element.h>
  6. #include <LibHTML/Dump.h>
  7. #include <LibHTML/Parser/CSSParser.h>
  8. #include <stdio.h>
  9. StyleResolver::StyleResolver(Document& document)
  10. : m_document(document)
  11. {
  12. }
  13. StyleResolver::~StyleResolver()
  14. {
  15. }
  16. static StyleSheet& default_stylesheet()
  17. {
  18. static StyleSheet* sheet;
  19. if (!sheet) {
  20. extern const char default_stylesheet_source[];
  21. String css = default_stylesheet_source;
  22. sheet = &parse_css(css).leak_ref();
  23. }
  24. return *sheet;
  25. }
  26. template<typename Callback>
  27. void StyleResolver::for_each_stylesheet(Callback callback) const
  28. {
  29. callback(default_stylesheet());
  30. for (auto& sheet : document().stylesheets()) {
  31. callback(sheet);
  32. }
  33. }
  34. NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
  35. {
  36. NonnullRefPtrVector<StyleRule> matching_rules;
  37. for_each_stylesheet([&](auto& sheet) {
  38. for (auto& rule : sheet.rules()) {
  39. for (auto& selector : rule.selectors()) {
  40. if (SelectorEngine::matches(selector, element)) {
  41. matching_rules.append(rule);
  42. break;
  43. }
  44. }
  45. }
  46. });
  47. #ifdef HTML_DEBUG
  48. dbgprintf("Rules matching Element{%p}\n", &element);
  49. for (auto& rule : matching_rules) {
  50. dump_rule(rule);
  51. }
  52. #endif
  53. return matching_rules;
  54. }
  55. bool StyleResolver::is_inherited_property(CSS::PropertyID property_id)
  56. {
  57. static HashTable<CSS::PropertyID> inherited_properties;
  58. if (inherited_properties.is_empty()) {
  59. inherited_properties.set(CSS::PropertyID::BorderCollapse);
  60. inherited_properties.set(CSS::PropertyID::BorderSpacing);
  61. inherited_properties.set(CSS::PropertyID::Color);
  62. inherited_properties.set(CSS::PropertyID::FontFamily);
  63. inherited_properties.set(CSS::PropertyID::FontSize);
  64. inherited_properties.set(CSS::PropertyID::FontStyle);
  65. inherited_properties.set(CSS::PropertyID::FontVariant);
  66. inherited_properties.set(CSS::PropertyID::FontWeight);
  67. inherited_properties.set(CSS::PropertyID::LetterSpacing);
  68. inherited_properties.set(CSS::PropertyID::LineHeight);
  69. inherited_properties.set(CSS::PropertyID::ListStyle);
  70. inherited_properties.set(CSS::PropertyID::ListStyleImage);
  71. inherited_properties.set(CSS::PropertyID::ListStylePosition);
  72. inherited_properties.set(CSS::PropertyID::ListStyleType);
  73. inherited_properties.set(CSS::PropertyID::TextAlign);
  74. inherited_properties.set(CSS::PropertyID::TextIndent);
  75. inherited_properties.set(CSS::PropertyID::TextTransform);
  76. inherited_properties.set(CSS::PropertyID::Visibility);
  77. inherited_properties.set(CSS::PropertyID::WhiteSpace);
  78. inherited_properties.set(CSS::PropertyID::WordSpacing);
  79. // FIXME: This property is not supposed to be inherited, but we currently
  80. // rely on inheritance to propagate decorations into line boxes.
  81. inherited_properties.set(CSS::PropertyID::TextDecoration);
  82. }
  83. return inherited_properties.contains(property_id);
  84. }
  85. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& element, const StyleProperties* parent_style) const
  86. {
  87. auto style = StyleProperties::create();
  88. if (parent_style) {
  89. parent_style->for_each_property([&](auto property_id, auto& value) {
  90. if (is_inherited_property(property_id))
  91. style->set_property(property_id, value);
  92. });
  93. }
  94. element.apply_presentational_hints(*style);
  95. auto matching_rules = collect_matching_rules(element);
  96. for (auto& rule : matching_rules) {
  97. for (auto& property : rule.declaration().properties()) {
  98. style->set_property(property.property_id, property.value);
  99. }
  100. }
  101. auto style_attribute = element.attribute("style");
  102. if (!style_attribute.is_null()) {
  103. if (auto declaration = parse_css_declaration(style_attribute)) {
  104. for (auto& property : declaration->properties()) {
  105. style->set_property(property.property_id, property.value);
  106. }
  107. }
  108. }
  109. return style;
  110. }