CSSRuleList.cpp 8.1 KB

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