2021-09-28 12:20:32 -03:00
|
|
|
/*
|
2024-09-03 07:43:20 -03:00
|
|
|
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2021-09-28 12:20:32 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2026-04-18 05:54:06 -03:00
|
|
|
#include <LibWeb/Bindings/CSSGroupingRule.h>
|
2022-09-24 19:34:04 -03:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-08-07 08:51:40 -03:00
|
|
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
2021-09-28 12:20:32 -03:00
|
|
|
#include <LibWeb/CSS/CSSGroupingRule.h>
|
|
|
|
|
#include <LibWeb/CSS/CSSRuleList.h>
|
2026-02-11 03:33:58 -03:00
|
|
|
#include <LibWeb/CSS/CSSStyleSheet.h>
|
2022-08-28 08:42:07 -03:00
|
|
|
#include <LibWeb/HTML/Window.h>
|
2021-09-28 12:20:32 -03:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2024-10-28 16:16:28 -03:00
|
|
|
CSSGroupingRule::CSSGroupingRule(JS::Realm& realm, CSSRuleList& rules, Type type)
|
|
|
|
|
: CSSRule(realm, type)
|
2022-08-07 10:46:44 -03:00
|
|
|
, m_rules(rules)
|
2021-09-28 12:20:32 -03:00
|
|
|
{
|
2025-04-14 13:27:00 -03:00
|
|
|
m_rules->set_owner_rule(*this);
|
2023-02-26 20:09:02 -03:00
|
|
|
for (auto& rule : *m_rules)
|
|
|
|
|
rule->set_parent_rule(this);
|
2021-09-28 12:20:32 -03:00
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void CSSGroupingRule::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
{
|
2024-03-16 09:13:08 -03:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSGroupingRule);
|
2025-04-20 11:22:57 -03:00
|
|
|
Base::initialize(realm);
|
2023-01-10 08:28:20 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-07 10:46:44 -03:00
|
|
|
void CSSGroupingRule::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-02-26 20:09:02 -03:00
|
|
|
visitor.visit(m_rules);
|
2022-08-07 10:46:44 -03:00
|
|
|
}
|
|
|
|
|
|
2024-11-04 14:13:51 -03:00
|
|
|
void CSSGroupingRule::clear_caches()
|
|
|
|
|
{
|
|
|
|
|
Base::clear_caches();
|
|
|
|
|
for (auto& rule : *m_rules)
|
|
|
|
|
rule->clear_caches();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 11:36:42 -03:00
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssgroupingrule-insertrule
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<u32> CSSGroupingRule::insert_rule(StringView rule, u32 index)
|
2021-09-28 12:20:32 -03:00
|
|
|
{
|
2025-04-14 11:36:42 -03:00
|
|
|
// The insertRule(rule, index) method must return the result of invoking insert a CSS rule rule into the child CSS
|
|
|
|
|
// rules at index, with the nested flag set.
|
2026-05-22 09:59:55 -03:00
|
|
|
HashTable<FlyString> declared_namespaces;
|
|
|
|
|
if (auto* sheet = parent_style_sheet())
|
|
|
|
|
declared_namespaces = sheet->declared_namespaces();
|
|
|
|
|
TRY(m_rules->insert_a_css_rule(rule, index, CSSRuleList::Nested::Yes, declared_namespaces));
|
2025-04-14 11:36:42 -03:00
|
|
|
|
|
|
|
|
// AD-HOC: The spec doesn't say where to set the parent rule, so we'll do it here.
|
2023-02-26 20:09:02 -03:00
|
|
|
m_rules->item(index)->set_parent_rule(this);
|
2026-05-22 09:59:55 -03:00
|
|
|
if (auto* sheet = parent_style_sheet())
|
|
|
|
|
sheet->invalidate_owners(DOM::StyleInvalidationReason::StyleSheetInsertRule);
|
2022-04-22 15:56:22 -03:00
|
|
|
return index;
|
2021-09-28 12:20:32 -03:00
|
|
|
}
|
|
|
|
|
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<void> CSSGroupingRule::delete_rule(u32 index)
|
2021-09-28 12:20:32 -03:00
|
|
|
{
|
2026-05-21 09:46:57 -03:00
|
|
|
TRY(m_rules->remove_a_css_rule(index));
|
2026-05-22 09:59:55 -03:00
|
|
|
if (auto* sheet = parent_style_sheet())
|
|
|
|
|
sheet->invalidate_owners(DOM::StyleInvalidationReason::StyleSheetDeleteRule);
|
2026-05-21 09:46:57 -03:00
|
|
|
return {};
|
2021-09-28 12:20:32 -03:00
|
|
|
}
|
|
|
|
|
|
2024-09-03 07:43:20 -03:00
|
|
|
void CSSGroupingRule::for_each_effective_rule(TraversalOrder order, Function<void(Web::CSS::CSSRule const&)> const& callback) const
|
2021-10-08 13:02:47 -03:00
|
|
|
{
|
2024-09-03 07:43:20 -03:00
|
|
|
m_rules->for_each_effective_rule(order, callback);
|
2023-05-26 17:00:54 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 15:56:22 -03:00
|
|
|
void CSSGroupingRule::set_parent_style_sheet(CSSStyleSheet* parent_style_sheet)
|
|
|
|
|
{
|
|
|
|
|
CSSRule::set_parent_style_sheet(parent_style_sheet);
|
2023-02-26 20:09:02 -03:00
|
|
|
for (auto& rule : *m_rules)
|
|
|
|
|
rule->set_parent_style_sheet(parent_style_sheet);
|
2022-04-22 15:56:22 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 12:20:32 -03:00
|
|
|
}
|