@@ -69,12 +69,15 @@ describe('IntersectionObserver', function() {
6969 io = new IntersectionObserver ( noop ) ;
7070 expect ( io . root ) . to . be ( null ) ;
7171
72+ io = new IntersectionObserver ( noop , { root : document } ) ;
73+ expect ( io . root ) . to . be ( document ) ;
74+
7275 io = new IntersectionObserver ( noop , { root : rootEl } ) ;
7376 expect ( io . root ) . to . be ( rootEl ) ;
7477 } ) ;
7578
7679
77- it ( 'throws when root is not an Element' , function ( ) {
80+ it ( 'throws when root is not a Document or Element' , function ( ) {
7881 expect ( function ( ) {
7982 io = new IntersectionObserver ( noop , { root : 'foo' } ) ;
8083 } ) . to . throwException ( ) ;
@@ -1398,6 +1401,89 @@ describe('IntersectionObserver', function() {
13981401 io . observe ( iframeTargetEl2 ) ;
13991402 } ) ;
14001403
1404+ it ( 'handles tracking iframe viewport' , function ( done ) {
1405+ iframe . style . height = '100px' ;
1406+ iframe . style . top = '100px' ;
1407+ iframeWin . scrollTo ( 0 , 110 ) ;
1408+ // {root:iframeDoc} means to track the iframe viewport.
1409+ var io = new IntersectionObserver (
1410+ function ( records ) {
1411+ io . unobserve ( iframeTargetEl1 ) ;
1412+
1413+ var intersectionRect = rect ( {
1414+ top : 0 , // if root=null, then this would be 100.
1415+ left : 0 ,
1416+ height : 90 ,
1417+ width : bodyWidth
1418+ } ) ;
1419+ expect ( records . length ) . to . be ( 1 ) ;
1420+ expect ( rect ( records [ 0 ] . rootBounds ) ) . to . eql ( getRootRect ( iframeDoc ) ) ;
1421+ expect ( rect ( records [ 0 ] . intersectionRect ) ) . to . eql ( intersectionRect ) ;
1422+ done ( ) ;
1423+ } ,
1424+ { root : iframeDoc }
1425+ ) ;
1426+
1427+ io . observe ( iframeTargetEl1 ) ;
1428+ } ) ;
1429+
1430+ it ( 'handles tracking iframe viewport with rootMargin' , function ( done ) {
1431+ iframe . style . height = '100px' ;
1432+
1433+ var io = new IntersectionObserver (
1434+ function ( records ) {
1435+ io . unobserve ( iframeTargetEl1 ) ;
1436+ var intersectionRect = rect ( {
1437+ top : 0 , // if root=null, then this would be 100.
1438+ left : 0 ,
1439+ height : 200 ,
1440+ width : bodyWidth
1441+ } ) ;
1442+
1443+ // rootMargin: 100% --> 3x width + 3x height.
1444+ var expectedRootBounds = rect ( {
1445+ top : - 100 ,
1446+ left : - bodyWidth ,
1447+ width : bodyWidth * 3 ,
1448+ height : 100 * 3
1449+ } ) ;
1450+ expect ( records . length ) . to . be ( 1 ) ;
1451+ expect ( rect ( records [ 0 ] . rootBounds ) ) . to . eql ( expectedRootBounds ) ;
1452+ expect ( rect ( records [ 0 ] . intersectionRect ) ) . to . eql ( intersectionRect ) ;
1453+ done ( ) ;
1454+ } ,
1455+ { root : iframeDoc , rootMargin : '100%' }
1456+ ) ;
1457+
1458+ io . observe ( iframeTargetEl1 ) ;
1459+ } ) ;
1460+
1461+ // Current spec indicates that cross-document tracking yields
1462+ // an essentially empty IntersectionObserverEntry.
1463+ // See: https://github.com/w3c/IntersectionObserver/issues/87
1464+ it ( 'does not track cross-document elements' , function ( done ) {
1465+ var io = new IntersectionObserver (
1466+ function ( records ) {
1467+ io . unobserve ( iframeTargetEl1 )
1468+
1469+ expect ( records . length ) . to . be ( 1 ) ;
1470+ const zeroesRect = rect ( {
1471+ top : 0 ,
1472+ left : 0 ,
1473+ width : 0 ,
1474+ height : 0
1475+ } ) ;
1476+ expect ( rect ( records [ 0 ] . rootBounds ) ) . to . eql ( zeroesRect ) ;
1477+ expect ( rect ( records [ 0 ] . intersectionRect ) ) . to . eql ( zeroesRect ) ;
1478+ expect ( records . isIntersecting ) . false ;
1479+ done ( ) ;
1480+ } ,
1481+ { root : document }
1482+ ) ;
1483+
1484+ io . observe ( iframeTargetEl1 ) ;
1485+ } ) ;
1486+
14011487 it ( 'handles style changes' , function ( done ) {
14021488 var spy = sinon . spy ( ) ;
14031489
@@ -3022,6 +3108,62 @@ describe('IntersectionObserver', function() {
30223108 }
30233109 ] , done ) ;
30243110 } ) ;
3111+
3112+ it ( 'handles tracking iframe viewport' , function ( done ) {
3113+ iframe . style . height = '100px' ;
3114+ iframe . style . top = '100px' ;
3115+ iframeWin . scrollTo ( 0 , 110 ) ;
3116+ // {root:iframeDoc} means to track the iframe viewport.
3117+ var io = createObserver (
3118+ function ( records ) {
3119+ io . unobserve ( iframeTargetEl1 ) ;
3120+ var intersectionRect = rect ( {
3121+ top : 0 , // if root=null, then this would be 100.
3122+ left : 0 ,
3123+ height : 90 ,
3124+ width : bodyWidth
3125+ } ) ;
3126+ expect ( records . length ) . to . be ( 1 ) ;
3127+ expect ( rect ( records [ 0 ] . rootBounds ) ) . to . eql ( getRootRect ( iframeDoc ) ) ;
3128+ expect ( rect ( records [ 0 ] . intersectionRect ) ) . to . eql ( intersectionRect ) ;
3129+ done ( ) ;
3130+ } ,
3131+ { root : iframeDoc }
3132+ ) ;
3133+
3134+ io . observe ( iframeTargetEl1 ) ;
3135+ } ) ;
3136+
3137+ it ( 'handles tracking iframe viewport with rootMargin' , function ( done ) {
3138+ iframe . style . height = '100px' ;
3139+
3140+ var io = createObserver (
3141+ function ( records ) {
3142+ io . unobserve ( iframeTargetEl1 ) ;
3143+ var intersectionRect = rect ( {
3144+ top : 0 , // if root=null, then this would be 100.
3145+ left : 0 ,
3146+ height : 200 ,
3147+ width : bodyWidth
3148+ } ) ;
3149+
3150+ // rootMargin: 100% --> 3x width + 3x height.
3151+ var expectedRootBounds = rect ( {
3152+ top : - 100 ,
3153+ left : - bodyWidth ,
3154+ width : bodyWidth * 3 ,
3155+ height : 100 * 3
3156+ } ) ;
3157+ expect ( records . length ) . to . be ( 1 ) ;
3158+ expect ( rect ( records [ 0 ] . rootBounds ) ) . to . eql ( expectedRootBounds ) ;
3159+ expect ( rect ( records [ 0 ] . intersectionRect ) ) . to . eql ( intersectionRect ) ;
3160+ done ( ) ;
3161+ } ,
3162+ { root : iframeDoc , rootMargin : '100%' }
3163+ ) ;
3164+
3165+ io . observe ( iframeTargetEl1 ) ;
3166+ } ) ;
30253167 } ) ;
30263168 } ) ;
30273169} ) ;
0 commit comments