This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fixed backspace and updated specs.

  • Loading branch information...
1 parent e2c09b9 commit 50e999ef411e1c779eb9b2d777d209dddf1ab042 @digitalBush committed Feb 12, 2011
Showing with 143 additions and 66 deletions.
  1. +142 −65 spec/Backspace.Spec.js
  2. +1 −1 src/jquery.maskedinput.js
View
@@ -1,74 +1,151 @@
-describe("Backspace Specifications", function() {
- var input;
-
- beforeEach(function(){
- input =
- $("<input />")
- .appendTo("body");
- });
-
- afterEach(function(){
- input.remove();
- });
-
- describe("when backspacing",function(){
- describe("over a literal",function(){
- beforeEach(function(){
+feature("Backspace Key", function() {
+ story('User presses backspace with cursor to the right of a mask literal',function(){
+ scenario('character at cursor matches definition to the left',function(){
+ given("an input with a mask definition of '9-99'", function(){
+ input
+ .mask("9-99")
+ .mashKeys("123");
+ });
+
+ given("the input has cursor positioned to the right of literal", function(){
+ input.caret(2);
+ });
+
+ when("hitting the backspace key",function(){
+ input.mashKeys(function(keys){keys.type(keys.backspace)});
+ });
+
+ then("value should be correct",function(){
+ expect(input).toHaveValue('2-3_');
+ });
+
+ and("caret position should be correct",function(){
+ expect(input.caret().begin).toEqual(0);
+ });
+ });
+
+ scenario('character at cursor does not match definition to the left',function(){
+ given("an input with a mask definition of 'a-99'", function(){
input
- .mask("9-9")
- .mashKeys(function(keys){keys.type('1',keys.backspace);});
- });
-
- it("should have the correct placeholder text", function(){
- expect(input).toHaveValue('_-_');
- });
-
- it("should have the correct caret position", function(){
- var caret=input.caret();
- expect(caret.begin).toEqual(0);
- expect(caret.end).toEqual(0);
- });
+ .mask("a-99")
+ .mashKeys("z12");
+ });
+
+ given("the input has cursor positioned to the right of literal", function(){
+ input.caret(2);
+ });
+
+ when("hitting the backspace key",function(){
+ input.mashKeys(function(keys){keys.type(keys.backspace)});
+ });
+
+ then("value should be correct",function(){
+ expect(input).toHaveValue('_-12');
+ });
+
+ and("caret position should be correct",function(){
+ expect(input.caret().begin).toEqual(0);
+ });
});
-
-
- describe("with character to right of caret which matches mask definition to left",function(){
- beforeEach(function(){
+ });
+
+ story('User presses backspace with cursor on last character',function(){
+ scenario('cursor character matches definition to the left',function(){
+ given("an input with a mask definition of '99'", function(){
input
.mask("99")
- .mashKeys("12")
- .caret(1)
- .mashKeys(function(keys){keys.type(keys.backspace)});
- });
-
- it("should shift the character to the left", function(){
- expect(input).toHaveValue('2_');
- });
-
- it("should have the correct caret position", function(){
- var caret=input.caret();
- expect(caret.begin).toEqual(0);
- expect(caret.end).toEqual(0);
- });
+ .mashKeys("12");
+ });
+
+ given("the input has cursor positioned on first character", function(){
+ input.caret(1);
+ });
+
+ when("hitting the backspace key",function(){
+ input.mashKeys(function(keys){keys.type(keys.backspace)});
+ });
+
+ then("value should be correct",function(){
+ expect(input).toHaveValue('2_');
+ });
+
+ and("caret position should be correct",function(){
+ expect(input.caret().begin).toEqual(0);
+ });
});
-
- describe("with character to right of caret which does not match mask definition to left",function(){
- beforeEach(function(){
+
+ scenario('cursor character does not match definition to the left',function(){
+ given("an input with a mask definition of '9a'", function(){
input
.mask("9a")
- .mashKeys("1z")
- .caret(1)
- .mashKeys(function(keys){keys.type(keys.backspace)});
- });
-
- it("should not shift the character to the left", function(){
- expect(input).toHaveValue('_z');
- });
-
- it("should have the correct caret position", function(){
- var caret=input.caret();
- expect(caret.begin).toEqual(0);
- expect(caret.end).toEqual(0);
- });
+ .mashKeys("1z");
+ });
+
+ given("the input has cursor positioned on first character", function(){
+ input.caret(1);
+ });
+
+ when("hitting the backspace key",function(){
+ input.mashKeys(function(keys){keys.type(keys.backspace)});
+ });
+
+ then("value should be correct",function(){
+ expect(input).toHaveValue('_z');
+ });
+
+ and("caret position should be correct",function(){
+ expect(input.caret().begin).toEqual(0);
+ });
+ });
+
+ describe('There is a mask literal between the two placeholders',function(){
+ scenario('character at end matches definition of first position',function(){
+ given("an input with a mask definition of '9-9'", function(){
+ input
+ .mask("9-9")
+ .mashKeys("12");
+ });
+
+ given("the input has cursor positioned on literal", function(){
+ input.caret(1);
+ });
+
+ when("hitting the backspace key",function(){
+ input.mashKeys(function(keys){keys.type(keys.backspace)});
+ });
+
+ then("value should be correct",function(){
+ expect(input).toHaveValue('2-_');
+ });
+
+ and("caret position should be correct",function(){
+ expect(input.caret().begin).toEqual(0);
+ });
+ });
+
+ scenario('character at end does not match definition of first position',function(){
+ given("an input with a mask definition of '9-9'", function(){
+ input
+ .mask("9-a")
+ .mashKeys("1z");
+ });
+
+ given("the input has cursor positioned on literal", function(){
+ input.caret(1);
+ });
+
+ when("hitting the backspace key",function(){
+ input.mashKeys(function(keys){keys.type(keys.backspace)});
+ });
+
+ then("value should be correct",function(){
+ expect(input).toHaveValue('_-z');
+ });
+
+ and("caret position should be correct",function(){
+ expect(input.caret().begin).toEqual(0);
+ });
+ });
});
});
-});
+});
@@ -132,7 +132,7 @@
end = pos.end;
if(end-begin==0){
- end=begin=k!=46?seekPrev(begin):seekNext(begin-1);
+ begin=k!=46?seekPrev(begin):(end=seekNext(begin-1));
end=k==46?seekNext(end):end;
}
clearBuffer(begin, end);

0 comments on commit 50e999e

Please sign in to comment.