CSSRuleList.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(HTML::Window&, JS::MarkedVector<CSSRule*> const&);
  22. static CSSRuleList* create_empty(HTML::Window&);
  23. explicit CSSRuleList(HTML::Window&);
  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. using ConstIterator = AK::SimpleIterator<Vector<CSSRule&> const, CSSRule const>;
  39. using Iterator = AK::SimpleIterator<Vector<CSSRule&>, CSSRule>;
  40. ConstIterator const begin() const { return m_rules.begin(); }
  41. Iterator begin() { return m_rules.begin(); }
  42. ConstIterator const end() const { return m_rules.end(); }
  43. Iterator end() { return m_rules.end(); }
  44. virtual bool is_supported_property_index(u32 index) const override;
  45. virtual JS::Value item_value(size_t index) const override;
  46. WebIDL::ExceptionOr<void> remove_a_css_rule(u32 index);
  47. WebIDL::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, CSSRule*>, u32 index);
  48. void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
  49. // Returns whether the match state of any media queries changed after evaluation.
  50. bool evaluate_media_queries(HTML::Window const&);
  51. private:
  52. virtual void visit_edges(Cell::Visitor&) override;
  53. Vector<CSSRule&> m_rules;
  54. };
  55. }