ladybird/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp
Sam Atkins 6e6607a92f LibWeb: Allow passing StringView to CSSRuleList::insert_a_css_rule()
The spec is a little bizarre here. One caller of this
(`CSSStyleSheet::insert_rule()`) wants to give it a parsed CSSRule, but
the spec itself wants it to take a string. (As will be used by
`CSSGroupingRule::insert_rule()`) Using a Variant isn't pretty but it's
the best solution I've come to - having two overloads was worse, whether
one called the other or they just duplicated the logic. This seems the
least bad.
2022-05-11 20:16:10 +02:00

151 lines
5.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CSSRuleList.h>
#include <LibWeb/CSS/CSSSupportsRule.h>
#include <LibWeb/CSS/Parser/Parser.h>
namespace Web::CSS {
CSSRuleList::CSSRuleList(NonnullRefPtrVector<CSSRule>&& rules)
: m_rules(move(rules))
{
}
bool CSSRuleList::is_supported_property_index(u32 index) const
{
// The objects supported property indices are the numbers in the range zero to one less than the number of CSSRule objects represented by the collection.
// If there are no such CSSRule objects, then there are no supported property indices.
return index < m_rules.size();
}
// https://www.w3.org/TR/cssom/#insert-a-css-rule
DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView, NonnullRefPtr<CSSRule>> rule, u32 index)
{
// 1. Set length to the number of items in list.
auto length = m_rules.size();
// 2. If index is greater than length, then throw an IndexSizeError exception.
if (index > length)
return DOM::IndexSizeError::create("CSS rule index out of bounds.");
// 3. Set new rule to the results of performing parse a CSS rule on argument rule.
// NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
// spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
// if that variant holds a CSSRule already.
RefPtr<CSSRule> new_rule;
if (rule.has<StringView>()) {
new_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule.get<StringView>());
} else {
new_rule = rule.get<NonnullRefPtr<CSSRule>>();
}
// 4. If new rule is a syntax error, throw a SyntaxError exception.
if (!new_rule)
return DOM::SyntaxError::create("Unable to parse CSS rule.");
// FIXME: 5. If new rule cannot be inserted into list at the zero-index position index due to constraints specified by CSS, then throw a HierarchyRequestError exception. [CSS21]
// FIXME: 6. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
// 7. Insert new rule into list at the zero-indexed position index.
m_rules.insert(index, new_rule.release_nonnull());
// 8. Return index.
return index;
}
// https://www.w3.org/TR/cssom/#remove-a-css-rule
DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
{
// 1. Set length to the number of items in list.
auto length = m_rules.size();
// 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
if (index >= length)
return DOM::IndexSizeError::create("CSS rule index out of bounds.");
// 3. Set old rule to the indexth item in list.
auto& old_rule = m_rules[index];
// FIXME: 4. If old rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
(void)old_rule;
// 5. Remove rule old rule from list at the zero-indexed position index.
m_rules.remove(index);
// FIXME: 6. Set old rules parent CSS rule and parent CSS style sheet to null.
return {};
}
void CSSRuleList::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
{
for (auto const& rule : m_rules) {
switch (rule.type()) {
case CSSRule::Type::FontFace:
break;
case CSSRule::Type::Import: {
auto const& import_rule = static_cast<CSSImportRule const&>(rule);
if (import_rule.has_import_result())
import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
break;
}
case CSSRule::Type::Media:
static_cast<CSSMediaRule const&>(rule).for_each_effective_style_rule(callback);
break;
case CSSRule::Type::Style:
callback(static_cast<CSSStyleRule const&>(rule));
break;
case CSSRule::Type::Supports:
static_cast<CSSSupportsRule const&>(rule).for_each_effective_style_rule(callback);
break;
}
}
}
bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
{
bool any_media_queries_changed_match_state = false;
for (auto& rule : m_rules) {
switch (rule.type()) {
case CSSRule::Type::FontFace:
break;
case CSSRule::Type::Import: {
auto& import_rule = verify_cast<CSSImportRule>(rule);
if (import_rule.has_import_result() && import_rule.loaded_style_sheet()->evaluate_media_queries(window))
any_media_queries_changed_match_state = true;
break;
}
case CSSRule::Type::Media: {
auto& media_rule = verify_cast<CSSMediaRule>(rule);
bool did_match = media_rule.condition_matches();
bool now_matches = media_rule.evaluate(window);
if (did_match != now_matches)
any_media_queries_changed_match_state = true;
if (now_matches && media_rule.css_rules().evaluate_media_queries(window))
any_media_queries_changed_match_state = true;
break;
}
case CSSRule::Type::Style:
break;
case CSSRule::Type::Supports: {
auto& supports_rule = verify_cast<CSSSupportsRule>(rule);
if (supports_rule.condition_matches() && supports_rule.css_rules().evaluate_media_queries(window))
any_media_queries_changed_match_state = true;
break;
}
}
}
return any_media_queries_changed_match_state;
}
}