Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/interaction-harness/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ seeking and sampling run a curve forward. The recorder keeps one entry per
`Animation` object, so one reversed or replayed mid-run is misread: a watch
taken before the reversal rejects, and an unwatched wait returns at once with
a curve dated before it. Take a fresh animation for each run a proof reads.
`sampleCurve(locator, times, {pseudoElement: '::before'})` samples the
element's `::before` or `::after` instead: its presented values, labelled by
that pseudo-element's own animations.

**Waiting for an animation.** The page records every animation from the first
call into `curves.ts` on it (and on every later document): a frame loop on the
Expand Down
31 changes: 23 additions & 8 deletions internal/interaction-harness/src/curves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ export interface CurveSample {
* The time the sampled element presents: `t`, or just inside the end of
* its own animations when the sample was taken at or past it (see
* `seekAnimations`). Read from the element's own animations only (from
* every held one if none targets it).
* every held one if none targets it); when sampling a pseudo-element, from
* that pseudo-element's.
*/
readonly presentedAt: number;
/**
Expand Down Expand Up @@ -584,13 +585,19 @@ export interface CurveSample {
* held origin, as for `seekAnimations`). Hold the animations first. An animation of the
* individual `translate`, `rotate` or `scale` properties is included: they
* are composed with `transform` into one matrix.
*
* `pseudoElement` samples the element's `::before` or `::after` instead:
* its presented values, labelled by its own animations (those whose effect
* targets the element with that pseudo-element), as a component that animates
* a pseudo-element (`element.animate(keyframes, {pseudoElement})`) draws it.
*/
export async function sampleCurve(
locator: Locator,
times: readonly number[],
{pseudoElement}: {pseudoElement?: '::before' | '::after'} = {},
): Promise<CurveSample[]> {
const raw = await locator.evaluate(
(element, {ts, inset}) => {
(element, {ts, inset, pseudo}) => {
const w = window as unknown as {
__harnessHeld?: Animation[];
__harnessHeldStarts?: number[];
Expand All @@ -613,12 +620,17 @@ export async function sampleCurve(
'sampleCurve: no held animations (call holdAnimations first)',
);
}
// The properties each held animation of this element animates. A
// `::before` or `::after` animation is not the element's own: its
// clock would label the element's samples with another box's time.
// The properties each held animation of the sampled box animates: the
// element's own, or its pseudo-element's when one is sampled. Another
// box's animation (the element's `::before` when the element is
// sampled) is not its own: its clock would label the samples with
// another box's time.
const own = held.map(animation => {
const effect = animation.effect as KeyframeEffect | null;
if (effect?.target !== element || effect.pseudoElement != null) {
if (
effect?.target !== element ||
(effect.pseudoElement ?? null) !== pseudo
) {
return null;
}
const properties = new Set<string>();
Expand Down Expand Up @@ -667,7 +679,10 @@ export async function sampleCurve(
const or = (value: number, fallback: number) =>
Number.isFinite(value) ? value : fallback;
const presentedAt = or(presentedOwn, or(presented, t));
const style = getComputedStyle(element);
const style =
pseudo == null
? getComputedStyle(element)
: getComputedStyle(element, pseudo);
// translate · rotate · scale · transform, as CSS applies them (all
// about the same transform origin, which `transform` excludes too).
// A percentage resolves against the border box, unrounded
Expand Down Expand Up @@ -771,7 +786,7 @@ export async function sampleCurve(
};
});
},
{ts: [...times], inset: END_INSET_MS},
{ts: [...times], inset: END_INSET_MS, pseudo: pseudoElement ?? null},
);
return raw.map(r => ({
t: r.t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,81 @@ test.describe('curve sampling', () => {
});
});

// A full-size veil on the fixture's box, drawn by its ::before.
const VEIL_STYLE =
"#box::before { content: ''; position: absolute; inset: 0; background: #000; opacity: 0; }";

test.describe('pseudo-element animations', () => {
test('sampleCurve reads a ::before animation when given its pseudo-element', async ({
page,
}) => {
await fixture(page);
const box = page.locator('#box');
await box.evaluate((el, veil) => {
const style = document.createElement('style');
style.textContent = veil;
document.head.append(style);
el.animate(
[
{opacity: 0, transform: 'translateY(0px)'},
{opacity: 1, transform: 'translateY(40px)'},
],
{duration: 300, easing: 'linear', pseudoElement: '::before'},
);
}, VEIL_STYLE);
await holdAnimations(page, box);
const samples = await sampleCurve(box, [0, 75, 150, 225, 300], {
pseudoElement: '::before',
});
// Labelled by the veil's own clock: the last read just inside its end.
expect(samples.at(-1)).toMatchObject({t: 300, presentedAt: 299.5});
const fade = compareCurve(curveOf(samples, 'opacity'), t => t / 300, 0.005);
expect(fade.maxError, JSON.stringify(fade.worst)).toBeLessThanOrEqual(
0.005,
);
const move = compareCurve(
curveOf(samples, 'translateY'),
t => (40 * t) / 300,
0.05,
);
expect(move.maxError, JSON.stringify(move.worst)).toBeLessThanOrEqual(0.05);
// Without it, the box itself, which does not move.
const [boxAt150] = await sampleCurve(box, [150]);
expect(boxAt150.opacity).toBe(1);
expect(boxAt150.transform.translateY).toBe(0);
});

test('waitForRest waits out a ::before animation', async ({page}) => {
await fixture(page);
const box = page.locator('#box');
await box.evaluate((el, veil) => {
const style = document.createElement('style');
style.textContent = veil;
document.head.append(style);
(window as unknown as {__veil: Animation}).__veil = el.animate(
[{opacity: 0}, {opacity: 1}],
{
duration: 600,
easing: 'linear',
fill: 'forwards',
pseudoElement: '::before',
},
);
}, VEIL_STYLE);
// Its subtree read includes the element's pseudo-elements.
await waitForRest(box);
const veil = await box.evaluate(element => {
const a = (window as unknown as {__veil: Animation}).__veil;
return {
playState: a.playState,
currentTime: Number(a.currentTime),
opacity: getComputedStyle(element, '::before').opacity,
};
});
expect(veil).toMatchObject({playState: 'finished', opacity: '1'});
});
});

test.describe('holding', () => {
test('a sample at an end reports the time it was read at', async ({page}) => {
await fixture(page);
Expand Down