From 1d825f17c045e0d54e49bf0e431d1123675614fc Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 24 Feb 2024 07:46:59 +0000 Subject: [PATCH] LibWeb: Remove first rule if no argument is given for `remove_rule()` While this isn't explicitly mentioned in the specification, there is a WPT test that checks for this behavior. --- .../expected/css/CSSStyleSheet-removeRule.txt | 12 ++++++ .../input/css/CSSStyleSheet-removeRule.html | 41 +++++++++++++++++++ .../Libraries/LibWeb/CSS/CSSStyleSheet.cpp | 4 +- Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h | 2 +- .../Libraries/LibWeb/CSS/CSSStyleSheet.idl | 2 +- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/css/CSSStyleSheet-removeRule.txt create mode 100644 Tests/LibWeb/Text/input/css/CSSStyleSheet-removeRule.html diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleSheet-removeRule.txt b/Tests/LibWeb/Text/expected/css/CSSStyleSheet-removeRule.txt new file mode 100644 index 0000000000..2a83cb2bf5 --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/CSSStyleSheet-removeRule.txt @@ -0,0 +1,12 @@ +Exception thrown when removeRule() called on empty sheet: IndexSizeError +Rule count after adding 3 rules: 3 +Rule text: .test { padding: 10px; } +Rule text: .test { margin: 5px; } +Rule text: .test { font-size: 14px; } +Rule count after calling removeRule with no arguments: 2 +Rule text: .test { margin: 5px; } +Rule text: .test { font-size: 14px; } +Rule count after calling removeRule with explicit index: 1 +Rule text: .test { margin: 5px; } +Exception thrown when given a negative index: IndexSizeError +Exception thrown when index out of range: IndexSizeError diff --git a/Tests/LibWeb/Text/input/css/CSSStyleSheet-removeRule.html b/Tests/LibWeb/Text/input/css/CSSStyleSheet-removeRule.html new file mode 100644 index 0000000000..83dbea49a4 --- /dev/null +++ b/Tests/LibWeb/Text/input/css/CSSStyleSheet-removeRule.html @@ -0,0 +1,41 @@ + + + diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index 2f4a5eaff7..0a49ab9bbc 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -286,10 +286,10 @@ WebIDL::ExceptionOr CSSStyleSheet::add_rule(Optional selec } // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule -WebIDL::ExceptionOr CSSStyleSheet::remove_rule(unsigned index) +WebIDL::ExceptionOr CSSStyleSheet::remove_rule(Optional index) { // The removeRule(index) method must run the same steps as deleteRule(). - return delete_rule(index); + return delete_rule(index.value_or(0)); } void CSSStyleSheet::for_each_effective_style_rule(Function const& callback) const diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index 0621f9a535..9f02ff8da5 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -51,7 +51,7 @@ public: WebIDL::ExceptionOr insert_rule(StringView rule, unsigned index); WebIDL::ExceptionOr add_rule(Optional selector, Optional style, Optional index); - WebIDL::ExceptionOr remove_rule(unsigned index); + WebIDL::ExceptionOr remove_rule(Optional index); WebIDL::ExceptionOr delete_rule(unsigned index); JS::NonnullGCPtr replace(String text); diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl index 8dcce0584f..c7f223b7b9 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl @@ -19,7 +19,7 @@ interface CSSStyleSheet : StyleSheet { // https://drafts.csswg.org/cssom/#legacy-css-style-sheet-members [SameObject, ImplementedAs=css_rules] readonly attribute CSSRuleList rules; long addRule(optional DOMString selector = "undefined", optional DOMString style = "undefined", optional unsigned long index); - undefined removeRule(unsigned long index); + undefined removeRule(optional unsigned long index); }; dictionary CSSStyleSheetInit {