CSSRuleList.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2021-2022, 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/CSSRule.h>
  10. #include <LibWeb/CSS/CSSRuleList.h>
  11. #include <LibWeb/CSS/CSSSupportsRule.h>
  12. #include <LibWeb/CSS/Parser/Parser.h>
  13. #include <LibWeb/HTML/Window.h>
  14. namespace Web::CSS {
  15. CSSRuleList* CSSRuleList::create(HTML::Window& window_object, JS::MarkedVector<CSSRule*> const& rules)
  16. {
  17. auto* rule_list = window_object.heap().allocate<CSSRuleList>(window_object.realm(), window_object);
  18. for (auto* rule : rules)
  19. rule_list->m_rules.append(*rule);
  20. return rule_list;
  21. }
  22. CSSRuleList::CSSRuleList(HTML::Window& window_object)
  23. : Bindings::LegacyPlatformObject(window_object.cached_web_prototype("CSSRuleList"))
  24. {
  25. }
  26. CSSRuleList* CSSRuleList::create_empty(HTML::Window& window_object)
  27. {
  28. return window_object.heap().allocate<CSSRuleList>(window_object.realm(), window_object);
  29. }
  30. void CSSRuleList::visit_edges(Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. for (auto& rule : m_rules)
  34. visitor.visit(&rule);
  35. }
  36. bool CSSRuleList::is_supported_property_index(u32 index) const
  37. {
  38. // 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.
  39. // If there are no such CSSRule objects, then there are no supported property indices.
  40. return index < m_rules.size();
  41. }
  42. // https://www.w3.org/TR/cssom/#insert-a-css-rule
  43. WebIDL::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView, CSSRule*> rule, u32 index)
  44. {
  45. // 1. Set length to the number of items in list.
  46. auto length = m_rules.size();
  47. // 2. If index is greater than length, then throw an IndexSizeError exception.
  48. if (index > length)
  49. return DOM::IndexSizeError::create(global_object(), "CSS rule index out of bounds.");
  50. // 3. Set new rule to the results of performing parse a CSS rule on argument rule.
  51. // NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
  52. // spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
  53. // if that variant holds a CSSRule already.
  54. CSSRule* new_rule = nullptr;
  55. if (rule.has<StringView>()) {
  56. new_rule = parse_css_rule(
  57. CSS::Parser::ParsingContext { static_cast<HTML::Window&>(global_object()) },
  58. rule.get<StringView>());
  59. } else {
  60. new_rule = rule.get<CSSRule*>();
  61. }
  62. // 4. If new rule is a syntax error, throw a SyntaxError exception.
  63. if (!new_rule)
  64. return DOM::SyntaxError::create(global_object(), "Unable to parse CSS rule.");
  65. // 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]
  66. // 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.
  67. // 7. Insert new rule into list at the zero-indexed position index.
  68. m_rules.insert(index, *new_rule);
  69. // 8. Return index.
  70. return index;
  71. }
  72. // https://www.w3.org/TR/cssom/#remove-a-css-rule
  73. WebIDL::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
  74. {
  75. // 1. Set length to the number of items in list.
  76. auto length = m_rules.size();
  77. // 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
  78. if (index >= length)
  79. return DOM::IndexSizeError::create(global_object(), "CSS rule index out of bounds.");
  80. // 3. Set old rule to the indexth item in list.
  81. CSSRule& old_rule = m_rules[index];
  82. // 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.
  83. // 5. Remove rule old rule from list at the zero-indexed position index.
  84. m_rules.remove(index);
  85. // 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
  86. old_rule.set_parent_rule(nullptr);
  87. old_rule.set_parent_style_sheet(nullptr);
  88. return {};
  89. }
  90. void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  91. {
  92. for (auto const& rule : m_rules) {
  93. switch (rule.type()) {
  94. case CSSRule::Type::FontFace:
  95. break;
  96. case CSSRule::Type::Import: {
  97. auto const& import_rule = static_cast<CSSImportRule const&>(rule);
  98. if (import_rule.has_import_result() && import_rule.loaded_style_sheet())
  99. import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
  100. break;
  101. }
  102. case CSSRule::Type::Media:
  103. static_cast<CSSMediaRule const&>(rule).for_each_effective_style_rule(callback);
  104. break;
  105. case CSSRule::Type::Style:
  106. callback(static_cast<CSSStyleRule const&>(rule));
  107. break;
  108. case CSSRule::Type::Supports:
  109. static_cast<CSSSupportsRule const&>(rule).for_each_effective_style_rule(callback);
  110. break;
  111. }
  112. }
  113. }
  114. bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
  115. {
  116. bool any_media_queries_changed_match_state = false;
  117. for (auto& rule : m_rules) {
  118. switch (rule.type()) {
  119. case CSSRule::Type::FontFace:
  120. break;
  121. case CSSRule::Type::Import: {
  122. auto& import_rule = verify_cast<CSSImportRule>(rule);
  123. if (import_rule.has_import_result() && import_rule.loaded_style_sheet() && import_rule.loaded_style_sheet()->evaluate_media_queries(window))
  124. any_media_queries_changed_match_state = true;
  125. break;
  126. }
  127. case CSSRule::Type::Media: {
  128. auto& media_rule = verify_cast<CSSMediaRule>(rule);
  129. bool did_match = media_rule.condition_matches();
  130. bool now_matches = media_rule.evaluate(window);
  131. if (did_match != now_matches)
  132. any_media_queries_changed_match_state = true;
  133. if (now_matches && media_rule.css_rules().evaluate_media_queries(window))
  134. any_media_queries_changed_match_state = true;
  135. break;
  136. }
  137. case CSSRule::Type::Style:
  138. break;
  139. case CSSRule::Type::Supports: {
  140. auto& supports_rule = verify_cast<CSSSupportsRule>(rule);
  141. if (supports_rule.condition_matches() && supports_rule.css_rules().evaluate_media_queries(window))
  142. any_media_queries_changed_match_state = true;
  143. break;
  144. }
  145. }
  146. }
  147. return any_media_queries_changed_match_state;
  148. }
  149. JS::Value CSSRuleList::item_value(size_t index) const
  150. {
  151. return item(index);
  152. }
  153. }