Skip to content

Truly independent web browser

LibWeb: Animate shorthands written with var() in keyframes

Andreas Kling3c61190c0fea
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index fdc972dba2f..883b561827a 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -1836,10 +1836,10 @@ void record_element_animation_effect_descriptions(DOM::Element& element, u8 slot
if (use_initial && !property.is_custom_property() && property_is_shorthand(property.id()))
continue;
StyleValueFFI::StyleValueData const* data = use_initial ? nullptr : value.get<RustStyleValueHandle>().data();
- // A shorthand's pending substitution animates nothing, and neither does a value that
- // is invalid at computed-value time.
+ // A value that is invalid at computed-value time animates nothing. A longhand pending the
+ // substitution of its shorthand's value is substituted with it when the engine samples it.
// https://drafts.csswg.org/css-values-5/#invalid-at-computed-value-time
- if (!use_initial && (data->tag == StyleValueFFI::StyleValueData::Tag::PendingSubstitution || (data->tag == StyleValueFFI::StyleValueData::Tag::GuaranteedInvalid && !property.is_custom_property())))
+ if (!use_initial && data->tag == StyleValueFFI::StyleValueData::Tag::GuaranteedInvalid && !property.is_custom_property())
continue;
if (property.is_custom_property())
ffi_custom_declarations.append({ .name = property.name().raw_identity(), .value = data });
diff --git a/Libraries/LibWeb/Rust/src/css/animation.rs b/Libraries/LibWeb/Rust/src/css/animation.rs
index b15e72ebd2e..602f39772ca 100644
--- a/Libraries/LibWeb/Rust/src/css/animation.rs
+++ b/Libraries/LibWeb/Rust/src/css/animation.rs
@@ -7081,7 +7081,11 @@ pub(crate) fn resolve_published_animation_declarations(
easing_from_computed_timing_function(value)
});
easings.push(substituted_easing.unwrap_or_else(|| keyframe.easing.clone()));
- for declaration in effect.declarations_of(keyframe) {
+ let keyframe_declarations = effect.declarations_of(keyframe);
+ // The values of the shorthands written with arbitrary substitution functions that this keyframe's
+ // longhands pend the substitution of, each already standing for its longhands.
+ let mut substituted_shorthand_values: Vec<*const StyleValueData> = Vec::new();
+ for declaration in keyframe_declarations {
let (value, use_initial) = match &declaration.value {
PublishedValue::ElementValue => {
let Some(value) = table.get(declaration.property_id) else {
@@ -7090,6 +7094,72 @@ pub(crate) fn resolve_published_animation_declarations(
(value.clone_retained(), true)
}
PublishedValue::Declared(value) => match value.data() {
+ // https://drafts.csswg.org/css-values-5/#pending-substitution-value
+ // The longhands of a shorthand written with an arbitrary substitution function take their
+ // parts of its value once it substitutes, so the shorthand stands for them.
+ StyleValueData::PendingSubstitution { .. } => {
+ // NB: The longhands of a shorthand nested in the one written (`background-position` in
+ // `background`) pend the nested shorthand's pending value in turn.
+ fn written_shorthand_value(value: &RetainedStyleValueData) -> &RetainedStyleValueData {
+ match value.data() {
+ StyleValueData::PendingSubstitution {
+ original_shorthand_value,
+ } => written_shorthand_value(original_shorthand_value),
+ _ => value,
+ }
+ }
+ let original_shorthand_value = written_shorthand_value(value);
+ let original = original_shorthand_value.pointer();
+ if substituted_shorthand_values.contains(&original) {
+ continue;
+ }
+ substituted_shorthand_values.push(original);
+ let pending_longhands = keyframe_declarations
+ .iter()
+ .filter(|candidate| {
+ matches!(&candidate.value, PublishedValue::Declared(written)
+ if matches!(written.data(), StyleValueData::PendingSubstitution { .. })
+ && written_shorthand_value(written).pointer() == original)
+ })
+ .map(|candidate| candidate.property_id)
+ .collect::<Vec<_>>();
+ // NB: A later declaration may have replaced some of the written shorthand's longhands,
+ // so this takes the smallest shorthand with the pending longhands whose grammar
+ // parses the substituted value (`border` rather than `border-width` for
+ // `border: 2px solid` where only the widths still pend).
+ // https://drafts.csswg.org/css-values-5/#invalid-at-computed-value-time
+ let Some((shorthand, value)) =
+ crate::css::property_metadata::shorthands_with_leaf_longhands(&pending_longhands)
+ .into_iter()
+ .map(|shorthand| (shorthand, substitute(shorthand, &[], original_shorthand_value)))
+ .find(|(_, value)| !matches!(value.data(), StyleValueData::GuaranteedInvalid))
+ else {
+ continue;
+ };
+ // NB: Only the longhands that still pend the shorthand's value take their parts of it; a
+ // later declaration in the keyframe replaced the others.
+ crate::css::style_compute::expand_shorthands_with(
+ shorthand,
+ &value,
+ false,
+ &mut |longhand, data, _| {
+ if pending_longhands.contains(&longhand) {
+ declarations.push(AnimationDeclaration {
+ keyframe_index,
+ property_id: longhand,
+ custom_name_id: 0,
+ custom_is_inherited: false,
+ custom_is_important: false,
+ value: data.clone_retained(),
+ style_sheet_resource_context,
+ use_initial: false,
+ is_transition,
+ });
+ }
+ },
+ );
+ continue;
+ }
StyleValueData::Unresolved { .. } => {
let value = substitute(declaration.property_id, &[], value);
// https://drafts.csswg.org/css-values-5/#invalid-at-computed-value-time
diff --git a/Libraries/LibWeb/Rust/src/css/property_metadata.rs b/Libraries/LibWeb/Rust/src/css/property_metadata.rs
index 8738d7fe60a..6eb87e93034 100644
--- a/Libraries/LibWeb/Rust/src/css/property_metadata.rs
+++ b/Libraries/LibWeb/Rust/src/css/property_metadata.rs
@@ -259,6 +259,54 @@ pub(crate) fn property_defines_a_css_transition(property_id: u16) -> bool {
)
}
+/// Each shorthand with its longhands, through any shorthand nested in it (`border-width` in `border`), sorted.
+fn shorthand_leaf_longhands() -> &'static [(u16, Box<[u16]>)] {
+ fn leaf_longhands(property: u16, leaves: &mut Vec<u16>) {
+ for &longhand in longhands_for_shorthand(property) {
+ if longhand < FIRST_LONGHAND_PROPERTY_ID {
+ leaf_longhands(longhand, leaves);
+ } else {
+ leaves.push(longhand);
+ }
+ }
+ }
+ static SHORTHAND_LEAVES: std::sync::OnceLock<Vec<(u16, Box<[u16]>)>> = std::sync::OnceLock::new();
+ SHORTHAND_LEAVES.get_or_init(|| {
+ (FIRST_SHORTHAND_PROPERTY_ID..=LAST_SHORTHAND_PROPERTY_ID)
+ .map(|shorthand| {
+ let mut leaves = Vec::new();
+ leaf_longhands(shorthand, &mut leaves);
+ leaves.sort_unstable();
+ leaves.dedup();
+ (shorthand, leaves.into_boxed_slice())
+ })
+ .collect()
+ })
+}
+
+/// The shorthand whose longhands, through any shorthand nested in it, are exactly `longhands`: the one a value pending
+/// substitution in each of them was written for.
+pub(crate) fn shorthand_with_leaf_longhands(mut longhands: Vec<u16>) -> Option<u16> {
+ longhands.sort_unstable();
+ longhands.dedup();
+ shorthand_leaf_longhands()
+ .iter()
+ .find(|(_, leaves)| **leaves == *longhands)
+ .map(|(shorthand, _)| *shorthand)
+}
+
+/// The shorthands whose longhands, through any shorthand nested in it, include all of `longhands`, with the fewest
+/// longhands first: those a value pending substitution in each of them may have been written for, where a later
+/// declaration may have replaced some of the shorthand's other longhands.
+pub(crate) fn shorthands_with_leaf_longhands(longhands: &[u16]) -> Vec<u16> {
+ let mut shorthands = shorthand_leaf_longhands()
+ .iter()
+ .filter(|(_, leaves)| longhands.iter().all(|longhand| leaves.binary_search(longhand).is_ok()))
+ .collect::<Vec<_>>();
+ shorthands.sort_by_key(|(_, leaves)| leaves.len());
+ shorthands.into_iter().map(|(shorthand, _)| *shorthand).collect()
+}
+
/// Returns the longhands a shorthand expands to, in Properties.json order.
pub fn longhands_for_shorthand(property_id: u16) -> &'static [u16] {
if !property_is_shorthand(property_id) {
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index d5ec12c12fb..0bf77044446 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -3147,27 +3147,7 @@ impl RetainedState {
}
}
let original = original?;
- pending_longhands.sort_unstable();
- pending_longhands.dedup();
- fn leaf_longhands(property: u16, leaves: &mut Vec<u16>) {
- for &longhand in crate::css::property_metadata::longhands_for_shorthand(property) {
- if longhand < crate::css::property_metadata::FIRST_LONGHAND_PROPERTY_ID {
- leaf_longhands(longhand, leaves);
- } else {
- leaves.push(longhand);
- }
- }
- }
- let mut leaves = Vec::with_capacity(pending_longhands.len());
- let shorthand = (crate::css::property_metadata::FIRST_SHORTHAND_PROPERTY_ID
- ..=crate::css::property_metadata::LAST_SHORTHAND_PROPERTY_ID)
- .find(|&candidate| {
- leaves.clear();
- leaf_longhands(candidate, &mut leaves);
- leaves.sort_unstable();
- leaves.dedup();
- leaves == pending_longhands
- })?;
+ let shorthand = crate::css::property_metadata::shorthand_with_leaf_longhands(pending_longhands)?;
Some((shorthand, original.clone_retained()))
}
diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.py
index 2dab6480661..b22a25fc534 100755
--- a/Meta/Linters/check_rust_process_wide_state.py
+++ b/Meta/Linters/check_rust_process_wide_state.py
@@ -152,6 +152,7 @@ RENDER_STATE_ALLOWED = {
"css/custom_properties.rs:EMPTY",
"css/parser/stylesheet_cache.rs:HASHER",
"css/parser/stylesheet_cache.rs:KEYS",
+ "css/property_metadata.rs:SHORTHAND_LEAVES",
"css/style/publication.rs:REMAINING",
"css/style_compute.rs:KINDS",
"css/style_compute.rs:LONGHANDS",
diff --git a/Tests/LibWeb/Text/expected/css/animation-keyframe-shorthand-with-var.txt b/Tests/LibWeb/Text/expected/css/animation-keyframe-shorthand-with-var.txt
new file mode 100644
index 00000000000..7ff5cd0cb1b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/animation-keyframe-shorthand-with-var.txt
@@ -0,0 +1,6 @@
+longhand: rgb(209, 163, 21) 0% 0% 0px 0px
+shorthand: rgb(209, 163, 21) 0% 0% 0px 0px
+shorthand-to-literal: rgb(209, 163, 21) 0% 0% 0px 0px
+nested-shorthand: rgb(209, 163, 21) 15px 25px 0px 0px
+partly-replaced-shorthand: rgba(0, 0, 0, 0) 0% 0% 5px 5px 5px 10px 0px
+shorthand-with-only-nested-longhands-pending: rgba(0, 0, 0, 0) 0% 0% 0px 5px
diff --git a/Tests/LibWeb/Text/input/css/animation-keyframe-shorthand-with-var.html b/Tests/LibWeb/Text/input/css/animation-keyframe-shorthand-with-var.html
new file mode 100644
index 00000000000..ca28025da1b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/animation-keyframe-shorthand-with-var.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @keyframes longhand {
+ from { background-color: var(--from); }
+ to { background-color: var(--to); }
+ }
+ @keyframes shorthand {
+ from { background: var(--from); }
+ to { background: var(--to); }
+ }
+ @keyframes shorthand-to-literal {
+ from { background: var(--from); }
+ to { background: rgb(69, 77, 84); }
+ }
+ @keyframes nested-shorthand {
+ from { background: var(--from) 10px 20px; }
+ to { background: var(--to) 30px 40px; }
+ }
+ @keyframes partly-replaced-shorthand {
+ from { margin: var(--from-margin); margin-left: 0px; }
+ to { margin: var(--to-margin); margin-left: 40px; }
+ }
+ @keyframes shorthand-with-only-nested-longhands-pending {
+ from { border: var(--from-border); border-style: solid; border-color: red; border-image: none; }
+ to { border: var(--to-border); border-style: solid; border-color: red; border-image: none; }
+ }
+ .animated {
+ --from-margin: 0px;
+ --to-margin: 20px;
+ --from-border: 0px dotted blue;
+ --to-border: 20px dotted blue;
+ --from: rgb(255, 191, 0);
+ --to: rgb(69, 77, 84);
+ animation: none 1s linear both paused;
+ animation-delay: -0.25s;
+ }
+</style>
+<div class="animated" style="animation-name: longhand"></div>
+<div class="animated" style="animation-name: shorthand"></div>
+<div class="animated" style="animation-name: shorthand-to-literal"></div>
+<div class="animated" style="animation-name: nested-shorthand"></div>
+<div class="animated" style="animation-name: partly-replaced-shorthand"></div>
+<div class="animated" style="animation-name: shorthand-with-only-nested-longhands-pending"></div>
+<script>
+ // The longhands of a shorthand written with var() in a keyframe take their parts of its value once it
+ // substitutes against the element.
+ test(() => {
+ for (const element of document.querySelectorAll(".animated")) {
+ const style = getComputedStyle(element);
+ println(`${style.animationName}: ${style.backgroundColor} ${style.backgroundPosition} ${style.margin} ${style.borderTopWidth}`);
+ }
+ });
+</script>