CSSRuleList.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSRuleList.h>
  7. #include <LibWeb/DOM/ExceptionOr.h>
  8. namespace Web::CSS {
  9. CSSRuleList::CSSRuleList(NonnullRefPtrVector<CSSRule>&& rules)
  10. : m_rules(move(rules))
  11. {
  12. }
  13. CSSRuleList::~CSSRuleList()
  14. {
  15. }
  16. bool CSSRuleList::is_supported_property_index(u32 index) const
  17. {
  18. // 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.
  19. // If there are no such CSSRule objects, then there are no supported property indices.
  20. return index < m_rules.size();
  21. }
  22. // https://drafts.csswg.org/cssom/#insert-a-css-rule
  23. DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule> rule, u32 index)
  24. {
  25. // 1. Set length to the number of items in list.
  26. auto length = m_rules.size();
  27. // 2. If index is greater than length, then throw an IndexSizeError exception.
  28. if (index > length)
  29. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  30. // NOTE: These steps don't apply since we're receiving a parsed rule.
  31. // 3. Set new rule to the results of performing parse a CSS rule on argument rule.
  32. // 4. If new rule is a syntax error, throw a SyntaxError exception.
  33. // 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]
  34. // 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.
  35. // 7. Insert new rule into list at the zero-indexed position index.
  36. m_rules.insert(index, move(rule));
  37. // 8. Return index.
  38. return index;
  39. }
  40. // https://drafts.csswg.org/cssom/#remove-a-css-rule
  41. DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
  42. {
  43. // 1. Set length to the number of items in list.
  44. auto length = m_rules.size();
  45. // 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
  46. if (index >= length)
  47. return DOM::IndexSizeError::create("CSS rule index out of bounds.");
  48. // 3. Set old rule to the indexth item in list.
  49. auto& old_rule = m_rules[index];
  50. // 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.
  51. (void)old_rule;
  52. // 5. Remove rule old rule from list at the zero-indexed position index.
  53. m_rules.remove(index);
  54. // FIXME: 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
  55. return {};
  56. }
  57. }