2021-03-07 12:14:04 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2019-2021, Andreas Kling <andreas@ladybird.org>
|
2024-02-24 04:46:59 -03:00
|
|
|
* Copyright (c) 2024, Tim Ledbetter <timledbetter@gmail.com>
|
2026-02-05 14:40:15 -03:00
|
|
|
* Copyright (c) 2026, Sam Atkins <sam@ladybird.org>
|
2021-03-07 12:14:04 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-07 12:14:04 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-09-30 17:57:35 -03:00
|
|
|
#include <AK/Function.h>
|
2026-04-28 05:27:31 -03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
#include <AK/RefPtr.h>
|
2026-05-01 14:01:23 -03:00
|
|
|
#include <LibWeb/Bindings/CSSStyleSheet.h>
|
2023-07-31 13:46:57 -03:00
|
|
|
#include <LibWeb/CSS/CSSNamespaceRule.h>
|
2021-03-07 12:14:04 -03:00
|
|
|
#include <LibWeb/CSS/CSSRule.h>
|
2021-09-28 12:18:20 -03:00
|
|
|
#include <LibWeb/CSS/CSSRuleList.h>
|
2021-09-30 17:57:35 -03:00
|
|
|
#include <LibWeb/CSS/CSSStyleRule.h>
|
2026-05-23 08:16:30 -03:00
|
|
|
#include <LibWeb/CSS/SelectorInsights.h>
|
2021-03-07 12:14:04 -03:00
|
|
|
#include <LibWeb/CSS/StyleSheet.h>
|
2026-03-19 07:22:18 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
|
2026-02-11 05:20:13 -03:00
|
|
|
#include <LibWeb/DOM/StyleInvalidationReason.h>
|
2025-07-19 23:35:33 -03:00
|
|
|
#include <LibWeb/Export.h>
|
2024-02-24 04:46:59 -03:00
|
|
|
#include <LibWeb/WebIDL/Types.h>
|
2021-03-07 12:14:04 -03:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2021-09-30 17:57:35 -03:00
|
|
|
class CSSImportRule;
|
2026-04-28 05:27:31 -03:00
|
|
|
class StyleScope;
|
2026-04-22 17:34:46 -03:00
|
|
|
struct ShadowRootStylesheetEffects;
|
2026-04-28 05:27:31 -03:00
|
|
|
struct StyleCache;
|
2021-09-30 17:57:35 -03:00
|
|
|
|
2025-04-08 09:35:26 -03:00
|
|
|
// https://drafts.csswg.org/cssom-1/#cssstylesheet
|
2025-07-19 23:35:33 -03:00
|
|
|
class WEB_API CSSStyleSheet final : public StyleSheet {
|
2022-08-28 08:42:07 -03:00
|
|
|
WEB_PLATFORM_OBJECT(CSSStyleSheet, StyleSheet);
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(CSSStyleSheet);
|
2021-03-08 07:22:18 -03:00
|
|
|
|
2022-08-07 08:14:54 -03:00
|
|
|
public:
|
2026-02-05 14:40:15 -03:00
|
|
|
enum class LoadingState : u8 {
|
|
|
|
|
Unloaded,
|
|
|
|
|
Loading,
|
|
|
|
|
Loaded,
|
|
|
|
|
Error,
|
|
|
|
|
};
|
|
|
|
|
static StringView loading_state_name(LoadingState);
|
|
|
|
|
|
|
|
|
|
class Subresource {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Subresource() = default;
|
|
|
|
|
|
|
|
|
|
virtual GC::Ptr<CSSStyleSheet> parent_style_sheet_for_subresource() = 0;
|
|
|
|
|
LoadingState loading_state() const { return m_loading_state; }
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) = 0;
|
|
|
|
|
|
|
|
|
|
void set_loading_state(LoadingState);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LoadingState m_loading_state { LoadingState::Unloaded };
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-08 09:35:26 -03:00
|
|
|
[[nodiscard]] static GC::Ref<CSSStyleSheet> create(JS::Realm&, CSSRuleList&, MediaList&, Optional<::URL::URL> location);
|
2026-05-01 14:01:23 -03:00
|
|
|
static WebIDL::ExceptionOr<GC::Ref<CSSStyleSheet>> construct_impl(JS::Realm&, Optional<Bindings::CSSStyleSheetInit> const& options = {});
|
2021-03-07 12:14:04 -03:00
|
|
|
|
2026-04-28 05:27:31 -03:00
|
|
|
virtual ~CSSStyleSheet() override;
|
2021-03-08 12:16:28 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<CSSRule const> owner_rule() const { return m_owner_css_rule; }
|
|
|
|
|
GC::Ptr<CSSRule> owner_rule() { return m_owner_css_rule; }
|
2021-09-29 18:46:16 -03:00
|
|
|
void set_owner_css_rule(CSSRule* rule) { m_owner_css_rule = rule; }
|
|
|
|
|
|
2023-09-02 23:19:49 -03:00
|
|
|
virtual String type() const override { return "text/css"_string; }
|
2021-03-07 12:14:04 -03:00
|
|
|
|
2022-08-07 08:51:40 -03:00
|
|
|
CSSRuleList const& rules() const { return *m_rules; }
|
|
|
|
|
CSSRuleList& rules() { return *m_rules; }
|
2021-09-29 14:41:46 -03:00
|
|
|
|
|
|
|
|
CSSRuleList* css_rules() { return m_rules; }
|
|
|
|
|
CSSRuleList const* css_rules() const { return m_rules; }
|
2021-03-07 12:14:04 -03:00
|
|
|
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<unsigned> insert_rule(StringView rule, unsigned index);
|
2024-02-24 04:46:59 -03:00
|
|
|
WebIDL::ExceptionOr<WebIDL::Long> add_rule(Optional<String> selector, Optional<String> style, Optional<WebIDL::UnsignedLong> index);
|
2024-02-24 04:46:59 -03:00
|
|
|
WebIDL::ExceptionOr<void> remove_rule(Optional<WebIDL::UnsignedLong> index);
|
2022-09-25 13:03:42 -03:00
|
|
|
WebIDL::ExceptionOr<void> delete_rule(unsigned index);
|
2021-09-29 15:28:32 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<WebIDL::Promise> replace(String text);
|
2024-02-24 04:46:59 -03:00
|
|
|
WebIDL::ExceptionOr<void> replace_sync(StringView text);
|
2024-02-24 04:46:59 -03:00
|
|
|
|
2024-09-03 07:43:20 -03:00
|
|
|
void for_each_effective_rule(TraversalOrder, Function<void(CSSRule const&)> const& callback) const;
|
2024-10-17 09:48:00 -03:00
|
|
|
void for_each_effective_style_producing_rule(Function<void(CSSRule const&)> const& callback) const;
|
2022-02-17 09:34:36 -03:00
|
|
|
// Returns whether the match state of any media queries changed after evaluation.
|
2025-10-06 08:54:19 -03:00
|
|
|
bool evaluate_media_queries(DOM::Document const&);
|
2026-05-25 13:00:37 -03:00
|
|
|
bool evaluate_media_queries(DOM::Document const&, Function<void(CSSRule const&)> const& changed_rule_callback);
|
2023-05-26 17:00:54 -03:00
|
|
|
void for_each_effective_keyframes_at_rule(Function<void(CSSKeyframesRule const&)> const& callback) const;
|
2026-03-06 22:46:54 -03:00
|
|
|
void for_each_effective_counter_style_at_rule(Function<void(CSSCounterStyleRule const&)> const& callback) const;
|
2021-03-07 12:14:04 -03:00
|
|
|
|
2026-04-23 12:47:53 -03:00
|
|
|
HashTable<GC::Ptr<DOM::Node>> const& owning_documents_or_shadow_roots() const { return m_owning_documents_or_shadow_roots; }
|
2025-01-10 14:00:43 -03:00
|
|
|
void add_owning_document_or_shadow_root(DOM::Node& document_or_shadow_root);
|
|
|
|
|
void remove_owning_document_or_shadow_root(DOM::Node& document_or_shadow_root);
|
2026-04-22 17:34:46 -03:00
|
|
|
void invalidate_owners(DOM::StyleInvalidationReason, ShadowRootStylesheetEffects const* previous_sheet_effects = nullptr);
|
2025-04-10 12:01:17 -03:00
|
|
|
GC::Ptr<DOM::Document> owning_document() const;
|
2026-04-22 17:35:21 -03:00
|
|
|
virtual void set_disabled(bool) override;
|
2026-04-07 04:56:37 -03:00
|
|
|
void for_each_owning_style_scope(Function<void(StyleScope&)> const&) const;
|
2026-06-18 08:34:09 -03:00
|
|
|
NonnullRefPtr<StyleCache> shared_single_constructed_sheet_style_cache();
|
2026-05-23 08:16:30 -03:00
|
|
|
SelectorInsights const& selector_insights() const;
|
2022-03-09 15:57:15 -03:00
|
|
|
|
2023-11-24 03:14:24 -03:00
|
|
|
Optional<FlyString> default_namespace() const;
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<CSSNamespaceRule> default_namespace_rule() const { return m_default_namespace_rule; }
|
2025-06-23 07:40:37 -03:00
|
|
|
HashTable<FlyString> declared_namespaces() const;
|
2024-03-16 04:03:46 -03:00
|
|
|
|
2023-11-24 03:14:24 -03:00
|
|
|
Optional<FlyString> namespace_uri(StringView namespace_prefix) const;
|
2023-07-29 13:51:15 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
Vector<GC::Ref<CSSImportRule>> const& import_rules() const { return m_import_rules; }
|
2024-08-23 12:46:33 -03:00
|
|
|
|
2025-04-08 09:35:26 -03:00
|
|
|
Optional<::URL::URL> base_url() const { return m_base_url; }
|
|
|
|
|
void set_base_url(Optional<::URL::URL> base_url) { m_base_url = move(base_url); }
|
2024-02-24 04:46:59 -03:00
|
|
|
|
2026-03-19 07:22:18 -03:00
|
|
|
void register_pending_image_value(ImageStyleValue& value) { m_pending_image_values.append(value); }
|
|
|
|
|
void load_pending_image_resources(DOM::Document&);
|
|
|
|
|
|
2024-02-24 04:46:59 -03:00
|
|
|
bool constructed() const { return m_constructed; }
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<DOM::Document const> constructor_document() const { return m_constructor_document; }
|
|
|
|
|
void set_constructor_document(GC::Ptr<DOM::Document const> constructor_document) { m_constructor_document = constructor_document; }
|
2024-02-24 04:46:59 -03:00
|
|
|
|
2024-02-24 04:46:59 -03:00
|
|
|
bool disallow_modification() const { return m_disallow_modification; }
|
|
|
|
|
|
2026-06-04 12:59:14 -03:00
|
|
|
void set_source_text(String source) { m_source_text = move(source); }
|
|
|
|
|
Optional<String> source_text() const { return m_source_text; }
|
2024-08-22 13:23:54 -03:00
|
|
|
|
2026-02-05 14:40:15 -03:00
|
|
|
void add_critical_subresource(Subresource&);
|
|
|
|
|
void remove_critical_subresource(Subresource&);
|
|
|
|
|
LoadingState loading_state() const;
|
|
|
|
|
void check_if_loading_completed();
|
|
|
|
|
|
2021-03-07 12:14:04 -03:00
|
|
|
private:
|
2025-04-08 09:35:26 -03:00
|
|
|
CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional<::URL::URL> location);
|
2022-09-24 19:34:04 -03:00
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2022-08-07 08:29:49 -03:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
2026-05-06 03:36:34 -03:00
|
|
|
virtual size_t external_memory_size() const override;
|
2022-08-07 08:29:49 -03:00
|
|
|
|
2024-08-23 12:46:33 -03:00
|
|
|
void recalculate_rule_caches();
|
2026-04-28 05:27:31 -03:00
|
|
|
void invalidate_shared_style_cache();
|
2023-07-31 13:46:57 -03:00
|
|
|
|
2024-02-24 04:46:59 -03:00
|
|
|
void set_constructed(bool constructed) { m_constructed = constructed; }
|
2024-02-24 04:46:59 -03:00
|
|
|
void set_disallow_modification(bool disallow_modification) { m_disallow_modification = disallow_modification; }
|
2024-02-24 04:46:59 -03:00
|
|
|
|
2025-04-08 14:10:38 -03:00
|
|
|
Parser::ParsingParams make_parsing_params() const;
|
|
|
|
|
|
2024-08-22 13:23:54 -03:00
|
|
|
Optional<String> m_source_text;
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<CSSRuleList> m_rules;
|
|
|
|
|
GC::Ptr<CSSNamespaceRule> m_default_namespace_rule;
|
|
|
|
|
HashMap<FlyString, GC::Ptr<CSSNamespaceRule>> m_namespace_rules;
|
|
|
|
|
Vector<GC::Ref<CSSImportRule>> m_import_rules;
|
2021-09-29 18:46:16 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<CSSRule> m_owner_css_rule;
|
2024-02-24 04:46:59 -03:00
|
|
|
|
2025-04-08 09:35:26 -03:00
|
|
|
Optional<::URL::URL> m_base_url;
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<DOM::Document const> m_constructor_document;
|
2025-01-10 14:00:43 -03:00
|
|
|
HashTable<GC::Ptr<DOM::Node>> m_owning_documents_or_shadow_roots;
|
2024-02-24 04:46:59 -03:00
|
|
|
bool m_constructed { false };
|
2024-02-24 04:46:59 -03:00
|
|
|
bool m_disallow_modification { false };
|
2024-08-11 12:34:37 -03:00
|
|
|
Optional<bool> m_did_match;
|
2026-05-23 08:16:30 -03:00
|
|
|
mutable Optional<SelectorInsights> m_selector_insights;
|
2026-04-28 05:27:31 -03:00
|
|
|
RefPtr<StyleCache> m_shared_single_constructed_sheet_style_cache;
|
2024-09-22 13:10:46 -03:00
|
|
|
|
2026-02-05 14:40:15 -03:00
|
|
|
Vector<Subresource&> m_critical_subresources;
|
2026-03-19 07:22:18 -03:00
|
|
|
|
2026-05-24 23:29:10 -03:00
|
|
|
Vector<WeakPtr<ImageStyleValue>> m_pending_image_values;
|
2021-03-07 12:14:04 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|