CSSRuleList.cpp 6.8 KB

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