Skip to content

Truly independent web browser

LibWeb: Transition custom properties

Andreas Klingdb890c1b762e
diff --git a/Libraries/LibWeb/Animations/Animatable.cpp b/Libraries/LibWeb/Animations/Animatable.cpp
index e5edcbb8c5d..3b7d690dd02 100644
--- a/Libraries/LibWeb/Animations/Animatable.cpp
+++ b/Libraries/LibWeb/Animations/Animatable.cpp
@@ -24,7 +24,7 @@ namespace Web::Animations {
struct Animatable::Transition {
AK_ALLOC_WITH_KMALLOC;
- HashMap<CSS::PropertyID, GC::Ref<CSS::CSSTransition>> associated_transitions;
+ HashMap<CSS::PropertyNameAndID, GC::Ref<CSS::CSSTransition>> associated_transitions;
};
Animatable::Impl::~Impl() = default;
@@ -324,13 +324,13 @@ bool Animatable::has_existing_transitions(Optional<CSS::PseudoElement> pseudo_el
return transition && !transition->associated_transitions.is_empty();
}
-HashMap<CSS::PropertyID, GC::Ref<CSS::CSSTransition>> const* Animatable::existing_transitions(Optional<CSS::PseudoElement> pseudo_element) const
+HashMap<CSS::PropertyNameAndID, GC::Ref<CSS::CSSTransition>> const* Animatable::existing_transitions(Optional<CSS::PseudoElement> pseudo_element) const
{
auto const* transition = transition_if_exists(pseudo_element);
return transition ? &transition->associated_transitions : nullptr;
}
-GC::Ptr<CSS::CSSTransition> Animatable::property_transition(Optional<CSS::PseudoElement> pseudo_element, CSS::PropertyID property) const
+GC::Ptr<CSS::CSSTransition> Animatable::property_transition(Optional<CSS::PseudoElement> pseudo_element, CSS::PropertyNameAndID const& property) const
{
auto const* maybe_transition = transition_if_exists(pseudo_element);
if (!maybe_transition)
@@ -341,7 +341,7 @@ GC::Ptr<CSS::CSSTransition> Animatable::property_transition(Optional<CSS::Pseudo
return {};
}
-void Animatable::set_transition(Optional<CSS::PseudoElement> pseudo_element, CSS::PropertyID property, GC::Ref<CSS::CSSTransition> animation)
+void Animatable::set_transition(Optional<CSS::PseudoElement> pseudo_element, CSS::PropertyNameAndID const& property, GC::Ref<CSS::CSSTransition> animation)
{
auto maybe_transition = ensure_transition(pseudo_element);
if (!maybe_transition)
@@ -351,15 +351,15 @@ void Animatable::set_transition(Optional<CSS::PseudoElement> pseudo_element, CSS
transition.associated_transitions.set(property, animation);
}
-void Animatable::remove_transition(Optional<CSS::PseudoElement> pseudo_element, CSS::PropertyID property_id)
+void Animatable::remove_transition(Optional<CSS::PseudoElement> pseudo_element, CSS::PropertyNameAndID const& property)
{
auto maybe_transition = ensure_transition(pseudo_element);
if (!maybe_transition)
return;
auto& transition = *maybe_transition;
- auto removed_transition = transition.associated_transitions.get(property_id);
+ auto removed_transition = transition.associated_transitions.get(property);
VERIFY(removed_transition.has_value());
- transition.associated_transitions.remove(property_id);
+ transition.associated_transitions.remove(property);
removed_transition.value()->schedule_disassociation_from_target();
}
diff --git a/Libraries/LibWeb/Animations/Animatable.h b/Libraries/LibWeb/Animations/Animatable.h
index 56fb34948ff..3f530f80160 100644
--- a/Libraries/LibWeb/Animations/Animatable.h
+++ b/Libraries/LibWeb/Animations/Animatable.h
@@ -14,6 +14,7 @@
#include <LibGC/RootVector.h>
#include <LibWeb/Animations/KeyframeEffect.h>
#include <LibWeb/Bindings/Animatable.h>
+#include <LibWeb/CSS/PropertyNameAndID.h>
#include <LibWeb/Export.h>
namespace Web::CSS {
@@ -67,12 +68,12 @@ public:
// Whether the installed style gives any longhand a matching transition-property entry, answered without listing them.
bool has_matching_transition_property_entry(Optional<CSS::PseudoElement>) const;
- void set_transition(Optional<CSS::PseudoElement>, CSS::PropertyID, GC::Ref<CSS::CSSTransition>);
- void remove_transition(Optional<CSS::PseudoElement>, CSS::PropertyID);
+ void set_transition(Optional<CSS::PseudoElement>, CSS::PropertyNameAndID const&, GC::Ref<CSS::CSSTransition>);
+ void remove_transition(Optional<CSS::PseudoElement>, CSS::PropertyNameAndID const&);
bool has_existing_transitions(Optional<CSS::PseudoElement>) const;
// The running or completed transition for each property, if the element holds any.
- HashMap<CSS::PropertyID, GC::Ref<CSS::CSSTransition>> const* existing_transitions(Optional<CSS::PseudoElement>) const;
- GC::Ptr<CSS::CSSTransition> property_transition(Optional<CSS::PseudoElement>, CSS::PropertyID) const;
+ HashMap<CSS::PropertyNameAndID, GC::Ref<CSS::CSSTransition>> const* existing_transitions(Optional<CSS::PseudoElement>) const;
+ GC::Ptr<CSS::CSSTransition> property_transition(Optional<CSS::PseudoElement>, CSS::PropertyNameAndID const&) const;
protected:
void visit_edges(JS::Cell::Visitor&);
diff --git a/Libraries/LibWeb/CSS/CSSTransition.cpp b/Libraries/LibWeb/CSS/CSSTransition.cpp
index 640ec488e47..c844bfb1cac 100644
--- a/Libraries/LibWeb/CSS/CSSTransition.cpp
+++ b/Libraries/LibWeb/CSS/CSSTransition.cpp
@@ -24,7 +24,7 @@ GC_DEFINE_ALLOCATOR(CSSTransition);
GC::Ref<CSSTransition> CSSTransition::start_a_provisional_transition(
DOM::AbstractElement abstract_element,
- PropertyID property_id,
+ PropertyNameAndID property,
size_t transition_generation,
double delay,
double start_time,
@@ -36,12 +36,12 @@ GC::Ref<CSSTransition> CSSTransition::start_a_provisional_transition(
EasingFunction timing_function)
{
auto& environment = abstract_element.document().relevant_settings_object();
- return GC::Heap::the().allocate<CSSTransition>(environment, abstract_element, property_id, transition_generation, delay, start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor, move(timing_function));
+ return GC::Heap::the().allocate<CSSTransition>(environment, abstract_element, move(property), transition_generation, delay, start_time, end_time, start_value, end_value, reversing_adjusted_start_value, reversing_shortening_factor, move(timing_function));
}
Utf16FlyString const& CSSTransition::transition_property() const
{
- return string_from_property_id(m_transition_property);
+ return m_transition_property.name();
}
Animations::AnimationClass CSSTransition::animation_class() const
@@ -94,7 +94,7 @@ int CSSTransition::class_specific_composite_order(GC::Ref<Animations::Animation>
CSSTransition::CSSTransition(
HTML::EnvironmentSettingsObject& environment,
DOM::AbstractElement abstract_element,
- PropertyID property_id,
+ PropertyNameAndID property,
size_t transition_generation,
double delay,
double start_time,
@@ -105,7 +105,7 @@ CSSTransition::CSSTransition(
double reversing_shortening_factor,
EasingFunction timing_function)
: Animations::Animation(environment)
- , m_transition_property(property_id)
+ , m_transition_property(move(property))
, m_transition_generation(transition_generation)
, m_start_time(start_time + delay)
, m_end_time(end_time + delay)
@@ -139,10 +139,10 @@ CSSTransition::CSSTransition(
auto key_frame_set = adopt_ref(*new Animations::KeyframeEffect::KeyFrameSet);
Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame initial_keyframe;
- initial_keyframe.properties.set(PropertyNameAndID::from_id(property_id), RustStyleValueHandle::retained(m_start_value->rust_style_value_data()));
+ initial_keyframe.properties.set(m_transition_property, RustStyleValueHandle::retained(m_start_value->rust_style_value_data()));
Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame final_keyframe;
- final_keyframe.properties.set(PropertyNameAndID::from_id(property_id), RustStyleValueHandle::retained(m_end_value->rust_style_value_data()));
+ final_keyframe.properties.set(m_transition_property, RustStyleValueHandle::retained(m_end_value->rust_style_value_data()));
key_frame_set->keyframes_by_key.insert(0, initial_keyframe);
key_frame_set->keyframes_by_key.insert(100 * Animations::KeyframeEffect::AnimationKeyFrameKeyScaleFactor, final_keyframe);
@@ -199,9 +199,9 @@ void CSSTransition::adopt_lane_transitions(DOM::Document& document)
if (!element || !element->is_connected())
return;
// A transition the lane started that the host took in already runs from when the lane started it.
- auto runs_as_lane_does = [&](CSSTransition const& transition, PropertyID property_id) {
+ auto runs_as_lane_does = [&](CSSTransition const& transition, PropertyNameAndID const& property) {
return any_of(lane_transitions, [&](auto const& lane_transition) {
- if (lane_transition.property_id != to_underlying(property_id))
+ if (lane_transition.property_id != to_underlying(property.id()))
return false;
if (lane_transition.host_identity != 0)
return lane_transition.host_identity == effect_identity(transition);
@@ -213,10 +213,10 @@ void CSSTransition::adopt_lane_transitions(DOM::Document& document)
// with its events. One the host started since the lane forked is its own: the lane decided nothing over it, and
// the host decides what becomes of it, and of its property, in its turn. So does one script finished meanwhile.
GC::RootVector<GC::Ref<CSSTransition>> ended;
- Vector<PropertyID> host_decides;
+ Vector<PropertyNameAndID> host_decides;
if (auto const* existing = element->existing_transitions({})) {
- for (auto const& [property_id, transition] : *existing) {
- if (transition->is_idle() || transition->is_finished() || runs_as_lane_does(transition, property_id))
+ for (auto const& [property, transition] : *existing) {
+ if (transition->is_idle() || transition->is_finished() || runs_as_lane_does(transition, property))
continue;
auto identity = effect_identity(transition);
if (cancelled.contains_slow(identity)) {
@@ -226,7 +226,7 @@ void CSSTransition::adopt_lane_transitions(DOM::Document& document)
} else if (transition->m_adopted_from_lane) {
ended.append(transition);
} else {
- host_decides.append(property_id);
+ host_decides.append(property);
}
}
}
@@ -238,7 +238,7 @@ void CSSTransition::adopt_lane_transitions(DOM::Document& document)
for (auto const& lane_transition : lane_transitions) {
if (lane_transition.host_identity == 0)
continue;
- auto transition = element->property_transition({}, static_cast<PropertyID>(lane_transition.property_id));
+ auto transition = element->property_transition({}, PropertyNameAndID::from_id(static_cast<PropertyID>(lane_transition.property_id)));
if (transition && effect_identity(*transition) == lane_transition.host_identity && !transition->start_time().has_value()
&& transition->pending() && transition->play_state() != Bindings::AnimationPlayState::Paused)
(void)transition->set_start_time_for_bindings(Animations::NullableCSSNumberish { lane_transition.start_time });
@@ -246,19 +246,19 @@ void CSSTransition::adopt_lane_transitions(DOM::Document& document)
for (auto const& lane_transition : lane_transitions) {
if (lane_transition.host_identity != 0)
continue;
- auto property_id = static_cast<PropertyID>(lane_transition.property_id);
- if (host_decides.contains_slow(property_id))
+ auto property = PropertyNameAndID::from_id(static_cast<PropertyID>(lane_transition.property_id));
+ if (host_decides.contains_slow(property))
continue;
- if (auto current = element->property_transition({}, property_id)) {
- if (runs_as_lane_does(*current, property_id))
+ if (auto current = element->property_transition({}, property)) {
+ if (runs_as_lane_does(*current, property))
continue;
// A completed transition of the property gives way to the one the lane started. One the lane saw end,
// which the host's timeline has yet to reach, ends now.
if (!current->is_idle() && !current->is_finished())
(void)current->finish();
- element->remove_transition({}, property_id);
+ element->remove_transition({}, property);
}
- auto transition = start_a_provisional_transition(*element, property_id, document.transition_generation(),
+ auto transition = start_a_provisional_transition(*element, property, document.transition_generation(),
lane_transition.delay, lane_transition.start_time, lane_transition.start_time + lane_transition.duration,
adopt_value(lane_transition.start_value), adopt_value(lane_transition.end_value), adopt_value(lane_transition.reversing_adjusted_start_value),
lane_transition.reversing_shortening_factor, EasingFunction::from_style_value(adopt_value(lane_transition.timing_function)));
diff --git a/Libraries/LibWeb/CSS/CSSTransition.h b/Libraries/LibWeb/CSS/CSSTransition.h
index 198edf9ef05..b8049aeb2ea 100644
--- a/Libraries/LibWeb/CSS/CSSTransition.h
+++ b/Libraries/LibWeb/CSS/CSSTransition.h
@@ -9,6 +9,7 @@
#include <LibWeb/Animations/Animation.h>
#include <LibWeb/CSS/EasingFunction.h>
+#include <LibWeb/CSS/PropertyNameAndID.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
namespace Web::CSS {
@@ -21,7 +22,7 @@ public:
// A transition is started provisionally, and associated with its target only once committed.
static GC::Ref<CSSTransition> start_a_provisional_transition(
DOM::AbstractElement,
- PropertyID,
+ PropertyNameAndID,
size_t transition_generation,
double delay,
double start_time,
@@ -47,6 +48,7 @@ public:
double style_change_event_time() const;
double transition_start_time() const { return m_start_time; }
double transition_end_time() const { return m_end_time; }
+ NonnullRefPtr<StyleValue const> transition_start_value() const { return m_start_value; }
NonnullRefPtr<StyleValue const> transition_end_value() const { return m_end_value; }
NonnullRefPtr<StyleValue const> reversing_adjusted_start_value() const { return m_reversing_adjusted_start_value; }
double reversing_shortening_factor() const { return m_reversing_shortening_factor; }
@@ -68,7 +70,7 @@ private:
CSSTransition(
HTML::EnvironmentSettingsObject&,
DOM::AbstractElement,
- PropertyID,
+ PropertyNameAndID,
size_t transition_generation,
double delay,
double start_time,
@@ -83,7 +85,7 @@ private:
virtual bool is_css_transition() const override { return true; }
- PropertyID m_transition_property;
+ PropertyNameAndID m_transition_property;
// https://drafts.csswg.org/css-transitions-2/#transition-generation
size_t m_transition_generation;
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index 9330b459931..cc3dd684eaf 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -416,8 +416,8 @@ void StyleComputer::commit_transition_stabilization_epoch()
VERIFY(state.element);
auto& element = *state.element;
auto remove_committed_transition = [&] {
- if (element.property_transition(state.pseudo_element, state.property_id) == state.committed_transition)
- element.remove_transition(state.pseudo_element, state.property_id);
+ if (element.property_transition(state.pseudo_element, state.property) == state.committed_transition)
+ element.remove_transition(state.pseudo_element, state.property);
};
auto cancel_and_remove_committed_transition = [&] {
VERIFY(state.committed_transition);
@@ -1193,11 +1193,25 @@ RefPtr<ComputedStyleWorkingSet> StyleComputer::start_needed_transitions(Layout::
auto style_change_event_time = current_time->value;
Vector<StyleValueFFI::FfiExistingTransition> existing_transitions;
+ Vector<NonnullRefPtr<StyleValue const>> custom_current_values;
+ // A running transition of a custom property holds it at what the overlay of the target's environment answers. One
+ // not sampled yet holds it at its start value.
+ auto custom_current_value = [&](PropertyNameAndID const& property, CSSTransition const& transition) -> StyleValueFFI::StyleValueData const* {
+ auto value = transition.transition_start_value();
+ if (auto data = abstract_element.custom_property_data(); data && abstract_element.custom_property_data_is_animation_overlay()) {
+ if (auto animated = data->get(property.name()); animated.has_value())
+ value = *animated->value;
+ }
+ custom_current_values.append(value);
+ return value->rust_style_value_data();
+ };
if (auto const* transitions = element.existing_transitions(pseudo_element)) {
- for (auto const& [property_id, transition] : *transitions) {
+ for (auto const& [property, transition] : *transitions) {
bool running = !transition->is_finished() && !transition->is_idle();
existing_transitions.append({
- .property_id = to_underlying(property_id),
+ .property_id = to_underlying(property.id()),
+ .custom_name = property.is_custom_property() ? property.name().raw_identity() : 0,
+ .custom_current_value = property.is_custom_property() && running ? custom_current_value(property, transition) : nullptr,
.running = running,
.end_value = transition->transition_end_value()->rust_style_value_data(),
.reversing_adjusted_start_value = transition->reversing_adjusted_start_value()->rust_style_value_data(),
@@ -1219,23 +1233,37 @@ RefPtr<ComputedStyleWorkingSet> StyleComputer::start_needed_transitions(Layout::
state.proposed_transition = nullptr;
state.action = action;
};
- auto ensure_state = [&](PropertyID property_id) -> ProvisionalTransitionState& {
- auto index = m_provisional_transition_state_indices.ensure((target_key << 16) | to_underlying(property_id), [&] {
+ auto ensure_state = [&](PropertyNameAndID const& property) -> ProvisionalTransitionState& {
+ auto create_state = [&] {
VERIFY(document().is_in_style_stabilization_epoch());
m_provisional_transition_state_indices_by_target.ensure(target_key).append(m_provisional_transition_states.size());
m_provisional_transition_states.append({
.element = element,
.pseudo_element = pseudo_element,
- .property_id = property_id,
- .committed_transition = element.property_transition(pseudo_element, property_id),
+ .property = property,
+ .committed_transition = element.property_transition(pseudo_element, property),
.proposed_transition = nullptr,
.action = StyleValueFFI::FfiTransitionActionKind::None,
.has_decision = false,
});
return m_provisional_transition_states.size() - 1;
- });
+ };
+ if (property.is_custom_property()) {
+ auto indices = m_provisional_transition_state_indices_by_target.get(target_key);
+ for (auto index : indices.has_value() ? indices->span() : ReadonlySpan<size_t> {}) {
+ if (m_provisional_transition_states[index].property == property)
+ return m_provisional_transition_states[index];
+ }
+ return m_provisional_transition_states[create_state()];
+ }
+ auto index = m_provisional_transition_state_indices.ensure((target_key << 16) | to_underlying(property.id()), create_state);
return m_provisional_transition_states[index];
};
+ auto property_of_action = [](StyleValueFFI::FfiTransitionAction const& action) {
+ if (action.custom_name != 0)
+ return PropertyNameAndID::from_name(Utf16FlyString::from_raw(action.custom_name)).release_value();
+ return PropertyNameAndID::from_id(static_cast<PropertyID>(action.property_id));
+ };
auto adopt = [](StyleValueFFI::StyleValueData const* value) {
return StyleValue::adopt_rust_style_value_data(StyleValueFFI::rust_style_value_retain(value));
};
@@ -1244,8 +1272,8 @@ RefPtr<ComputedStyleWorkingSet> StyleComputer::start_needed_transitions(Layout::
GC::RootHashTable<Animations::KeyframeEffect*> replaced_transition_effects;
auto on_actions = [&](ReadonlySpan<StyleValueFFI::FfiTransitionAction> actions) {
for (auto const& action : actions) {
- auto property_id = static_cast<PropertyID>(action.property_id);
- auto& state = ensure_state(property_id);
+ auto property = property_of_action(action);
+ auto& state = ensure_state(property);
decide(state, action.kind);
if (action.kind != StyleValueFFI::FfiTransitionActionKind::None && action.kind != StyleValueFFI::FfiTransitionActionKind::Start) {
VERIFY(state.committed_transition);
@@ -1254,7 +1282,7 @@ RefPtr<ComputedStyleWorkingSet> StyleComputer::start_needed_transitions(Layout::
}
if (!action.start_value)
continue;
- auto transition = CSSTransition::start_a_provisional_transition(abstract_element, property_id, document().transition_generation(),
+ auto transition = CSSTransition::start_a_provisional_transition(abstract_element, property, document().transition_generation(),
action.delay, style_change_event_time, style_change_event_time + action.active_duration,
adopt(action.start_value), adopt(action.end_value), adopt(action.reversing_adjusted_start_value),
action.reversing_shortening_factor, EasingFunction::from_style_value(adopt(action.timing_function)));
@@ -1265,7 +1293,7 @@ RefPtr<ComputedStyleWorkingSet> StyleComputer::start_needed_transitions(Layout::
auto indices = m_provisional_transition_state_indices_by_target.get(target_key);
for (auto index : indices.has_value() ? indices->span() : ReadonlySpan<size_t> {}) {
auto& state = m_provisional_transition_states[index];
- if (!any_of(actions, [&](auto const& action) { return action.property_id == to_underlying(state.property_id); }))
+ if (!any_of(actions, [&](auto const& action) { return property_of_action(action) == state.property; }))
decide(state, StyleValueFFI::FfiTransitionActionKind::None);
}
};
diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.h
index e5fa42cf587..6e83311518e 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.h
+++ b/Libraries/LibWeb/CSS/StyleComputer.h
@@ -304,7 +304,7 @@ private:
struct ProvisionalTransitionState {
GC::Ptr<DOM::Element> element;
Optional<PseudoElement> pseudo_element;
- PropertyID property_id;
+ PropertyNameAndID property;
GC::Ptr<CSSTransition> committed_transition;
GC::Ptr<CSSTransition> proposed_transition;
StyleValueFFI::FfiTransitionActionKind action {};
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index d3ebf1246a8..c6003b78a09 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -995,6 +995,12 @@ impl CustomPropertyStore {
.or_else(|| self.parent.as_ref()?.get(name_raw))
}
+ /// The names of the values the store, or a store it is built over, holds. A name can come more than once.
+ pub(crate) fn held_names(&self) -> impl Iterator<Item = &Utf16FlyString> {
+ std::iter::successors(Some(self), |store| store.parent.as_deref())
+ .flat_map(|store| store.own_values.values().map(|entry| &entry.name))
+ }
+
/// Whether the store, or a store it is built over, holds a value of `name`.
pub(crate) fn holds(&self, name: &Utf16FlyString) -> bool {
self.get_by_name_with_owner(name).is_some()
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index 0908c90c788..6a910ff2a11 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -1605,11 +1605,15 @@ pub unsafe extern "C" fn style_engine_decide_transitions(
let element_box = crate::layout::node_data::NodeSlotId {
index: input.element_box_slot,
};
- let actions = with_engine_and_arena(read, host, |engine, arena| {
+ let (mut actions, custom_properties) = with_engine_and_arena(read, host, |engine, arena| {
let reference_box =
crate::painting::ffi::committed_transform_reference_box(&arena.paintable_rows(), element_box);
- decision.decide(engine, reference_box, existing)
+ (
+ decision.decide(engine, reference_box, existing),
+ decision.decide_custom_properties(engine, existing),
+ )
});
+ actions.extend_from_slice(&custom_properties.actions);
hand_over(&actions);
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
index 609ef9d4c02..a608d29c631 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/records.rs
@@ -591,9 +591,7 @@ impl Record {
}
/// The custom properties the element holds, which its inherited set holds beside its groups.
- pub(in super::super) fn custom_properties(
- &self,
- ) -> Option<&Arc<crate::css::custom_properties::CustomPropertyStore>> {
+ pub(crate) fn custom_properties(&self) -> Option<&Arc<crate::css::custom_properties::CustomPropertyStore>> {
self.payloads.custom_properties()
}
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 79c557e6506..53339014ffa 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -6208,7 +6208,7 @@ fn active_transition_property_ids_of<'a>(
}
/// Whether computed `transition-property`, `transition-duration` and `transition-delay` values
-/// make any longhand transitionable.
+/// make any property transitionable.
pub(crate) fn transition_values_declare_transitions(
properties: &RetainedStyleValueData,
durations: &RetainedStyleValueData,
@@ -6220,19 +6220,17 @@ pub(crate) fn transition_values_declare_transitions(
};
values.as_slice()
}
- fn names_a_longhand(property: u16) -> bool {
+ fn names_a_property(property: u16) -> bool {
use crate::css::property_metadata::property_id as prop;
property == prop::ALL
- || if property_is_shorthand(property) {
- longhands_for_shorthand(property)
- .iter()
- .any(|&longhand| names_a_longhand(longhand))
- } else {
- property != prop::CUSTOM
- }
+ || property == prop::CUSTOM
+ || !property_is_shorthand(property)
+ || longhands_for_shorthand(property)
+ .iter()
+ .any(|&longhand| names_a_property(longhand))
}
active_transition_property_ids_of(value_list(properties), value_list(durations), value_list(delays))
- .any(names_a_longhand)
+ .any(names_a_property)
}
/// The `transition-*` values of `longhands`, per physical longhand they name: shorthands expanded,
@@ -6294,28 +6292,101 @@ pub(crate) fn transition_entries(
entries
}
+/// The `transition-*` values of `longhands` as they apply to custom properties: a name takes its last entry, and a
+/// `transition: all` entry stands for every name no later entry gives its own.
+#[derive(Default)]
+pub(crate) struct CustomPropertyTransitionEntries {
+ named: Vec<(ak::Utf16FlyString, crate::css::transition::TransitionEntry)>,
+ all: Option<crate::css::transition::TransitionEntry>,
+}
+
+impl CustomPropertyTransitionEntries {
+ pub(crate) fn is_empty(&self) -> bool {
+ self.named.is_empty() && self.all.is_none()
+ }
+
+ pub(crate) fn names_all_properties(&self) -> bool {
+ self.all.is_some()
+ }
+
+ pub(crate) fn named_properties(&self) -> impl Iterator<Item = &ak::Utf16FlyString> {
+ self.named.iter().map(|(name, _)| name)
+ }
+
+ pub(crate) fn entry_for(&self, raw_name: usize) -> Option<&crate::css::transition::TransitionEntry> {
+ self.named
+ .iter()
+ .find(|(name, _)| name.raw_identity() == raw_name)
+ .map(|(_, entry)| entry)
+ .or(self.all.as_ref())
+ }
+}
+
+pub(crate) fn custom_property_transition_entries(
+ longhands: crate::css::style::ViewLonghands<'_>,
+) -> CustomPropertyTransitionEntries {
+ use crate::css::property_metadata::property_id as prop;
+
+ let mut entries = CustomPropertyTransitionEntries::default();
+ let property_values = computed_value_list(longhands, prop::TRANSITION_PROPERTY);
+ for (index, property_value) in property_values.iter().enumerate() {
+ let StyleValueData::CustomIdent { custom_ident } = property_value.data() else {
+ continue;
+ };
+ let property = property_id_from_custom_ident(custom_ident);
+ if property != Some(prop::ALL) && property != Some(prop::CUSTOM) {
+ continue;
+ }
+ let at = |list: u16| {
+ let values = computed_value_list(longhands, list);
+ &values[index % values.len()]
+ };
+ let entry = crate::css::transition::TransitionEntry {
+ property_id: prop::CUSTOM,
+ delay: time_value_to_milliseconds(at(prop::TRANSITION_DELAY).data()),
+ duration: time_value_to_milliseconds(at(prop::TRANSITION_DURATION).data()),
+ timing_function: at(prop::TRANSITION_TIMING_FUNCTION).pointer(),
+ behavior: match at(prop::TRANSITION_BEHAVIOR).data() {
+ StyleValueData::Keyword { keyword } => keyword_to_transition_behavior(*keyword).unwrap(),
+ _ => unreachable!("computed transition-behavior must be a keyword"),
+ },
+ };
+ if property == Some(prop::ALL) {
+ entries.named.clear();
+ entries.all = Some(entry);
+ continue;
+ }
+ let name = ak::Utf16FlyString::from_utf16(custom_ident.units());
+ entries
+ .named
+ .retain(|(named, _)| named.raw_identity() != name.raw_identity());
+ entries.named.push((name, entry));
+ }
+ entries
+}
+
/// Whether `transition_entries` answers any entry for `longhands`, without expanding them.
pub(crate) fn has_transition_entries(longhands: crate::css::style::ViewLonghands<'_>) -> bool {
use crate::css::property_metadata::property_id as prop;
- // Whether an entry for `property` names a longhand, as `append_transition_longhands` would.
- fn names_a_longhand(property: u16) -> bool {
- if property == prop::ALL {
+ // Whether an entry for `property` names a longhand or a custom property.
+ fn names_a_property(property: u16) -> bool {
+ if property == prop::ALL || property == prop::CUSTOM {
return true;
}
if property_is_shorthand(property) {
return longhands_for_shorthand(property)
.iter()
- .any(|&longhand| names_a_longhand(longhand));
+ .any(|&longhand| names_a_property(longhand));
}
- property != prop::CUSTOM
+ true
}
computed_value_list(longhands, prop::TRANSITION_PROPERTY)
.iter()
.any(|value| match value.data() {
StyleValueData::CustomIdent { custom_ident } => {
- property_id_from_custom_ident(custom_ident).is_some_and(names_a_longhand)
+ property_id_from_custom_ident(custom_ident).is_some_and(names_a_property)
}
_ => false,
})
diff --git a/Libraries/LibWeb/Rust/src/css/transition.rs b/Libraries/LibWeb/Rust/src/css/transition.rs
index b101b109b71..f370995d979 100644
--- a/Libraries/LibWeb/Rust/src/css/transition.rs
+++ b/Libraries/LibWeb/Rust/src/css/transition.rs
@@ -23,6 +23,11 @@ pub enum FfiTransitionActionKind {
#[repr(C)]
pub struct FfiExistingTransition {
pub property_id: u16,
+ /// The raw name of the custom property it transitions, or 0 for a longhand.
+ pub custom_name: usize,
+ /// Where it transitions a custom property and is running, the value it holds the property at, which the overlay of
+ /// the target's environment answers. Null otherwise: the record of a longhand holds its current value.
+ pub custom_current_value: *const StyleValueData,
/// Whether the transition is running rather than completed.
pub running: bool,
pub end_value: *const StyleValueData,
@@ -52,6 +57,8 @@ pub struct FfiTransitionInput {
#[repr(C)]
pub struct FfiTransitionAction {
pub property_id: u16,
+ /// The raw name of the custom property the action is for, or 0 for a longhand.
+ pub custom_name: usize,
pub kind: FfiTransitionActionKind,
pub delay: f64,
pub active_duration: f64,
@@ -132,6 +139,21 @@ fn property_values_are_transitionable(
new_value: *const StyleValueData,
allow_discrete: bool,
) -> bool {
+ // https://drafts.css-houdini.org/css-properties-values-api/#animation-behavior-of-custom-properties
+ // When referenced by animations and transitions, custom property values interpolate by computed value, in
+ // accordance with the type that they parsed as.
+ if property_id == crate::css::property_metadata::property_id::CUSTOM {
+ return allow_discrete
+ || crate::css::animation::interpolate_value(
+ Some(context),
+ property_id,
+ unsafe { &*old_value },
+ unsafe { &*new_value },
+ 0.5,
+ )
+ .value
+ .is_some();
+ }
let animation_type = crate::css::property_metadata::property_animation_type(property_id);
// https://drafts.csswg.org/css-transitions/#transitionable
@@ -205,6 +227,7 @@ fn decide_transition(
);
let mut action = FfiTransitionAction {
property_id: input.property_id,
+ custom_name: 0,
kind: FfiTransitionActionKind::None,
delay: input.delay,
active_duration: input.duration,
@@ -475,24 +498,15 @@ impl TransitionDecision {
self.decide_over(engine, reference_box, None, entries, existing, &[])
}
- /// As `decide_entries`, with `after_overlay` in place of the after-change record's overlay where it is given: the
- /// current values of the target's running transitions, which the record holds none of.
- fn decide_over(
- &self,
+ /// What the values of the decision interpolate in, with `after_overlay` over the after-change record.
+ fn animation_context<'a>(
+ &'a self,
engine: &crate::css::style::StyleEngine,
reference_box: Option<crate::css::css_pixels::CssPixelRect>,
- after_overlay: Option<&crate::css::animated_overlay::AnimatedOverlay>,
- entries: &[TransitionEntry],
- existing: &[FfiExistingTransition],
- ended: &[u16],
- ) -> Vec<FfiTransitionAction> {
- let element = self.element;
- let length_resolution_context = engine.transition_length_resolution_context(&self.after);
- let before = crate::css::style::StyleRecordView::of_host_record(&self.before);
+ after_overlay: Option<&'a crate::css::animated_overlay::AnimatedOverlay>,
+ ) -> crate::css::animation::FfiAnimationContext<'a> {
let after = crate::css::style::StyleRecordView::of_host_record(&self.after);
- let (before_longhands, before_overlay) = (before.longhands(), before.animated_overlay);
let after_longhands = after.longhands();
- let after_overlay = after_overlay.or(after.animated_overlay);
let mut context = crate::css::animation::FfiAnimationContext {
allow_discrete: false,
// The value that matters is the one in the after-change style.
@@ -504,12 +518,109 @@ impl TransitionDecision {
after_longhands.effective_value(after_overlay, crate::css::property_metadata::property_id::COLOR),
),
has_length_resolution_context: true,
- length_resolution_context,
+ length_resolution_context: engine.transition_length_resolution_context(&self.after),
has_transform_reference_box: false,
transform_reference_box_width: 0.0,
transform_reference_box_height: 0.0,
};
context.set_transform_reference_box(reference_box);
+ context
+ }
+
+ /// Runs the CSS Transitions decision algorithm for the custom properties the after-change style's `transition-*`
+ /// values name, then for those of `existing` transitions they do not name. The longhands are `decide`'s.
+ pub(crate) fn decide_custom_properties(
+ &self,
+ engine: &crate::css::style::StyleEngine,
+ existing: &[FfiExistingTransition],
+ ) -> CustomPropertyDecisions {
+ use crate::css::custom_properties::CustomPropertyStore;
+ let mut decisions = CustomPropertyDecisions::default();
+ let existing_for = |name: usize| existing.iter().find(|existing| existing.custom_name == name);
+ let has_existing = existing.iter().any(|existing| existing.custom_name != 0);
+ let after = crate::css::style::StyleRecordView::of_host_record(&self.after);
+ let after_longhands = after.longhands();
+ if !has_existing && crate::css::style_compute::transition_delay_and_duration_are_single_zero(after_longhands) {
+ return decisions;
+ }
+ let entries = crate::css::style_compute::custom_property_transition_entries(after_longhands);
+ if !has_existing && entries.is_empty() {
+ return decisions;
+ }
+ let before_store = self.before.custom_properties();
+ let after_store = self.after.custom_properties();
+
+ let mut names: Vec<ak::Utf16FlyString> = entries.named_properties().cloned().collect();
+ // A `transition: all` entry names every custom property either style holds.
+ if entries.names_all_properties()
+ && !std::ptr::eq(
+ before_store.map_or(std::ptr::null(), std::sync::Arc::as_ptr),
+ after_store.map_or(std::ptr::null(), std::sync::Arc::as_ptr),
+ )
+ {
+ for store in [before_store, after_store].into_iter().flatten() {
+ names.extend(store.held_names().cloned());
+ }
+ }
+ for existing in existing.iter().filter(|existing| existing.custom_name != 0) {
+ // SAFETY: The caller keeps the name of the transition alive.
+ names.push(unsafe { ak::Utf16FlyString::from_raw_borrowed(existing.custom_name) });
+ }
+ names.sort_unstable_by_key(ak::Utf16FlyString::raw_identity);
+ names.dedup_by_key(|name| name.raw_identity());
+
+ let registry = engine.custom_property_registry();
+ let value_in = |store: Option<&std::sync::Arc<CustomPropertyStore>>, name: &ak::Utf16FlyString| {
+ store
+ .and_then(|store| store.get(name.raw_identity()))
+ .map_or_else(|| registry.initial_value(&name.to_utf16()), |entry| entry.value.clone())
+ };
+ let context = self.animation_context(engine, None, after.animated_overlay);
+ for name in names {
+ let raw_name = name.raw_identity();
+ let existing = existing_for(raw_name);
+ let mut property = TransitionProperty::new(
+ crate::css::property_metadata::property_id::CUSTOM,
+ entries.entry_for(raw_name),
+ existing,
+ );
+ let before_value = value_in(before_store, &name);
+ let after_value = value_in(after_store, &name);
+ property.before_change_value = before_value.pointer();
+ property.after_change_value = after_value.pointer();
+ if let Some(existing) = existing.filter(|existing| existing.running) {
+ property.current_value = existing.custom_current_value;
+ }
+ let mut action = decide_transition(&context, &property, false);
+ action.custom_name = raw_name;
+ if action.kind == FfiTransitionActionKind::None && action.start_value.is_null() && existing.is_none() {
+ continue;
+ }
+ decisions.actions.push(action);
+ decisions.values.extend([before_value, after_value]);
+ decisions.names.push(name);
+ }
+ decisions
+ }
+
+ /// As `decide_entries`, with `after_overlay` in place of the after-change record's overlay where it is given: the
+ /// current values of the target's running transitions, which the record holds none of.
+ fn decide_over(
+ &self,
+ engine: &crate::css::style::StyleEngine,
+ reference_box: Option<crate::css::css_pixels::CssPixelRect>,
+ after_overlay: Option<&crate::css::animated_overlay::AnimatedOverlay>,
+ entries: &[TransitionEntry],
+ existing: &[FfiExistingTransition],
+ ended: &[u16],
+ ) -> Vec<FfiTransitionAction> {
+ let element = self.element;
+ let before = crate::css::style::StyleRecordView::of_host_record(&self.before);
+ let after = crate::css::style::StyleRecordView::of_host_record(&self.after);
+ let (before_longhands, before_overlay) = (before.longhands(), before.animated_overlay);
+ let after_longhands = after.longhands();
+ let after_overlay = after_overlay.or(after.animated_overlay);
+ let context = self.animation_context(engine, reference_box, after_overlay);
let existing_for = |property_id| existing.iter().find(|existing| existing.property_id == property_id);
// OPTIMIZATION: An element inherits an animated value only from an ancestor holding one, so one walk up the
// tree, taken for the first inherited property, answers for every property where none does.
@@ -522,7 +633,9 @@ impl TransitionDecision {
.map(|entry| (entry.property_id, Some(entry), existing_for(entry.property_id)));
let unmatched = existing
.iter()
- .filter(|existing| entries.iter().all(|entry| entry.property_id != existing.property_id))
+ .filter(|existing| {
+ existing.custom_name == 0 && entries.iter().all(|entry| entry.property_id != existing.property_id)
+ })
.map(|existing| (existing.property_id, None, Some(existing)));
matched
.chain(unmatched)
@@ -576,6 +689,14 @@ impl TransitionDecision {
}
}
+/// What a transition step does to the transitions of custom properties, with what its actions point into.
+#[derive(Default)]
+pub(crate) struct CustomPropertyDecisions {
+ pub(crate) actions: Vec<FfiTransitionAction>,
+ values: Vec<crate::css::style_value::RetainedStyleValueData>,
+ names: Vec<ak::Utf16FlyString>,
+}
+
/// What the transition step a style update's row owes decides, where nothing but the engine's records decides it
/// (see [`DecidedTransitionStep::of_row`]).
pub(crate) struct RowTransitions {
@@ -597,6 +718,18 @@ pub(crate) struct RowTransitions {
after_overlay: Option<Box<crate::css::animated_overlay::AnimatedOverlay>>,
}
+/// Whether `before` and `after` hold the same custom-property environment.
+fn same_custom_properties(
+ before: &crate::css::style::pipeline::Record,
+ after: &crate::css::style::pipeline::Record,
+) -> bool {
+ match (before.custom_properties(), after.custom_properties()) {
+ (Some(before), Some(after)) => std::sync::Arc::ptr_eq(before, after),
+ (None, None) => true,
+ _ => false,
+ }
+}
+
/// A transition an element runs, as it stands at the time a step decides at: one the host runs, as the engine's
/// description of its effect says, or one a lane's hover started, as the lane runs it.
struct RunningTransitionAt {
@@ -817,6 +950,12 @@ impl RowTransitions {
} else {
crate::css::style_compute::transition_entries(after_longhands)
};
+ // The host holds the custom property values a transition runs over.
+ if !crate::css::style_compute::custom_property_transition_entries(after_longhands).is_empty()
+ && !same_custom_properties(&before, &after)
+ {
+ return Err("a transition of a custom property");
+ }
// A transform interpolates against the box it transforms.
if reference_box.is_none() && entries.iter().any(|entry| entry.property_id == prop::TRANSFORM) {
return Err("a transition of transform");
@@ -835,6 +974,8 @@ impl RowTransitions {
.iter()
.map(|transition| FfiExistingTransition {
property_id: transition.property_id,
+ custom_name: 0,
+ custom_current_value: std::ptr::null(),
running: transition.key.is_some(),
end_value: transition.end_value,
reversing_adjusted_start_value: transition.reversing_adjusted_start_value,
@@ -1750,6 +1891,7 @@ mod tests {
] {
let action = FfiTransitionAction {
property_id: crate::css::property_metadata::property_id::OPACITY,
+ custom_name: 0,
kind,
delay: 100.0,
active_duration: 1000.0,
diff --git a/Tests/LibWeb/Text/expected/css/registered-custom-property-transition.txt b/Tests/LibWeb/Text/expected/css/registered-custom-property-transition.txt
new file mode 100644
index 00000000000..953b6162973
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/registered-custom-property-transition.txt
@@ -0,0 +1,13 @@
+started: [--length] --length = 0px, width = 0px
+a quarter in: [--length] --length = 25px, width = 25px
+retargeted: [--length] --length = 25px, width = 25px
+no longer named: [] --length = 100px, width = 100px
+transition: all: [--length] --length = 25px, width = 784px
+finished: [] --length = 100px, width = 100px
+started back: [--length] --length = 100px, width = 100px
+color: [--color] --color = rgb(50, 0, 0), width = 0px
+unregistered: [] --plain = 100px, width = 0px
+unregistered, allow-discrete: [--plain] --plain = 0px, width = 0px
+custom-ident: [] --ident = b, width = 0px
+no duration: [] --length = 100px, width = 100px
+inherited by a child: [] --color = rgb(50, 0, 0), width = 0px
diff --git a/Tests/LibWeb/Text/input/css/registered-custom-property-transition.html b/Tests/LibWeb/Text/input/css/registered-custom-property-transition.html
new file mode 100644
index 00000000000..a0b2f47d46e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/registered-custom-property-transition.html
@@ -0,0 +1,110 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @property --length { syntax: "<length>"; inherits: false; initial-value: 0px; }
+ @property --color { syntax: "<color>"; inherits: true; initial-value: black; }
+ @property --ident { syntax: "<custom-ident>"; inherits: false; initial-value: a; }
+ .box { width: var(--length); }
+ .all-properties { --length: 0px; transition: all 100s linear; }
+ .all-properties.on { --length: 100px; }
+ .length { --length: 0px; transition: --length 100s linear; }
+ .length.on { --length: 100px; }
+ .color { --color: rgb(0, 0, 0); transition: --color 100s linear; }
+ .color.on { --color: rgb(200, 0, 0); }
+ .unregistered { --plain: 0px; transition: --plain 100s linear; }
+ .unregistered.on { --plain: 100px; }
+ .discrete { --plain: 0px; transition: --plain 100s linear allow-discrete; }
+ .discrete.on { --plain: 100px; }
+ .ident { --ident: a; transition: --ident 100s linear; }
+ .ident.on { --ident: b; }
+ .zero { --length: 0px; transition: --length 0s; }
+ .zero.on { --length: 100px; }
+ .parent-on { --color: rgb(200, 0, 0); }
+</style>
+<body>
+<script>
+ function build(className) {
+ const box = document.createElement("div");
+ box.className = "box " + className;
+ document.body.append(box);
+ getComputedStyle(box).width;
+ return box;
+ }
+
+ function atQuarter(box) {
+ const [transition] = box.getAnimations();
+ if (!transition)
+ return null;
+ transition.pause();
+ transition.currentTime = 25000;
+ return transition;
+ }
+
+ function report(label, box, property) {
+ const style = getComputedStyle(box);
+ const names = box.getAnimations().map(animation => animation.transitionProperty).join(",");
+ println(`${label}: [${names}] ${property} = ${style.getPropertyValue(property)}, width = ${style.width}`);
+ }
+
+ test(() => {
+ let box = build("length");
+ box.classList.add("on");
+ report("started", box, "--length");
+ atQuarter(box);
+ report("a quarter in", box, "--length");
+ box.style.setProperty("--length", "50px");
+ report("retargeted", box, "--length");
+
+ box = build("length");
+ box.classList.add("on");
+ atQuarter(box);
+ box.style.transitionProperty = "none";
+ report("no longer named", box, "--length");
+
+ box = build("all-properties");
+ box.classList.remove("box");
+ getComputedStyle(box).width;
+ box.classList.add("on");
+ atQuarter(box);
+ report("transition: all", box, "--length");
+
+ box = build("length");
+ box.classList.add("on");
+ box.getAnimations()[0].finish();
+ report("finished", box, "--length");
+ box.classList.remove("on");
+ report("started back", box, "--length");
+
+ box = build("color");
+ box.classList.add("on");
+ atQuarter(box);
+ report("color", box, "--color");
+
+ box = build("unregistered");
+ box.classList.add("on");
+ report("unregistered", box, "--plain");
+
+ box = build("discrete");
+ box.classList.add("on");
+ report("unregistered, allow-discrete", box, "--plain");
+
+ box = build("ident");
+ box.classList.add("on");
+ report("custom-ident", box, "--ident");
+
+ box = build("zero");
+ box.classList.add("on");
+ report("no duration", box, "--length");
+
+ const parent = build("");
+ const child = document.createElement("div");
+ child.className = "color";
+ parent.append(child);
+ getComputedStyle(child).width;
+ child.className = "color on";
+ atQuarter(child);
+ const grandchild = document.createElement("div");
+ child.append(grandchild);
+ report("inherited by a child", grandchild, "--color");
+ });
+</script>