Skip to content

Commit

Permalink
fix remove non-existant recognizer
Browse files Browse the repository at this point in the history
* When removing a recognizer, make sure it exists before removal. If recognizer has not been registered to manager inArray will return -1. The negative index passed to Array.splice will remove last element of the recognizers array.
  • Loading branch information
ohnnyj committed Dec 3, 2015
1 parent 189098f commit f1c2d3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,19 @@ Manager.prototype = {
return this;
}

var recognizers = this.recognizers;
recognizer = this.get(recognizer);
recognizers.splice(inArray(recognizers, recognizer), 1);

this.touchAction.update();
// let's make sure this recognizer exists
if (recognizer) {
var recognizers = this.recognizers;
var index = inArray(recognizers, recognizer);

if (index !== -1) {
recognizers.splice(index, 1);
this.touchAction.update();
}
}

return this;
},

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_hammer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,17 @@ test('Hammer.Manager accepts recognizers as arrays.', function() {
equal(2, Object.keys(recognizerActual.simultaneous).length);
equal(1, recognizerActual.requireFail.length);
});

/*
* Removing a recognizer which cannot be found would errantly remove the last recognizer in the
* manager's list.
*/
test('Remove non-existent recognizer.', function() {
expect(1);

hammer = new Hammer(el, {recognizers: []});
hammer.add(new Hammer.Swipe());
hammer.remove('tap');

equal(1, hammer.recognizers.length);
});

0 comments on commit f1c2d3b

Please sign in to comment.