Skip to content

Commit

Permalink
Remove IE8 event.target polyfill via srcElement (#11515)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Jan 5, 2018
1 parent 43af41b commit fe10b8d
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,68 +79,6 @@ describe('SyntheticClipboardEvent', () => {
});

describe('EventInterface', () => {
it('normalizes properties from the Event interface', () => {
let expectedCount = 0;
let div;

const eventHandler = type => event => {
expect(event.target).toBe(div);
expect(event.type).toBe(type);
expectedCount++;
};

div = ReactDOM.render(
<div
onCopy={eventHandler('copy')}
onCut={eventHandler('cut')}
onPaste={eventHandler('paste')}
/>,
container,
);

let event;
event = document.createEvent('Event');
event.initEvent('copy', true, true);
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return div;
},
});
div.dispatchEvent(event);

event = document.createEvent('Event');
event.initEvent('cut', true, true);
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return div;
},
});
div.dispatchEvent(event);

event = document.createEvent('Event');
event.initEvent('paste', true, true);
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return div;
},
});
div.dispatchEvent(event);

expect(expectedCount).toBe(3);
});

it('is able to `preventDefault` and `stopPropagation`', () => {
let expectedCount = 0;

Expand Down
27 changes: 0 additions & 27 deletions packages/react-dom/src/events/__tests__/SyntheticEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,6 @@ describe('SyntheticEvent', () => {
container = null;
});

it('should normalize `target` from the nativeEvent', () => {
let node;
let expectedCount = 0;

const eventHandler = syntheticEvent => {
expect(syntheticEvent.target).toBe(node);

expectedCount++;
};
node = ReactDOM.render(<div onClick={eventHandler} />, container);

const event = document.createEvent('Event');
event.initEvent('click', true, true);
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return node;
},
});
node.dispatchEvent(event);

expect(expectedCount).toBe(1);
});

it('should be able to `preventDefault`', () => {
let node;
let expectedCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,76 +454,6 @@ describe('SyntheticKeyboardEvent', () => {
});

describe('EventInterface', () => {
it('normalizes properties from the Event interface', () => {
let expectedCount = 0;
let div;
const eventHandler = type => event => {
expect(event.target).toBe(div);
expect(event.type).toBe(type);
expectedCount++;
};
div = ReactDOM.render(
<div
onKeyDown={eventHandler('keydown')}
onKeyPress={eventHandler('keypress')}
onKeyUp={eventHandler('keyup')}
/>,
container,
);

let event;
event = new KeyboardEvent('keydown', {
keyCode: 40,
bubbles: true,
cancelable: true,
});
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return div;
},
});
div.dispatchEvent(event);

event = new KeyboardEvent('keyup', {
keyCode: 40,
bubbles: true,
cancelable: true,
});
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return div;
},
});
div.dispatchEvent(event);

event = new KeyboardEvent('keypress', {
charCode: 40,
keyCode: 40,
bubbles: true,
cancelable: true,
});
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return div;
},
});
div.dispatchEvent(event);

expect(expectedCount).toBe(3);
});

it('is able to `preventDefault` and `stopPropagation`', () => {
let expectedCount = 0;
const eventHandler = event => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,6 @@ describe('SyntheticWheelEvent', () => {
container = null;
});

it('should normalize properties from the Event interface', () => {
const events = [];
const onWheel = event => {
event.persist();
events.push(event);
};
ReactDOM.render(<div onWheel={onWheel} />, container);

const event = new MouseEvent('wheel', {
bubbles: true,
});
// Emulate IE8
Object.defineProperty(event, 'target', {
get() {},
});
Object.defineProperty(event, 'srcElement', {
get() {
return container.firstChild;
},
});
container.firstChild.dispatchEvent(event);

expect(events.length).toBe(1);
expect(events[0].target).toBe(container.firstChild);
expect(events[0].type).toBe('wheel');
});

it('should normalize properties from the MouseEvent interface', () => {
const events = [];
const onWheel = event => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/events/getEventTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {TEXT_NODE} from '../shared/HTMLNodeType';
* @return {DOMEventTarget} Target node.
*/
function getEventTarget(nativeEvent) {
let target = nativeEvent.target || nativeEvent.srcElement || window;
let target = nativeEvent.target || window;

// Normalize SVG <use> element events #4963
if (target.correspondingUseElement) {
Expand Down

0 comments on commit fe10b8d

Please sign in to comment.