StyleComputer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/Optional.h>
  10. #include <AK/OwnPtr.h>
  11. #include <LibWeb/Animations/KeyframeEffect.h>
  12. #include <LibWeb/CSS/CSSFontFaceRule.h>
  13. #include <LibWeb/CSS/CSSKeyframesRule.h>
  14. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  15. #include <LibWeb/CSS/Selector.h>
  16. #include <LibWeb/CSS/StyleProperties.h>
  17. #include <LibWeb/Forward.h>
  18. namespace Web::CSS {
  19. // https://www.w3.org/TR/css-cascade/#origin
  20. enum class CascadeOrigin : u8 {
  21. Author,
  22. User,
  23. UserAgent,
  24. Animation,
  25. Transition,
  26. };
  27. struct MatchingRule {
  28. JS::GCPtr<DOM::ShadowRoot const> shadow_root;
  29. JS::GCPtr<CSSStyleRule const> rule;
  30. JS::GCPtr<CSSStyleSheet const> sheet;
  31. size_t style_sheet_index { 0 };
  32. size_t rule_index { 0 };
  33. size_t selector_index { 0 };
  34. u32 specificity { 0 };
  35. CascadeOrigin cascade_origin;
  36. bool contains_pseudo_element { false };
  37. bool contains_root_pseudo_class { false };
  38. bool can_use_fast_matches { false };
  39. };
  40. struct FontFaceKey {
  41. FlyString family_name;
  42. int weight { 0 };
  43. int slope { 0 };
  44. [[nodiscard]] u32 hash() const { return pair_int_hash(family_name.hash(), pair_int_hash(weight, slope)); }
  45. [[nodiscard]] bool operator==(FontFaceKey const&) const = default;
  46. };
  47. class StyleComputer {
  48. public:
  49. enum class AllowUnresolved {
  50. Yes,
  51. No,
  52. };
  53. static void for_each_property_expanding_shorthands(PropertyID, StyleValue const&, AllowUnresolved, Function<void(PropertyID, StyleValue const&)> const& set_longhand_property);
  54. static void set_property_expanding_shorthands(StyleProperties&, PropertyID, StyleValue const&, CSS::CSSStyleDeclaration const*, StyleProperties::PropertyValues const& properties_for_revert, StyleProperties::Important = StyleProperties::Important::No);
  55. static NonnullRefPtr<StyleValue const> get_inherit_value(JS::Realm& initial_value_context_realm, CSS::PropertyID, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type> = {});
  56. explicit StyleComputer(DOM::Document&);
  57. ~StyleComputer();
  58. DOM::Document& document() { return m_document; }
  59. DOM::Document const& document() const { return m_document; }
  60. NonnullRefPtr<StyleProperties> create_document_style() const;
  61. NonnullRefPtr<StyleProperties> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type> = {}) const;
  62. RefPtr<StyleProperties> compute_pseudo_element_style_if_needed(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>) const;
  63. Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement::Type>) const;
  64. void invalidate_rule_cache();
  65. Gfx::Font const& initial_font() const;
  66. void did_load_font(FlyString const& family_name);
  67. void load_fonts_from_sheet(CSSStyleSheet const&);
  68. RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(DOM::Element const* element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch, int math_depth = 0) const;
  69. void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
  70. enum class AnimationRefresh {
  71. No,
  72. Yes,
  73. };
  74. void collect_animation_into(JS::NonnullGCPtr<Animations::KeyframeEffect> animation, StyleProperties& style_properties, AnimationRefresh = AnimationRefresh::No) const;
  75. private:
  76. enum class ComputeStyleMode {
  77. Normal,
  78. CreatePseudoElementStyleIfNeeded,
  79. };
  80. class FontLoader;
  81. struct MatchingFontCandidate;
  82. RefPtr<StyleProperties> compute_style_impl(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, ComputeStyleMode) const;
  83. void compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const;
  84. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  85. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  86. RefPtr<Gfx::FontCascadeList const> font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const;
  87. void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  88. void compute_math_depth(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  89. void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  90. void absolutize_values(StyleProperties&) const;
  91. void resolve_effective_overflow_values(StyleProperties&) const;
  92. void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement::Type>) const;
  93. void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement::Type>) const;
  94. void set_all_properties(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, StyleProperties&, StyleValue const&, DOM::Document&, CSS::CSSStyleDeclaration const*, StyleProperties::PropertyValues const& properties_for_revert, StyleProperties::Important = StyleProperties::Important::No) const;
  95. template<typename Callback>
  96. void for_each_stylesheet(CascadeOrigin, Callback) const;
  97. [[nodiscard]] CSSPixelRect viewport_rect() const { return m_viewport_rect; }
  98. [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const;
  99. struct MatchingRuleSet {
  100. Vector<MatchingRule> user_agent_rules;
  101. Vector<MatchingRule> user_rules;
  102. Vector<MatchingRule> author_rules;
  103. };
  104. void cascade_declarations(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, Vector<MatchingRule> const&, CascadeOrigin, Important) const;
  105. void build_rule_cache();
  106. void build_rule_cache_if_needed() const;
  107. JS::NonnullGCPtr<DOM::Document> m_document;
  108. struct RuleCache {
  109. HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
  110. HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
  111. HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
  112. HashMap<FlyString, Vector<MatchingRule>, AK::ASCIICaseInsensitiveFlyStringTraits> rules_by_attribute_name;
  113. Vector<MatchingRule> pseudo_element_rules;
  114. Vector<MatchingRule> root_rules;
  115. Vector<MatchingRule> other_rules;
  116. HashMap<FlyString, NonnullRefPtr<Animations::KeyframeEffect::KeyFrameSet>> rules_by_animation_keyframes;
  117. };
  118. NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin);
  119. RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const;
  120. OwnPtr<RuleCache> m_author_rule_cache;
  121. OwnPtr<RuleCache> m_user_rule_cache;
  122. OwnPtr<RuleCache> m_user_agent_rule_cache;
  123. JS::Handle<CSSStyleSheet> m_user_style_sheet;
  124. using FontLoaderList = Vector<NonnullOwnPtr<FontLoader>>;
  125. HashMap<FontFaceKey, FontLoaderList> m_loaded_fonts;
  126. Length::FontMetrics m_default_font_metrics;
  127. Length::FontMetrics m_root_element_font_metrics;
  128. CSSPixelRect m_viewport_rect;
  129. };
  130. }