CSSRuleList.cpp 7.3 KB

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