Kaynağa Gözat

LibWeb: Implement most of CSSStyleRule.insertRule()

Andreas Kling 3 yıl önce
ebeveyn
işleme
994e33b0f7

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

@@ -25,6 +25,31 @@ bool CSSRuleList::is_supported_property_index(u32 index) const
     return index < m_rules.size();
 }
 
+// https://drafts.csswg.org/cssom/#insert-a-css-rule
+DOM::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(NonnullRefPtr<CSSRule> rule, u32 index)
+{
+    // 1. Set length to the number of items in list.
+    auto length = m_rules.size();
+
+    // 2. If index is greater than length, then throw an IndexSizeError exception.
+    if (index > length)
+        return DOM::IndexSizeError::create("CSS rule index out of bounds.");
+
+    // NOTE: These steps don't apply since we're receiving a parsed rule.
+    // 3. Set new rule to the results of performing parse a CSS rule on argument rule.
+    // 4. If new rule is a syntax error, throw a SyntaxError exception.
+
+    // FIXME: 5. If new rule cannot be inserted into list at the zero-index position index due to constraints specified by CSS, then throw a HierarchyRequestError exception. [CSS21]
+
+    // FIXME: 6. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception.
+
+    // 7. Insert new rule into list at the zero-indexed position index.
+    m_rules.insert(index, move(rule));
+
+    // 8. Return index.
+    return index;
+}
+
 // https://drafts.csswg.org/cssom/#remove-a-css-rule
 DOM::ExceptionOr<void> CSSRuleList::remove_a_css_rule(u32 index)
 {

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

@@ -48,6 +48,7 @@ public:
     bool is_supported_property_index(u32 index) const;
 
     DOM::ExceptionOr<void> remove_a_css_rule(u32 index);
+    DOM::ExceptionOr<unsigned> insert_a_css_rule(NonnullRefPtr<CSSRule>, u32 index);
 
 private:
     explicit CSSRuleList(NonnullRefPtrVector<CSSRule>&&);

+ 9 - 8
Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp

@@ -5,6 +5,7 @@
  */
 
 #include <LibWeb/CSS/CSSStyleSheet.h>
+#include <LibWeb/CSS/Parser/Parser.h>
 #include <LibWeb/DOM/ExceptionOr.h>
 
 namespace Web::CSS {
@@ -25,17 +26,17 @@ DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned
 
     // 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.
+    // 3. Let parsed rule be the return value of invoking parse a rule with rule.
+    auto parsed_rule = parse_css_rule(CSS::ParsingContext {}, rule);
 
-    // If parsed rule is a syntax error, return parsed rule.
+    // 4. If parsed rule is a syntax error, return parsed rule.
+    if (!parsed_rule)
+        return DOM::SyntaxError::create("Unable to parse CSS rule.");
 
-    // If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
+    // FIXME: 5. 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();
+    // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
+    return m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
 }
 
 // https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule

+ 6 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -3298,6 +3298,12 @@ RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const& context, Stri
     return parser.parse_as_css_value(property_id);
 }
 
+RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const& context, StringView css_text)
+{
+    CSS::Parser parser(context, css_text);
+    return parser.parse_as_rule();
+}
+
 Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const& context, StringView const& selector_text)
 {
     CSS::Parser parser(context, selector_text);

+ 1 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -235,6 +235,7 @@ RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const&, StringView cons
 RefPtr<CSS::PropertyOwningCSSStyleDeclaration> parse_css_declaration(CSS::ParsingContext const&, StringView const&);
 RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView const&, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
 Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView const&);
+RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);
 
 RefPtr<CSS::StyleValue> parse_html_length(DOM::Document const&, StringView const&);