StyleComputer.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2018-2024, 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. // A counting bloom filter with 2 hash functions.
  20. // NOTE: If a counter overflows, it's kept maxed-out until the whole filter is cleared.
  21. template<typename CounterType, size_t key_bits>
  22. class CountingBloomFilter {
  23. public:
  24. CountingBloomFilter() { }
  25. void clear() { __builtin_memset(m_buckets, 0, sizeof(m_buckets)); }
  26. void increment(u32 key)
  27. {
  28. auto& first = bucket1(key);
  29. if (first < NumericLimits<CounterType>::max())
  30. ++first;
  31. auto& second = bucket2(key);
  32. if (second < NumericLimits<CounterType>::max())
  33. ++second;
  34. }
  35. void decrement(u32 key)
  36. {
  37. auto& first = bucket1(key);
  38. if (first < NumericLimits<CounterType>::max())
  39. --first;
  40. auto& second = bucket2(key);
  41. if (second < NumericLimits<CounterType>::max())
  42. --second;
  43. }
  44. [[nodiscard]] bool may_contain(u32 hash) const
  45. {
  46. return bucket1(hash) && bucket2(hash);
  47. }
  48. private:
  49. static constexpr u32 bucket_count = 1 << key_bits;
  50. static constexpr u32 key_mask = bucket_count - 1;
  51. [[nodiscard]] u32 hash1(u32 key) const { return key & key_mask; }
  52. [[nodiscard]] u32 hash2(u32 key) const { return (key >> 16) & key_mask; }
  53. [[nodiscard]] CounterType& bucket1(u32 key) { return m_buckets[hash1(key)]; }
  54. [[nodiscard]] CounterType& bucket2(u32 key) { return m_buckets[hash2(key)]; }
  55. [[nodiscard]] CounterType bucket1(u32 key) const { return m_buckets[hash1(key)]; }
  56. [[nodiscard]] CounterType bucket2(u32 key) const { return m_buckets[hash2(key)]; }
  57. CounterType m_buckets[bucket_count];
  58. };
  59. // https://www.w3.org/TR/css-cascade/#origin
  60. enum class CascadeOrigin : u8 {
  61. Author,
  62. User,
  63. UserAgent,
  64. Animation,
  65. Transition,
  66. };
  67. struct MatchingRule {
  68. JS::GCPtr<DOM::ShadowRoot const> shadow_root;
  69. JS::GCPtr<CSSStyleRule const> rule;
  70. JS::GCPtr<CSSStyleSheet const> sheet;
  71. size_t style_sheet_index { 0 };
  72. size_t rule_index { 0 };
  73. size_t selector_index { 0 };
  74. u32 specificity { 0 };
  75. CascadeOrigin cascade_origin;
  76. bool contains_pseudo_element { false };
  77. bool contains_root_pseudo_class { false };
  78. bool can_use_fast_matches { false };
  79. };
  80. struct FontFaceKey {
  81. FlyString family_name;
  82. int weight { 0 };
  83. int slope { 0 };
  84. [[nodiscard]] u32 hash() const { return pair_int_hash(family_name.hash(), pair_int_hash(weight, slope)); }
  85. [[nodiscard]] bool operator==(FontFaceKey const&) const = default;
  86. };
  87. class StyleComputer {
  88. public:
  89. enum class AllowUnresolved {
  90. Yes,
  91. No,
  92. };
  93. static void for_each_property_expanding_shorthands(PropertyID, StyleValue const&, AllowUnresolved, Function<void(PropertyID, StyleValue const&)> const& set_longhand_property);
  94. static void set_property_expanding_shorthands(StyleProperties&, PropertyID, StyleValue const&, CSS::CSSStyleDeclaration const*, StyleProperties::PropertyValues const& properties_for_revert, StyleProperties::Important = StyleProperties::Important::No);
  95. static NonnullRefPtr<StyleValue const> get_inherit_value(JS::Realm& initial_value_context_realm, CSS::PropertyID, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type> = {});
  96. explicit StyleComputer(DOM::Document&);
  97. ~StyleComputer();
  98. DOM::Document& document() { return m_document; }
  99. DOM::Document const& document() const { return m_document; }
  100. void reset_ancestor_filter();
  101. void push_ancestor(DOM::Element const&);
  102. void pop_ancestor(DOM::Element const&);
  103. NonnullRefPtr<StyleProperties> create_document_style() const;
  104. NonnullRefPtr<StyleProperties> compute_style(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type> = {}) const;
  105. RefPtr<StyleProperties> compute_pseudo_element_style_if_needed(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>) const;
  106. Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin, Optional<CSS::Selector::PseudoElement::Type>) const;
  107. void invalidate_rule_cache();
  108. Gfx::Font const& initial_font() const;
  109. void did_load_font(FlyString const& family_name);
  110. void load_fonts_from_sheet(CSSStyleSheet const&);
  111. 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;
  112. void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
  113. enum class AnimationRefresh {
  114. No,
  115. Yes,
  116. };
  117. void collect_animation_into(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, JS::NonnullGCPtr<Animations::KeyframeEffect> animation, StyleProperties& style_properties, AnimationRefresh = AnimationRefresh::No) const;
  118. private:
  119. enum class ComputeStyleMode {
  120. Normal,
  121. CreatePseudoElementStyleIfNeeded,
  122. };
  123. class FontLoader;
  124. struct MatchingFontCandidate;
  125. [[nodiscard]] bool should_reject_with_ancestor_filter(Selector const&) const;
  126. RefPtr<StyleProperties> compute_style_impl(DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, ComputeStyleMode) const;
  127. void compute_cascaded_values(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, bool& did_match_any_pseudo_element_rules, ComputeStyleMode) const;
  128. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_ascending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  129. static RefPtr<Gfx::FontCascadeList const> find_matching_font_weight_descending(Vector<MatchingFontCandidate> const& candidates, int target_weight, float font_size_in_pt, bool inclusive);
  130. RefPtr<Gfx::FontCascadeList const> font_matching_algorithm(FontFaceKey const& key, float font_size_in_pt) const;
  131. void compute_font(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  132. void compute_math_depth(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  133. void compute_defaulted_values(StyleProperties&, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type>) const;
  134. void absolutize_values(StyleProperties&) const;
  135. void resolve_effective_overflow_values(StyleProperties&) const;
  136. void transform_box_type_if_needed(StyleProperties&, DOM::Element const&, Optional<CSS::Selector::PseudoElement::Type>) const;
  137. void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID, Optional<CSS::Selector::PseudoElement::Type>) const;
  138. 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;
  139. template<typename Callback>
  140. void for_each_stylesheet(CascadeOrigin, Callback) const;
  141. [[nodiscard]] CSSPixelRect viewport_rect() const { return m_viewport_rect; }
  142. [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const;
  143. struct MatchingRuleSet {
  144. Vector<MatchingRule> user_agent_rules;
  145. Vector<MatchingRule> user_rules;
  146. Vector<MatchingRule> author_rules;
  147. };
  148. void cascade_declarations(StyleProperties&, DOM::Element&, Optional<CSS::Selector::PseudoElement::Type>, Vector<MatchingRule> const&, CascadeOrigin, Important) const;
  149. void build_rule_cache();
  150. void build_rule_cache_if_needed() const;
  151. JS::NonnullGCPtr<DOM::Document> m_document;
  152. struct RuleCache {
  153. HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
  154. HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
  155. HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
  156. HashMap<FlyString, Vector<MatchingRule>, AK::ASCIICaseInsensitiveFlyStringTraits> rules_by_attribute_name;
  157. Vector<MatchingRule> pseudo_element_rules;
  158. Vector<MatchingRule> root_rules;
  159. Vector<MatchingRule> other_rules;
  160. HashMap<FlyString, NonnullRefPtr<Animations::KeyframeEffect::KeyFrameSet>> rules_by_animation_keyframes;
  161. };
  162. NonnullOwnPtr<RuleCache> make_rule_cache_for_cascade_origin(CascadeOrigin);
  163. RuleCache const& rule_cache_for_cascade_origin(CascadeOrigin) const;
  164. OwnPtr<RuleCache> m_author_rule_cache;
  165. OwnPtr<RuleCache> m_user_rule_cache;
  166. OwnPtr<RuleCache> m_user_agent_rule_cache;
  167. JS::Handle<CSSStyleSheet> m_user_style_sheet;
  168. using FontLoaderList = Vector<NonnullOwnPtr<FontLoader>>;
  169. HashMap<FontFaceKey, FontLoaderList> m_loaded_fonts;
  170. Length::FontMetrics m_default_font_metrics;
  171. Length::FontMetrics m_root_element_font_metrics;
  172. CSSPixelRect m_viewport_rect;
  173. CountingBloomFilter<u8, 14> m_ancestor_filter;
  174. };
  175. }