fix(hdr): address review feedback across stack

- Document groupIntoLayers tie-break (V8 stable sort → DOM order).
- Expand layerCompositor docstring: merge rationale, visibility inclusion.
- Add tests: empty input, negative z-index, stable tie-break at equal z.
- Document getEffectiveZIndex CSS stacking-context limitations.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vance Ingalls
2026-04-19 14:26:24 -07:00
co-authored by Claude Opus 4.6
parent b9fafd2def
commit aa33ab9705
3 changed files with 73 additions and 5 deletions
@@ -262,6 +262,22 @@ export async function queryElementStacking(
// positioned ancestor with a z-index. CSS z-index only applies to
// positioned elements; video elements inside positioned wrappers
// inherit the wrapper's stacking context.
//
// ## Supported subset
//
// This implementation looks for explicit `z-index` on positioned
// (non-static) ancestors. It does NOT detect the CSS stacking contexts
// created implicitly by other properties — including `opacity < 1`,
// `transform`, `filter`, `will-change`, `isolation: isolate`, and
// `mix-blend-mode`. GSAP routinely sets `transform` on wrappers, which
// creates an implicit stacking context with auto z-index; an HDR video
// inside such a wrapper with no explicit z-index will return the
// wrapper-of-the-wrapper's z-index here, potentially reordering layers
// incorrectly relative to sibling stacking contexts.
//
// The workaround is to set explicit `z-index` on the positioned wrapper
// when you want it treated as a compositing layer root. This matches
// what compositions need to do anyway for deterministic z-ordering.
function getEffectiveZIndex(node: Element): number {
let current: Element | null = node;
while (current) {
@@ -3,7 +3,21 @@ import { groupIntoLayers } from "./layerCompositor.js";
import type { ElementStackingInfo } from "../services/videoFrameInjector.js";
function makeEl(id: string, zIndex: number, isHdr: boolean): ElementStackingInfo {
return { id, zIndex, x: 0, y: 0, width: 1920, height: 1080, opacity: 1, visible: true, isHdr };
return {
id,
zIndex,
x: 0,
y: 0,
width: 1920,
height: 1080,
layoutWidth: 1920,
layoutHeight: 1080,
opacity: 1,
visible: true,
isHdr,
transform: "none",
borderRadius: [0, 0, 0, 0],
};
}
describe("groupIntoLayers", () => {
@@ -97,4 +111,31 @@ describe("groupIntoLayers", () => {
expect(layers[0]!.elementIds).toEqual(["bg", "hidden-sdr"]);
}
});
it("returns an empty array for empty input", () => {
expect(groupIntoLayers([])).toEqual([]);
});
it("handles negative z-index (valid CSS back layers)", () => {
const elements = [makeEl("fg", 1, false), makeEl("bg", -5, false)];
const layers = groupIntoLayers(elements);
expect(layers).toHaveLength(1);
expect(layers[0]!.type).toBe("dom");
if (layers[0]!.type === "dom") {
expect(layers[0]!.elementIds).toEqual(["bg", "fg"]);
}
});
it("preserves input order for equal z-index (stable tie-break)", () => {
const elements = [
makeEl("first", 0, false),
makeEl("second", 0, false),
makeEl("third", 0, false),
];
const layers = groupIntoLayers(elements);
expect(layers).toHaveLength(1);
if (layers[0]!.type === "dom") {
expect(layers[0]!.elementIds).toEqual(["first", "second", "third"]);
}
});
});
+15 -4
View File
@@ -15,11 +15,22 @@ export type CompositeLayer =
| { type: "hdr"; element: ElementStackingInfo };
/**
* Group z-sorted elements into composite layers. Adjacent DOM elements
* merge into a single layer. Each HDR video is its own layer.
* Group z-sorted elements into composite layers. Adjacent DOM elements merge
* into a single layer; each HDR video/image is its own layer.
*
* Elements are sorted by zIndex ascending (back to front) before grouping.
* Invisible elements are filtered out.
* Elements are sorted by \`zIndex\` ascending (back to front). Ties fall
* through to V8's stable sort, which preserves \`querySelectorAll\` DOM order —
* this is the same order Chrome uses for equal-z elements in a stacking
* context, so the blit order matches what the user sees in-browser.
*
* The DOM merge doesn't lose information: DOM layers are rendered via a
* full-page screenshot with non-layer elements hidden, so within-layer
* z-order is handled by Chrome itself.
*
* Invisible elements ARE included (video elements are hidden by the frame
* injector, but their injected \`<img>\` replacements are visible — they must
* stay in the correct z-ordered layer so sibling layers' DOM screenshots
* hide them).
*/
export function groupIntoLayers(elements: ElementStackingInfo[]): CompositeLayer[] {
// Include ALL elements regardless of visibility. Video elements are hidden by