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.
This commit is contained in:
parent
dfba0cb2d9
commit
6e6607a92f
Notes:
sideshowbarker
2024-07-18 04:38:32 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/6e6607a92f Pull-request: https://github.com/SerenityOS/serenity/pull/13774
2 changed files with 19 additions and 7 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -9,7 +9,7 @@
|
|||
#include <LibWeb/CSS/CSSMediaRule.h>
|
||||
#include <LibWeb/CSS/CSSRuleList.h>
|
||||
#include <LibWeb/CSS/CSSSupportsRule.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/CSS/Parser/Parser.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
|
@ -26,7 +26,7 @@ bool CSSRuleList::is_supported_property_index(u32 index) const
|
|||
}
|
||||
|
||||
// https://www.w3.org/TR/cssom/#insert-a-css-rule
|
||||
DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule> rule, u32 index)
|
||||
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();
|
||||
|
@ -35,16 +35,27 @@ DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule>
|
|||
if (index > length)
|
||||
return DOM::IndexSizeError::create("CSS rule index out of bounds.");
|
||||
|
||||
// NOTE: These steps don't apply since we're receiving a parsed rule.
|
||||
// 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, move(rule));
|
||||
m_rules.insert(index, new_rule.release_nonnull());
|
||||
|
||||
// 8. Return index.
|
||||
return index;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -12,6 +12,7 @@
|
|||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <LibWeb/CSS/CSSRule.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
@ -49,7 +50,7 @@ public:
|
|||
bool is_supported_property_index(u32 index) const;
|
||||
|
||||
DOM::ExceptionOr<void> remove_a_css_rule(u32 index);
|
||||
DOM::ExceptionOr<unsigned> insert_a_css_rule(NonnullRefPtr<CSSRule>, u32 index);
|
||||
DOM::ExceptionOr<unsigned> insert_a_css_rule(Variant<StringView, NonnullRefPtr<CSSRule>>, u32 index);
|
||||
|
||||
void for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const;
|
||||
// Returns whether the match state of any media queries changed after evaluation.
|
||||
|
|
Loading…
Add table
Reference in a new issue