Skip to content

Commit a4e6d96

Browse files
clkaotbosch
authored andcommitted
feat(input): hold listener during text composition
When composing text in CJKV, intermediate buffer for unfinished text should not be updating the bound scope variables. Closes angular#4684
1 parent 7874a4d commit a4e6d96

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/ng/directive/input.js

+13
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,21 @@ var inputType = {
392392

393393

394394
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
395+
// In composition mode, users are still inputing intermediate text buffer,
396+
// hold the listener until composition is done.
397+
// More about composition events: https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent
398+
var composing = false;
399+
400+
element.on('compositionstart', function() {
401+
composing = true;
402+
});
403+
404+
element.on('compositionend', function() {
405+
composing = false;
406+
});
395407

396408
var listener = function() {
409+
if (composing) return;
397410
var value = element.val();
398411

399412
// By default we will trim the value

test/ng/directive/inputSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,20 @@ describe('input', function() {
454454
expect(scope.name).toEqual('adam');
455455
});
456456

457+
it('should not update the model between "compositionstart" and "compositionend"', function() {
458+
compileInput('<input type="text" ng-model="name" name="alias"" />');
459+
changeInputValueTo('a');
460+
expect(scope.name).toEqual('a');
461+
if (!(msie < 9)) {
462+
browserTrigger(inputElm, 'compositionstart');
463+
changeInputValueTo('adam');
464+
expect(scope.name).toEqual('a');
465+
browserTrigger(inputElm, 'compositionend');
466+
}
467+
changeInputValueTo('adam');
468+
expect(scope.name).toEqual('adam');
469+
});
470+
457471
describe('"paste" and "cut" events', function() {
458472
beforeEach(function() {
459473
// Force browser to report a lack of an 'input' event

0 commit comments

Comments
 (0)