Bug 1319137 - (intersection-observer) Add isIntersecting property to IntersectionObserverEntry
MozReview-Commit-ID: GmwpD0FYWAa
--- a/dom/base/DOMIntersectionObserver.cpp
+++ b/dom/base/DOMIntersectionObserver.cpp
@@ -450,16 +450,17 @@ DOMIntersectionObserver::QueueIntersecti
intersectionRect->SetLayoutRect(aIntersectionRect.value());
}
RefPtr<DOMIntersectionObserverEntry> entry = new DOMIntersectionObserverEntry(
this,
time,
rootBounds.forget(),
boundingClientRect.forget(),
intersectionRect.forget(),
+ aIntersectionRect.isSome(),
aTarget, aIntersectionRatio);
mQueuedEntries.AppendElement(entry.forget());
}
void
DOMIntersectionObserver::Notify()
{
if (!mQueuedEntries.Length()) {
--- a/dom/base/DOMIntersectionObserver.h
+++ b/dom/base/DOMIntersectionObserver.h
@@ -25,23 +25,25 @@ class DOMIntersectionObserverEntry final
~DOMIntersectionObserverEntry() {}
public:
DOMIntersectionObserverEntry(nsISupports* aOwner,
DOMHighResTimeStamp aTime,
RefPtr<DOMRect> aRootBounds,
RefPtr<DOMRect> aBoundingClientRect,
RefPtr<DOMRect> aIntersectionRect,
+ bool aIsIntersecting,
Element* aTarget,
double aIntersectionRatio)
: mOwner(aOwner),
mTime(aTime),
mRootBounds(aRootBounds),
mBoundingClientRect(aBoundingClientRect),
mIntersectionRect(aIntersectionRect),
+ mIsIntersecting(aIsIntersecting),
mTarget(aTarget),
mIntersectionRatio(aIntersectionRatio)
{
}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMIntersectionObserverEntry)
nsISupports* GetParentObject() const
@@ -69,32 +71,38 @@ public:
return mBoundingClientRect;
}
DOMRect* IntersectionRect()
{
return mIntersectionRect;
}
+ bool IsIntersecting()
+ {
+ return mIsIntersecting;
+ }
+
double IntersectionRatio()
{
return mIntersectionRatio;
}
Element* Target()
{
return mTarget;
}
protected:
nsCOMPtr<nsISupports> mOwner;
DOMHighResTimeStamp mTime;
RefPtr<DOMRect> mRootBounds;
RefPtr<DOMRect> mBoundingClientRect;
RefPtr<DOMRect> mIntersectionRect;
+ bool mIsIntersecting;
RefPtr<Element> mTarget;
double mIntersectionRatio;
};
#define NS_DOM_INTERSECTION_OBSERVER_IID \
{ 0x8570a575, 0xe303, 0x4d18, \
{ 0xb6, 0xb1, 0x4d, 0x2b, 0x49, 0xd8, 0xef, 0x94 } }
--- a/dom/base/test/test_intersectionobservers.html
+++ b/dom/base/test/test_intersectionobservers.html
@@ -716,16 +716,17 @@ limitations under the License.
targetEl1.style.left = '-20px';
targetEl2.style.top = '-21px';
targetEl2.style.left = '0px';
spy.waitForNotification(function() {
expect(spy.callCount).to.be(2);
var records = sortRecords(spy.lastCall.args[0]);
expect(records.length).to.be(2);
expect(records[0].intersectionRatio).to.be(0);
+ expect(records[0].isIntersecting).to.be.ok();
expect(records[0].target).to.be(targetEl1);
expect(records[1].intersectionRatio).to.be(0);
expect(records[1].target).to.be(targetEl2);
done();
}, ASYNC_TIMEOUT);
},
function(done) {
targetEl1.style.top = '-20px';
@@ -748,41 +749,60 @@ limitations under the License.
targetEl4.style.left = '20px';
io.observe(targetEl3);
io.observe(targetEl4);
spy.waitForNotification(function() {
expect(spy.callCount).to.be(4);
var records = sortRecords(spy.lastCall.args[0]);
expect(records.length).to.be(2);
expect(records[0].intersectionRatio).to.be(0);
+ expect(records[0].isIntersecting).to.be.ok();
expect(records[0].target).to.be(targetEl3);
expect(records[1].intersectionRatio).to.be(0);
expect(records[1].target).to.be(targetEl4);
done();
}, ASYNC_TIMEOUT);
}
], done);
});
it('handles zero-size targets within the root coordinate space',
function(done) {
- io = new IntersectionObserver(function(records) {
- expect(records.length).to.be(1);
- expect(records[0].intersectionRatio).to.be(0);
- done();
- }, {root: rootEl});
+ var spy = sinon.spy();
+ io = new IntersectionObserver(spy, {root: rootEl});
- targetEl1.style.top = '0px';
- targetEl1.style.left = '0px';
- targetEl1.style.width = '0px';
- targetEl1.style.height = '0px';
- io.observe(targetEl1);
+ runSequence([
+ function(done) {
+ targetEl1.style.top = '0px';
+ targetEl1.style.left = '0px';
+ targetEl1.style.width = '0px';
+ targetEl1.style.height = '0px';
+ io.observe(targetEl1);
+ spy.waitForNotification(function() {
+ var records = sortRecords(spy.lastCall.args[0]);
+ expect(records.length).to.be(1);
+ expect(records[0].intersectionRatio).to.be(0);
+ expect(records[0].isIntersecting).to.be.ok();
+ done();
+ }, ASYNC_TIMEOUT);
+ },
+ function(done) {
+ targetEl1.style.top = '-1px';
+ spy.waitForNotification(function() {
+ var records = sortRecords(spy.lastCall.args[0]);
+ expect(records.length).to.be(1);
+ expect(records[0].intersectionRatio).to.be(0);
+ expect(records[0].isIntersecting).to.be(false);
+ done();
+ }, ASYNC_TIMEOUT);
+ }
+ ], done);
});
it('handles root/target elements not yet in the DOM', function(done) {
rootEl.remove();
targetEl1.remove();
--- a/dom/webidl/IntersectionObserver.webidl
+++ b/dom/webidl/IntersectionObserver.webidl
@@ -13,16 +13,18 @@ interface IntersectionObserverEntry {
readonly attribute DOMHighResTimeStamp time;
[Constant]
readonly attribute DOMRectReadOnly? rootBounds;
[Constant]
readonly attribute DOMRectReadOnly boundingClientRect;
[Constant]
readonly attribute DOMRectReadOnly intersectionRect;
[Constant]
+ readonly attribute boolean isIntersecting;
+ [Constant]
readonly attribute double intersectionRatio;
[Constant]
readonly attribute Element target;
};
[Constructor(IntersectionCallback intersectionCallback,
optional IntersectionObserverInit options),
Pref="dom.IntersectionObserver.enabled"]