OLD | NEW |
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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 if (ancestorFrame != descendantFrame) | 137 if (ancestorFrame != descendantFrame) |
138 return false; | 138 return false; |
139 | 139 |
140 while (descendant && descendant != ancestor) | 140 while (descendant && descendant != ancestor) |
141 descendant = descendant->containingBlock(); | 141 descendant = descendant->containingBlock(); |
142 return descendant; | 142 return descendant; |
143 } | 143 } |
144 | 144 |
145 bool IntersectionObservation::computeGeometry( | 145 bool IntersectionObservation::computeGeometry( |
146 IntersectionGeometry& geometry) const { | 146 IntersectionGeometry& geometry) const { |
147 // In the first few lines here, before initializeGeometry is called, "return t
rue" | 147 // In the first few lines here, before initializeGeometry is called, "return |
148 // effectively means "if the previous observed state was that root and target
were | 148 // true" effectively means "if the previous observed state was that root and |
149 // intersecting, then generate a notification indicating that they are no long
er | 149 // target were intersecting, then generate a notification indicating that they |
150 // intersecting." This happens, for example, when root or target is removed f
rom the | 150 // are no longer intersecting." This happens, for example, when root or |
151 // DOM tree and not reinserted before the next frame is generated, or display:
none | 151 // target is removed from the DOM tree and not reinserted before the next |
152 // is set on the root or target. | 152 // frame is generated, or display:none is set on the root or target. |
153 Element* targetElement = target(); | 153 Element* targetElement = target(); |
154 if (!targetElement) | 154 if (!targetElement) |
155 return false; | 155 return false; |
156 if (!targetElement->isConnected()) | 156 if (!targetElement->isConnected()) |
157 return true; | 157 return true; |
158 DCHECK(m_observer); | 158 DCHECK(m_observer); |
159 Element* rootElement = m_observer->root(); | 159 Element* rootElement = m_observer->root(); |
160 if (rootElement && !rootElement->isConnected()) | 160 if (rootElement && !rootElement->isConnected()) |
161 return true; | 161 return true; |
162 | 162 |
(...skipping 28 matching lines...) Expand all Loading... |
191 return true; | 191 return true; |
192 } | 192 } |
193 | 193 |
194 void IntersectionObservation::computeIntersectionObservations( | 194 void IntersectionObservation::computeIntersectionObservations( |
195 DOMHighResTimeStamp timestamp) { | 195 DOMHighResTimeStamp timestamp) { |
196 IntersectionGeometry geometry; | 196 IntersectionGeometry geometry; |
197 if (!computeGeometry(geometry)) | 197 if (!computeGeometry(geometry)) |
198 return; | 198 return; |
199 | 199 |
200 // Some corner cases for threshold index: | 200 // Some corner cases for threshold index: |
201 // - If target rect is zero area, because it has zero width and/or zero heig
ht, | 201 // - If target rect is zero area, because it has zero width and/or zero |
| 202 // height, |
202 // only two states are recognized: | 203 // only two states are recognized: |
203 // - 0 means not intersecting. | 204 // - 0 means not intersecting. |
204 // - 1 means intersecting. | 205 // - 1 means intersecting. |
205 // No other threshold crossings are possible. | 206 // No other threshold crossings are possible. |
206 // - Otherwise: | 207 // - Otherwise: |
207 // - 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. |
208 // - If root and target intersect but the intersection has zero-area (i.e.
, they | 209 // - If root and target intersect but the intersection has zero-area |
209 // have a coincident edge or corner), we consider the intersection to ha
ve | 210 // (i.e., they have a coincident edge or corner), we consider the |
210 // "crossed" a zero threshold, but not crossed any non-zero threshold. | 211 // intersection to have "crossed" a zero threshold, but not crossed |
| 212 // any non-zero threshold. |
211 unsigned newThresholdIndex; | 213 unsigned newThresholdIndex; |
212 float newVisibleRatio = 0; | 214 float newVisibleRatio = 0; |
213 if (geometry.targetRect.isEmpty()) { | 215 if (geometry.targetRect.isEmpty()) { |
214 newThresholdIndex = geometry.doesIntersect ? 1 : 0; | 216 newThresholdIndex = geometry.doesIntersect ? 1 : 0; |
215 } else if (!geometry.doesIntersect) { | 217 } else if (!geometry.doesIntersect) { |
216 newThresholdIndex = 0; | 218 newThresholdIndex = 0; |
217 } else { | 219 } else { |
218 float intersectionArea = | 220 float intersectionArea = |
219 geometry.intersectionRect.size().width().toFloat() * | 221 geometry.intersectionRect.size().width().toFloat() * |
220 geometry.intersectionRect.size().height().toFloat(); | 222 geometry.intersectionRect.size().height().toFloat(); |
(...skipping 26 matching lines...) Expand all Loading... |
247 target()->ensureIntersectionObserverData().removeObservation(observer()); | 249 target()->ensureIntersectionObserverData().removeObservation(observer()); |
248 m_observer.clear(); | 250 m_observer.clear(); |
249 } | 251 } |
250 | 252 |
251 DEFINE_TRACE(IntersectionObservation) { | 253 DEFINE_TRACE(IntersectionObservation) { |
252 visitor->trace(m_observer); | 254 visitor->trace(m_observer); |
253 visitor->trace(m_target); | 255 visitor->trace(m_target); |
254 } | 256 } |
255 | 257 |
256 } // namespace blink | 258 } // namespace blink |
OLD | NEW |