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

Commit

Permalink
feat(input): hold listener during text composition
Browse files Browse the repository at this point in the history
When composing text in CJKV, intermediate buffer for unfinished text should not
be updating the bound scope variables.

Closes #4684
  • Loading branch information
clkao authored and tbosch committed Nov 21, 2013
1 parent 7874a4d commit a4e6d96
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,21 @@ var inputType = {


function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
// In composition mode, users are still inputing intermediate text buffer,
// hold the listener until composition is done.
// More about composition events: https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent
var composing = false;

element.on('compositionstart', function() {
composing = true;
});

element.on('compositionend', function() {
composing = false;
});

var listener = function() {
if (composing) return;
var value = element.val();

// By default we will trim the value
Expand Down
14 changes: 14 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,20 @@ describe('input', function() {
expect(scope.name).toEqual('adam');
});

it('should not update the model between "compositionstart" and "compositionend"', function() {
compileInput('<input type="text" ng-model="name" name="alias"" />');
changeInputValueTo('a');
expect(scope.name).toEqual('a');
if (!(msie < 9)) {
browserTrigger(inputElm, 'compositionstart');
changeInputValueTo('adam');
expect(scope.name).toEqual('a');
browserTrigger(inputElm, 'compositionend');
}
changeInputValueTo('adam');
expect(scope.name).toEqual('adam');
});

describe('"paste" and "cut" events', function() {
beforeEach(function() {
// Force browser to report a lack of an 'input' event
Expand Down

0 comments on commit a4e6d96

Please sign in to comment.