Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp

Issue 2566943004: IntersectionObserver: compute correct intersection ratio for 0x0 elements (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/intersection-observer/zero-area-element-visible.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/dom/IntersectionObservation.h" 5 #include "core/dom/IntersectionObservation.h"
6 6
7 #include "core/dom/ElementRareData.h" 7 #include "core/dom/ElementRareData.h"
8 #include "core/dom/IntersectionObserver.h" 8 #include "core/dom/IntersectionObserver.h"
9 #include "core/frame/FrameView.h" 9 #include "core/frame/FrameView.h"
10 #include "core/frame/LocalFrame.h" 10 #include "core/frame/LocalFrame.h"
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 // - 0 means not intersecting. 204 // - 0 means not intersecting.
205 // - 1 means intersecting. 205 // - 1 means intersecting.
206 // No other threshold crossings are possible. 206 // No other threshold crossings are possible.
207 // - Otherwise: 207 // - Otherwise:
208 // - If root and target do not intersect, the threshold index is 0. 208 // - If root and target do not intersect, the threshold index is 0.
209 // - If root and target intersect but the intersection has zero-area 209 // - If root and target intersect but the intersection has zero-area
210 // (i.e., they have a coincident edge or corner), we consider the 210 // (i.e., they have a coincident edge or corner), we consider the
211 // intersection to have "crossed" a zero threshold, but not crossed 211 // intersection to have "crossed" a zero threshold, but not crossed
212 // any non-zero threshold. 212 // any non-zero threshold.
213 unsigned newThresholdIndex; 213 unsigned newThresholdIndex;
214 float newVisibleRatio = 0; 214 float newVisibleRatio;
215 if (geometry.targetRect.isEmpty()) { 215 if (geometry.doesIntersect) {
216 newThresholdIndex = geometry.doesIntersect ? 1 : 0; 216 if (geometry.targetRect.isEmpty()) {
217 } else if (!geometry.doesIntersect) { 217 newVisibleRatio = 1;
218 } else {
219 float intersectionArea =
220 geometry.intersectionRect.size().width().toFloat() *
221 geometry.intersectionRect.size().height().toFloat();
222 float targetArea = geometry.targetRect.size().width().toFloat() *
223 geometry.targetRect.size().height().toFloat();
224 newVisibleRatio = intersectionArea / targetArea;
225 }
226 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio);
227 } else {
228 newVisibleRatio = 0;
218 newThresholdIndex = 0; 229 newThresholdIndex = 0;
219 } else {
220 float intersectionArea =
221 geometry.intersectionRect.size().width().toFloat() *
222 geometry.intersectionRect.size().height().toFloat();
223 float targetArea = geometry.targetRect.size().width().toFloat() *
224 geometry.targetRect.size().height().toFloat();
225 newVisibleRatio = intersectionArea / targetArea;
226 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio);
227 } 230 }
228 if (m_lastThresholdIndex != newThresholdIndex) { 231 if (m_lastThresholdIndex != newThresholdIndex) {
229 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect); 232 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect);
230 IntRect* rootBoundsPointer = 233 IntRect* rootBoundsPointer =
231 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; 234 m_shouldReportRootBounds ? &snappedRootBounds : nullptr;
232 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( 235 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry(
233 timestamp, newVisibleRatio, pixelSnappedIntRect(geometry.targetRect), 236 timestamp, newVisibleRatio, pixelSnappedIntRect(geometry.targetRect),
234 rootBoundsPointer, pixelSnappedIntRect(geometry.intersectionRect), 237 rootBoundsPointer, pixelSnappedIntRect(geometry.intersectionRect),
235 target()); 238 target());
236 observer().enqueueIntersectionObserverEntry(*newEntry); 239 observer().enqueueIntersectionObserverEntry(*newEntry);
(...skipping 12 matching lines...) Expand all
249 target()->ensureIntersectionObserverData().removeObservation(observer()); 252 target()->ensureIntersectionObserverData().removeObservation(observer());
250 m_observer.clear(); 253 m_observer.clear();
251 } 254 }
252 255
253 DEFINE_TRACE(IntersectionObservation) { 256 DEFINE_TRACE(IntersectionObservation) {
254 visitor->trace(m_observer); 257 visitor->trace(m_observer);
255 visitor->trace(m_target); 258 visitor->trace(m_target);
256 } 259 }
257 260
258 } // namespace blink 261 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/intersection-observer/zero-area-element-visible.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698