2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-12-03 17:00:31 -03:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-27 12:47:59 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-12-03 17:00:31 -03:00
|
|
|
#include <AK/HashMap.h>
|
2019-06-27 12:47:59 -03:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-09-21 09:32:17 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2021-05-24 18:02:58 -03:00
|
|
|
#include <LibWeb/CSS/CSSStyleDeclaration.h>
|
2021-12-03 09:32:12 -03:00
|
|
|
#include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2020-07-26 14:37:56 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
2019-06-27 12:47:59 -03:00
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
namespace Web::CSS {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2020-06-12 19:44:26 -03:00
|
|
|
struct MatchingRule {
|
2021-03-07 11:00:02 -03:00
|
|
|
RefPtr<CSSStyleRule> rule;
|
2020-06-12 19:44:26 -03:00
|
|
|
size_t style_sheet_index { 0 };
|
|
|
|
|
size_t rule_index { 0 };
|
|
|
|
|
size_t selector_index { 0 };
|
2021-05-24 18:01:24 -03:00
|
|
|
u32 specificity { 0 };
|
2020-06-12 19:44:26 -03:00
|
|
|
};
|
|
|
|
|
|
2021-12-03 17:00:31 -03:00
|
|
|
class PropertyDependencyNode : public RefCounted<PropertyDependencyNode> {
|
|
|
|
|
public:
|
|
|
|
|
static NonnullRefPtr<PropertyDependencyNode> create(String name)
|
|
|
|
|
{
|
|
|
|
|
return adopt_ref(*new PropertyDependencyNode(move(name)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void add_child(NonnullRefPtr<PropertyDependencyNode>);
|
|
|
|
|
bool has_cycles();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
explicit PropertyDependencyNode(String name);
|
|
|
|
|
|
|
|
|
|
String m_name;
|
|
|
|
|
NonnullRefPtrVector<PropertyDependencyNode> m_children;
|
|
|
|
|
bool m_marked { false };
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-24 08:49:57 -03:00
|
|
|
class StyleComputer {
|
2019-06-27 12:47:59 -03:00
|
|
|
public:
|
2021-09-24 08:49:57 -03:00
|
|
|
explicit StyleComputer(DOM::Document&);
|
|
|
|
|
~StyleComputer();
|
2019-06-27 12:47:59 -03:00
|
|
|
|
2020-07-26 14:37:56 -03:00
|
|
|
DOM::Document& document() { return m_document; }
|
2021-07-14 12:56:11 -03:00
|
|
|
DOM::Document const& document() const { return m_document; }
|
2019-06-27 12:47:59 -03:00
|
|
|
|
2021-09-23 14:48:41 -03:00
|
|
|
NonnullRefPtr<StyleProperties> create_document_style() const;
|
2021-09-24 08:49:57 -03:00
|
|
|
NonnullRefPtr<StyleProperties> compute_style(DOM::Element&) const;
|
2019-06-27 12:47:59 -03:00
|
|
|
|
2021-09-21 06:38:18 -03:00
|
|
|
// https://www.w3.org/TR/css-cascade/#origin
|
|
|
|
|
enum class CascadeOrigin {
|
|
|
|
|
Any, // FIXME: This is not part of the spec. Get rid of it.
|
|
|
|
|
Author,
|
|
|
|
|
User,
|
|
|
|
|
UserAgent,
|
|
|
|
|
Animation,
|
|
|
|
|
Transition,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin = CascadeOrigin::Any) const;
|
2020-12-14 18:31:10 -03:00
|
|
|
void sort_matching_rules(Vector<MatchingRule>&) const;
|
2021-05-24 18:02:58 -03:00
|
|
|
struct CustomPropertyResolutionTuple {
|
|
|
|
|
Optional<StyleProperty> style {};
|
|
|
|
|
u32 specificity { 0 };
|
|
|
|
|
};
|
2021-07-14 12:56:11 -03:00
|
|
|
CustomPropertyResolutionTuple resolve_custom_property_with_specificity(DOM::Element&, String const&) const;
|
|
|
|
|
Optional<StyleProperty> resolve_custom_property(DOM::Element&, String const&) const;
|
2019-06-27 15:40:21 -03:00
|
|
|
|
2019-06-27 12:47:59 -03:00
|
|
|
private:
|
2021-09-21 06:38:18 -03:00
|
|
|
void compute_cascaded_values(StyleProperties&, DOM::Element&) const;
|
2021-09-23 14:48:41 -03:00
|
|
|
void compute_font(StyleProperties&, DOM::Element const*) const;
|
|
|
|
|
void compute_defaulted_values(StyleProperties&, DOM::Element const*) const;
|
|
|
|
|
void absolutize_values(StyleProperties&, DOM::Element const*) const;
|
2022-01-24 10:41:48 -03:00
|
|
|
void transform_box_type_if_needed(StyleProperties&, DOM::Element const&) const;
|
2021-09-23 08:13:51 -03:00
|
|
|
|
2021-09-23 14:48:41 -03:00
|
|
|
void compute_defaulted_property_value(StyleProperties&, DOM::Element const*, CSS::PropertyID) const;
|
2021-09-21 06:38:18 -03:00
|
|
|
|
2021-12-03 09:32:12 -03:00
|
|
|
RefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, PropertyID, UnresolvedStyleValue const&) const;
|
2021-12-03 17:00:31 -03:00
|
|
|
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 = 0) const;
|
2021-12-03 09:32:12 -03:00
|
|
|
|
2019-10-05 04:01:12 -03:00
|
|
|
template<typename Callback>
|
2021-09-21 06:38:18 -03:00
|
|
|
void for_each_stylesheet(CascadeOrigin, Callback) const;
|
|
|
|
|
|
|
|
|
|
struct MatchingRuleSet {
|
|
|
|
|
Vector<MatchingRule> user_agent_rules;
|
|
|
|
|
Vector<MatchingRule> author_rules;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, bool important) const;
|
2019-10-05 04:01:12 -03:00
|
|
|
|
2020-07-26 14:37:56 -03:00
|
|
|
DOM::Document& m_document;
|
2019-06-27 12:47:59 -03:00
|
|
|
};
|
2020-03-07 06:27:02 -03:00
|
|
|
|
|
|
|
|
}
|