diff --git a/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp b/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp index d33eb145d4..75e03e388c 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp @@ -5,6 +5,7 @@ */ #include +#include 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 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 {}; +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSRuleList.h b/Userland/Libraries/LibWeb/CSS/CSSRuleList.h index 697c5fdf66..4ea6430270 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRuleList.h +++ b/Userland/Libraries/LibWeb/CSS/CSSRuleList.h @@ -47,6 +47,8 @@ public: bool is_supported_property_index(u32 index) const; + DOM::ExceptionOr remove_a_css_rule(u32 index); + private: explicit CSSRuleList(NonnullRefPtrVector&&); diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index 0321d2fcd9..262b058d82 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -5,6 +5,7 @@ */ #include +#include namespace Web::CSS { @@ -17,4 +18,42 @@ CSSStyleSheet::~CSSStyleSheet() { } +// https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule +DOM::ExceptionOr 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 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 CSSStyleSheet::remove_rule(unsigned index) +{ + // The removeRule(index) method must run the same steps as deleteRule(). + return delete_rule(index); +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index 03d3a04bf0..9afaadce50 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/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 insert_rule(StringView rule, unsigned index); + DOM::ExceptionOr remove_rule(unsigned index); + DOM::ExceptionOr delete_rule(unsigned index); + template void for_each_effective_style_rule(Callback callback) const { diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl index 7f5d324248..fa973a9389 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl +++ b/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); };