StyleResolver.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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& selector, const Element& element)
  16. {
  17. // FIXME: Support compound selectors.
  18. ASSERT(selector.components().size() == 1);
  19. auto& component = selector.components().first();
  20. switch (component.type) {
  21. case Selector::Component::Type::Id:
  22. return component.value == element.attribute("id");
  23. case Selector::Component::Type::Class:
  24. return element.has_class(component.value);
  25. case Selector::Component::Type::TagName:
  26. return component.value == element.tag_name();
  27. default:
  28. ASSERT_NOT_REACHED();
  29. }
  30. }
  31. NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Element& element) const
  32. {
  33. NonnullRefPtrVector<StyleRule> matching_rules;
  34. for (auto& sheet : document().stylesheets()) {
  35. for (auto& rule : sheet.rules()) {
  36. for (auto& selector : rule.selectors()) {
  37. if (matches(selector, element)) {
  38. matching_rules.append(rule);
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. #ifdef HTML_DEBUG
  45. dbgprintf("Rules matching Element{%p}\n", &element);
  46. for (auto& rule : matching_rules) {
  47. dump_rule(rule);
  48. }
  49. #endif
  50. return matching_rules;
  51. }
  52. NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& element, const StyleProperties* parent_properties) const
  53. {
  54. auto style_properties = StyleProperties::create();
  55. if (parent_properties) {
  56. parent_properties->for_each_property([&](const StringView& name, auto& value) {
  57. // TODO: proper inheritance
  58. if (name.starts_with("font") || name == "white-space" || name == "color" || name == "text-decoration")
  59. style_properties->set_property(name, value);
  60. });
  61. }
  62. element.apply_presentational_hints(*style_properties);
  63. auto matching_rules = collect_matching_rules(element);
  64. for (auto& rule : matching_rules) {
  65. for (auto& property : rule.declaration().properties()) {
  66. style_properties->set_property(property.name, property.value);
  67. }
  68. }
  69. auto style_attribute = element.attribute("style");
  70. if (!style_attribute.is_null()) {
  71. if (auto declaration = parse_css_declaration(style_attribute)) {
  72. for (auto& property : declaration->properties()) {
  73. style_properties->set_property(property.name, property.value);
  74. }
  75. }
  76. }
  77. return style_properties;
  78. }