Skip to content

Commit c85137d

Browse files
committed
fix(inputs): ignoring input events in IE caused by placeholder changes or focus/blur on inputs with placeholders
1 parent 23e5109 commit c85137d

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/ng/directive/input.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ function addNativeHtml5Validators(ctrl, validatorName, badFlags, ignoreFlags, va
475475

476476
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
477477
var validity = element.prop(VALIDITY_STATE_PROPERTY);
478-
var placeholder = element[0].placeholder, noevent = {};
479478
ctrl.$$validityState = validity;
480479

481480
// In composition mode, users are still inputing intermediate text buffer,
@@ -494,18 +493,33 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
494493
});
495494
}
496495

496+
// IE (11 and under) seem to emit an 'input' event if the placeholder value changes or when
497+
// an input with a placeholder gains or loses focus. These issues do not happen if the input
498+
// has a value at the time.
499+
if (msie && ('placeholder' in element[0]) && $sniffer.hasEvent('input')) {
500+
var ignoreNextInput;
501+
502+
// Changing placeholders triggers an input if there is no value
503+
attr.$observe("placeholder", function ieIgnoreNextInputPlaceholder() {
504+
ignoreNextInput = !element[0].value;
505+
});
506+
507+
// Focus in/out triggers an input if there is no value but there is a placeholder
508+
// Event orders:
509+
// focus: focusin focus input
510+
// blur: focusout input blur
511+
element.on("focusin focusout", function ieIgnoreNextInputFocus() {
512+
ignoreNextInput = this.placeholder && !this.value;
513+
});
514+
}
515+
497516
var listener = function(ev) {
498517
if (composing) return;
499-
var value = element.val();
500-
501-
// IE (11 and under) seem to emit an 'input' event if the placeholder value changes.
502-
// We don't want to dirty the value when this happens, so we abort here. Unfortunately,
503-
// IE also sends input events for other non-input-related things, (such as focusing on a
504-
// form control), so this change is not entirely enough to solve this.
505-
if (msie && (ev || noevent).type === 'input' && element[0].placeholder !== placeholder) {
506-
placeholder = element[0].placeholder;
518+
if (ignoreNextInput && ev && ev.type === 'input') {
519+
ignoreNextInput = false;
507520
return;
508521
}
522+
var value = element.val();
509523

510524
// By default we will trim the value
511525
// If the attribute ng-trim exists we will avoid trimming

0 commit comments

Comments
 (0)