CSSRuleList.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/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 WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSRuleList>> create(JS::Realm&, JS::MarkedVector<CSSRule*> const&);
  22. static WebIDL::ExceptionOr<JS::NonnullGCPtr<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. auto begin() const { return m_rules.begin(); }
  38. auto begin() { return m_rules.begin(); }
  39. auto end() const { return m_rules.end(); }
  40. auto end() { return m_rules.end(); }
  41. virtual bool is_supported_property_index(u32 index) const override;
  42. virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
  43. WebIDL::ExceptionOr<void> remove_a_css_rule(u32 index);
  44. WebIDL::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, CSSRule*>, u32 index);
  45. void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
  46. // Returns whether the match state of any media queries changed after evaluation.
  47. bool evaluate_media_queries(HTML::Window const&);
  48. private:
  49. explicit CSSRuleList(JS::Realm&);
  50. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  51. virtual void visit_edges(Cell::Visitor&) override;
  52. // ^Bindings::LegacyPlatformObject
  53. virtual bool supports_indexed_properties() const override { return true; }
  54. virtual bool supports_named_properties() const override { return false; }
  55. virtual bool has_indexed_property_setter() const override { return false; }
  56. virtual bool has_named_property_setter() const override { return false; }
  57. virtual bool has_named_property_deleter() const override { return false; }
  58. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
  59. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
  60. virtual bool has_global_interface_extended_attribute() const override { return false; }
  61. virtual bool indexed_property_setter_has_identifier() const override { return false; }
  62. virtual bool named_property_setter_has_identifier() const override { return false; }
  63. virtual bool named_property_deleter_has_identifier() const override { return false; }
  64. Vector<JS::NonnullGCPtr<CSSRule>> m_rules;
  65. };
  66. }