Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6bf44b

Browse files
committed
fix(events): Don't emit events for inputs that already have filled value attributes
Related to #2
1 parent bfb5be0 commit e6bf44b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/autofill-event.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
});
2626

2727
window.document.addEventListener('DOMContentLoaded', function() {
28+
// mark all values that are present when the DOM is ready.
29+
// We don't need to trigger a change event here,
30+
// as js libs start with those values already being set!
31+
forEach(document.getElementsByTagName('input'), markValue);
32+
2833
// The timeout is needed for Chrome as it auto fills
2934
// login forms some time after DOMContentLoaded!
3035
window.setTimeout(function() {
@@ -48,6 +53,14 @@
4853
}
4954

5055
function valueMarked(el) {
56+
if (! ("$$currentValue" in el) ) {
57+
// First time we see an element we take it's value attribute
58+
// as real value. This might have been filled in the backend,
59+
// ...
60+
// Note: it's important to not use the value property here!
61+
el.$$currentValue = el.getAttribute('value');
62+
}
63+
5164
var val = el.value,
5265
$$currentValue = el.$$currentValue;
5366
if (!val && !$$currentValue) {
@@ -114,4 +127,6 @@
114127
element.dispatchEvent(event);
115128
}
116129

130+
131+
117132
})(window);

test/unit/autofill-eventSpec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ describe('check other inputs when one input fires a blur event', function() {
9292
expect(input[0].changeEventCount).toBeUndefined();
9393
});
9494

95+
});
96+
97+
describe('misc', function() {
98+
it('should not fire for untouched inputs with empty value', function() {
99+
input.checkAndTriggerAutoFillEvent();
100+
expect(input[0].changeEventCount).toBeUndefined();
101+
});
102+
103+
it('should not fire if inputs are added with predefined value', function() {
104+
container.append('<input type="text" value="test">');
105+
var newInput = container.children().eq(1);
106+
newInput.checkAndTriggerAutoFillEvent();
107+
expect(newInput[0].changeEventCount).toBeUndefined();
108+
});
95109

96110
});
97111

0 commit comments

Comments
 (0)