2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-02-17 14:06:55 +00:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-27 15:47:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-12-03 20:00:31 +00:00
|
|
|
#include <AK/HashMap.h>
|
2022-02-24 15:54:12 +00:00
|
|
|
#include <AK/Optional.h>
|
2019-09-21 12:32:17 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2022-03-29 00:14:20 +00:00
|
|
|
#include <LibWeb/CSS/CSSFontFaceRule.h>
|
2021-05-24 21:02:58 +00:00
|
|
|
#include <LibWeb/CSS/CSSStyleDeclaration.h>
|
2022-03-31 10:43:07 +00:00
|
|
|
#include <LibWeb/CSS/Parser/ComponentValue.h>
|
2022-09-27 15:46:22 +00:00
|
|
|
#include <LibWeb/CSS/Parser/TokenStream.h>
|
2022-02-24 15:54:12 +00:00
|
|
|
#include <LibWeb/CSS/Selector.h>
|
2020-03-07 09:32:51 +00:00
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2020-07-26 17:37:56 +00:00
|
|
|
#include <LibWeb/Forward.h>
|
2019-06-27 15:47:59 +00:00
|
|
|
|
2020-07-26 18:01:35 +00:00
|
|
|
namespace Web::CSS {
|
2020-03-07 09:27:02 +00:00
|
|
|
|
2020-06-12 22:44:26 +00:00
|
|
|
struct MatchingRule {
|
2023-02-26 23:09:02 +00:00
|
|
|
JS::GCPtr<CSSStyleRule const> rule;
|
2020-06-12 22:44:26 +00:00
|
|
|
size_t style_sheet_index { 0 };
|
|
|
|
size_t rule_index { 0 };
|
|
|
|
size_t selector_index { 0 };
|
2021-05-24 21:01:24 +00:00
|
|
|
u32 specificity { 0 };
|
2023-03-09 18:27:23 +00:00
|
|
|
bool contains_pseudo_element { false };
|
2020-06-12 22:44:26 +00:00
|
|
|
};
|
|
|
|
|
2021-12-03 20:00:31 +00:00
|
|
|
class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> {
|
|
|
|
public:
|
2023-02-17 14:19:16 +00:00
|
|
|
static NonnullRefPtr<PropertyDependencyNode> create(String name)
|
2021-12-03 20:00:31 +00:00
|
|
|
{
|
|
|
|
return adopt_ref(*new PropertyDependencyNode(move(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_child(NonnullRefPtr<PropertyDependencyNode>);
|
|
|
|
bool has_cycles();
|
|
|
|
|
|
|
|
private:
|
2023-02-17 14:19:16 +00:00
|
|
|
explicit PropertyDependencyNode(String name);
|
2021-12-03 20:00:31 +00:00
|
|
|
|
2023-02-17 14:19:16 +00:00
|
|
|
String m_name;
|
2023-03-06 13:17:01 +00:00
|
|
|
Vector<NonnullRefPtr<PropertyDependencyNode>> m_children;
|
2021-12-03 20:00:31 +00:00
|
|
|
bool m_marked { false };
|
|
|
|
};
|
|
|
|
|
2021-09-24 11:49:57 +00:00
|
|
|
class StyleComputer {
|
2019-06-27 15:47:59 +00:00
|
|
|
public:
|
2021-09-24 11:49:57 +00:00
|
|
|
explicit StyleComputer(DOM::Document&);
|
2022-03-29 00:14:20 +00:00
|
|
|
~StyleComputer();
|
2019-06-27 15:47:59 +00:00
|
|
|
|
2020-07-26 17:37:56 +00:00
|
|
|
DOM::Document& document() { return m_document; }
|
2021-07-14 15:56:11 +00:00
|
|
|
DOM::Document const& document() const { return m_document; }
|
2019-06-27 15:47:59 +00:00
|
|
|
|
2021-09-23 17:48:41 +00:00
|
|
|
NonnullRefPtr<StyleProperties> create_document_style() const;
|
2023-03-14 15:36:20 +00:00
|
|
|
|
2022-12-06 06:08:20 +00:00
|
|
|
ErrorOr<NonnullRefPtr<StyleProperties>> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement> = {}) const;
|
2023-03-14 15:36:20 +00:00
|
|
|
ErrorOr<RefPtr<StyleProperties>> compute_pseudo_element_style_if_needed(DOM::Element&, Optional<CSS::Selector::PseudoElement>) const;
|
2019-06-27 15:47:59 +00:00
|
|
|
|
2021-09-21 09:38:18 +00:00
|
|
|
// https://www.w3.org/TR/css-cascade/#origin
|
|
|
|
enum class CascadeOrigin {
|
|
|
|
Author,
|
|
|
|
User,
|
|
|
|
UserAgent,
|
|
|
|
Animation,
|
|
|
|
Transition,
|
|
|
|
};
|
|
|
|
|
2022-02-24 15:54:12 +00:00
|
|
|
Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement>) const;
|
2019-06-27 18:40:21 +00:00
|
|
|
|
2022-02-10 16:49:50 +00:00
|
|
|
void invalidate_rule_cache();
|
|
|
|
|
2022-03-11 12:53:32 +00:00
|
|
|
Gfx::Font const& initial_font() const;
|
|
|
|
|
2023-02-17 14:06:55 +00:00
|
|
|
void did_load_font(FlyString const& family_name);
|
2022-03-29 00:14:20 +00:00
|
|
|
|
2022-04-08 19:27:35 +00:00
|
|
|
void load_fonts_from_sheet(CSSStyleSheet const&);
|
|
|
|
|
2019-06-27 15:47:59 +00:00
|
|
|
private:
|
2023-03-14 15:36:20 +00:00
|
|
|
enum class ComputeStyleMode {
|
|
|
|
Normal,
|
|
|
|
CreatePseudoElementStyleIfNeeded,
|
|
|
|
};
|
|
|
|
|
|
|
|
ErrorOr<RefPtr<StyleProperties>> compute_style_impl(DOM::Element&, Optional<CSS::Selector::PseudoElement>, ComputeStyleMode) const;
|
2023-03-14 17:45:24 +00:00
|
|
|
ErrorOr<void> compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement>, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const;
|
2022-02-24 15:54:12 +00:00
|
|
|
void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const;
|
|
|
|
void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const;
|
|
|
|
void absolutize_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const;
|
|
|
|
void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement>) const;
|
2021-09-23 11:13:51 +00:00
|
|
|
|
2022-02-24 15:54:12 +00:00
|
|
|
void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement>) const;
|
2021-09-21 09:38:18 +00:00
|
|
|
|
2022-03-13 16:07:14 +00:00
|
|
|
RefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&) const;
|
2023-02-17 14:19:16 +00:00
|
|
|
bool expand_variables(DOM::Element&, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const;
|
2022-11-02 19:19:03 +00:00
|
|
|
bool expand_unresolved_values(DOM::Element&, StringView property_name, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const;
|
2021-12-03 12:32:12 +00:00
|
|
|
|
2019-10-05 07:01:12 +00:00
|
|
|
template<typename Callback>
|
2021-09-21 09:38:18 +00:00
|
|
|
void for_each_stylesheet(CascadeOrigin, Callback) const;
|
|
|
|
|
2022-11-09 12:32:20 +00:00
|
|
|
CSSPixelRect viewport_rect() const;
|
2022-11-08 17:29:52 +00:00
|
|
|
CSSPixels root_element_font_size() const;
|
2022-03-19 17:08:52 +00:00
|
|
|
|
2021-09-21 09:38:18 +00:00
|
|
|
struct MatchingRuleSet {
|
|
|
|
Vector<MatchingRule> user_agent_rules;
|
|
|
|
Vector<MatchingRule> author_rules;
|
|
|
|
};
|
|
|
|
|
2022-03-13 16:07:14 +00:00
|
|
|
void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, Important important) const;
|
2019-10-05 07:01:12 +00:00
|
|
|
|
2022-02-10 16:49:50 +00:00
|
|
|
void build_rule_cache();
|
|
|
|
void build_rule_cache_if_needed() const;
|
|
|
|
|
2023-02-26 23:09:02 +00:00
|
|
|
JS::NonnullGCPtr<DOM::Document> m_document;
|
2022-02-10 16:49:50 +00:00
|
|
|
|
|
|
|
struct RuleCache {
|
2023-02-17 14:19:16 +00:00
|
|
|
HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
|
|
|
|
HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
|
|
|
|
HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
|
2022-02-10 16:49:50 +00:00
|
|
|
Vector<MatchingRule> other_rules;
|
|
|
|
};
|
2023-03-07 19:13:13 +00:00
|
|
|
|
|
|
|
NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin);
|
|
|
|
|
|
|
|
RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const;
|
|
|
|
|
|
|
|
OwnPtr<RuleCache> m_author_rule_cache;
|
|
|
|
OwnPtr<RuleCache> m_user_agent_rule_cache;
|
2022-03-29 00:14:20 +00:00
|
|
|
|
|
|
|
class FontLoader;
|
2023-02-17 14:06:55 +00:00
|
|
|
HashMap<String, NonnullOwnPtr<FontLoader>> m_loaded_fonts;
|
2019-06-27 15:47:59 +00:00
|
|
|
};
|
2020-03-07 09:27:02 +00:00
|
|
|
|
|
|
|
}
|