Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit a32e16c

Browse files
fix(mask): Get unit tests working, add demo page, allow interpolated masks
1 parent ad3721d commit a32e16c

3 files changed

Lines changed: 48 additions & 33 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
<script type="text/javascript" src="http://code.angularjs.org/1.1.3/angular.js"></script>
4+
<script type="text/javascript">
5+
angular.module('ui.directives',[]);
6+
</script>
7+
<script type="text/javascript" src="../mask.js"></script>
8+
</head>
9+
<body ng-app="ui.directives">
10+
<input ui-mask="'(9)9'" ng-model="x">
11+
</body>
12+
</html>

modules/directives/mask/mask.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Attaches jquery-ui input mask onto input element
2+
Attaches input mask onto input element
33
*/
44
angular.module('ui.directives').directive('uiMask', [
55
function () {
@@ -28,8 +28,7 @@ angular.module('ui.directives').directive('uiMask', [
2828
unbindEventListeners();
2929
return false;
3030
}
31-
mask = scope.$eval(maskAttr);
32-
processRawMask(mask);
31+
processRawMask(maskAttr);
3332
if (!maskProcessed) {
3433
unbindEventListeners();
3534
return false;
@@ -128,7 +127,7 @@ angular.module('ui.directives').directive('uiMask', [
128127
function unmaskValue(value) {
129128
var valueUnmasked = '',
130129
maskPatternCopys = maskPatterns.slice();
131-
angular.forEach(value.split(''), function(chr, i) {
130+
angular.forEach(value.toString().split(''), function(chr, i) {
132131
if (maskPatternCopys.length && maskPatternCopys[0].test(chr)) {
133132
valueUnmasked += chr;
134133
maskPatternCopys.shift();
@@ -293,7 +292,7 @@ angular.module('ui.directives').directive('uiMask', [
293292
if (!Array.prototype.indexOf) {
294293
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
295294
"use strict";
296-
if (this == null) {
295+
if (this === null) {
297296
throw new TypeError();
298297
}
299298
var t = Object(this);
@@ -306,7 +305,7 @@ angular.module('ui.directives').directive('uiMask', [
306305
n = Number(arguments[1]);
307306
if (n != n) { // shortcut for verifying if it's NaN
308307
n = 0;
309-
} else if (n != 0 && n != Infinity && n != -Infinity) {
308+
} else if (n !== 0 && n !== Infinity && n !== -Infinity) {
310309
n = (n > 0 || -1) * Math.floor(Math.abs(n));
311310
}
312311
}
Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
1-
xdescribe('uiMask', function () {
1+
describe('uiMask', function () {
22

3-
var inputHtml = "<input ui-mask=\"'(9)9'\" ng-model='x'>";
4-
var $compile, $rootScope, element;
3+
var staticMaskHtml = "<input ui-mask='(9)9' ng-model='x'>";
4+
var dynamicMaskHtml = "<input ui-mask='{{mask}}' ng-model='x'>";
5+
var compileElement, scope;
56

67
beforeEach(module('ui.directives'));
7-
beforeEach(inject(function (_$rootScope_, _$compile_) {
8-
$rootScope = _$rootScope_;
9-
$compile = _$compile_;
8+
beforeEach(inject(function ($rootScope, $compile) {
9+
scope = $rootScope;
10+
compileElement = function(html) {
11+
return $compile(html)(scope);
12+
};
1013
}));
1114

1215
describe('ui changes on model changes', function () {
1316
it('should update ui valid model value', function () {
14-
$rootScope.x = undefined;
15-
element = $compile(inputHtml)($rootScope);
16-
$rootScope.$digest();
17-
expect(element.val()).toBe('');
18-
$rootScope.$apply(function () {
19-
$rootScope.x = 12;
20-
});
17+
var element = compileElement(staticMaskHtml);
18+
scope.$digest();
19+
expect(element.val()).toBe('(_)_');
20+
scope.$apply('x = 12');
2121
expect(element.val()).toBe('(1)2');
2222
});
2323
it('should wipe out ui on invalid model value', function () {
24-
$rootScope.x = 12;
25-
element = $compile(inputHtml)($rootScope);
26-
$rootScope.$digest();
24+
var element = compileElement(staticMaskHtml);
25+
scope.$apply('x = 12');
2726
expect(element.val()).toBe('(1)2');
28-
$rootScope.$apply(function () {
29-
$rootScope.x = 1;
30-
});
27+
scope.$apply('x = 1');
3128
expect(element.val()).toBe('');
3229
});
3330
});
3431

35-
describe('model binding on ui change', function () {
36-
//TODO: was having har time writing those tests, will open a separate issue for those
32+
describe('interpolated masks', function() {
33+
it('should allow mask to change', function() {
34+
var element = compileElement(dynamicMaskHtml);
35+
scope.$apply('mask = "(99)99"; x = 1234');
36+
expect(element.val()).toBe('(12)34');
37+
scope.$apply('mask = "(9)9"');
38+
expect(element.val()).toBe('(1)2');
39+
});
3740
});
3841

39-
describe('should fail', function() {
40-
it('errors on missing quotes', function() {
41-
$rootScope.x = 42;
42-
var errorInputHtml = "<input ui-mask=\"(9)9\" ng-model='x'>";
43-
element = $compile(errorInputHtml)($rootScope);
44-
expect($rootScope.$digest).toThrow('The Mask widget is not correctly set up');
42+
xdescribe('model binding on ui change', function () {
43+
it('should change model when element value changes', function() {
44+
var element = compileElement(staticMaskHtml);
45+
element.val('(2)4');
46+
element.trigger('blur');
47+
expect(scope.x).toBe(24);
4548
});
4649
});
50+
4751
});

0 commit comments

Comments
 (0)