2021-03-07 16:14:04 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-07 16:14:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
|
|
|
#include <AK/TypeCasts.h>
|
|
|
|
#include <LibWeb/CSS/CSSImportRule.h>
|
|
|
|
#include <LibWeb/CSS/CSSRule.h>
|
2021-09-28 16:18:20 +01:00
|
|
|
#include <LibWeb/CSS/CSSRuleList.h>
|
2021-03-07 16:14:04 +01:00
|
|
|
#include <LibWeb/CSS/StyleSheet.h>
|
|
|
|
#include <LibWeb/Loader/Resource.h>
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
|
class CSSStyleSheet final : public StyleSheet {
|
|
|
|
public:
|
2021-03-08 11:22:18 +01:00
|
|
|
using WrapperType = Bindings::CSSStyleSheetWrapper;
|
|
|
|
|
2021-03-07 16:14:04 +01:00
|
|
|
static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules)
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new CSSStyleSheet(move(rules)));
|
2021-03-07 16:14:04 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 16:16:28 +01:00
|
|
|
virtual ~CSSStyleSheet() override;
|
|
|
|
|
|
|
|
virtual String type() const override { return "text/css"; }
|
2021-03-07 16:14:04 +01:00
|
|
|
|
2021-09-28 16:18:20 +01:00
|
|
|
CSSRuleList const& rules() const { return m_rules; }
|
|
|
|
CSSRuleList& rules() { return m_rules; }
|
2021-03-07 16:14:04 +01:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_effective_style_rule(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto& rule : m_rules)
|
|
|
|
if (rule.type() == CSSRule::Type::Style) {
|
2021-06-24 19:53:42 +02:00
|
|
|
callback(verify_cast<CSSStyleRule>(rule));
|
2021-03-07 16:14:04 +01:00
|
|
|
} else if (rule.type() == CSSRule::Type::Import) {
|
2021-06-24 19:53:42 +02:00
|
|
|
const auto& import_rule = verify_cast<CSSImportRule>(rule);
|
2021-03-07 16:14:04 +01:00
|
|
|
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) {
|
2021-06-24 19:53:42 +02:00
|
|
|
auto& import_rule = verify_cast<CSSImportRule>(rule);
|
2021-03-07 16:14:04 +01:00
|
|
|
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>);
|
|
|
|
|
2021-09-28 16:18:20 +01:00
|
|
|
CSSRuleList m_rules;
|
2021-03-07 16:14:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-03-08 11:22:18 +01:00
|
|
|
|
|
|
|
namespace Web::Bindings {
|
|
|
|
|
|
|
|
CSSStyleSheetWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleSheet&);
|
|
|
|
|
|
|
|
}
|