diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-04-23 11:29:31 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-05-11 20:16:10 +0200 |
commit | 6e6607a92f3e00a1e349556f6a211507d26774ab (patch) | |
tree | 58219bab4ebb6e009b38b462a6392aa556b46982 /Userland | |
parent | dfba0cb2d9a1319e37cb0575ec32fbe179ddb741 (diff) | |
download | serenity-6e6607a92f3e00a1e349556f6a211507d26774ab.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSRuleList.h | 5 |
2 files changed, 19 insertions, 7 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp b/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp index dbd55a1fdd..1a04ef0311 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp @@ -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; diff --git a/Userland/Libraries/LibWeb/CSS/CSSRuleList.h b/Userland/Libraries/LibWeb/CSS/CSSRuleList.h index 65d8e86693..9dd88099e5 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRuleList.h +++ b/Userland/Libraries/LibWeb/CSS/CSSRuleList.h @@ -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. |