StyleComputer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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/NonnullRefPtrVector.h>
  10. #include <AK/OwnPtr.h>
  11. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  12. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  13. #include <LibWeb/CSS/StyleProperties.h>
  14. #include <LibWeb/Forward.h>
  15. namespace Web::CSS {
  16. struct MatchingRule {
  17. RefPtr<CSSStyleRule> rule;
  18. size_t style_sheet_index { 0 };
  19. size_t rule_index { 0 };
  20. size_t selector_index { 0 };
  21. u32 specificity { 0 };
  22. };
  23. class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> {
  24. public:
  25. static NonnullRefPtr<PropertyDependencyNode> create(String name)
  26. {
  27. return adopt_ref(*new PropertyDependencyNode(move(name)));
  28. }
  29. void add_child(NonnullRefPtr<PropertyDependencyNode>);
  30. bool has_cycles();
  31. private:
  32. explicit PropertyDependencyNode(String name);
  33. String m_name;
  34. NonnullRefPtrVector<PropertyDependencyNode> m_children;
  35. bool m_marked { false };
  36. };
  37. class StyleComputer {
  38. public:
  39. explicit StyleComputer(DOM::Document&);
  40. ~StyleComputer();
  41. DOM::Document& document() { return m_document; }
  42. DOM::Document const& document() const { return m_document; }
  43. NonnullRefPtr<StyleProperties> create_document_style() const;
  44. NonnullRefPtr<StyleProperties> compute_style(DOM::Element&) const;
  45. // https://www.w3.org/TR/css-cascade/#origin
  46. enum class CascadeOrigin {
  47. Author,
  48. User,
  49. UserAgent,
  50. Animation,
  51. Transition,
  52. };
  53. Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin) const;
  54. void invalidate_rule_cache();
  55. private:
  56. void compute_cascaded_values(StyleProperties&, DOM::Element&) const;
  57. void compute_font(StyleProperties&, DOM::Element const*) const;
  58. void compute_defaulted_values(StyleProperties&, DOM::Element const*) const;
  59. void absolutize_values(StyleProperties&, DOM::Element const*) const;
  60. void transform_box_type_if_needed(StyleProperties&, DOM::Element const&) const;
  61. void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID) const;
  62. RefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&, HashMap<String, StyleProperty const*> const&) const;
  63. bool expand_unresolved_values(DOM::Element&, StringView property_name, HashMap<String, NonnullRefPtr<PropertyDependencyNode>>& dependencies, Vector<StyleComponentValueRule> const& source, Vector<StyleComponentValueRule>& dest, size_t source_start_index, HashMap<String, StyleProperty const*> const& custom_properties) const;
  64. template<typename Callback>
  65. void for_each_stylesheet(CascadeOrigin, Callback) const;
  66. struct MatchingRuleSet {
  67. Vector<MatchingRule> user_agent_rules;
  68. Vector<MatchingRule> author_rules;
  69. };
  70. void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, Important important, HashMap<String, StyleProperty const*> const&) const;
  71. void build_rule_cache();
  72. void build_rule_cache_if_needed() const;
  73. DOM::Document& m_document;
  74. struct RuleCache {
  75. HashMap<FlyString, Vector<MatchingRule>> rules_by_id;
  76. HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
  77. HashMap<FlyString, Vector<MatchingRule>> rules_by_tag_name;
  78. Vector<MatchingRule> other_rules;
  79. int generation { 0 };
  80. };
  81. OwnPtr<RuleCache> m_rule_cache;
  82. };
  83. }