LibWeb: Split StyleComputer work into two phases with separate outputs
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.
2024-12-12 10:06:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <LibGC/CellAllocator.h>
|
|
|
|
#include <LibJS/Heap/Cell.h>
|
|
|
|
#include <LibWeb/CSS/CascadeOrigin.h>
|
|
|
|
#include <LibWeb/CSS/PropertyID.h>
|
|
|
|
#include <LibWeb/CSS/Selector.h>
|
|
|
|
#include <LibWeb/CSS/StyleProperty.h>
|
2025-08-08 10:11:51 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/StyleValue.h>
|
LibWeb: Split StyleComputer work into two phases with separate outputs
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.
2024-12-12 10:06:29 +01:00
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
|
|
class CascadedProperties final : public JS::Cell {
|
|
|
|
GC_CELL(CascadedProperties, JS::Cell);
|
|
|
|
GC_DECLARE_ALLOCATOR(CascadedProperties);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~CascadedProperties() override;
|
|
|
|
|
2025-08-08 10:11:51 +01:00
|
|
|
[[nodiscard]] RefPtr<StyleValue const> property(PropertyID) const;
|
LibWeb: Split StyleComputer work into two phases with separate outputs
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.
2024-12-12 10:06:29 +01:00
|
|
|
[[nodiscard]] GC::Ptr<CSSStyleDeclaration const> property_source(PropertyID) const;
|
|
|
|
[[nodiscard]] bool is_property_important(PropertyID) const;
|
|
|
|
|
2025-08-08 10:11:51 +01:00
|
|
|
void set_property(PropertyID, NonnullRefPtr<StyleValue const>, Important, CascadeOrigin, Optional<FlyString> layer_name, GC::Ptr<CSS::CSSStyleDeclaration const> source);
|
|
|
|
void set_property_from_presentational_hint(PropertyID, NonnullRefPtr<StyleValue const>);
|
LibWeb: Split StyleComputer work into two phases with separate outputs
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.
2024-12-12 10:06:29 +01:00
|
|
|
|
|
|
|
void revert_property(PropertyID, Important, CascadeOrigin);
|
|
|
|
void revert_layer_property(PropertyID, Important, Optional<FlyString> layer_name);
|
|
|
|
|
2025-09-05 14:07:53 +01:00
|
|
|
void resolve_unresolved_properties(DOM::AbstractElement);
|
LibWeb: Split StyleComputer work into two phases with separate outputs
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.
2024-12-12 10:06:29 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
CascadedProperties();
|
|
|
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
|
|
struct Entry {
|
|
|
|
StyleProperty property;
|
|
|
|
CascadeOrigin origin;
|
|
|
|
Optional<FlyString> layer_name;
|
|
|
|
GC::Ptr<CSS::CSSStyleDeclaration const> source;
|
|
|
|
};
|
|
|
|
HashMap<PropertyID, Vector<Entry>> m_properties;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|