forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibWeb: Create base class CSSRule for all CSS rules
This is a foundation for handling other ("at") CSS rules.
- Loading branch information
1 parent
b807d51
commit 04d67d0
Showing
12 changed files
with
147 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 @@ 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>&&); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -28,7 +29,7 @@ | |
|
||
namespace Web::CSS { | ||
|
||
StyleSheet::StyleSheet(NonnullRefPtrVector<StyleRule>&& rules) | ||
StyleSheet::StyleSheet(NonnullRefPtrVector<CSSRule>&& rules) | ||
: m_rules(move(rules)) | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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&); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -27,6 +28,7 @@ | |
#pragma once | ||
|
||
namespace Web::CSS { | ||
class CSSRule; | ||
class Length; | ||
class Selector; | ||
class StyleDeclaration; | ||
|