CSSRuleList.cpp 8.4 KB

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