Explorar o código

LibWeb: Split CSS::StyleSheet into StyleSheet and CSSStyleSheet

This is a little convoluted but matches the CSSOM specification.
Andreas Kling %!s(int64=4) %!d(string=hai) anos
pai
achega
fefb05f6f3

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -12,6 +12,7 @@ set(SOURCES
     CSS/CSSImportRule.cpp
     CSS/CSSRule.cpp
     CSS/CSSStyleRule.cpp
+    CSS/CSSStyleSheet.cpp
     CSS/DefaultStyleSheetSource.cpp
     CSS/Length.cpp
     CSS/Parser/CSSParser.cpp

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSImportRule.cpp

@@ -26,7 +26,7 @@
 
 #include <AK/URL.h>
 #include <LibWeb/CSS/CSSImportRule.h>
-#include <LibWeb/CSS/StyleSheet.h>
+#include <LibWeb/CSS/CSSStyleSheet.h>
 
 namespace Web::CSS {
 

+ 4 - 4
Userland/Libraries/LibWeb/CSS/CSSImportRule.h

@@ -46,18 +46,18 @@ public:
     const URL& url() const { return m_url; }
 
     bool has_import_result() const { return !m_style_sheet.is_null(); }
-    RefPtr<StyleSheet> loaded_style_sheet() { return m_style_sheet; }
-    const RefPtr<StyleSheet> loaded_style_sheet() const { return m_style_sheet; }
+    RefPtr<CSSStyleSheet> loaded_style_sheet() { return m_style_sheet; }
+    const RefPtr<CSSStyleSheet> loaded_style_sheet() const { return m_style_sheet; }
     void set_style_sheet(const RefPtr<StyleSheet>& style_sheet) { m_style_sheet = style_sheet; }
 
     virtual StringView class_name() const { return "CSSImportRule"; };
     virtual Type type() const { return Type::Import; };
 
 private:
-    CSSImportRule(URL);
+    explicit CSSImportRule(URL);
 
     URL m_url;
-    RefPtr<StyleSheet> m_style_sheet;
+    RefPtr<CSSStyleSheet> m_style_sheet;
 };
 
 }

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

@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <LibWeb/CSS/CSSStyleSheet.h>
+
+namespace Web::CSS {
+
+CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules)
+    : m_rules(move(rules))
+{
+}
+
+CSSStyleSheet::~CSSStyleSheet()
+{
+}
+
+}

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

@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/NonnullRefPtrVector.h>
+#include <AK/TypeCasts.h>
+#include <LibWeb/CSS/CSSImportRule.h>
+#include <LibWeb/CSS/CSSRule.h>
+#include <LibWeb/CSS/StyleSheet.h>
+#include <LibWeb/Loader/Resource.h>
+
+namespace Web::CSS {
+
+class CSSStyleSheet final : public StyleSheet {
+public:
+    static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules)
+    {
+        return adopt(*new CSSStyleSheet(move(rules)));
+    }
+
+    virtual ~CSSStyleSheet();
+
+    const NonnullRefPtrVector<CSSRule>& rules() const { return m_rules; }
+    NonnullRefPtrVector<CSSRule>& rules() { return m_rules; }
+
+    template<typename Callback>
+    void for_each_effective_style_rule(Callback callback) const
+    {
+        for (auto& rule : m_rules)
+            if (rule.type() == CSSRule::Type::Style) {
+                callback(downcast<CSSStyleRule>(rule));
+            } else if (rule.type() == CSSRule::Type::Import) {
+                const auto& import_rule = downcast<CSSImportRule>(rule);
+                if (import_rule.has_import_result())
+                    import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
+            }
+    }
+
+    template<typename Callback>
+    bool for_first_not_loaded_import_rule(Callback callback)
+    {
+        for (auto& rule : m_rules)
+            if (rule.type() == CSSRule::Type::Import) {
+                auto& import_rule = downcast<CSSImportRule>(rule);
+                if (!import_rule.has_import_result()) {
+                    callback(import_rule);
+                    return true;
+                }
+
+                if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
+                    return true;
+                }
+            }
+
+        return false;
+    }
+
+private:
+    explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>);
+
+    NonnullRefPtrVector<CSSRule> m_rules;
+};
+
+}

+ 4 - 4
Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp

@@ -934,7 +934,7 @@ public:
         consume_whitespace_or_comments();
     }
 
-    RefPtr<CSS::StyleSheet> parse_sheet()
+    RefPtr<CSS::CSSStyleSheet> parse_sheet()
     {
         if (peek(0) == (char)0xef && peek(1) == (char)0xbb && peek(2) == (char)0xbf) {
             // HACK: Skip UTF-8 BOM.
@@ -945,7 +945,7 @@ public:
             parse_rule();
         }
 
-        return CSS::StyleSheet::create(move(rules));
+        return CSS::CSSStyleSheet::create(move(rules));
     }
 
     RefPtr<CSS::StyleDeclaration> parse_standalone_declaration()
@@ -986,10 +986,10 @@ Optional<CSS::Selector> parse_selector(const CSS::ParsingContext& context, const
     return parser.parse_individual_selector();
 }
 
-RefPtr<CSS::StyleSheet> parse_css(const CSS::ParsingContext& context, const StringView& css)
+RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext& context, const StringView& css)
 {
     if (css.is_empty())
-        return CSS::StyleSheet::create({});
+        return CSS::CSSStyleSheet::create({});
     CSSParser parser(context, css);
     return parser.parse_sheet();
 }

+ 2 - 2
Userland/Libraries/LibWeb/CSS/Parser/CSSParser.h

@@ -28,7 +28,7 @@
 
 #include <AK/NonnullRefPtr.h>
 #include <AK/String.h>
-#include <LibWeb/CSS/StyleSheet.h>
+#include <LibWeb/CSS/CSSStyleSheet.h>
 
 namespace Web::CSS {
 class ParsingContext {
@@ -48,7 +48,7 @@ private:
 
 namespace Web {
 
-RefPtr<CSS::StyleSheet> parse_css(const CSS::ParsingContext&, const StringView&);
+RefPtr<CSS::CSSStyleSheet> parse_css(const CSS::ParsingContext&, const StringView&);
 RefPtr<CSS::StyleDeclaration> parse_css_declaration(const CSS::ParsingContext&, const StringView&);
 RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext&, const StringView&, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
 Optional<CSS::Selector> parse_selector(const CSS::ParsingContext&, const StringView&);

+ 4 - 2
Userland/Libraries/LibWeb/CSS/StyleResolver.cpp

@@ -87,8 +87,10 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e
 
     size_t style_sheet_index = 0;
     for_each_stylesheet([&](auto& sheet) {
+        if (!is<CSSStyleSheet>(sheet))
+            return;
         size_t rule_index = 0;
-        sheet.for_each_effective_style_rule([&](auto& rule) {
+        static_cast<const CSSStyleSheet&>(sheet).for_each_effective_style_rule([&](auto& rule) {
             size_t selector_index = 0;
             for (auto& selector : rule.selectors()) {
                 if (SelectorEngine::matches(selector, element)) {
@@ -100,7 +102,7 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e
             ++rule_index;
         });
         ++style_sheet_index;
-    });
+   });
 
     return matching_rules;
 }

+ 0 - 9
Userland/Libraries/LibWeb/CSS/StyleSheet.cpp

@@ -29,13 +29,4 @@
 
 namespace Web::CSS {
 
-StyleSheet::StyleSheet(NonnullRefPtrVector<CSSRule>&& rules)
-    : m_rules(move(rules))
-{
-}
-
-StyleSheet::~StyleSheet()
-{
-}
-
 }

+ 4 - 50
Userland/Libraries/LibWeb/CSS/StyleSheet.h

@@ -27,62 +27,16 @@
 
 #pragma once
 
-#include <AK/NonnullRefPtrVector.h>
-#include <AK/TypeCasts.h>
-#include <LibWeb/CSS/CSSImportRule.h>
-#include <LibWeb/CSS/CSSRule.h>
-#include <LibWeb/Loader/Resource.h>
+#include <AK/RefCounted.h>
 
 namespace Web::CSS {
 
 class StyleSheet : public RefCounted<StyleSheet> {
 public:
-    static NonnullRefPtr<StyleSheet> create(NonnullRefPtrVector<CSSRule>&& rules)
-    {
-        return adopt(*new StyleSheet(move(rules)));
-    }
+    virtual ~StyleSheet() = default;
 
-    ~StyleSheet();
-
-    const NonnullRefPtrVector<CSSRule>& rules() const { return m_rules; }
-    NonnullRefPtrVector<CSSRule>& rules() { return m_rules; }
-
-    template<typename Callback>
-    void for_each_effective_style_rule(Callback callback) const
-    {
-        for (auto& rule : m_rules)
-            if (rule.type() == CSSRule::Type::Style) {
-                callback(downcast<CSSStyleRule>(rule));
-            } else if (rule.type() == CSSRule::Type::Import) {
-                const CSSImportRule& import_rule = downcast<CSSImportRule>(rule);
-                if (import_rule.has_import_result())
-                    import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
-            }
-    }
-
-    template<typename Callback>
-    bool for_first_not_loaded_import_rule(Callback callback)
-    {
-        for (auto& rule : m_rules)
-            if (rule.type() == CSSRule::Type::Import) {
-                CSSImportRule& import_rule = downcast<CSSImportRule>(rule);
-                if (!import_rule.has_import_result()) {
-                    callback(import_rule);
-                    return true;
-                }
-
-                if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
-                    return true;
-                }
-            }
-
-        return false;
-    }
-
-private:
-    explicit StyleSheet(NonnullRefPtrVector<CSSRule>&&);
-
-    NonnullRefPtrVector<CSSRule> m_rules;
+protected:
+    StyleSheet() = default;
 };
 
 }

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

@@ -26,8 +26,10 @@
 
 #pragma once
 
+#include <AK/NonnullRefPtrVector.h>
 #include <AK/RefCounted.h>
 #include <LibWeb/CSS/StyleSheet.h>
+#include <LibWeb/Forward.h>
 
 namespace Web::CSS {
 

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Document.h

@@ -37,8 +37,8 @@
 #include <LibJS/Forward.h>
 #include <LibWeb/Bindings/ScriptExecutionContext.h>
 #include <LibWeb/Bindings/WindowObject.h>
+#include <LibWeb/CSS/CSSStyleSheet.h>
 #include <LibWeb/CSS/StyleResolver.h>
-#include <LibWeb/CSS/StyleSheet.h>
 #include <LibWeb/CSS/StyleSheetList.h>
 #include <LibWeb/DOM/DOMImplementation.h>
 #include <LibWeb/DOM/ExceptionOr.h>

+ 5 - 3
Userland/Libraries/LibWeb/Dump.cpp

@@ -31,8 +31,8 @@
 #include <LibWeb/CSS/CSSImportRule.h>
 #include <LibWeb/CSS/CSSRule.h>
 #include <LibWeb/CSS/CSSStyleRule.h>
+#include <LibWeb/CSS/CSSStyleSheet.h>
 #include <LibWeb/CSS/PropertyID.h>
-#include <LibWeb/CSS/StyleSheet.h>
 #include <LibWeb/DOM/Comment.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Element.h>
@@ -440,9 +440,11 @@ void dump_sheet(const CSS::StyleSheet& sheet)
 
 void dump_sheet(StringBuilder& builder, const CSS::StyleSheet& sheet)
 {
-    builder.appendff("StyleSheet{{{}}}: {} rule(s)\n", &sheet, sheet.rules().size());
+    VERIFY(is<CSS::CSSStyleSheet>(sheet));
 
-    for (auto& rule : sheet.rules()) {
+    builder.appendff("CSSStyleSheet{{{}}}: {} rule(s)\n", &sheet, static_cast<const CSS::CSSStyleSheet&>(sheet).rules().size());
+
+    for (auto& rule : static_cast<const CSS::CSSStyleSheet&>(sheet).rules()) {
         dump_rule(builder, rule);
     }
 }

+ 1 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -31,6 +31,7 @@ namespace Web::CSS {
 class CSSRule;
 class CSSImportRule;
 class CSSStyleRule;
+class CSSStyleSheet;
 class Length;
 class Selector;
 class StyleDeclaration;

+ 0 - 1
Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp

@@ -25,7 +25,6 @@
  */
 
 #include <LibGfx/Bitmap.h>
-#include <LibGfx/ImageDecoder.h>
 #include <LibWeb/CSS/Parser/CSSParser.h>
 #include <LibWeb/CSS/StyleResolver.h>
 #include <LibWeb/DOM/Document.h>

+ 2 - 2
Userland/Libraries/LibWeb/Loader/CSSLoader.cpp

@@ -43,14 +43,14 @@ void CSSLoader::load_from_text(const String& text)
 {
     m_style_sheet = parse_css(CSS::ParsingContext(*m_document), text);
     if (!m_style_sheet)
-        m_style_sheet = CSS::StyleSheet::create({});
+        m_style_sheet = CSS::CSSStyleSheet::create({});
 
     load_next_import_if_needed();
 }
 
 void CSSLoader::load_from_url(const URL& url)
 {
-    m_style_sheet = CSS::StyleSheet::create({});
+    m_style_sheet = CSS::CSSStyleSheet::create({});
 
     LoadRequest request;
     request.set_url(url);

+ 3 - 3
Userland/Libraries/LibWeb/Loader/CSSLoader.h

@@ -27,7 +27,7 @@
 #pragma once
 
 #include <AK/Function.h>
-#include <LibWeb/CSS/StyleSheet.h>
+#include <LibWeb/CSS/CSSStyleSheet.h>
 #include <LibWeb/Loader/Resource.h>
 
 namespace Web {
@@ -41,7 +41,7 @@ public:
 
     void load_next_import_if_needed();
 
-    RefPtr<CSS::StyleSheet> style_sheet() const { return m_style_sheet; };
+    RefPtr<CSS::CSSStyleSheet> style_sheet() const { return m_style_sheet; };
 
     Function<void()> on_load;
     Function<void()> on_fail;
@@ -51,7 +51,7 @@ private:
     virtual void resource_did_load() override;
     virtual void resource_did_fail() override;
 
-    RefPtr<CSS::StyleSheet> m_style_sheet;
+    RefPtr<CSS::CSSStyleSheet> m_style_sheet;
     const DOM::Document* m_document;
 };