Skip to content
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

Merged
merged 17 commits into from Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions extensions/amp-access/0.1/test/test-amp-access.js
Expand Up @@ -1030,14 +1030,10 @@ describes.fakeWin('AccessService pingback', {
service.reportViewToServer_ = sandbox.spy();
const p = service.reportWhenViewed_(/* timeToView */ 2000);
return Promise.resolve().then(() => {
let clickEvent;
if (document.createEvent) {
clickEvent = document.createEvent('MouseEvent');
clickEvent.initMouseEvent('click', true, true, window, 1);
} else {
clickEvent = document.createEventObject();
clickEvent.type = 'click';
}
const clickEvent = new MouseEvent(
'click',
{bubbles: true, cancelable: true, view: window, detail: 1}
);
document.documentElement.dispatchEvent(clickEvent);
return p;
}).then(() => {}, () => {}).then(() => {
Expand Down
8 changes: 1 addition & 7 deletions extensions/amp-bind/0.1/bind-impl.js
Expand Up @@ -959,13 +959,7 @@ export class Bind {
*/
dispatchEventForTesting_(name) {
if (getMode().test) {
let event;
if (typeof this.localWin_.Event === 'function') {
event = new Event(name, {bubbles: true, cancelable: true});
} else {
event = this.localWin_.document.createEvent('Event');
event.initEvent(name, /* bubbles */ true, /* cancelable */ true);
}
const event = new Event(name, {bubbles: true, cancelable: true});
this.localWin_.dispatchEvent(event);
}
}
Expand Down
6 changes: 4 additions & 2 deletions extensions/amp-live-list/0.1/amp-live-list.js
Expand Up @@ -866,8 +866,10 @@ export class AmpLiveList extends AMP.BaseElement {
}

sendAmpDomUpdateEvent_() {
const event = this.win.document.createEvent('Event');
event.initEvent(AmpEvents.DOM_UPDATE, true, true);
const event = new this.win.Event(

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.

AmpEvents.DOM_UPDATE,
{bubbles: true, cancelable: true}
);
this.win.document.dispatchEvent(event);
}
}
Expand Down
52 changes: 13 additions & 39 deletions extensions/amp-sidebar/0.1/test/test-amp-sidebar.js
Expand Up @@ -293,16 +293,14 @@ describes.realWin('amp-sidebar 0.1 version', {
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('keydown', true, true);
}
const eventObj = new Event(
'keydown',
{bubbles: true, cancelable: true}
);
eventObj.keyCode = KeyCodes.ESCAPE;
eventObj.which = KeyCodes.ESCAPE;
const el = iframe.doc.documentElement;
el.dispatchEvent ?
el.dispatchEvent(eventObj) : el.fireEvent('onkeydown', eventObj);
el.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.false;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('true');
expect(sidebarElement.style.display).to.equal('none');
Expand Down Expand Up @@ -410,11 +408,7 @@ describes.realWin('amp-sidebar 0.1 version', {
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
const eventObj = new Event('click', {bubbles: true, cancelable: true});
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
Expand All @@ -424,9 +418,7 @@ describes.realWin('amp-sidebar 0.1 version', {
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
anchor.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.false;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('true');
expect(sidebarElement.style.display).to.equal('none');
Expand All @@ -453,11 +445,7 @@ describes.realWin('amp-sidebar 0.1 version', {
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
const eventObj = new Event('click', {bubbles: true, cancelable: true});
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
Expand All @@ -468,9 +456,7 @@ describes.realWin('amp-sidebar 0.1 version', {
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
anchor.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
Expand All @@ -497,11 +483,7 @@ describes.realWin('amp-sidebar 0.1 version', {
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
const eventObj = new Event('click', {bubbles: true, cancelable: true});
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
Expand All @@ -513,9 +495,7 @@ describes.realWin('amp-sidebar 0.1 version', {
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
anchor.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
Expand All @@ -541,14 +521,8 @@ describes.realWin('amp-sidebar 0.1 version', {
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
li.dispatchEvent ?
li.dispatchEvent(eventObj) :
li.fireEvent('onkeydown', eventObj);
const eventObj = new Event('click', {bubbles: true, cancelable: true});
li.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
Expand Down
55 changes: 16 additions & 39 deletions extensions/amp-sidebar/1.0/test/test-amp-sidebar.js
Expand Up @@ -331,16 +331,14 @@
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('keydown', true, true);
}
const eventObj = new Event(
'keydown',
{bubbles: true, cancelable: true}
);
eventObj.keyCode = KeyCodes.ESCAPE;
eventObj.which = KeyCodes.ESCAPE;
const el = iframe.doc.documentElement;
el.dispatchEvent ?
el.dispatchEvent(eventObj) : el.fireEvent('onkeydown', eventObj);
el.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.false;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('true');
expect(sidebarElement.style.display).to.equal('none');
Expand Down Expand Up @@ -448,11 +446,7 @@
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
const eventObj = new Event('click', {bubbles: true, cancelable: true});
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
Expand All @@ -462,9 +456,7 @@
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
anchor.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.false;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('true');
expect(sidebarElement.style.display).to.equal('none');
Expand Down Expand Up @@ -492,11 +484,7 @@
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
const eventObj = new Event('click', {bubbles: true, cancelable: true});
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
Expand All @@ -507,9 +495,7 @@
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
anchor.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
Expand All @@ -536,11 +522,7 @@
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
const eventObj = new Event('click', {bubbles: true, cancelable: true});
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
Expand All @@ -552,9 +534,7 @@
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
anchor.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
Expand All @@ -580,14 +560,11 @@
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
li.dispatchEvent ?
li.dispatchEvent(eventObj) :
li.fireEvent('onkeydown', eventObj);
const eventObj = new Event(
'click',
{bubbles: true, cancelable: true}
);
li.dispatchEvent(eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
Expand Down
3 changes: 1 addition & 2 deletions src/custom-element.js
Expand Up @@ -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});

Choose a reason for hiding this comment

The 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);
}

Expand Down
9 changes: 4 additions & 5 deletions src/polyfills.js
Expand Up @@ -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';

Choose a reason for hiding this comment

The 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
Expand All @@ -44,3 +42,4 @@ installObjectAssign(self);
installPromise(self);
installDocContains(self);
installArrayIncludes(self);
installEvent(self);
44 changes: 44 additions & 0 deletions src/polyfills/event.js
@@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return; since this function doesn't have a return value in other cases.

}

function Event(event, params) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Let's use name instead of event here to be clearer.

params = params || {bubbles: false, cancelable: false, detail: undefined};
const evt = win.document.createEvent('Event');
evt.initCustomEvent(
event,
params.bubbles,
params.cancelable,
params.detail
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Event.initEvent doesn't take a detail param (CustomEvent does). https://developer.mozilla.org/en-US/docs/Web/API/Event/initEvent

return evt;
}

// supports >= IE 9. Below IE 9, window.Event.prototype is undefined
Event.prototype = win.Event.prototype;

win.Event = Event;
}
6 changes: 2 additions & 4 deletions test/functional/test-base-element.js
Expand Up @@ -177,11 +177,9 @@ describe('BaseElement', () => {
const timer = timerFor(element.win);
target = document.createElement('div');

event1 = document.createEvent('Event');
event1.initEvent('event1', false, true);
event1 = new Event('event1', {bubbles: false, cancelable: true});

event2 = document.createEvent('Event');
event2.initEvent('event2', false, true);
event2 = new Event('event2', {bubbles: false, cancelable: true});

event1Promise = listenOncePromise(element.element, 'event1');
event1Promise = timer.timeoutPromise(TIMEOUT, event1Promise);
Expand Down
3 changes: 1 addition & 2 deletions test/functional/test-event-helper.js
Expand Up @@ -28,8 +28,7 @@ import * as sinon from 'sinon';
describe('EventHelper', () => {

function getEvent(name, target) {
const event = document.createEvent('Event');
event.initEvent(name, true, true);
const event = new Event(name, {bubbles: true, cancelable: true});
event.testTarget = target;
return event;
}
Expand Down