Bläddra i källkod

LibWeb: Add CSSStyleSheet.{insert,delete,remove}Rule() APIs

Note that insertRule() is really just a big TODO right now.
Andreas Kling 3 år sedan
förälder
incheckning
30d710a0a2

+ 25 - 0
Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp

@@ -5,6 +5,7 @@
  */
 
 #include <LibWeb/CSS/CSSRuleList.h>
+#include <LibWeb/DOM/ExceptionOr.h>
 
 namespace Web::CSS {
 
@@ -24,4 +25,28 @@ bool CSSRuleList::is_supported_property_index(u32 index) const
     return index < m_rules.size();
 }
 
+// https://drafts.csswg.org/cssom/#remove-a-css-rule
+DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
+{
+    // 1. Set length to the number of items in list.
+    auto length = m_rules.size();
+
+    // 2. If index is greater than or equal to length, then throw an IndexSizeError exception.
+    if (index >= length)
+        return DOM::IndexSizeError::create("CSS rule index out of bounds.");
+
+    // 3. Set old rule to the indexth item in list.
+    auto& old_rule = m_rules[index];
+
+    // FIXME: 4. If old rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
+    (void)old_rule;
+
+    // 5. Remove rule old rule from list at the zero-indexed position index.
+    m_rules.remove(index);
+
+    // FIXME: 6. Set old rule’s parent CSS rule and parent CSS style sheet to null.
+
+    return {};
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/CSS/CSSRuleList.h

@@ -47,6 +47,8 @@ public:
 
     bool is_supported_property_index(u32 index) const;
 
+    DOM::ExceptionOr<void> remove_a_css_rule(u32 index);
+
 private:
     explicit CSSRuleList(NonnullRefPtrVector<CSSRule>&&);
 

+ 39 - 0
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp

@@ -5,6 +5,7 @@
  */
 
 #include <LibWeb/CSS/CSSStyleSheet.h>
+#include <LibWeb/DOM/ExceptionOr.h>
 
 namespace Web::CSS {
 
@@ -17,4 +18,42 @@ CSSStyleSheet::~CSSStyleSheet()
 {
 }
 
+// https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule
+DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
+{
+    // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
+
+    // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
+
+    // Let parsed rule be the return value of invoking parse a rule with rule.
+
+    // If parsed rule is a syntax error, return parsed rule.
+
+    // If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
+
+    // Return the result of invoking insert a CSS rule rule in the CSS rules at index.
+
+    (void)index;
+    (void)rule;
+    TODO();
+}
+
+// https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule
+DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
+{
+    // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
+
+    // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
+
+    // 3. Remove a CSS rule in the CSS rules at index.
+    return m_rules->remove_a_css_rule(index);
+}
+
+// https://drafts.csswg.org/cssom/#dom-cssstylesheet-removerule
+DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
+{
+    // The removeRule(index) method must run the same steps as deleteRule().
+    return delete_rule(index);
+}
+
 }

+ 4 - 0
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h

@@ -36,6 +36,10 @@ public:
     CSSRuleList* css_rules() { return m_rules; }
     CSSRuleList const* css_rules() const { return m_rules; }
 
+    DOM::ExceptionOr<unsigned> insert_rule(StringView rule, unsigned index);
+    DOM::ExceptionOr<void> remove_rule(unsigned index);
+    DOM::ExceptionOr<void> delete_rule(unsigned index);
+
     template<typename Callback>
     void for_each_effective_style_rule(Callback callback) const
     {

+ 3 - 2
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl

@@ -1,6 +1,7 @@
 interface CSSStyleSheet : StyleSheet {
     // readonly attribute CSSRule? ownerRule;
     [SameObject] readonly attribute CSSRuleList cssRules;
-    // unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
-    // undefined deleteRule(unsigned long index);
+    unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0);
+    undefined deleteRule(unsigned long index);
+    undefined removeRule(unsigned long index);
 };