Skip to content

Commit b5804dd

Browse files
author
luke.whyte
committed
Added filtering of 'typeable' inputs to syn.typeable
1 parent 41f734b commit b5804dd

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/key.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@ require('./browsers');
55

66
var 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);

src/typeable.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ syn.typeable.test = function (el) {
5454
var type = syn.typeable;
5555

5656
// Inputs and textareas
57-
var typeableExp = /input|textarea/i;
57+
var typeableExp = /input|textarea/i,
58+
selectableInputs = ['textarea', 'email', 'number', 'password', 'search', 'tel', 'text', 'url'],
59+
isSelectable = function (type) {
60+
return __indexOf.call(selectableInputs, type) !== -1;
61+
};
62+
5863
type(function (el) {
59-
return typeableExp.test(el.nodeName);
64+
return typeableExp.test(el.nodeName) && isSelectable(el.type);
6065
});
6166

6267
// Content editable

0 commit comments

Comments
 (0)