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

Selection setting fixed #1565

Merged
merged 3 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});