Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!DOCTYPE html>
<title>IntersectionObserver observing an SVG element changing position</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/intersection-observer-test-utils.js"></script>
<svg width="100" height="10000">
<!-- Ensure the svg bounds are unchanged with a white fill -->
<rect x="0" y="0" width="100" height="10000" fill="white"/>
<rect id="target" x="0" y="9000" width="100" height="100" fill="blue"/>
</svg>
<script>
const viewportWidth = document.documentElement.clientWidth;
const viewportHeight = document.documentElement.clientHeight;
setup(() => {
window.entries = [];
window.target = document.getElementById("target");
window.targetRect = target.getBoundingClientRect();
});
runTestCycle(function() {
assert_true(!!target, "target exists");
const observer = new IntersectionObserver(function(changes) {
entries = entries.concat(changes);
});
observer.observe(target);
entries = entries.concat(observer.takeRecords());
assert_equals(entries.length, 0, "No initial notifications");
runTestCycle(step0, "First rAF.");
});
function step0() {
target.setAttribute('y', '0');
runTestCycle(step1, "Move SVG element up");
// The numbers in brackets are target client rect; intersection rect;
// and root bounds.
checkLastEntry(entries, 0, [
// the 8 pixels comes from the html body padding.
8, 100 + 8, 9000 + 8, 9000 + 8 + targetRect.height,
0, 0, 0, 0,
0, viewportWidth, 0, viewportHeight,
false,
]);
}
function step1() {
checkLastEntry(entries, 1, [
8, 8 + targetRect.width, 8, 8 + targetRect.height,
8, 8 + targetRect.width, 8, 8 + targetRect.height,
0, viewportWidth, 0, viewportHeight,
true,
]);
}
</script>