CSSRuleList.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibWeb/CSS/CSSImportRule.h>
  8. #include <LibWeb/CSS/CSSMediaRule.h>
  9. #include <LibWeb/CSS/CSSRuleList.h>
  10. #include <LibWeb/CSS/CSSSupportsRule.h>
  11. #include <LibWeb/DOM/ExceptionOr.h>
  12. namespace Web::CSS {
  13. CSSRuleList::CSSRuleList(NonnullRefPtrVector<CSSRule>&& rules)
  14. : m_rules(move(rules))
  15. {
  16. }
  17. bool CSSRuleList::is_supported_property_index(u32 index) const
  18. {
  19. // The object’s supported property indices are the numbers in the range zero to one less than the number of CSSRule objects represented by the collection.
  20. // If there are no such CSSRule objects, then there are no supported property indices.
  21. return index < m_rules.size();
  22. }
  23. // https://www.w3.org/TR/cssom/#insert-a-css-rule
  24. DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule> rule, u32 index)
  25. {
  26. // 1. Set length to the number of items in list.
  27. auto length = m_rules.size();
  28. // 2. If index is greater than length, then throw an IndexSizeError exception.
  29. if (index > length)
  30. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  31. // NOTE: These steps don't apply since we're receiving a parsed rule.
  32. // 3. Set new rule to the results of performing parse a CSS rule on argument rule.
  33. // 4. If new rule is a syntax error, throw a SyntaxError exception.
  34. // FIXME: 5. If new rule cannot be inserted into list at the zero-index position index due to constraints specified by CSS, then throw a HierarchyRequestError exception. [CSS21]
  35. // FIXME: 6. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
  36. // 7. Insert new rule into list at the zero-indexed position index.
  37. m_rules.insert(index, move(rule));
  38. // 8. Return index.
  39. return index;
  40. }
  41. // https://www.w3.org/TR/cssom/#remove-a-css-rule
  42. DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
  43. {
  44. // 1. Set length to the number of items in list.
  45. auto length = m_rules.size();
  46. // 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
  47. if (index >= length)
  48. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  49. // 3. Set old rule to the indexth item in list.
  50. auto& old_rule = m_rules[index];
  51. // FIXME: 4. If old rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
  52. (void)old_rule;
  53. // 5. Remove rule old rule from list at the zero-indexed position index.
  54. m_rules.remove(index);
  55. // FIXME: 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
  56. return {};
  57. }
  58. void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  59. {
  60. for (auto const& rule : m_rules) {
  61. switch (rule.type()) {
  62. case CSSRule::Type::FontFace:
  63. break;
  64. case CSSRule::Type::Import: {
  65. auto const& import_rule = static_cast<CSSImportRule const&>(rule);
  66. if (import_rule.has_import_result())
  67. import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
  68. break;
  69. }
  70. case CSSRule::Type::Media:
  71. static_cast<CSSMediaRule const&>(rule).for_each_effective_style_rule(callback);
  72. break;
  73. case CSSRule::Type::Style:
  74. callback(static_cast<CSSStyleRule const&>(rule));
  75. break;
  76. case CSSRule::Type::Supports:
  77. static_cast<CSSSupportsRule const&>(rule).for_each_effective_style_rule(callback);
  78. break;
  79. case CSSRule::Type::__Count:
  80. VERIFY_NOT_REACHED();
  81. }
  82. }
  83. }
  84. bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
  85. {
  86. bool any_media_queries_changed_match_state = false;
  87. for (auto& rule : m_rules) {
  88. switch (rule.type()) {
  89. case CSSRule::Type::FontFace:
  90. break;
  91. case CSSRule::Type::Import: {
  92. auto& import_rule = verify_cast<CSSImportRule>(rule);
  93. if (import_rule.has_import_result() && import_rule.loaded_style_sheet()->evaluate_media_queries(window))
  94. any_media_queries_changed_match_state = true;
  95. break;
  96. }
  97. case CSSRule::Type::Media: {
  98. auto& media_rule = verify_cast<CSSMediaRule>(rule);
  99. bool did_match = media_rule.condition_matches();
  100. bool now_matches = media_rule.evaluate(window);
  101. if (did_match != now_matches)
  102. any_media_queries_changed_match_state = true;
  103. if (now_matches && media_rule.css_rules().evaluate_media_queries(window))
  104. any_media_queries_changed_match_state = true;
  105. break;
  106. }
  107. case CSSRule::Type::Style:
  108. break;
  109. case CSSRule::Type::Supports: {
  110. auto& supports_rule = verify_cast<CSSSupportsRule>(rule);
  111. if (supports_rule.condition_matches() && supports_rule.css_rules().evaluate_media_queries(window))
  112. any_media_queries_changed_match_state = true;
  113. break;
  114. }
  115. case CSSRule::Type::__Count:
  116. VERIFY_NOT_REACHED();
  117. }
  118. }
  119. return any_media_queries_changed_match_state;
  120. }
  121. }