Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c0afe4d

Browse files
committed
fixup: call the listener directly instead of triggering an input event
1 parent a0ceb82 commit c0afe4d

File tree

2 files changed

+49
-64
lines changed

2 files changed

+49
-64
lines changed

src/ng/directive/input.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,9 +1141,9 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
11411141
// without an accompanying change in the input value (thus no `input` event).
11421142
// (This is only necessary on browsers that support inputs of that type - other browsers set the
11431143
// `type` property to "text".)
1144-
var isTypeSupported = (type === attr.type);
1145-
var listenForKeyup = isTypeSupported && (DATE_INPUT_TYPES.indexOf(type) !== -1);
1146-
if (listenForKeyup) element.on('keyup', function() { element.triggerHandler('input'); });
1144+
var browserSupportsType = (type === attr.type);
1145+
var listenForKeyup = browserSupportsType && (DATE_INPUT_TYPES.indexOf(type) !== -1);
1146+
if (listenForKeyup) element.on('keyup', function() { listener('input'); });
11471147
} else {
11481148
var timeout;
11491149

test/ng/directive/inputSpec.js

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* globals getInputCompileHelper: false */
44

5-
describe('input', function() {
5+
ddescribe('input', function() {
66
var helper, $compile, $rootScope, $browser, $sniffer, $timeout, $q;
77

88
beforeEach(function() {
@@ -707,21 +707,18 @@ describe('input', function() {
707707
});
708708

709709

710-
it('should fire \'input\' event on \'keyup\' only on browsers that support this input type',
711-
function() {
712-
var inputType = 'month';
713-
714-
var inputEventExpectedToFire = browserSupportsInputTypeAndEvent(inputType, 'input');
715-
var inputEventActuallyFired = false;
710+
it('should listen for \'keyup\' only on browsers that support this input type', function() {
711+
var inputType = 'month';
716712

717-
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
718-
inputElm.on('input', function() { inputEventActuallyFired = true; });
713+
var shouldListenForKeyup = browserSupportsInputTypeAndEvent(inputType, 'input');
714+
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
715+
var ctrl = inputElm.controller('ngModel');
716+
spyOn(ctrl, '$setViewValue');
719717

720-
browserTrigger(inputElm, 'keyup');
718+
inputElm.triggerHandler('keyup');
721719

722-
expect(inputEventActuallyFired).toBe(inputEventExpectedToFire);
723-
}
724-
);
720+
expect(ctrl.$setViewValue.callCount).toBe(shouldListenForKeyup ? 1 : 0);
721+
});
725722

726723

727724
describe('min', function() {
@@ -989,21 +986,18 @@ describe('input', function() {
989986
});
990987

991988

992-
it('should fire \'input\' event on \'keyup\' only on browsers that support this input type',
993-
function() {
994-
var inputType = 'week';
995-
996-
var inputEventExpectedToFire = browserSupportsInputTypeAndEvent(inputType, 'input');
997-
var inputEventActuallyFired = false;
989+
it('should listen for \'keyup\' only on browsers that support this input type', function() {
990+
var inputType = 'week';
998991

999-
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
1000-
inputElm.on('input', function() { inputEventActuallyFired = true; });
992+
var shouldListenForKeyup = browserSupportsInputTypeAndEvent(inputType, 'input');
993+
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
994+
var ctrl = inputElm.controller('ngModel');
995+
spyOn(ctrl, '$setViewValue');
1001996

1002-
browserTrigger(inputElm, 'keyup');
997+
inputElm.triggerHandler('keyup');
1003998

1004-
expect(inputEventActuallyFired).toBe(inputEventExpectedToFire);
1005-
}
1006-
);
999+
expect(ctrl.$setViewValue.callCount).toBe(shouldListenForKeyup ? 1 : 0);
1000+
});
10071001

10081002

10091003
describe('min', function() {
@@ -1306,21 +1300,18 @@ describe('input', function() {
13061300
});
13071301

13081302

1309-
it('should fire \'input\' event on \'keyup\' only on browsers that support this input type',
1310-
function() {
1311-
var inputType = 'datetime-local';
1312-
1313-
var inputEventExpectedToFire = browserSupportsInputTypeAndEvent(inputType, 'input');
1314-
var inputEventActuallyFired = false;
1303+
it('should listen for \'keyup\' only on browsers that support this input type', function() {
1304+
var inputType = 'datetime-local';
13151305

1316-
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
1317-
inputElm.on('input', function() { inputEventActuallyFired = true; });
1306+
var shouldListenForKeyup = browserSupportsInputTypeAndEvent(inputType, 'input');
1307+
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
1308+
var ctrl = inputElm.controller('ngModel');
1309+
spyOn(ctrl, '$setViewValue');
13181310

1319-
browserTrigger(inputElm, 'keyup');
1311+
inputElm.triggerHandler('keyup');
13201312

1321-
expect(inputEventActuallyFired).toBe(inputEventExpectedToFire);
1322-
}
1323-
);
1313+
expect(ctrl.$setViewValue.callCount).toBe(shouldListenForKeyup ? 1 : 0);
1314+
});
13241315

13251316

13261317
describe('min', function() {
@@ -1699,21 +1690,18 @@ describe('input', function() {
16991690
});
17001691

17011692

1702-
it('should fire \'input\' event on \'keyup\' only on browsers that support this input type',
1703-
function() {
1704-
var inputType = 'time';
1705-
1706-
var inputEventExpectedToFire = browserSupportsInputTypeAndEvent(inputType, 'input');
1707-
var inputEventActuallyFired = false;
1693+
it('should listen for \'keyup\' only on browsers that support this input type', function() {
1694+
var inputType = 'time';
17081695

1709-
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
1710-
inputElm.on('input', function() { inputEventActuallyFired = true; });
1696+
var shouldListenForKeyup = browserSupportsInputTypeAndEvent(inputType, 'input');
1697+
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
1698+
var ctrl = inputElm.controller('ngModel');
1699+
spyOn(ctrl, '$setViewValue');
17111700

1712-
browserTrigger(inputElm, 'keyup');
1701+
inputElm.triggerHandler('keyup');
17131702

1714-
expect(inputEventActuallyFired).toBe(inputEventExpectedToFire);
1715-
}
1716-
);
1703+
expect(ctrl.$setViewValue.callCount).toBe(shouldListenForKeyup ? 1 : 0);
1704+
});
17171705

17181706

17191707
describe('min', function() {
@@ -2067,21 +2055,18 @@ describe('input', function() {
20672055
});
20682056

20692057

2070-
it('should fire \'input\' event on \'keyup\' only on browsers that support this input type',
2071-
function() {
2072-
var inputType = 'date';
2058+
it('should listen for \'keyup\' only on browsers that support this input type', function() {
2059+
var inputType = 'date';
20732060

2074-
var inputEventExpectedToFire = browserSupportsInputTypeAndEvent(inputType, 'input');
2075-
var inputEventActuallyFired = false;
2061+
var shouldListenForKeyup = browserSupportsInputTypeAndEvent(inputType, 'input');
2062+
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
2063+
var ctrl = inputElm.controller('ngModel');
2064+
spyOn(ctrl, '$setViewValue');
20762065

2077-
var inputElm = helper.compileInput('<input type="' + inputType + '" ng-model="val" />');
2078-
inputElm.on('input', function() { inputEventActuallyFired = true; });
2066+
inputElm.triggerHandler('keyup');
20792067

2080-
browserTrigger(inputElm, 'keyup');
2081-
2082-
expect(inputEventActuallyFired).toBe(inputEventExpectedToFire);
2083-
}
2084-
);
2068+
expect(ctrl.$setViewValue.callCount).toBe(shouldListenForKeyup ? 1 : 0);
2069+
});
20852070

20862071

20872072
describe('min', function() {

0 commit comments

Comments
 (0)