Skip to content

Commit

Permalink
LibWeb: Create base class CSSRule for all CSS rules
Browse files Browse the repository at this point in the history
This is a foundation for handling other ("at") CSS rules.
  • Loading branch information
speles authored and awesomekling committed Feb 28, 2021
1 parent b807d51 commit 04d67d0
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 17 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions Userland/Libraries/LibWeb/CSS/CSSRule.cpp
Original file line number Diff line number Diff line change
@@ -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()
{
}

}
53 changes: 53 additions & 0 deletions Userland/Libraries/LibWeb/CSS/CSSRule.h
Original file line number Diff line number Diff line change
@@ -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:
};

}
4 changes: 3 additions & 1 deletion Userland/Libraries/LibWeb/CSS/Parser/CSSParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -853,7 +855,7 @@ class CSSParser {
private:
CSS::ParsingContext m_context;

NonnullRefPtrVector<CSS::StyleRule> rules;
NonnullRefPtrVector<CSS::CSSRule> rules;

struct CurrentRule {
Vector<CSS::Selector> selectors;
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/CSS/StyleInvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <LibWeb/CSS/StyleInvalidator.h>
#include <LibWeb/CSS/StyleRule.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>

Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibWeb/CSS/StyleResolver.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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)) {
Expand All @@ -96,7 +98,7 @@ Vector<MatchingRule> StyleResolver::collect_matching_rules(const DOM::Element& e
++selector_index;
}
++rule_index;
}
});
++style_sheet_index;
});

Expand Down
7 changes: 6 additions & 1 deletion Userland/Libraries/LibWeb/CSS/StyleRule.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -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);

Expand All @@ -47,6 +49,9 @@ class StyleRule : public RefCounted<StyleRule> {
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>&&);

Expand Down
3 changes: 2 additions & 1 deletion Userland/Libraries/LibWeb/CSS/StyleSheet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -28,7 +29,7 @@

namespace Web::CSS {

StyleSheet::StyleSheet(NonnullRefPtrVector<StyleRule>&& rules)
StyleSheet::StyleSheet(NonnullRefPtrVector<CSSRule>&& rules)
: m_rules(move(rules))
{
}
Expand Down
24 changes: 18 additions & 6 deletions Userland/Libraries/LibWeb/CSS/StyleSheet.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -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;
};

}
22 changes: 18 additions & 4 deletions Userland/Libraries/LibWeb/Dump.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibWeb/Dump.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -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&);

Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/Forward.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -27,6 +28,7 @@
#pragma once

namespace Web::CSS {
class CSSRule;
class Length;
class Selector;
class StyleDeclaration;
Expand Down

0 comments on commit 04d67d0

Please sign in to comment.