Commit 8296d3b0 authored by skyostil's avatar skyostil Committed by Commit bot

IntersectionObserver: compute correct intersection ratio for 0x0 elements

When a 0x0 element is in view, its intersection ratio is defined[1] to
be 1.

BUG=662826

[1] https://wicg.github.io/IntersectionObserver/#update-intersection-observations-algo

Review-Url: https://codereview.chromium.org/2556243003
Cr-Commit-Position: refs/heads/master@{#437617}
parent 804d3453
<!DOCTYPE html>
<script src="../resources/intersection-observer-helper-functions.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<title>Ensure that a hidden zero-area element is treated correctly</title>
<div id='target' style='width: 0px; height: 0px; position: fixed; top: -1000px'</div>"
<script>
'use strict';
async_test(t => {
let target = document.getElementById('target');
let entries = [];
new IntersectionObserver(changes => {
entries.push(...changes);
}).observe(target);
waitForNotification(t.step_func_done(() => {
// Since the element is initially assumed to be hidden, there are no entries.
assert_equals(entries.length, 0);
}));
});
</script>
<!DOCTYPE html>
<script src="../resources/intersection-observer-helper-functions.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<title>Ensure that a visible zero-area element is given the correct intersection ratio</title>
<div id='target' style='width: 0px; height: 0px'</div>"
<script>
'use strict';
async_test(t => {
let target = document.getElementById('target');
let entries = [];
new IntersectionObserver(changes => {
entries.push(...changes);
}).observe(target);
waitForNotification(t.step_func_done(() => {
assert_equals(entries.length, 1);
assert_equals(entries[0].intersectionRatio, 1);
}));
});
</script>
......@@ -50,19 +50,22 @@ void IntersectionObservation::computeIntersectionObservations(
// intersection to have "crossed" a zero threshold, but not crossed
// any non-zero threshold.
unsigned newThresholdIndex;
float newVisibleRatio = 0;
if (geometry.targetRect().isEmpty()) {
newThresholdIndex = geometry.doesIntersect() ? 1 : 0;
} else if (!geometry.doesIntersect()) {
newThresholdIndex = 0;
} else {
float intersectionArea =
geometry.intersectionRect().size().width().toFloat() *
geometry.intersectionRect().size().height().toFloat();
float targetArea = geometry.targetRect().size().width().toFloat() *
geometry.targetRect().size().height().toFloat();
newVisibleRatio = intersectionArea / targetArea;
float newVisibleRatio;
if (geometry.doesIntersect()) {
if (geometry.targetRect().isEmpty()) {
newVisibleRatio = 1;
} else {
float intersectionArea =
geometry.intersectionRect().size().width().toFloat() *
geometry.intersectionRect().size().height().toFloat();
float targetArea = geometry.targetRect().size().width().toFloat() *
geometry.targetRect().size().height().toFloat();
newVisibleRatio = intersectionArea / targetArea;
}
newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio);
} else {
newVisibleRatio = 0;
newThresholdIndex = 0;
}
if (m_lastThresholdIndex != newThresholdIndex) {
IntRect snappedRootBounds = geometry.rootIntRect();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment