CSSRuleList.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <AK/Iterator.h>
  10. #include <AK/NonnullRefPtrVector.h>
  11. #include <AK/RefPtr.h>
  12. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  13. #include <LibWeb/CSS/CSSRule.h>
  14. #include <LibWeb/Forward.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::CSS {
  17. // https://www.w3.org/TR/cssom/#the-cssrulelist-interface
  18. class CSSRuleList : public Bindings::LegacyPlatformObject {
  19. WEB_PLATFORM_OBJECT(CSSRuleList, Bindings::LegacyPlatformObject);
  20. public:
  21. static CSSRuleList* create(JS::Realm&, JS::MarkedVector<CSSRule*> const&);
  22. static CSSRuleList* create_empty(JS::Realm&);
  23. ~CSSRuleList() = default;
  24. CSSRule const* item(size_t index) const
  25. {
  26. if (index >= length())
  27. return nullptr;
  28. return &m_rules[index];
  29. }
  30. CSSRule* item(size_t index)
  31. {
  32. if (index >= length())
  33. return nullptr;
  34. return &m_rules[index];
  35. }
  36. size_t length() const { return m_rules.size(); }
  37. using ConstIterator = AK::SimpleIterator<Vector<CSSRule&> const, CSSRule const>;
  38. using Iterator = AK::SimpleIterator<Vector<CSSRule&>, CSSRule>;
  39. ConstIterator const begin() const { return m_rules.begin(); }
  40. Iterator begin() { return m_rules.begin(); }
  41. ConstIterator const end() const { return m_rules.end(); }
  42. Iterator end() { return m_rules.end(); }
  43. virtual bool is_supported_property_index(u32 index) const override;
  44. virtual JS::Value item_value(size_t index) const override;
  45. WebIDL::ExceptionOr<void> remove_a_css_rule(u32 index);
  46. WebIDL::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, CSSRule*>, u32 index);
  47. void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
  48. // Returns whether the match state of any media queries changed after evaluation.
  49. bool evaluate_media_queries(HTML::Window const&);
  50. private:
  51. explicit CSSRuleList(JS::Realm&);
  52. virtual void visit_edges(Cell::Visitor&) override;
  53. Vector<CSSRule&> m_rules;
  54. };
  55. }