Index: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp |
diff --git a/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..51d0cab7d5b3c30de217100bdfbe3b485801e176 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp |
@@ -0,0 +1,64 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "core/dom/IntersectionObservation.h" |
+ |
+#include "core/dom/IntersectionObserver.h" |
+#include "core/layout/LayoutObject.h" |
+ |
+namespace blink { |
+ |
+IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element* target) |
+ : m_observer(observer) |
+ , m_target(target->createWeakPtr()) |
+ , m_active(true) |
+ , m_lastVisibleRatio(0) |
+{ |
+ target->addIntersectionObservation(*this); |
+} |
+ |
+void IntersectionObservation::computeIntersectionObservations(int timestamp) |
+{ |
+ // TODO: rootMargin is not yet supported; need to clarify how it interacts with |
+ // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/issues/70) |
+ Element* targetElement = target(); |
+ if (!targetElement || !isActive()) |
+ return; |
+ Element* rootElement = observer()->root(); |
+ LayoutObject* targetLayoutObject = targetElement->layoutObject(); |
+ // TODO: Support SVG |
+ if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObject->isInline())) |
+ return; |
+ |
+ LayoutRect targetRect, rootRect, intersectionRect; |
+ if (!targetElement->computeIntersection(rootElement, targetRect, rootRect, intersectionRect)) |
+ return; |
+ |
+ float intersectionArea = intersectionRect.size().width().toFloat() * intersectionRect.size().height().toFloat(); |
+ float targetArea = targetRect.size().width().toFloat() * targetRect.size().height().toFloat(); |
+ if (!targetArea) |
+ return; |
+ float newVisibleRatio = intersectionArea / targetArea; |
+ size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio); |
+ size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisibleRatio()); |
+ setLastVisibleRatio(newVisibleRatio); |
+ if (oldThresholdIndex != newThresholdIndex) |
+ observer()->enqueueIntersectionObserverEntry(new IntersectionObserverEntry(timestamp / 1000., pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootRect), pixelSnappedIntRect(intersectionRect), targetElement)); |
+} |
+ |
+void IntersectionObservation::disconnect() |
+{ |
+ observer()->disconnect(*this); |
+ if (m_target) |
+ m_target->removeIntersectionObservation(*this); |
+} |
+ |
+DEFINE_TRACE(IntersectionObservation) |
+{ |
+ visitor->trace(m_observer); |
+ visitor->trace(m_target); |
+} |
+ |
+} // namespace blink { |