Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Event constructor polyfill (#10401)
* Add Event polyfill * Swap document.Create with Event constructor * Remove global references for window and document * Correct comment * Replace usages of document.createEvent * correct linting error * Replace document.createEvent w/ Event constructor * Replace document.createEvent in test-amp-access * Remove document.createEvent fallback in bind-impl.js * Implement corrections per code review comments - Alphabetizes imports in polyfills.js - Corrects spacing and line lengths per linting rules - Corrects return in Event polyfill - Renames event type parameter - Corrects Event initialization and removes details parameter * Add event.js to whitelist for polyfills dep-check * Add event polyfill test and correct closure compiler error * Correct tests and add type argument check * Add Event constructor to fake-dom * Construct events using proper window * Add test for default event configuration * Correct spacing
- Loading branch information
Showing
13 changed files
with
169 additions
and
112 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
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
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,57 @@ | ||
/** | ||
* Copyright 2017 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. | ||
*/ | ||
|
||
/** | ||
* @constructor | ||
*/ | ||
function Event(name, params) { | ||
if (!name) { | ||
throw new TypeError( | ||
"Failed to construct 'Event': 1 argument required but only 0 present", | ||
'event.js', | ||
20 | ||
); | ||
} | ||
params = params || {bubbles: false, cancelable: false}; | ||
const evt = self.document.createEvent('Event'); | ||
evt.initEvent( | ||
name, | ||
params.bubbles, | ||
params.cancelable | ||
); | ||
return evt; | ||
} | ||
|
||
/** | ||
* Sets the Event polyfill if it does not exist. | ||
* @param {!Window} win | ||
*/ | ||
export function install(win) { | ||
// win.Event is a function on Edge, Chrome, FF, Safari but | ||
// is an object on IE 11. | ||
if (typeof win.Event !== 'function') { | ||
|
||
// supports >= IE 9. Below IE 9, window.Event.prototype is undefined | ||
Event.prototype = win.Event.prototype; | ||
|
||
win.Object.defineProperty(win, 'Event', { | ||
configurable: false, | ||
enumerable: false, | ||
value: Event, | ||
writable: false, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.