Serenity Operating System
at master 129 lines 4.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <AK/HashMap.h> 11#include <AK/Optional.h> 12#include <AK/OwnPtr.h> 13#include <LibWeb/CSS/CSSFontFaceRule.h> 14#include <LibWeb/CSS/CSSStyleDeclaration.h> 15#include <LibWeb/CSS/Parser/ComponentValue.h> 16#include <LibWeb/CSS/Parser/TokenStream.h> 17#include <LibWeb/CSS/Selector.h> 18#include <LibWeb/CSS/StyleProperties.h> 19#include <LibWeb/Forward.h> 20 21namespace Web::CSS { 22 23struct MatchingRule { 24 CSSStyleRule const* rule { nullptr }; 25 size_t style_sheet_index { 0 }; 26 size_t rule_index { 0 }; 27 size_t selector_index { 0 }; 28 u32 specificity { 0 }; 29 bool contains_pseudo_element { false }; 30}; 31 32class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> { 33public: 34 static NonnullRefPtr<PropertyDependencyNode> create(String name) 35 { 36 return adopt_ref(*new PropertyDependencyNode(move(name))); 37 } 38 39 void add_child(NonnullRefPtr<PropertyDependencyNode>); 40 bool has_cycles(); 41 42private: 43 explicit PropertyDependencyNode(String name); 44 45 String m_name; 46 Vector<NonnullRefPtr<PropertyDependencyNode>> m_children; 47 bool m_marked { false }; 48}; 49 50class StyleComputer { 51public: 52 explicit StyleComputer(DOM::Document&); 53 ~StyleComputer(); 54 55 DOM::Document& document() { return m_document; } 56 DOM::Document const& document() const { return m_document; } 57 58 NonnullRefPtr<StyleProperties> create_document_style() const; 59 ErrorOr<NonnullRefPtr<StyleProperties>> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement> = {}) const; 60 61 // https://www.w3.org/TR/css-cascade/#origin 62 enum class CascadeOrigin { 63 Author, 64 User, 65 UserAgent, 66 Animation, 67 Transition, 68 }; 69 70 Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement>) const; 71 72 void invalidate_rule_cache(); 73 74 Gfx::Font const& initial_font() const; 75 76 void did_load_font(FlyString const& family_name); 77 78 void load_fonts_from_sheet(CSSStyleSheet const&); 79 80private: 81 ErrorOr<void> compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement>) const; 82 void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const; 83 void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const; 84 void absolutize_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const; 85 void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement>) const; 86 87 void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement>) const; 88 89 RefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&) const; 90 bool expand_variables(DOM::Element&, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const; 91 bool expand_unresolved_values(DOM::Element&, StringView property_name, Parser::TokenStream<Parser::ComponentValue>& source, Vector<Parser::ComponentValue>& dest) const; 92 93 template<typename Callback> 94 void for_each_stylesheet(CascadeOrigin, Callback) const; 95 96 CSSPixelRect viewport_rect() const; 97 CSSPixels root_element_font_size() const; 98 99 struct MatchingRuleSet { 100 Vector<MatchingRule> user_agent_rules; 101 Vector<MatchingRule> author_rules; 102 }; 103 104 void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, Important important) const; 105 106 void build_rule_cache(); 107 void build_rule_cache_if_needed() const; 108 109 DOM::Document& m_document; 110 111 struct RuleCache { 112 HashMap<FlyString, Vector<MatchingRule>> rules_by_id; 113 HashMap<FlyString, Vector<MatchingRule>> rules_by_class; 114 HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name; 115 Vector<MatchingRule> other_rules; 116 }; 117 118 NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin); 119 120 RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const; 121 122 OwnPtr<RuleCache> m_author_rule_cache; 123 OwnPtr<RuleCache> m_user_agent_rule_cache; 124 125 class FontLoader; 126 HashMap<String, NonnullOwnPtr<FontLoader>> m_loaded_fonts; 127}; 128 129}