New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Event constructor polyfill #10401
Changes from 9 commits
b13f4fa
dd801be
61e9b46
c8c7238
3fa3472
55138c6
8b3cd17
daa07d6
1eaee49
ae21c92
e48a11d
aa3c2b6
c84f541
c65d474
f8c39a9
05d6a70
7afa7ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1111,9 +1111,8 @@ function createBaseCustomElementClass(win) { | |
const data = opt_data || {}; | ||
// Constructors of events need to come from the correct window. Sigh. | ||
const win = this.ownerDocument.defaultView; | ||
const event = win.document.createEvent('Event'); | ||
const event = new win.Event(name, {bubbles: true, cancelable: true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
event.data = data; | ||
event.initEvent(name, /* bubbles */ true, /* cancelable */ true); | ||
this.dispatchEvent(event); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,15 @@ | |
|
||
// Importing the document-register-element module has the side effect | ||
// of installing the custom elements polyfill if necessary. | ||
import installCustomElements from | ||
'document-register-element/build/document-register-element.node'; | ||
import { | ||
install as installDOMTokenListToggle, | ||
} from './polyfills/domtokenlist-toggle'; | ||
import installCustomElements from 'document-register-element/build/document-register-element.node'; | ||
import {install as installDOMTokenListToggle} from './polyfills/domtokenlist-toggle'; | ||
import {install as installDocContains} from './polyfills/document-contains'; | ||
import {install as installMathSign} from './polyfills/math-sign'; | ||
import {install as installObjectAssign} from './polyfills/object-assign'; | ||
import {install as installPromise} from './polyfills/promise'; | ||
import {install as installArrayIncludes} from './polyfills/array-includes'; | ||
import {getMode} from './mode'; | ||
import {install as installEvent} from './polyfills/event'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Please alphabetize. |
||
|
||
/** | ||
Only install in closure binary and not in babel/browserify binary, since in | ||
|
@@ -44,3 +42,4 @@ installObjectAssign(self); | |
installPromise(self); | ||
installDocContains(self); | ||
installArrayIncludes(self); | ||
installEvent(self); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* 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') { | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
function Event(event, params) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: Let's use |
||
params = params || {bubbles: false, cancelable: false, detail: undefined}; | ||
const evt = win.document.createEvent('Event'); | ||
evt.initCustomEvent( | ||
event, | ||
params.bubbles, | ||
params.cancelable, | ||
params.detail | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return evt; | ||
} | ||
|
||
// supports >= IE 9. Below IE 9, window.Event.prototype is undefined | ||
Event.prototype = win.Event.prototype; | ||
|
||
win.Event = Event; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const event = new Event(
There are situations where we embed an AMP doc inside another AMP doc, wrapped in an iframe. In those cases, the polyfill won't be installed in the iframe's window so we should make sure we construct events using the top window.