-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Universal IntersectionObserver polyfill (#27595)
* Universal IntersectionObserver polyfill * fix starting condition * Implement the actual polyfill * Review fixes * New version of polyfill that supports FIE * Experiment guard * tests * Update polyfill to 0.9.0 and remove inabox experiment * fixing tests... * isIntersecting auto-polyfill * Allow some code dependencies to src/polyfills/in-ob.hs * Split code to simplify dep-check rules * Move scheduleUpgradeIfNeeded to stub modules * lints * esm dist command * remove --esm dist * fix deps
- Loading branch information
Dima Voytenko
committed
Apr 22, 2020
1 parent
b45e30d
commit 419131a
Showing
16 changed files
with
930 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
{ | ||
"experimentA": {}, | ||
"experimentA": { | ||
"name": "IntersectionObserverPolyfill", | ||
"environment": "AMP", | ||
"command": "gulp dist --define_experiment_constant=INTERSECTION_OBSERVER_POLYFILL", | ||
"issue": "https://github.com/ampproject/amphtml/issues/27807", | ||
"expirationDateUTC": "2020-05-31", | ||
"defineExperimentConstant": "INTERSECTION_OBSERVER_POLYFILL" | ||
}, | ||
"experimentB": {}, | ||
"experimentC": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
extensions/amp-intersection-observer-polyfill/0.1/amp-intersection-observer-polyfill.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {installIntersectionObserver} from 'intersection-observer/intersection-observer.install'; | ||
import {registerServiceBuilder} from '../../../src/service'; | ||
import {upgradePolyfill} from '../../../src/polyfillstub/intersection-observer-stub'; | ||
|
||
const TAG = 'amp-intersection-observer-polyfill'; | ||
|
||
/** | ||
* @param {!Window} win | ||
* @return {!Object} | ||
*/ | ||
function upgrade(win) { | ||
upgradePolyfill(win, () => { | ||
installIntersectionObserver(); | ||
// TODO(dvoytenko): hookup 3p host updater for inabox. | ||
}); | ||
return {}; | ||
} | ||
|
||
/** | ||
* Registers the polyfill. | ||
* @param {!Window} win | ||
*/ | ||
export function upgradeIntersectionObserverPolyfill(win) { | ||
registerServiceBuilder(win, TAG, upgrade, /* instantiate */ true); | ||
} | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
AMP.extension(TAG, '0.1', function (AMP) { | ||
upgradeIntersectionObserverPolyfill(window); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview | ||
* See https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver. | ||
*/ | ||
|
||
import {IntersectionObserverStub} from '../polyfillstub/intersection-observer-stub'; | ||
|
||
/** | ||
* @param {!Window} win | ||
*/ | ||
export function install(win) { | ||
if (!win.IntersectionObserver) { | ||
win.IntersectionObserver = /** @type {typeof IntersectionObserver} */ (IntersectionObserverStub); | ||
} | ||
fixEntry(win); | ||
} | ||
|
||
/** | ||
* @param {!Window} parentWin | ||
* @param {!Window} childWin | ||
*/ | ||
export function installForChildWin(parentWin, childWin) { | ||
if (childWin.IntersectionObserver) { | ||
fixEntry(childWin); | ||
} else if (parentWin.IntersectionObserver) { | ||
Object.defineProperties(childWin, { | ||
IntersectionObserver: {get: () => parentWin.IntersectionObserver}, | ||
IntersectionObserverEntry: { | ||
get: () => parentWin.IntersectionObserverEntry, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
/** @param {!Window} win */ | ||
function fixEntry(win) { | ||
// Minimal polyfill for Edge 15's lack of `isIntersecting` | ||
// See: https://github.com/w3c/IntersectionObserver/issues/211 | ||
if ( | ||
win.IntersectionObserverEntry && | ||
!('isIntersecting' in win.IntersectionObserverEntry.prototype) | ||
) { | ||
Object.defineProperty( | ||
win.IntersectionObserverEntry.prototype, | ||
'isIntersecting', | ||
{ | ||
enumerable: true, | ||
configurable: true, | ||
get() { | ||
return this.intersectionRatio > 0; | ||
}, | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.