@@ -5,21 +5,29 @@ require('./browsers');
5
5
6
6
var h = syn . helpers ,
7
7
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 ] ;
14
21
}
15
22
} ,
16
23
17
24
// gets the selection of an input or textarea
18
25
getSelection = function ( el ) {
19
- var real , r , start ;
26
+ var real , r , start ,
27
+ isInput = el . nodeName . toLowerCase ( ) === 'input' ;
20
28
21
29
// use selectionStart if we can
22
- if ( selectionStartAvailable ( el ) ) {
30
+ if ( isInput ? supportsSelectionStart ( el ) : el . selectionStart !== undefined ) {
23
31
// this is for opera, so we don't have to focus to type how we think we would
24
32
if ( document . activeElement && document . activeElement !== el &&
25
33
el . selectionStart === el . selectionEnd && el . selectionStart === 0 ) {
@@ -36,7 +44,7 @@ var h = syn.helpers,
36
44
//check if we aren't focused
37
45
try {
38
46
//try 2 different methods that work differently (IE breaks depending on type)
39
- if ( el . nodeName . toLowerCase ( ) === 'input' ) {
47
+ if ( isInput ) {
40
48
real = h . getWindow ( el )
41
49
. document . selection . createRange ( ) ;
42
50
r = el . createTextRange ( ) ;
@@ -76,6 +84,7 @@ var h = syn.helpers,
76
84
} ;
77
85
}
78
86
} catch ( e ) {
87
+ // the elements most likely to get caught here are input[type=number] & input[type=email]
79
88
var prop = formElExp . test ( el . nodeName ) ? "value" : "textContent" ;
80
89
81
90
return {
@@ -300,7 +309,7 @@ h.extend(syn, {
300
309
301
310
// selects text on an element
302
311
selectText : function ( el , start , end ) {
303
- if ( el . setSelectionRange && selectionStartAvailable ( el ) ) {
312
+ if ( el . setSelectionRange && supportsSelectionStart ( el ) ) {
304
313
if ( ! end ) {
305
314
syn . __tryFocus ( el ) ;
306
315
el . setSelectionRange ( start , start ) ;
0 commit comments