Skip to content

Commit

Permalink
Fix bug in IntersectionObserver for SVG elements with viewbox
Browse files Browse the repository at this point in the history
Allowing intersection observer to work with SVG elements created a bug
as the function MapToVisualRectInAncestorSpace doesn't map from viewbox
coordinates to the SVG elements coordinates. This patch adds code to fix
this problem

Bug: 1502965
Change-Id: I86123ed256f537a8840a3d3e74edacf27a2367bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5041547
Auto-Submit: Yotam Hacohen <yotha@chromium.org>
Commit-Queue: Yotam Hacohen <yotha@chromium.org>
Reviewed-by: Stefan Zager <szager@chromium.org>
Commit-Queue: Stefan Zager <szager@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1226418}
  • Loading branch information
yotam-hacohen authored and chromium-wpt-export-bot committed Nov 18, 2023
1 parent 0cde829 commit 351988b
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions intersection-observer/svg-viewbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<title>IntersectionObserver observing an SVG &lt;rect> element with changing 'transform'</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 id="container" width="10" height="10" viewBox="0 0 128 128" style="background: lightblue; position: absolute; top: 0; left: 0;">
<rect id="target" x="0" y="0" width="128" height="128" fill="green"></rect>
</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);
}, {root: container, threshold: [0, 0.5, 1]});
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', '-64');
runTestCycle(step1, "change target's y property to -64)");
// The numbers in brackets are target client rect; intersection rect;
// and root bounds.
checkLastEntry(entries, 0, [
0, targetRect.width, 0, targetRect.height,
0, targetRect.width, 0, targetRect.height,
0, 10, 0, 10,
true,
]);
}
function step1() {
target.style.transform = "translate(0, 50px)";
checkLastEntry(entries, 1, [
0, targetRect.width, -5, -5 + targetRect.height,
0, targetRect.width, 0, -5 + targetRect.height,
0, 10, 0, 10,
true,
]);
}
</script>

0 comments on commit 351988b

Please sign in to comment.