| 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..f96f63a0b32a5382a55fed2cd817f000a40cd044
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp
|
| @@ -0,0 +1,75 @@
|
| +// 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, WeakPtrWillBeRawPtr<Element> target, bool active)
|
| + : m_observer(observer)
|
| + , m_target(target)
|
| + , m_active(active)
|
| + , m_lastVisibleRatio(0)
|
| +{
|
| +}
|
| +
|
| +void IntersectionObservation::setActive(bool active)
|
| +{
|
| + if (active == m_active)
|
| + return;
|
| + m_active = active;
|
| + if (active)
|
| + observer()->trackingDocument()->intersectionObservationRegistry()->addObservation(*this);
|
| + else
|
| + observer()->trackingDocument()->intersectionObservationRegistry()->removeObservation(*this);
|
| +}
|
| +
|
| +void IntersectionObservation::computeIntersectionObservations(double 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();
|
| + // Only process observers for which this element is target, not root.
|
| + if (rootElement && rootElement == targetElement)
|
| + return;
|
| + 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, pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootRect), pixelSnappedIntRect(intersectionRect), targetElement));
|
| +}
|
| +
|
| +void IntersectionObservation::disconnect()
|
| +{
|
| + observer()->disconnect(*this);
|
| +}
|
| +
|
| +DEFINE_TRACE(IntersectionObservation)
|
| +{
|
| + visitor->trace(m_observer);
|
| + visitor->trace(m_target);
|
| +}
|
| +
|
| +} // namespace blink {
|
|
|