Skip to content

Commit

Permalink
fix textInput dispatch in safari
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev committed Apr 25, 2018
1 parent a246d72 commit 7bd9a08
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
25 changes: 21 additions & 4 deletions src/client/automation/playback/type/type-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,33 @@ function _excludeInvisibleSymbolsFromSelection (selection) {
return selection;
}

// NOTE: typing can be prevented in Chrome/Edge but can not be prevented in IE11 or Firefox
// NOTE: Typing can be prevented in Chrome/Edge but can not be prevented in IE11 or Firefox
// Firefox does not support TextInput event
// Safari support TextInput event but adds e.data to node value. So let's ignore it
// Safari supports the TextInput event but has a bug: e.data is added to the node value.
// So in Safari we need to call preventDefault in the last textInput handler but not prevent the Input event

let forceInputInSafari;

function simulateTextInput (element, text) {
const isTextInputIgnoredByBrowser = [ browserUtils.isFirefox, browserUtils.isSafari ].some(browser => browser);
const isInputEventRequired = isTextInputIgnoredByBrowser || eventSimulator.textInput(element, text);
forceInputInSafari = false;

if (browserUtils.isSafari)
document.addEventListener('textInput', onSafariTextInput);

const isInputEventRequired = browserUtils.isFirefox || eventSimulator.textInput(element, text) || forceInputInSafari;

if (browserUtils.isSafari)
document.removeEventListener('textInput', onSafariTextInput);

return isInputEventRequired || browserUtils.isIE11;
}

function onSafariTextInput (e) {
forceInputInSafari = !e.defaultPrevented;

e.preventDefault();
}

function _typeTextToContentEditable (element, text) {
var currentSelection = _getSelectionInElement(element);
var startNode = currentSelection.startPos.node;
Expand Down
2 changes: 1 addition & 1 deletion test/functional/fixtures/regression/gh-1956/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var expect = require('chai').expect;

var browsersWithLimitations = [ 'ie', 'safari', 'firefox', 'firefox-osx', 'ipad', 'iphone' ];
var browsersWithLimitations = [ 'ie', 'firefox', 'firefox-osx' ];

describe('Should support TextInput event[Regression](GH-1956)', function () {
it('Prevent Input event on TextInput when type to input element', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const contentEditableWithElementNode = Selector('#contentEditableWithElementNode
const contentEditableWithModify = Selector('#contentEditableWithModify');
const contentEditableWithReplace = Selector('#contentEditableWithReplace');

// NOTE: Chrome/Edge. Typing is prevented and Input event is not raised
// NOTE: Chrome/Edge/Safari. Typing is prevented and Input event is not raised
test('Prevent Input event on TextInput when type to input element', async t => {
await t
.typeText(simpleInput, 'Hello')
Expand All @@ -23,7 +23,7 @@ test('Prevent Input event on TextInput when type to input element IE11/Firefox',
.expect(simpleInput.value).eql('Hello');
});

// NOTE: Chrome/Edge. Typing is prevented. Input event is not raised
// NOTE: Chrome/Edge/Safari. Typing is prevented. Input event is not raised
test('Prevent Input event on TextInput when type to ContentEditable div', async t => {
await t
.typeText(simpleContentEditable, 'Hello')
Expand Down

0 comments on commit 7bd9a08

Please sign in to comment.