CSSRuleList.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. CSSRuleList::~CSSRuleList()
  18. {
  19. }
  20. bool CSSRuleList::is_supported_property_index(u32 index) const
  21. {
  22. // 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.
  23. // If there are no such CSSRule objects, then there are no supported property indices.
  24. return index < m_rules.size();
  25. }
  26. // https://www.w3.org/TR/cssom/#insert-a-css-rule
  27. DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule> rule, u32 index)
  28. {
  29. // 1. Set length to the number of items in list.
  30. auto length = m_rules.size();
  31. // 2. If index is greater than length, then throw an IndexSizeError exception.
  32. if (index > length)
  33. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  34. // NOTE: These steps don't apply since we're receiving a parsed rule.
  35. // 3. Set new rule to the results of performing parse a CSS rule on argument rule.
  36. // 4. If new rule is a syntax error, throw a SyntaxError exception.
  37. // 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]
  38. // 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.
  39. // 7. Insert new rule into list at the zero-indexed position index.
  40. m_rules.insert(index, move(rule));
  41. // 8. Return index.
  42. return index;
  43. }
  44. // https://www.w3.org/TR/cssom/#remove-a-css-rule
  45. DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
  46. {
  47. // 1. Set length to the number of items in list.
  48. auto length = m_rules.size();
  49. // 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
  50. if (index >= length)
  51. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  52. // 3. Set old rule to the indexth item in list.
  53. auto& old_rule = m_rules[index];
  54. // 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.
  55. (void)old_rule;
  56. // 5. Remove rule old rule from list at the zero-indexed position index.
  57. m_rules.remove(index);
  58. // FIXME: 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
  59. return {};
  60. }
  61. void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  62. {
  63. for (auto& rule : m_rules) {
  64. switch (rule.type()) {
  65. case CSSRule::Type::Style:
  66. callback(verify_cast<CSSStyleRule>(rule));
  67. break;
  68. case CSSRule::Type::Import: {
  69. auto const& import_rule = verify_cast<CSSImportRule>(rule);
  70. if (import_rule.has_import_result())
  71. import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
  72. break;
  73. }
  74. case CSSRule::Type::Media:
  75. verify_cast<CSSMediaRule>(rule).for_each_effective_style_rule(callback);
  76. break;
  77. case CSSRule::Type::Supports:
  78. verify_cast<CSSSupportsRule>(rule).for_each_effective_style_rule(callback);
  79. break;
  80. case CSSRule::Type::__Count:
  81. VERIFY_NOT_REACHED();
  82. }
  83. }
  84. }
  85. bool CSSRuleList::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
  86. {
  87. for (auto& rule : m_rules) {
  88. if (rule.type() == CSSRule::Type::Import) {
  89. auto& import_rule = verify_cast<CSSImportRule>(rule);
  90. if (!import_rule.has_import_result()) {
  91. callback(import_rule);
  92. return true;
  93. }
  94. if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
  95. return true;
  96. }
  97. } else if (rule.type() == CSSRule::Type::Media) {
  98. return verify_cast<CSSMediaRule>(rule).for_first_not_loaded_import_rule(callback);
  99. } else if (rule.type() == CSSRule::Type::Supports) {
  100. return verify_cast<CSSSupportsRule>(rule).for_first_not_loaded_import_rule(callback);
  101. }
  102. }
  103. return false;
  104. }
  105. void CSSRuleList::evaluate_media_queries(DOM::Window const& window)
  106. {
  107. for (auto& rule : m_rules) {
  108. switch (rule.type()) {
  109. case CSSRule::Type::Style:
  110. break;
  111. case CSSRule::Type::Import: {
  112. auto& import_rule = verify_cast<CSSImportRule>(rule);
  113. if (import_rule.has_import_result())
  114. import_rule.loaded_style_sheet()->evaluate_media_queries(window);
  115. break;
  116. }
  117. case CSSRule::Type::Media: {
  118. auto& media_rule = verify_cast<CSSMediaRule>(rule);
  119. if (media_rule.evaluate(window))
  120. media_rule.css_rules().evaluate_media_queries(window);
  121. break;
  122. }
  123. case CSSRule::Type::Supports: {
  124. auto& supports_rule = verify_cast<CSSSupportsRule>(rule);
  125. if (supports_rule.condition_matches())
  126. supports_rule.css_rules().evaluate_media_queries(window);
  127. break;
  128. }
  129. case CSSRule::Type::__Count:
  130. VERIFY_NOT_REACHED();
  131. }
  132. }
  133. }
  134. }