/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace Web::CSS { struct MatchingRule { CSSStyleRule const* rule { nullptr }; size_t style_sheet_index { 0 }; size_t rule_index { 0 }; size_t selector_index { 0 }; u32 specificity { 0 }; }; class PropertyDependencyNode : public RefCounted { public: static NonnullRefPtr create(String name) { return adopt_ref(*new PropertyDependencyNode(move(name))); } void add_child(NonnullRefPtr); bool has_cycles(); private: explicit PropertyDependencyNode(String name); String m_name; NonnullRefPtrVector m_children; bool m_marked { false }; }; class StyleComputer { public: explicit StyleComputer(DOM::Document&); ~StyleComputer(); DOM::Document& document() { return m_document; } DOM::Document const& document() const { return m_document; } NonnullRefPtr create_document_style() const; NonnullRefPtr compute_style(DOM::Element&, Optional = {}) const; // https://www.w3.org/TR/css-cascade/#origin enum class CascadeOrigin { Author, User, UserAgent, Animation, Transition, }; Vector collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional) const; void invalidate_rule_cache(); Gfx::Font const& initial_font() const; void did_load_font(FlyString const& family_name); void load_fonts_from_sheet(CSSStyleSheet const&); private: void compute_cascaded_values(StyleProperties&, DOM::Element&, Optional) const; void compute_font(StyleProperties&, DOM::Element const*, Optional) const; void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional) const; void absolutize_values(StyleProperties&, DOM::Element const*, Optional) const; void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional) const; void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional) const; RefPtr resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&) const; bool expand_unresolved_values(DOM::Element&, StringView property_name, HashMap>& dependencies, Vector const& source, Vector& dest, size_t source_start_index) const; template void for_each_stylesheet(CascadeOrigin, Callback) const; Gfx::IntRect viewport_rect() const; float root_element_font_size() const; struct MatchingRuleSet { Vector user_agent_rules; Vector author_rules; }; void cascade_declarations(StyleProperties&, DOM::Element&, Vector const&, CascadeOrigin, Important important) const; void build_rule_cache(); void build_rule_cache_if_needed() const; DOM::Document& m_document; struct RuleCache { HashMap> rules_by_id; HashMap> rules_by_class; HashMap> rules_by_tag_name; HashMap> rules_by_pseudo_element; Vector other_rules; }; OwnPtr m_rule_cache; class FontLoader; HashMap> m_loaded_fonts; }; }