2021-09-28 15:17:11 +01:00
|
|
|
/*
|
2025-04-14 15:36:42 +01:00
|
|
|
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2023-02-28 00:05:39 +00:00
|
|
|
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
2021-09-28 15:17:11 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-10-08 16:40:50 +01:00
|
|
|
#include <AK/Function.h>
|
2024-01-09 16:05:03 -07:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2021-09-28 15:17:11 +01:00
|
|
|
#include <LibWeb/CSS/CSSRule.h>
|
2025-04-14 17:27:00 +01:00
|
|
|
#include <LibWeb/CSS/Parser/RuleContext.h>
|
2025-07-19 19:35:33 -07:00
|
|
|
#include <LibWeb/Export.h>
|
2021-09-28 15:17:11 +01:00
|
|
|
#include <LibWeb/Forward.h>
|
2024-09-03 11:43:20 +01:00
|
|
|
#include <LibWeb/TraversalOrder.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-09-28 15:17:11 +01:00
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
2021-10-15 19:38:39 +01:00
|
|
|
// https://www.w3.org/TR/cssom/#the-cssrulelist-interface
|
2025-07-19 19:35:33 -07:00
|
|
|
class WEB_API CSSRuleList : public Bindings::PlatformObject {
|
2024-01-09 16:05:03 -07:00
|
|
|
WEB_PLATFORM_OBJECT(CSSRuleList, Bindings::PlatformObject);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(CSSRuleList);
|
2022-08-07 13:51:40 +02:00
|
|
|
|
2021-09-28 15:17:11 +01:00
|
|
|
public:
|
2025-04-14 16:02:29 +01:00
|
|
|
[[nodiscard]] static GC::Ref<CSSRuleList> create(JS::Realm&, ReadonlySpan<GC::Ref<CSSRule>> = {});
|
2021-09-29 19:41:46 +02:00
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
~CSSRuleList() = default;
|
2021-09-28 15:17:11 +01:00
|
|
|
|
2022-08-07 15:46:44 +02:00
|
|
|
CSSRule const* item(size_t index) const
|
|
|
|
{
|
|
|
|
if (index >= length())
|
|
|
|
return nullptr;
|
2023-02-26 16:09:02 -07:00
|
|
|
return m_rules[index];
|
2022-08-07 15:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSSRule* item(size_t index)
|
2021-09-28 15:17:11 +01:00
|
|
|
{
|
|
|
|
if (index >= length())
|
|
|
|
return nullptr;
|
2023-02-26 16:09:02 -07:00
|
|
|
return m_rules[index];
|
2021-09-28 15:17:11 +01:00
|
|
|
}
|
2022-08-07 15:46:44 +02:00
|
|
|
|
2021-09-28 15:17:11 +01:00
|
|
|
size_t length() const { return m_rules.size(); }
|
|
|
|
|
2023-02-26 16:09:02 -07:00
|
|
|
auto begin() const { return m_rules.begin(); }
|
|
|
|
auto begin() { return m_rules.begin(); }
|
2021-09-28 15:17:11 +01:00
|
|
|
|
2023-02-26 16:09:02 -07:00
|
|
|
auto end() const { return m_rules.end(); }
|
|
|
|
auto end() { return m_rules.end(); }
|
2021-09-28 15:17:11 +01:00
|
|
|
|
2024-07-25 18:15:51 +12:00
|
|
|
virtual Optional<JS::Value> item_value(size_t index) const override;
|
2021-09-29 19:41:46 +02:00
|
|
|
|
2022-09-25 17:03:42 +01:00
|
|
|
WebIDL::ExceptionOr<void> remove_a_css_rule(u32 index);
|
2025-04-14 15:36:42 +01:00
|
|
|
enum class Nested {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
2025-06-23 22:40:37 +12:00
|
|
|
WebIDL::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, CSSRule*>, u32 index, Nested, HashTable<FlyString> const& declared_namespaces);
|
2021-09-29 20:28:32 +02:00
|
|
|
|
2024-09-03 11:43:20 +01:00
|
|
|
void for_each_effective_rule(TraversalOrder, Function<void(CSSRule const&)> const& callback) const;
|
2022-02-17 12:34:36 +00:00
|
|
|
// Returns whether the match state of any media queries changed after evaluation.
|
2025-10-07 00:54:19 +13:00
|
|
|
bool evaluate_media_queries(DOM::Document const&);
|
2021-10-08 16:40:50 +01:00
|
|
|
|
2025-04-14 17:27:00 +01:00
|
|
|
void set_owner_rule(GC::Ref<CSSRule> owner_rule) { m_owner_rule = owner_rule; }
|
2024-11-15 04:01:23 +13:00
|
|
|
void set_rules(Badge<CSSStyleSheet>, Vector<GC::Ref<CSSRule>> rules) { m_rules = move(rules); }
|
2024-02-24 07:46:59 +00:00
|
|
|
|
2023-07-31 19:48:50 +01:00
|
|
|
Function<void()> on_change;
|
|
|
|
|
2021-09-28 15:17:11 +01:00
|
|
|
private:
|
2022-09-24 16:34:04 -06:00
|
|
|
explicit CSSRuleList(JS::Realm&);
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2022-08-07 15:46:44 +02:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2025-04-14 17:27:00 +01:00
|
|
|
Vector<Parser::RuleContext> rule_context() const;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
Vector<GC::Ref<CSSRule>> m_rules;
|
2025-04-14 17:27:00 +01:00
|
|
|
GC::Ptr<CSSRule> m_owner_rule;
|
2021-09-28 15:17:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|