LibWeb: Create base class CSSRule for all CSS rules

This is a foundation for handling other ("at") CSS rules.
This commit is contained in:
Sviatoslav Peleshko 2021-02-21 13:45:26 +02:00 committed by Andreas Kling
parent b807d51598
commit 04d67d0239
Notes: sideshowbarker 2024-07-18 21:52:19 +09:00
12 changed files with 147 additions and 17 deletions

View File

@ -9,6 +9,7 @@ set(SOURCES
Bindings/ScriptExecutionContext.cpp
Bindings/WindowObject.cpp
Bindings/Wrappable.cpp
CSS/CSSRule.cpp
CSS/DefaultStyleSheetSource.cpp
CSS/Length.cpp
CSS/Parser/CSSParser.cpp

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
* 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/CSSRule.h>
namespace Web::CSS {
CSSRule::~CSSRule()
{
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
* 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/RefCounted.h>
#include <AK/String.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/CSS/StyleDeclaration.h>
namespace Web::CSS {
class CSSRule : public RefCounted<CSSRule> {
public:
virtual ~CSSRule();
enum class Type : u32 {
Style,
Import,
Media,
__Count,
};
virtual StringView class_name() const = 0;
virtual Type type() const = 0;
private:
};
}

View File

@ -25,8 +25,10 @@
*/
#include <AK/HashMap.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/Parser/CSSParser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleRule.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/DOM/Document.h>
#include <ctype.h>
@ -853,7 +855,7 @@ public:
private:
CSS::ParsingContext m_context;
NonnullRefPtrVector<CSS::StyleRule> rules;
NonnullRefPtrVector<CSS::CSSRule> rules;
struct CurrentRule {
Vector<CSS::Selector> selectors;

View File

@ -25,6 +25,7 @@
*/
#include <LibWeb/CSS/StyleInvalidator.h>
#include <LibWeb/CSS/StyleRule.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,6 +29,7 @@
#include <LibWeb/CSS/Parser/CSSParser.h>
#include <LibWeb/CSS/SelectorEngine.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/CSS/StyleRule.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
@ -86,7 +88,7 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e
size_t style_sheet_index = 0;
for_each_stylesheet([&](auto& sheet) {
size_t rule_index = 0;
for (auto& rule : sheet.rules()) {
sheet.for_each_effective_style_rule([&](auto& rule) {
size_t selector_index = 0;
for (auto& selector : rule.selectors()) {
if (SelectorEngine::matches(selector, element)) {
@ -96,7 +98,7 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e
++selector_index;
}
++rule_index;
}
});
++style_sheet_index;
});

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,12 +28,13 @@
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/CSS/StyleDeclaration.h>
namespace Web::CSS {
class StyleRule : public RefCounted<StyleRule> {
class StyleRule : public CSSRule {
AK_MAKE_NONCOPYABLE(StyleRule);
AK_MAKE_NONMOVABLE(StyleRule);
@ -47,6 +49,9 @@ public:
const Vector<Selector>& selectors() const { return m_selectors; }
const StyleDeclaration& declaration() const { return m_declaration; }
virtual StringView class_name() const { return "StyleRule"; };
virtual Type type() const { return Type::Style; };
private:
StyleRule(Vector<Selector>&&, NonnullRefPtr<StyleDeclaration>&&);

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,7 +29,7 @@
namespace Web::CSS {
StyleSheet::StyleSheet(NonnullRefPtrVector<StyleRule>&& rules)
StyleSheet::StyleSheet(NonnullRefPtrVector<CSSRule>&& rules)
: m_rules(move(rules))
{
}

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,26 +28,37 @@
#pragma once
#include <AK/NonnullRefPtrVector.h>
#include <LibWeb/CSS/StyleRule.h>
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/Loader/Resource.h>
namespace Web::CSS {
class StyleSheet : public RefCounted<StyleSheet> {
public:
static NonnullRefPtr<StyleSheet> create(NonnullRefPtrVector<StyleRule>&& rules)
static NonnullRefPtr<StyleSheet> create(NonnullRefPtrVector<CSSRule>&& rules)
{
return adopt(*new StyleSheet(move(rules)));
}
~StyleSheet();
const NonnullRefPtrVector<StyleRule>& rules() const { return m_rules; }
NonnullRefPtrVector<StyleRule>& rules() { return m_rules; }
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<StyleRule>(rule));
}
}
private:
explicit StyleSheet(NonnullRefPtrVector<StyleRule>&&);
explicit StyleSheet(NonnullRefPtrVector<CSSRule>&&);
NonnullRefPtrVector<StyleRule> m_rules;
NonnullRefPtrVector<CSSRule> m_rules;
};
}

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,7 +28,9 @@
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleRule.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/DOM/Comment.h>
#include <LibWeb/DOM/Document.h>
@ -389,16 +392,27 @@ void dump_selector(StringBuilder& builder, const CSS::Selector& selector)
}
}
void dump_rule(const CSS::StyleRule& rule)
void dump_rule(const CSS::CSSRule& rule)
{
StringBuilder builder;
dump_rule(builder, rule);
dbgln("{}", builder.string_view());
}
void dump_rule(StringBuilder& builder, const CSS::StyleRule& rule)
void dump_rule(StringBuilder& builder, const CSS::CSSRule& rule)
{
builder.appendff("{}:\n", rule.class_name());
switch (rule.type()) {
case CSS::CSSRule::Type::Style:
dump_style_rule(builder, downcast<const CSS::StyleRule>(rule));
break;
default:
VERIFY_NOT_REACHED();
}
}
void dump_style_rule(StringBuilder& builder, const CSS::StyleRule& rule)
{
builder.append("Rule:\n");
for (auto& selector : rule.selectors()) {
dump_selector(builder, selector);
}
@ -417,7 +431,7 @@ void dump_sheet(const CSS::StyleSheet& sheet)
void dump_sheet(StringBuilder& builder, const CSS::StyleSheet& sheet)
{
builder.appendff("StyleSheet{{{}}}: {} rule(s)", &sheet, sheet.rules().size());
builder.appendff("StyleSheet{{{}}}: {} rule(s)\n", &sheet, sheet.rules().size());
for (auto& rule : sheet.rules()) {
dump_rule(builder, rule);

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -37,8 +38,9 @@ void dump_tree(StringBuilder&, const Layout::Node&, bool show_box_model = false,
void dump_tree(const Layout::Node&, bool show_box_model = false, bool show_specified_style = false);
void dump_sheet(StringBuilder&, const CSS::StyleSheet&);
void dump_sheet(const CSS::StyleSheet&);
void dump_rule(StringBuilder&, const CSS::StyleRule&);
void dump_rule(const CSS::StyleRule&);
void dump_rule(StringBuilder&, const CSS::CSSRule&);
void dump_rule(const CSS::CSSRule&);
void dump_style_rule(StringBuilder&, const CSS::StyleRule&);
void dump_selector(StringBuilder&, const CSS::Selector&);
void dump_selector(const CSS::Selector&);

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,6 +28,7 @@
#pragma once
namespace Web::CSS {
class CSSRule;
class Length;
class Selector;
class StyleDeclaration;