Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE html>
<html>
<title>Elements should appear in elementsFromPoint</title>
<link rel="help" href="https://github.com/WICG/html-in-canvas">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/canvas/resources/wait-for-canvas-paint.js"></script>
<style>
#canvas {
width: 200px;
height: 200px;
}
div {
width: 100px;
height: 100px;
background: green;
}
</style>
<canvas id=canvas width="200" height="200" layoutsubtree>
<div id=a></div>
<div id=b></div>
<div id=pointerEventsNone style="pointer-events: none;"></div>
<div id=inert inert></div>
</canvas>
<script>
promise_test(async function () {
assert_array_equals(document.elementsFromPoint(60, 60),
[canvas, document.body, document.documentElement],
'Should have returned a sequence with `[canvas, body, html]`');
}, 'Canvas descendants are not hit-testable before they are drawn.');
promise_test(async function () {
await waitForCanvasPaint(canvas);
const ctx = canvas.getContext('2d');
ctx.drawElementImage(b, 0, 0);
assert_array_equals(document.elementsFromPoint(60, 60),
[b, canvas, document.body, document.documentElement],
'Should have returned a sequence with `[b, canvas, body, html]`');
}, 'Canvas descendants are hittable after being drawn.');
promise_test(async function () {
await waitForCanvasPaint(canvas);
const ctx = canvas.getContext('2d');
ctx.drawElementImage(a, 0, 0);
assert_array_equals(document.elementsFromPoint(60, 60),
[a, b, canvas, document.body, document.documentElement],
'Should have returned a sequence with `[a, b, canvas, body, html]`');
}, 'Canvas descendants are hit in reverse drawing order.');
promise_test(async function () {
await waitForCanvasPaint(canvas);
const ctx = canvas.getContext('2d');
ctx.drawElementImage(b, 0, 0);
assert_array_equals(document.elementsFromPoint(60, 60),
[b, a, canvas, document.body, document.documentElement],
'Should have returned a sequence with `[b, a, canvas, body, html]`');
}, 'Re-drawing an element moves it to the top of the hit testing list.');
promise_test(async function () {
await waitForCanvasPaint(canvas);
const ctx = canvas.getContext('2d');
ctx.drawElementImage(pointerEventsNone, 0, 0);
ctx.drawElementImage(inert, 0, 0);
assert_array_equals(document.elementsFromPoint(60, 60),
[b, a, canvas, document.body, document.documentElement],
'Should have returned a sequence with `[b, a, canvas, body, html]`');
}, 'Pointer-events: none and inert should be excluded from elementsFromPoint');
promise_test(async function (t) {
b.style.transform = 'translate(50px, 50px)';
t.add_cleanup(() => {
b.style.transform = '';
});
assert_array_equals(document.elementsFromPoint(110, 110),
[b, canvas, document.body, document.documentElement],
'Should have returned a sequence with `[b, canvas, body, html]`');
}, 'CSS transform should be used for hit testing');
promise_test(async function (t) {
canvas.updateElementGeometry(b, {canvasTransform: new DOMMatrix().translate(50, 50)});
t.add_cleanup(() => {
canvas.clearElementGeometry(b);
});
assert_array_equals(document.elementsFromPoint(110, 110),
[b, canvas, document.body, document.documentElement],
'Should have returned a sequence with `[b, canvas, body, html]`');
}, 'Canvas transform should be used for hit testing');
</script>
</html>