CSSRuleList.cpp 6.4 KB

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