Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Mask: address Chrome timing issue in qunit-friendly way

  • Loading branch information...
commit 9d768f6cff189cab41d6de03f272726f93fe768d 1 parent 5cacdca
Zach Shepherd authored

Showing 1 changed file with 18 additions and 10 deletions. Show diff stats Hide diff stats

  1. +18 10 ui/jquery.ui.mask.js
28 ui/jquery.ui.mask.js
@@ -80,16 +80,7 @@ $.widget( "ui.mask", {
80 80 if ( begin !== undefined ) {
81 81 end = ( end === undefined ) ? begin : end;
82 82 if ( dom.setSelectionRange ) {
83   - /* Chrome fires off the focus events before positioning the
84   - cursor based on where the user clicked. This is annoying
85   - because, in the case of tyring to position the cursor at
86   - the beginning of an empty input, the eventual positioning
87   - based on the user's click overrides whatever we do here.
88   - There's no good fix for this except to wait until the
89   - click processesing resolves and _then_ reposition things. */
90   - setTimeout(function(){
91   - dom.setSelectionRange( begin, end );
92   - }, /* yes, zero is long enough */ 0);
  83 + dom.setSelectionRange( begin, end );
93 84 } else if ( dom.createTextRange ) {
94 85 range = dom.createTextRange();
95 86 range.collapse( true );
@@ -174,8 +165,25 @@ $.widget( "ui.mask", {
174 165 this.lastUnsavedValue = this.element.val();
175 166 this._paint( true );
176 167 this._caretSelect( this._seekRight( this._parseValue() ) );
  168 +
  169 + this._justFocused = true;
  170 + this._delay(function(){
  171 + this._justFocused = false;
  172 + }, 100);
  173 + },
  174 + click: function( event ) {
  175 + /* Normally, the call to handle this in the focus event handler would be
  176 + sufficient, but Chrome fires the focus events before positioning the
  177 + cursor based on where the user clicked (and then fires the click event). */
  178 +
  179 + // We only want to move the caret on clicks that resulted in focus
  180 + if ( this._justFocused ) {
  181 + this._caretSelect( this._seekRight( this._parseValue() ) );
  182 + this._justFocused = false;
  183 + }
177 184 },
178 185 blur: function( event ) {
  186 + this._justFocused = false;
179 187
180 188 // because we are constantly setting the value of the input, the change event
181 189 // never fires - we re-introduce the change event here

0 comments on commit 9d768f6

Please sign in to comment.
Something went wrong with that request. Please try again.