Skip to content

Commit

Permalink
Fix selecting then Tabbing away and back then selecting
Browse files Browse the repository at this point in the history
Steps to repro:
1. select stuff
2. while there's a selection, Tab away and back
3. at this point the math field should be focused and have a blinking
   cursor and no selection. Do Shift-Left or Shift-Right

The resulting selection will be quite wrong. This is because the
anticursor used is from the selection that was cleared when Tabbing
away.
  • Loading branch information
laughinghan committed Feb 23, 2016
1 parent ba1efa4 commit 5fa0ff5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/services/focusBlur.js
Expand Up @@ -18,7 +18,7 @@ Controller.open(function(_) {
ctrlr.blurred = true;
blurTimeout = setTimeout(function() { // wait for blur on window; if
root.postOrder('intentionalBlur'); // none, intentional blur: #264
cursor.clearSelection();
cursor.clearSelection().endSelection();
blur();
});
$(window).on('blur', windowBlur);
Expand Down
39 changes: 36 additions & 3 deletions test/unit/focusBlur.test.js
@@ -1,4 +1,8 @@
suite('focusBlur', function() {
function assertHasFocus(mq, name, invert) {
assert.ok(!!invert ^ $(mq.el()).find('textarea').is(':focus'), name + (invert ? ' does not have focus' : ' has focus'));
}

suite('handlers can shift focus away', function() {
var mq, mq2, wasUpOutOfCalled;
setup(function() {
Expand All @@ -21,9 +25,6 @@ suite('focusBlur', function() {
$(mq.el()).find('textarea').trigger(jQuery.Event('keydown', { which: 38 }));
assert.ok(wasUpOutOfCalled);
}
function assertHasFocus(mq, name) {
assert.ok($(mq.el()).find('textarea').is(':focus'), name + ' has focus');
}

test('normally', function() {
mq.focus();
Expand All @@ -50,4 +51,36 @@ suite('focusBlur', function() {
});
});
});

test('select behaves normally after blurring and re-focusing', function(done) {
var mq = MQ.MathField($('<span></span>').appendTo('#mock')[0]);

mq.focus();
assertHasFocus(mq, 'mq');

mq.typedText('asdf');
assert.equal(mq.latex(), 'asdf');

mq.keystroke('Shift-Left');
setTimeout(function() {
assert.equal($(mq.el()).find('textarea').val(), 'f');

mq.blur();
assertHasFocus(mq, 'mq', 'not');
setTimeout(function() {
assert.equal($(mq.el()).find('textarea').val(), '');

mq.focus();
assertHasFocus(mq, 'mq');

mq.keystroke('Shift-Left');
setTimeout(function() {
assert.equal($(mq.el()).find('textarea').val(), 'd');

$(mq.el()).remove();
done();
});
}, 10);
});
});
});

0 comments on commit 5fa0ff5

Please sign in to comment.