diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-29 20:28:32 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-29 21:21:57 +0200 |
commit | 30d710a0a230fd6f3473a9667ac08c46a84192d7 (patch) | |
tree | 4d0341f01b195dda311900f01fe1438dfc2a04e5 /Userland/Libraries | |
parent | 3a4565beec10f2898844884c19dc3aacf1922d37 (diff) | |
download | serenity-30d710a0a230fd6f3473a9667ac08c46a84192d7.zip |
LibWeb: Add CSSStyleSheet.{insert,delete,remove}Rule() APIs
Note that insertRule() is really just a big TODO right now.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp | 25 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSRuleList.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp | 39 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl | 5 |
5 files changed, 73 insertions, 2 deletions
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 <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 {}; +} + } 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<void> remove_a_css_rule(u32 index); + private: explicit CSSRuleList(NonnullRefPtrVector<CSSRule>&&); 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 <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); +} + } 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<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 { 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); }; |