CSSRuleList.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Function.h>
  10. #include <AK/Iterator.h>
  11. #include <AK/RefPtr.h>
  12. #include <LibWeb/Bindings/PlatformObject.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::PlatformObject {
  19. WEB_PLATFORM_OBJECT(CSSRuleList, Bindings::PlatformObject);
  20. JS_DECLARE_ALLOCATOR(CSSRuleList);
  21. public:
  22. [[nodiscard]] static JS::NonnullGCPtr<CSSRuleList> create(JS::Realm&, JS::MarkedVector<CSSRule*> const&);
  23. [[nodiscard]] static JS::NonnullGCPtr<CSSRuleList> create_empty(JS::Realm&);
  24. ~CSSRuleList() = default;
  25. CSSRule const* item(size_t index) const
  26. {
  27. if (index >= length())
  28. return nullptr;
  29. return m_rules[index];
  30. }
  31. CSSRule* item(size_t index)
  32. {
  33. if (index >= length())
  34. return nullptr;
  35. return m_rules[index];
  36. }
  37. size_t length() const { return m_rules.size(); }
  38. auto begin() const { return m_rules.begin(); }
  39. auto begin() { return m_rules.begin(); }
  40. auto end() const { return m_rules.end(); }
  41. auto end() { return m_rules.end(); }
  42. virtual bool is_supported_property_index(u32 index) const override;
  43. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  44. WebIDL::ExceptionOr<void> remove_a_css_rule(u32 index);
  45. WebIDL::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, CSSRule*>, u32 index);
  46. void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
  47. // Returns whether the match state of any media queries changed after evaluation.
  48. bool evaluate_media_queries(HTML::Window const&);
  49. void for_each_effective_keyframes_at_rule(Function<void(CSSKeyframesRule const&)> const& callback) const;
  50. void set_rules(Badge<CSSStyleSheet>, Vector<JS::NonnullGCPtr<CSSRule>> rules) { m_rules = move(rules); }
  51. Function<void()> on_change;
  52. private:
  53. explicit CSSRuleList(JS::Realm&);
  54. virtual void initialize(JS::Realm&) override;
  55. virtual void visit_edges(Cell::Visitor&) override;
  56. Vector<JS::NonnullGCPtr<CSSRule>> m_rules;
  57. };
  58. }