From 3e74c194f902cbcb2b80a76e2d414a6db2ce044a Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 29 Sep 2021 12:48:04 +0100 Subject: LibWeb: Add CSSMediaRule This is the class corresponding to a `@media` rule. It contains a list of media queries and a list of child css rules. --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/CSS/CSSMediaRule.cpp | 31 ++++++++++++++++++ Userland/Libraries/LibWeb/CSS/CSSMediaRule.h | 45 ++++++++++++++++++++++++++ Userland/Libraries/LibWeb/Dump.cpp | 37 +++++++++++++++++---- Userland/Libraries/LibWeb/Dump.h | 7 ++-- Userland/Libraries/LibWeb/Forward.h | 3 +- 6 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 Userland/Libraries/LibWeb/CSS/CSSMediaRule.cpp create mode 100644 Userland/Libraries/LibWeb/CSS/CSSMediaRule.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index b31840bbb3..20dbec40f0 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -17,6 +17,7 @@ set(SOURCES CSS/CSSConditionRule.cpp CSS/CSSGroupingRule.cpp CSS/CSSImportRule.cpp + CSS/CSSMediaRule.cpp CSS/CSSRule.cpp CSS/CSSRuleList.cpp CSS/CSSStyleDeclaration.cpp diff --git a/Userland/Libraries/LibWeb/CSS/CSSMediaRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSMediaRule.cpp new file mode 100644 index 0000000000..0666e08054 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSMediaRule.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Web::CSS { + +CSSMediaRule::CSSMediaRule(NonnullRefPtr&& media, NonnullRefPtrVector&& rules) + : CSSConditionRule(move(rules)) + , m_media(move(media)) +{ +} + +CSSMediaRule::~CSSMediaRule() +{ +} + +String CSSMediaRule::condition_text() const +{ + return m_media->media_text(); +} + +void CSSMediaRule::set_condition_text(String text) +{ + m_media->set_media_text(text); +} + +} diff --git a/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h b/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h new file mode 100644 index 0000000000..b856e9a03d --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSMediaRule.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::CSS { + +// https://www.w3.org/TR/css-conditional-3/#the-cssmediarule-interface +class CSSMediaRule final : public CSSConditionRule { + AK_MAKE_NONCOPYABLE(CSSMediaRule); + AK_MAKE_NONMOVABLE(CSSMediaRule); + +public: + static NonnullRefPtr create(NonnullRefPtr&& media_queries, NonnullRefPtrVector&& rules) + { + return adopt_ref(*new CSSMediaRule(move(media_queries), move(rules))); + } + + ~CSSMediaRule(); + + virtual StringView class_name() const override { return "CSSMediaRule"; }; + virtual Type type() const override { return Type::Media; }; + + virtual String condition_text() const override; + virtual void set_condition_text(String) override; + + NonnullRefPtr const& media() const { return m_media; } + +private: + explicit CSSMediaRule(NonnullRefPtr&&, NonnullRefPtrVector&&); + + NonnullRefPtr m_media; +}; + +template<> +inline bool CSSRule::fast_is() const { return type() == CSSRule::Type::Media; } + +} diff --git a/Userland/Libraries/LibWeb/Dump.cpp b/Userland/Libraries/LibWeb/Dump.cpp index 358c253b36..ba7c2f1210 100644 --- a/Userland/Libraries/LibWeb/Dump.cpp +++ b/Userland/Libraries/LibWeb/Dump.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,12 @@ namespace Web { +static void indent(StringBuilder& builder, int levels) +{ + for (int i = 0; i < levels; i++) + builder.append(" "); +} + void dump_tree(DOM::Node const& node) { StringBuilder builder; @@ -478,33 +485,51 @@ void dump_rule(CSS::CSSRule const& rule) dbgln("{}", builder.string_view()); } -void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule) +void dump_rule(StringBuilder& builder, CSS::CSSRule const& rule, int indent_levels) { + indent(builder, indent_levels); builder.appendff("{}:\n", rule.class_name()); + switch (rule.type()) { case CSS::CSSRule::Type::Style: - dump_style_rule(builder, verify_cast(rule)); + dump_style_rule(builder, verify_cast(rule), indent_levels); break; case CSS::CSSRule::Type::Import: - dump_import_rule(builder, verify_cast(rule)); + dump_import_rule(builder, verify_cast(rule), indent_levels); + break; + case CSS::CSSRule::Type::Media: + dump_media_rule(builder, verify_cast(rule), indent_levels); break; - default: + case CSS::CSSRule::Type::__Count: VERIFY_NOT_REACHED(); } } -void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule) +void dump_import_rule(StringBuilder& builder, CSS::CSSImportRule const& rule, int indent_levels) { + indent(builder, indent_levels); builder.appendff(" Document URL: {}\n", rule.url()); } -void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule) +void dump_media_rule(StringBuilder& builder, CSS::CSSMediaRule const& media, int indent_levels) +{ + indent(builder, indent_levels); + builder.appendff(" Media: {}\n Rules ({}):\n", media.condition_text(), media.css_rules().length()); + + for (auto& rule : media.css_rules()) { + dump_rule(builder, rule, indent_levels + 1); + } +} + +void dump_style_rule(StringBuilder& builder, CSS::CSSStyleRule const& rule, int indent_levels) { for (auto& selector : rule.selectors()) { dump_selector(builder, selector); } + indent(builder, indent_levels); builder.append(" Declarations:\n"); for (auto& property : verify_cast(rule.declaration()).properties()) { + indent(builder, indent_levels); builder.appendff(" {}: '{}'", CSS::string_from_property_id(property.property_id), property.value->to_string()); if (property.important) builder.append(" \033[31;1m!important\033[0m"); diff --git a/Userland/Libraries/LibWeb/Dump.h b/Userland/Libraries/LibWeb/Dump.h index 32827c8266..885e46ef08 100644 --- a/Userland/Libraries/LibWeb/Dump.h +++ b/Userland/Libraries/LibWeb/Dump.h @@ -18,10 +18,11 @@ void dump_tree(StringBuilder&, Layout::Node const&, bool show_box_model = false, void dump_tree(Layout::Node const&, bool show_box_model = false, bool show_specified_style = false); void dump_sheet(StringBuilder&, CSS::StyleSheet const&); void dump_sheet(CSS::StyleSheet const&); -void dump_rule(StringBuilder&, CSS::CSSRule const&); +void dump_rule(StringBuilder&, CSS::CSSRule const&, int indent_levels = 0); void dump_rule(CSS::CSSRule const&); -void dump_style_rule(StringBuilder&, CSS::CSSStyleRule const&); -void dump_import_rule(StringBuilder&, CSS::CSSImportRule const&); +void dump_style_rule(StringBuilder&, CSS::CSSStyleRule const&, int indent_levels = 0); +void dump_import_rule(StringBuilder&, CSS::CSSImportRule const&, int indent_levels = 0); +void dump_media_rule(StringBuilder&, CSS::CSSMediaRule const&, int indent_levels = 0); void dump_selector(StringBuilder&, CSS::Selector const&); void dump_selector(CSS::Selector const&); diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index a456b8bae0..da94990c07 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -19,8 +19,9 @@ class Crypto; namespace Web::CSS { class CalculatedStyleValue; -class CSSRule; class CSSImportRule; +class CSSMediaRule; +class CSSRule; class CSSStyleDeclaration; class CSSStyleRule; class CSSStyleSheet; -- cgit v1.2.3