Skip to content

Commit 0cbd58a

Browse files
committed
Integrated caret code from a previous jQuery UI mask plugin
1 parent 50288db commit 0cbd58a

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

ui/jquery.ui.autocomplete.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,9 @@ $.widget( "ui.autocomplete", {
208208
},
209209

210210
_getValue: function( value ){
211-
//find the caret
212-
this.caretPos = this.element[0].selectionStart;
211+
this.caretPos = $.ui.autocomplete.caret(this.element).end;
213212
/*
214-
* cut the string in half at the carot, split at the separators,
213+
* cut the string in half at the caret, split at the separators,
215214
* and then concat the outter array values so that editing the
216215
* middle of a string still correctly triggers the autocomplete
217216
*/
@@ -348,6 +347,34 @@ $.extend( $.ui.autocomplete, {
348347
return $.grep( array, function(value) {
349348
return matcher.test( value.label || value.value || value );
350349
});
350+
},
351+
caret: function(element, begin, end) { //Helper Function for Caret positioning taken from http://dev.jqueryui.com/browser/branches/dev/mask/ui/ui.mask.js?rev=2371
352+
var input = element[0];
353+
if (typeof begin == 'number') {
354+
end = (typeof end == 'number') ? end : begin;
355+
if (input.setSelectionRange) {
356+
input.focus();
357+
input.setSelectionRange(begin, end);
358+
} else if (input.createTextRange) {
359+
var range = input.createTextRange();
360+
range.collapse(true);
361+
range.moveEnd('character', end);
362+
range.moveStart('character', begin);
363+
range.select();
364+
}
365+
return element;
366+
} else {
367+
if (input.setSelectionRange) {
368+
begin = input.selectionStart;
369+
end = input.selectionEnd;
370+
}
371+
else if (document.selection && document.selection.createRange) {
372+
var range = document.selection.createRange();
373+
begin = 0 - range.duplicate().moveStart('character', -100000);
374+
end = begin + range.text.length;
375+
}
376+
return { begin: begin, end: end };
377+
}
351378
}
352379
});
353380

0 commit comments

Comments
 (0)