Forráskód Böngészése

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.
Tim Ledbetter 1 éve
szülő
commit
1d825f17c0

+ 12 - 0
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

+ 41 - 0
Tests/LibWeb/Text/input/css/CSSStyleSheet-removeRule.html

@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+    test(() => {
+        const sheet = new CSSStyleSheet();
+        try {
+            sheet.removeRule();
+            println("FAIL");
+        } catch (e) {
+            println(`Exception thrown when removeRule() called on empty sheet: ${e.name}`);
+        }
+
+        sheet.addRule(".test", "padding: 10px");
+        sheet.addRule(".test", "margin: 5px");
+        sheet.addRule(".test", "font-size: 14px");
+        println(`Rule count after adding 3 rules: ${sheet.cssRules.length}`);
+        for (const rule of sheet.cssRules) {
+            println(`Rule text: ${rule.cssText}`);
+        }
+        sheet.removeRule();
+        println(`Rule count after calling removeRule with no arguments: ${sheet.cssRules.length}`);
+        for (const rule of sheet.cssRules) {
+            println(`Rule text: ${rule.cssText}`);
+        }
+        sheet.removeRule(1);
+        println(`Rule count after calling removeRule with explicit index: ${sheet.cssRules.length}`);
+        println(`Rule text: ${sheet.cssRules[0].cssText}`);
+        try {
+            sheet.removeRule(-1);
+            println("FAIL");
+        } catch (e) {
+            println(`Exception thrown when given a negative index: ${e.name}`);
+        }
+        try {
+            sheet.removeRule(1);
+            println("FAIL");
+        } catch (e) {
+            println(`Exception thrown when index out of range: ${e.name}`);
+        }
+    });
+</script>

+ 2 - 2
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp

@@ -286,10 +286,10 @@ WebIDL::ExceptionOr<WebIDL::Long> CSSStyleSheet::add_rule(Optional<String> selec
 }
 
 // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
-WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
+WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(Optional<WebIDL::UnsignedLong> 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<void(CSSStyleRule const&)> const& callback) const

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h

@@ -51,7 +51,7 @@ public:
 
     WebIDL::ExceptionOr<unsigned> insert_rule(StringView rule, unsigned index);
     WebIDL::ExceptionOr<WebIDL::Long> add_rule(Optional<String> selector, Optional<String> style, Optional<WebIDL::UnsignedLong> index);
-    WebIDL::ExceptionOr<void> remove_rule(unsigned index);
+    WebIDL::ExceptionOr<void> remove_rule(Optional<WebIDL::UnsignedLong> index);
     WebIDL::ExceptionOr<void> delete_rule(unsigned index);
 
     JS::NonnullGCPtr<JS::Promise> replace(String text);

+ 1 - 1
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 {