@@ -5,21 +5,29 @@ require('./browsers');
55
66var h = syn . helpers ,
77
8- selectionStartAvailable = function ( el ) {
9- // checking for selectionStart might throw error
10- try {
11- return el . selectionStart !== undefined ;
12- } catch ( e ) {
13- return false ;
8+ inputSupportsSelectionStart = { } ,
9+
10+ supportsSelectionStart = function ( el ) {
11+ // checking selection attrs will trigger errors on all inputs but text, url, search, tel & password.
12+ // Ultimately, this creates an issue specifically w/ email and number inputs
13+ if ( ! inputSupportsSelectionStart [ el . type ] ) { // try to avoid having to run the try/catch repeatedly
14+ try {
15+ return inputSupportsSelectionStart [ el . type ] = el . selectionStart !== undefined ;
16+ } catch ( e ) {
17+ return inputSupportsSelectionStart [ el . type ] = false ;
18+ }
19+ } else {
20+ return inputSupportsSelectionStart [ el . type ] ;
1421 }
1522 } ,
1623
1724 // gets the selection of an input or textarea
1825 getSelection = function ( el ) {
19- var real , r , start ;
26+ var real , r , start ,
27+ isInput = el . nodeName . toLowerCase ( ) === 'input' ;
2028
2129 // use selectionStart if we can
22- if ( selectionStartAvailable ( el ) ) {
30+ if ( isInput ? supportsSelectionStart ( el ) : el . selectionStart !== undefined ) {
2331 // this is for opera, so we don't have to focus to type how we think we would
2432 if ( document . activeElement && document . activeElement !== el &&
2533 el . selectionStart === el . selectionEnd && el . selectionStart === 0 ) {
@@ -36,7 +44,7 @@ var h = syn.helpers,
3644 //check if we aren't focused
3745 try {
3846 //try 2 different methods that work differently (IE breaks depending on type)
39- if ( el . nodeName . toLowerCase ( ) === 'input' ) {
47+ if ( isInput ) {
4048 real = h . getWindow ( el )
4149 . document . selection . createRange ( ) ;
4250 r = el . createTextRange ( ) ;
@@ -76,6 +84,7 @@ var h = syn.helpers,
7684 } ;
7785 }
7886 } catch ( e ) {
87+ // the elements most likely to get caught here are input[type=number] & input[type=email]
7988 var prop = formElExp . test ( el . nodeName ) ? "value" : "textContent" ;
8089
8190 return {
@@ -300,7 +309,7 @@ h.extend(syn, {
300309
301310 // selects text on an element
302311 selectText : function ( el , start , end ) {
303- if ( el . setSelectionRange && selectionStartAvailable ( el ) ) {
312+ if ( el . setSelectionRange && supportsSelectionStart ( el ) ) {
304313 if ( ! end ) {
305314 syn . __tryFocus ( el ) ;
306315 el . setSelectionRange ( start , start ) ;
0 commit comments