1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:57:46 +00:00

LibWeb: Split CSS::StyleSheet into StyleSheet and CSSStyleSheet

This is a little convoluted but matches the CSSOM specification.
This commit is contained in:
Andreas Kling 2021-03-07 16:14:04 +01:00
parent 0af4762662
commit fefb05f6f3
17 changed files with 162 additions and 82 deletions

View file

@ -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 {

View file

@ -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;
};
}

View file

@ -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()
{
}
}

View file

@ -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;
};
}

View file

@ -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();
}

View file

@ -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&);

View file

@ -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;
}

View file

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

View file

@ -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;
};
}

View file

@ -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 {