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

Commit 8b86a9e

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

File tree

5 files changed

+185
-26
lines changed

5 files changed

+185
-26
lines changed

src/ng/directive/input.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,6 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
930930
}
931931

932932
function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
933-
var placeholder = element[0].placeholder, noevent = {};
934933
var type = lowercase(element[0].type);
935934

936935
// In composition mode, users are still inputing intermediate text buffer,
@@ -954,15 +953,6 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
954953
var value = element.val(),
955954
event = ev && ev.type;
956955

957-
// IE (11 and under) seem to emit an 'input' event if the placeholder value changes.
958-
// We don't want to dirty the value when this happens, so we abort here. Unfortunately,
959-
// IE also sends input events for other non-input-related things, (such as focusing on a
960-
// form control), so this change is not entirely enough to solve this.
961-
if (msie && (ev || noevent).type === 'input' && element[0].placeholder !== placeholder) {
962-
placeholder = element[0].placeholder;
963-
return;
964-
}
965-
966956
// By default we will trim the value
967957
// If the attribute ng-trim exists we will avoid trimming
968958
// If input type is 'password', the value is never trimmed

src/ng/sniffer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ function $SnifferProvider() {
6767
// IE9 implements 'input' event it's so fubared that we rather pretend that it doesn't have
6868
// it. In particular the event is not fired when backspace or delete key are pressed or
6969
// when cut operation is performed.
70-
if (event == 'input' && msie == 9) return false;
70+
// IE10+ implements 'input' event but it erroneously fires under various situations,
71+
// e.g. when placeholder changes, or a form is focused.
72+
if (event === 'input' && msie <= 11) return false;
7173

7274
if (isUndefined(eventSupport[event])) {
7375
var divElm = document.createElement('div');

src/ngScenario/dsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ angular.scenario.dsl('binding', function() {
199199
*/
200200
angular.scenario.dsl('input', function() {
201201
var chain = {};
202-
var supportInputEvent = 'oninput' in document.createElement('div') && msie != 9;
202+
var supportInputEvent = 'oninput' in document.createElement('div') && !(msie <= 11)
203203

204204
chain.enter = function(value, event) {
205205
return this.addFutureAction("input '" + this.name + "' enter '" + value + "'",

test/ng/directive/inputSpec.js

Lines changed: 179 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,22 +1388,188 @@ describe('input', function() {
13881388
expect(scope.name).toEqual('caitp');
13891389
});
13901390

1391-
it('should not dirty the model on an input event in response to a placeholder change', inject(function($sniffer) {
1392-
if (msie && $sniffer.hasEvent('input')) {
1393-
compileInput('<input type="text" ng-model="name" name="name" />');
1394-
inputElm.attr('placeholder', 'Test');
1395-
browserTrigger(inputElm, 'input');
1391+
describe("IE placeholder input events", function() {
1392+
//IE fires an input event whenever a placeholder visually changes, essentially treating it as a value
1393+
//Events:
1394+
// placeholder attribute change: *input*
1395+
// focus (which visually removes the placeholder value): focusin focus *input*
1396+
// blur (which visually creates the placeholder value): focusout *input* blur
1397+
//However none of these occur if the placeholder is not visible at the time of the event.
1398+
//These tests try simulate various scenerios which do/do-not fire the extra input event
1399+
1400+
it('should not dirty the model on an input event in response to a placeholder change', function() {
1401+
if (msie) {
1402+
compileInput('<input type="text" placeholder="Test" attr-capture ng-model="unsetValue" name="name" />');
1403+
browserTrigger(inputElm, 'input');
1404+
expect(inputElm.attr('placeholder')).toBe('Test');
1405+
expect(inputElm).toBePristine();
1406+
1407+
attrs.$set('placeholder', '');
1408+
browserTrigger(inputElm, 'input');
1409+
expect(inputElm.attr('placeholder')).toBe('');
1410+
expect(inputElm).toBePristine();
1411+
1412+
attrs.$set('placeholder', 'Test Again');
1413+
browserTrigger(inputElm, 'input');
1414+
expect(inputElm.attr('placeholder')).toBe('Test Again');
1415+
expect(inputElm).toBePristine();
1416+
1417+
attrs.$set('placeholder', undefined);
1418+
browserTrigger(inputElm, 'input');
1419+
expect(inputElm.attr('placeholder')).toBe(undefined);
1420+
expect(inputElm).toBePristine();
1421+
1422+
changeInputValueTo('foo');
1423+
expect(inputElm).toBeDirty();
1424+
}
1425+
});
13961426

1397-
expect(inputElm.attr('placeholder')).toBe('Test');
1398-
expect(inputElm).toBePristine();
1427+
it('should not dirty the model on an input event in response to a interpolated placeholder change', inject(function($rootScope) {
1428+
if (msie) {
1429+
compileInput('<input type="text" placeholder="{{ph}}" ng-model="unsetValue" name="name" />');
1430+
browserTrigger(inputElm, 'input');
1431+
expect(inputElm).toBePristine();
13991432

1400-
inputElm.attr('placeholder', 'Test Again');
1401-
browserTrigger(inputElm, 'input');
1433+
$rootScope.ph = 1;
1434+
$rootScope.$digest();
1435+
browserTrigger(inputElm, 'input');
1436+
expect(inputElm).toBePristine();
14021437

1403-
expect(inputElm.attr('placeholder')).toBe('Test Again');
1404-
expect(inputElm).toBePristine();
1405-
}
1406-
}));
1438+
$rootScope.ph = "";
1439+
$rootScope.$digest();
1440+
browserTrigger(inputElm, 'input');
1441+
expect(inputElm).toBePristine();
1442+
1443+
changeInputValueTo('foo');
1444+
expect(inputElm).toBeDirty();
1445+
}
1446+
}));
1447+
1448+
it('should dirty the model on an input event in response to a placeholder change while in focus', inject(function($rootScope) {
1449+
if (msie) {
1450+
$rootScope.ph = 'Test';
1451+
compileInput('<input type="text" ng-attr-placeholder="{{ph}}" ng-model="unsetValue" name="name" />');
1452+
expect(inputElm).toBePristine();
1453+
1454+
browserTrigger(inputElm, 'focusin');
1455+
browserTrigger(inputElm, 'focus');
1456+
browserTrigger(inputElm, 'input');
1457+
expect(inputElm.attr('placeholder')).toBe('Test');
1458+
expect(inputElm).toBePristine();
1459+
1460+
$rootScope.ph = 'Test Again';
1461+
$rootScope.$digest();
1462+
expect(inputElm).toBePristine();
1463+
1464+
changeInputValueTo('foo');
1465+
expect(inputElm).toBeDirty();
1466+
}
1467+
}));
1468+
1469+
it('should not dirty the model on an input event in response to a ng-attr-placeholder change', inject(function($rootScope) {
1470+
if (msie) {
1471+
compileInput('<input type="text" ng-attr-placeholder="{{ph}}" ng-model="unsetValue" name="name" />');
1472+
expect(inputElm).toBePristine();
1473+
1474+
$rootScope.ph = 1;
1475+
$rootScope.$digest();
1476+
browserTrigger(inputElm, 'input');
1477+
expect(inputElm).toBePristine();
1478+
1479+
$rootScope.ph = "";
1480+
$rootScope.$digest();
1481+
browserTrigger(inputElm, 'input');
1482+
expect(inputElm).toBePristine();
1483+
1484+
changeInputValueTo('foo');
1485+
expect(inputElm).toBeDirty();
1486+
}
1487+
}));
1488+
1489+
it('should not dirty the model on an input event in response to a focus', inject(function($sniffer) {
1490+
if (msie) {
1491+
compileInput('<input type="text" placeholder="Test" ng-model="unsetValue" name="name" />');
1492+
browserTrigger(inputElm, 'input');
1493+
expect(inputElm.attr('placeholder')).toBe('Test');
1494+
expect(inputElm).toBePristine();
1495+
1496+
browserTrigger(inputElm, 'focusin');
1497+
browserTrigger(inputElm, 'focus');
1498+
browserTrigger(inputElm, 'input');
1499+
expect(inputElm.attr('placeholder')).toBe('Test');
1500+
expect(inputElm).toBePristine();
1501+
1502+
changeInputValueTo('foo');
1503+
expect(inputElm).toBeDirty();
1504+
}
1505+
}));
1506+
1507+
it('should not dirty the model on an input event in response to a blur', inject(function($sniffer) {
1508+
if (msie) {
1509+
compileInput('<input type="text" placeholder="Test" ng-model="unsetValue" name="name" />');
1510+
browserTrigger(inputElm, 'input');
1511+
expect(inputElm.attr('placeholder')).toBe('Test');
1512+
expect(inputElm).toBePristine();
1513+
1514+
browserTrigger(inputElm, 'focusin');
1515+
browserTrigger(inputElm, 'focus');
1516+
browserTrigger(inputElm, 'input');
1517+
expect(inputElm).toBePristine();
1518+
1519+
browserTrigger(inputElm, 'focusout');
1520+
browserTrigger(inputElm, 'input');
1521+
browserTrigger(inputElm, 'blur');
1522+
expect(inputElm).toBePristine();
1523+
1524+
changeInputValueTo('foo');
1525+
expect(inputElm).toBeDirty();
1526+
}
1527+
}));
1528+
1529+
it('should dirty the model on an input event if there is a placeholder and value', inject(function($rootScope) {
1530+
if (msie) {
1531+
$rootScope.name = 'foo';
1532+
compileInput('<input type="text" placeholder="Test" ng-model="name" value="init" name="name" />');
1533+
expect(inputElm.val()).toBe($rootScope.name);
1534+
expect(inputElm).toBePristine();
1535+
1536+
changeInputValueTo('bar');
1537+
expect(inputElm).toBeDirty();
1538+
}
1539+
}));
1540+
1541+
it('should dirty the model on an input event if there is a placeholder and value after focusing', inject(function($rootScope) {
1542+
if (msie) {
1543+
$rootScope.name = 'foo';
1544+
compileInput('<input type="text" placeholder="Test" ng-model="name" value="init" name="name" />');
1545+
expect(inputElm.val()).toBe($rootScope.name);
1546+
expect(inputElm).toBePristine();
1547+
1548+
browserTrigger(inputElm, 'focusin');
1549+
browserTrigger(inputElm, 'focus');
1550+
changeInputValueTo('bar');
1551+
expect(inputElm).toBeDirty();
1552+
}
1553+
}));
1554+
1555+
it('should dirty the model on an input event if there is a placeholder and value after bluring', inject(function($rootScope) {
1556+
if (msie) {
1557+
$rootScope.name = 'foo';
1558+
compileInput('<input type="text" placeholder="Test" ng-model="name" value="init" name="name" />');
1559+
expect(inputElm.val()).toBe($rootScope.name);
1560+
expect(inputElm).toBePristine();
1561+
1562+
browserTrigger(inputElm, 'focusin');
1563+
browserTrigger(inputElm, 'focus');
1564+
expect(inputElm).toBePristine();
1565+
1566+
browserTrigger(inputElm, 'focusout');
1567+
browserTrigger(inputElm, 'blur');
1568+
changeInputValueTo('bar');
1569+
expect(inputElm).toBeDirty();
1570+
}
1571+
}));
1572+
});
14071573

14081574

14091575
it('should interpolate input names', function() {

test/ng/snifferSpec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ describe('$sniffer', function() {
6464

6565
it('should claim that IE9 doesn\'t have support for "oninput"', function() {
6666
// IE9 implementation is fubared, so it's better to pretend that it doesn't have the support
67+
// IE10+ implementation is fubared when mixed with placeholders
6768
mockDivElement = {oninput: noop};
6869

69-
expect($sniffer.hasEvent('input')).toBe((msie == 9) ? false : true);
70+
expect($sniffer.hasEvent('input')).toBe(!(msie && msie <= 11));
7071
});
7172
});
7273

0 commit comments

Comments
 (0)