mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 01:32:14 -05:00
ed7f4664c2
Before this change, StyleComputer would essentially take a DOM element, find all the CSS rules that apply to it, and resolve the computed value for each CSS property for that element. This worked great, but it meant we had to do all the work of selector matching and cascading every time. To enable new optimizations, this change introduces a break in the middle of this process where we've produced a "CascadedProperties". This object contains the result of the cascade, before we've begun turning cascaded values into computed values. The cascaded properties are now stored with each element, which will later allow us to do partial updates without re-running the full StyleComputer machine. This will be particularly valuable for re-implementing CSS inheritance, which is extremely heavy today. Note that CSS animations and CSS transitions operate entirely on the computed values, even though the cascade order would have you believe they happen earlier. I'm not confident we have the right architecture for this, but that's a separate issue.
141 lines
4.4 KiB
C++
141 lines
4.4 KiB
C++
/*
|
|
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibWeb/CSS/CSSStyleDeclaration.h>
|
|
#include <LibWeb/CSS/CascadedProperties.h>
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
|
|
|
namespace Web::CSS {
|
|
GC_DEFINE_ALLOCATOR(CascadedProperties);
|
|
|
|
CascadedProperties::CascadedProperties() = default;
|
|
|
|
CascadedProperties::~CascadedProperties() = default;
|
|
|
|
void CascadedProperties::visit_edges(Visitor& visitor)
|
|
{
|
|
Base::visit_edges(visitor);
|
|
for (auto const& [property_id, entries] : m_properties) {
|
|
for (auto const& entry : entries) {
|
|
visitor.visit(entry.source);
|
|
}
|
|
}
|
|
}
|
|
|
|
void CascadedProperties::revert_property(PropertyID property_id, Important important, CascadeOrigin cascade_origin)
|
|
{
|
|
auto it = m_properties.find(property_id);
|
|
if (it == m_properties.end())
|
|
return;
|
|
auto& entries = it->value;
|
|
entries.remove_all_matching([&](auto& entry) {
|
|
return entry.property.property_id == property_id
|
|
&& entry.property.important == important
|
|
&& cascade_origin == entry.origin;
|
|
});
|
|
if (entries.is_empty())
|
|
m_properties.remove(it);
|
|
}
|
|
|
|
void CascadedProperties::revert_layer_property(PropertyID property_id, Important important, Optional<FlyString> layer_name)
|
|
{
|
|
auto it = m_properties.find(property_id);
|
|
if (it == m_properties.end())
|
|
return;
|
|
auto& entries = it->value;
|
|
entries.remove_all_matching([&](auto& entry) {
|
|
return entry.property.property_id == property_id
|
|
&& entry.property.important == important
|
|
&& layer_name == entry.layer_name;
|
|
});
|
|
if (entries.is_empty())
|
|
m_properties.remove(it);
|
|
}
|
|
|
|
void CascadedProperties::resolve_unresolved_properties(GC::Ref<DOM::Element> element, Optional<Selector::PseudoElement::Type> pseudo_element)
|
|
{
|
|
for (auto& [property_id, entries] : m_properties) {
|
|
for (auto& entry : entries) {
|
|
if (!entry.property.value->is_unresolved())
|
|
continue;
|
|
entry.property.value = Parser::Parser::resolve_unresolved_style_value(Parser::ParsingContext { element->document() }, element, pseudo_element, property_id, entry.property.value->as_unresolved());
|
|
}
|
|
}
|
|
}
|
|
|
|
void CascadedProperties::set_property(PropertyID property_id, NonnullRefPtr<CSSStyleValue const> value, Important important, CascadeOrigin origin, Optional<FlyString> layer_name, GC::Ptr<CSS::CSSStyleDeclaration const> source)
|
|
{
|
|
auto& entries = m_properties.ensure(property_id);
|
|
|
|
for (auto& entry : entries.in_reverse()) {
|
|
if (entry.origin == origin && entry.layer_name == layer_name) {
|
|
if (entry.property.important == Important::Yes && important == Important::No)
|
|
return;
|
|
entry.property = StyleProperty {
|
|
.important = important,
|
|
.property_id = property_id,
|
|
.value = value,
|
|
};
|
|
return;
|
|
}
|
|
}
|
|
|
|
entries.append(Entry {
|
|
.property = StyleProperty {
|
|
.important = important,
|
|
.property_id = property_id,
|
|
.value = value,
|
|
},
|
|
.origin = origin,
|
|
.layer_name = move(layer_name),
|
|
.source = source,
|
|
});
|
|
}
|
|
|
|
void CascadedProperties::set_property_from_presentational_hint(PropertyID property_id, NonnullRefPtr<CSSStyleValue const> value)
|
|
{
|
|
auto& entries = m_properties.ensure(property_id);
|
|
|
|
entries.append(Entry {
|
|
.property = StyleProperty {
|
|
.important = Important::No,
|
|
.property_id = property_id,
|
|
.value = value,
|
|
},
|
|
.origin = CascadeOrigin::Author,
|
|
.layer_name = {},
|
|
.source = nullptr,
|
|
});
|
|
}
|
|
|
|
RefPtr<CSSStyleValue const> CascadedProperties::property(PropertyID property_id) const
|
|
{
|
|
auto it = m_properties.find(property_id);
|
|
if (it == m_properties.end())
|
|
return nullptr;
|
|
|
|
return it->value.last().property.value;
|
|
}
|
|
|
|
GC::Ptr<CSSStyleDeclaration const> CascadedProperties::property_source(PropertyID property_id) const
|
|
{
|
|
auto it = m_properties.find(property_id);
|
|
if (it == m_properties.end())
|
|
return nullptr;
|
|
|
|
return it->value.last().source;
|
|
}
|
|
|
|
bool CascadedProperties::is_property_important(PropertyID property_id) const
|
|
{
|
|
auto it = m_properties.find(property_id);
|
|
if (it == m_properties.end())
|
|
return false;
|
|
|
|
return it->value.last().property.important == Important::Yes;
|
|
}
|
|
|
|
}
|