Serenity Operating System
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
9#pragma once
10
11#include <AK/Function.h>
12#include <AK/Iterator.h>
13#include <AK/RefPtr.h>
14#include <LibWeb/Bindings/LegacyPlatformObject.h>
15#include <LibWeb/CSS/CSSRule.h>
16#include <LibWeb/Forward.h>
17#include <LibWeb/WebIDL/ExceptionOr.h>
18
19namespace Web::CSS {
20
21// https://www.w3.org/TR/cssom/#the-cssrulelist-interface
22class CSSRuleList : public Bindings::LegacyPlatformObject {
23 WEB_PLATFORM_OBJECT(CSSRuleList, Bindings::LegacyPlatformObject);
24
25public:
26 static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSRuleList>> create(JS::Realm&, JS::MarkedVector<CSSRule*> const&);
27 static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSRuleList>> create_empty(JS::Realm&);
28
29 ~CSSRuleList() = default;
30
31 CSSRule const* item(size_t index) const
32 {
33 if (index >= length())
34 return nullptr;
35 return &m_rules[index];
36 }
37
38 CSSRule* item(size_t index)
39 {
40 if (index >= length())
41 return nullptr;
42 return &m_rules[index];
43 }
44
45 size_t length() const { return m_rules.size(); }
46
47 using ConstIterator = AK::SimpleIterator<Vector<CSSRule&> const, CSSRule const>;
48 using Iterator = AK::SimpleIterator<Vector<CSSRule&>, CSSRule>;
49
50 ConstIterator const begin() const { return m_rules.begin(); }
51 Iterator begin() { return m_rules.begin(); }
52
53 ConstIterator const end() const { return m_rules.end(); }
54 Iterator end() { return m_rules.end(); }
55
56 virtual bool is_supported_property_index(u32 index) const override;
57 virtual WebIDL::ExceptionOr<JS::Value> item_value(size_t index) const override;
58
59 WebIDL::ExceptionOr<void> remove_a_css_rule(u32 index);
60 WebIDL::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, CSSRule*>, u32 index);
61
62 void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
63 // Returns whether the match state of any media queries changed after evaluation.
64 bool evaluate_media_queries(HTML::Window const&);
65
66private:
67 explicit CSSRuleList(JS::Realm&);
68
69 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
70 virtual void visit_edges(Cell::Visitor&) override;
71
72 // ^Bindings::LegacyPlatformObject
73 virtual bool supports_indexed_properties() const override { return true; }
74 virtual bool supports_named_properties() const override { return false; }
75 virtual bool has_indexed_property_setter() const override { return false; }
76 virtual bool has_named_property_setter() const override { return false; }
77 virtual bool has_named_property_deleter() const override { return false; }
78 virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return false; }
79 virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
80 virtual bool has_global_interface_extended_attribute() const override { return false; }
81 virtual bool indexed_property_setter_has_identifier() const override { return false; }
82 virtual bool named_property_setter_has_identifier() const override { return false; }
83 virtual bool named_property_deleter_has_identifier() const override { return false; }
84
85 Vector<CSSRule&> m_rules;
86};
87
88}