From 6e6607a92f3e00a1e349556f6a211507d26774ab Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 23 Apr 2022 11:29:31 +0100 Subject: 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. --- Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp | 21 ++++++++++++++++----- Userland/Libraries/LibWeb/CSS/CSSRuleList.h | 5 +++-- 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'Userland') 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 + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,7 +9,7 @@ #include #include #include -#include +#include 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 CSSRuleList::insert_a_css_rule(NonnullRefPtr rule, u32 index) +DOM::ExceptionOr CSSRuleList::insert_a_css_rule(Variant> rule, u32 index) { // 1. Set length to the number of items in list. auto length = m_rules.size(); @@ -35,16 +35,27 @@ DOM::ExceptionOr CSSRuleList::insert_a_css_rule(NonnullRefPtr 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 new_rule; + if (rule.has()) { + new_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule.get()); + } else { + new_rule = rule.get>(); + } + // 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 + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace Web::CSS { @@ -49,7 +50,7 @@ public: bool is_supported_property_index(u32 index) const; DOM::ExceptionOr remove_a_css_rule(u32 index); - DOM::ExceptionOr insert_a_css_rule(NonnullRefPtr, u32 index); + DOM::ExceptionOr insert_a_css_rule(Variant>, u32 index); void for_each_effective_style_rule(Function const& callback) const; // Returns whether the match state of any media queries changed after evaluation. -- cgit v1.2.3