Skip to content

Commit

Permalink
Selection setting fixed (#1565)
Browse files Browse the repository at this point in the history
* fix

* test added

* test fixed
  • Loading branch information
helen-dikareva authored and miherlosev committed Apr 11, 2018
1 parent 9871e36 commit 8156e91
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/client/sandbox/event/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ export default class Selection {

wrapSetterSelection (el, selectionSetter, needFocus, isContentEditable) {
const curDocument = domUtils.findDocument(el);
let activeElement = null;
let activeElement = domUtils.getActiveElement(curDocument);
let result = null;
let focusRaised = false;

// NOTE: we should not call focus during selection setting
// if element has been focused already (TestCafe GH-2301)
needFocus = needFocus && activeElement !== el;

const focusHandler = e => {
if (e.target === el || el.style.display === 'none')
focusRaised = true;
Expand Down Expand Up @@ -241,6 +245,7 @@ export default class Selection {
}
}
}

return result;
}
}
89 changes: 89 additions & 0 deletions test/client/fixtures/sandbox/selection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var isIE = browserUtils.isIE;
var isMobileBrowser = browserUtils.isIOS || browserUtils.isAndroid;
var browserVersion = browserUtils.version;

var FOCUS_TIMEOUT = browserUtils.isIE11 ? 100 : 0;

var createTestInput = function (type, value) {
var input = document.createElement('input');

Expand Down Expand Up @@ -80,3 +82,90 @@ test('Focus should stay on input with "number" type after setting selection', fu
strictEqual(document.activeElement, input);
document.body.removeChild(input);
});

asyncTest('Focus should not be called during setting selection if conteneditable element has been already focused (TestCafe GH - 2301)', function () {
var div = document.createElement('div');

div.setAttribute('contenteditable', 'true');
div.textContent = 'some text';

document.body.appendChild(div);

var focused = false;
var selectionSet = false;

div.addEventListener('focus', function () {
focused = true;
});

selectionSandbox.wrapSetterSelection(div, function () {
selectionSet = true;
}, true, true);

window.setTimeout(function () {
if (!browserUtils.isFirefox)
ok(focused);

ok(selectionSet);
div.focus();

window.setTimeout(function () {
focused = false;
selectionSet = false;

selectionSandbox.wrapSetterSelection(div, function () {
selectionSet = true;
}, true, true);

window.setTimeout(function () {
notOk(focused);
ok(selectionSet);
strictEqual(document.activeElement, div);

document.body.removeChild(div);
start();
}, FOCUS_TIMEOUT);
}, FOCUS_TIMEOUT);
}, FOCUS_TIMEOUT);
});

asyncTest('Focus should not be called during setting selection if editable element has been already focused (TestCafe GH - 2301)', function () {
var input = createTestInput('text', 'some text');
var focused = false;
var shouldBeFocused = browserUtils.isIE11 || browserUtils.isMSEdge || browserUtils.isSafari;
var startPos = 1;
var endPos = 3;

var isSelectionSet = function () {
strictEqual(input.selectionStart, startPos);
strictEqual(input.selectionEnd, endPos);
};

input.addEventListener('focus', function () {
focused = true;
});

input.setSelectionRange(startPos, endPos);

window.setTimeout(function () {
strictEqual(focused, shouldBeFocused);
isSelectionSet();

input.focus();

window.setTimeout(function () {
focused = false;

input.setSelectionRange(startPos, endPos);

window.setTimeout(function () {
notOk(focused);
isSelectionSet();
strictEqual(document.activeElement, input);

document.body.removeChild(input);
start();
}, FOCUS_TIMEOUT);
}, FOCUS_TIMEOUT);
}, FOCUS_TIMEOUT);
});

0 comments on commit 8156e91

Please sign in to comment.