CSSRuleList.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibWeb/CSS/CSSImportRule.h>
  8. #include <LibWeb/CSS/CSSMediaRule.h>
  9. #include <LibWeb/CSS/CSSRuleList.h>
  10. #include <LibWeb/DOM/ExceptionOr.h>
  11. namespace Web::CSS {
  12. CSSRuleList::CSSRuleList(NonnullRefPtrVector<CSSRule>&& rules)
  13. : m_rules(move(rules))
  14. {
  15. }
  16. CSSRuleList::~CSSRuleList()
  17. {
  18. }
  19. bool CSSRuleList::is_supported_property_index(u32 index) const
  20. {
  21. // 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.
  22. // If there are no such CSSRule objects, then there are no supported property indices.
  23. return index < m_rules.size();
  24. }
  25. // https://drafts.csswg.org/cssom/#insert-a-css-rule
  26. DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule> rule, u32 index)
  27. {
  28. // 1. Set length to the number of items in list.
  29. auto length = m_rules.size();
  30. // 2. If index is greater than length, then throw an IndexSizeError exception.
  31. if (index > length)
  32. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  33. // NOTE: These steps don't apply since we're receiving a parsed rule.
  34. // 3. Set new rule to the results of performing parse a CSS rule on argument rule.
  35. // 4. If new rule is a syntax error, throw a SyntaxError exception.
  36. // 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]
  37. // 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.
  38. // 7. Insert new rule into list at the zero-indexed position index.
  39. m_rules.insert(index, move(rule));
  40. // 8. Return index.
  41. return index;
  42. }
  43. // https://drafts.csswg.org/cssom/#remove-a-css-rule
  44. DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
  45. {
  46. // 1. Set length to the number of items in list.
  47. auto length = m_rules.size();
  48. // 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
  49. if (index >= length)
  50. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  51. // 3. Set old rule to the indexth item in list.
  52. auto& old_rule = m_rules[index];
  53. // 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.
  54. (void)old_rule;
  55. // 5. Remove rule old rule from list at the zero-indexed position index.
  56. m_rules.remove(index);
  57. // FIXME: 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
  58. return {};
  59. }
  60. void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  61. {
  62. for (auto& rule : m_rules) {
  63. switch (rule.type()) {
  64. case CSSRule::Type::Style:
  65. callback(verify_cast<CSSStyleRule>(rule));
  66. break;
  67. case CSSRule::Type::Import: {
  68. auto const& import_rule = verify_cast<CSSImportRule>(rule);
  69. if (import_rule.has_import_result())
  70. import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
  71. break;
  72. }
  73. case CSSRule::Type::Media:
  74. verify_cast<CSSMediaRule>(rule).for_each_effective_style_rule(callback);
  75. break;
  76. case CSSRule::Type::__Count:
  77. VERIFY_NOT_REACHED();
  78. }
  79. }
  80. }
  81. bool CSSRuleList::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
  82. {
  83. for (auto& rule : m_rules) {
  84. if (rule.type() == CSSRule::Type::Import) {
  85. auto& import_rule = verify_cast<CSSImportRule>(rule);
  86. if (!import_rule.has_import_result()) {
  87. callback(import_rule);
  88. return true;
  89. }
  90. if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
  91. return true;
  92. }
  93. } else if (rule.type() == CSSRule::Type::Media) {
  94. return verify_cast<CSSMediaRule>(rule).for_first_not_loaded_import_rule(callback);
  95. }
  96. }
  97. return false;
  98. }
  99. }