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

Commit 6611ad5

Browse files
committed
fixup! perf(input): prevent multiple validations on compilation
1 parent 3e1442a commit 6611ad5

File tree

3 files changed

+6
-36
lines changed

3 files changed

+6
-36
lines changed

src/ng/directive/validators.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,10 @@ var patternDirective = function($parse) {
181181
// ngPattern might be a scope expression, or an inlined regex, which is not parsable.
182182
// We get value of the attribute here, so we can compare the old and the new value
183183
// in the observer to avoid unnecessary validations
184-
try {
184+
if (tAttr.ngPattern.charAt(0) === '/' && REGEX_STRING_REGEXP.test(tAttr.ngPattern)) {
185+
parseFn = function() { return tAttr.ngPattern; };
186+
} else {
185187
parseFn = $parse(tAttr.ngPattern);
186-
} catch (e) {
187-
if (/^\[\$parse:lexerr\]/.test(e.message)) {
188-
parseFn = function() { return tAttr.ngPattern; };
189-
} else {
190-
throw e;
191-
}
192188
}
193189
}
194190

@@ -199,6 +195,8 @@ var patternDirective = function($parse) {
199195

200196
if (attr.ngPattern) {
201197
attrVal = parseFn(scope);
198+
} else {
199+
patternExp = attr.pattern;
202200
}
203201

204202
var regexp = parsePatternAttr(attrVal, patternExp, elm);
@@ -420,7 +418,7 @@ function parsePatternAttr(regex, patternExp, elm) {
420418
regex = new RegExp('^' + regex + '$');
421419
}
422420

423-
if (!(regex instanceof RegExp)) {
421+
if (!regex.test) {
424422
throw minErr('ngPattern')('noregexp',
425423
'Expected {0} to be a RegExp but was {1}. Element: {2}', patternExp,
426424
regex, startingTag(elm));

test/ng/directive/inputSpec.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,6 @@ describe('input', function() {
848848

849849
expect(helper.validationCounter.min).toBe(1);
850850

851-
helper.validationCounter = {};
852-
853851
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
854852
'<input type="month" ng-model="value" validation-spy="min" ng-min="minVal" />' +
855853
'</div>');
@@ -925,8 +923,6 @@ describe('input', function() {
925923

926924
expect(helper.validationCounter.max).toBe(1);
927925

928-
helper.validationCounter = {};
929-
930926
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
931927
'<input type="month" ng-model="value" validation-spy="max" ng-max="maxVal" />' +
932928
'</div>');
@@ -1159,8 +1155,6 @@ describe('input', function() {
11591155

11601156
expect(helper.validationCounter.min).toBe(1);
11611157

1162-
helper.validationCounter = {};
1163-
11641158
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
11651159
'<input type="week" ng-model="value" validation-spy="min" ng-min="minVal" />' +
11661160
'</div>');
@@ -1239,8 +1233,6 @@ describe('input', function() {
12391233

12401234
expect(helper.validationCounter.max).toBe(1);
12411235

1242-
helper.validationCounter = {};
1243-
12441236
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
12451237
'<input type="week" ng-model="value" validation-spy="max" ng-max="maxVal" />' +
12461238
'</div>');
@@ -1587,8 +1579,6 @@ describe('input', function() {
15871579

15881580
expect(helper.validationCounter.min).toBe(1);
15891581

1590-
helper.validationCounter = {};
1591-
15921582
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
15931583
'<input type="datetime-local" ng-model="value" validation-spy="min" ng-min="minVal" />' +
15941584
'</div>');
@@ -1665,8 +1655,6 @@ describe('input', function() {
16651655

16661656
expect(helper.validationCounter.max).toBe(1);
16671657

1668-
helper.validationCounter = {};
1669-
16701658
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
16711659
'<input type="datetime-local" ng-model="value" validation-spy="max" ng-max="maxVal" />' +
16721660
'</div>');
@@ -2090,8 +2078,6 @@ describe('input', function() {
20902078

20912079
expect(helper.validationCounter.min).toBe(1);
20922080

2093-
helper.validationCounter = {};
2094-
20952081
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
20962082
'<input type="time" ng-model="value" validation-spy="min" ng-min="minVal" />' +
20972083
'</div>');
@@ -2155,8 +2141,6 @@ describe('input', function() {
21552141

21562142
expect(helper.validationCounter.max).toBe(1);
21572143

2158-
helper.validationCounter = {};
2159-
21602144
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
21612145
'<input type="time" ng-model="value" validation-spy="max" ng-max="maxVal" />' +
21622146
'</div>');
@@ -2518,8 +2502,6 @@ describe('input', function() {
25182502

25192503
expect(helper.validationCounter.min).toBe(1);
25202504

2521-
helper.validationCounter = {};
2522-
25232505
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
25242506
'<input type="date" ng-model="value" validation-spy="min" ng-min="minVal" />' +
25252507
'</div>');
@@ -2607,8 +2589,6 @@ describe('input', function() {
26072589

26082590
expect(helper.validationCounter.max).toBe(1);
26092591

2610-
helper.validationCounter = {};
2611-
26122592
inputElm = helper.compileInput('<div ng-repeat="input in [0]">' +
26132593
'<input type="date" ng-model="value" validation-spy="max" ng-max="maxVal" />' +
26142594
'</div>');

test/ng/directive/validatorsSpec.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,6 @@ describe('validators', function() {
244244

245245
expect(helper.validationCounter.pattern).toBe(1);
246246

247-
helper.validationCounter = {};
248-
249247
helper.compileInput(
250248
'<div ng-repeat="input in [0]">' +
251249
'<input type="text" ng-model="value" ng-pattern="pattern" validation-spy="pattern" />' +
@@ -351,8 +349,6 @@ describe('validators', function() {
351349

352350
expect(helper.validationCounter.minlength).toBe(1);
353351

354-
helper.validationCounter = {};
355-
356352
element = helper.compileInput(
357353
'<div ng-repeat="input in [0]">' +
358354
'<input type="text" ng-model="value" ng-minlength="minlength" validation-spy="minlength" />' +
@@ -563,8 +559,6 @@ describe('validators', function() {
563559

564560
expect(helper.validationCounter.maxlength).toBe(1);
565561

566-
helper.validationCounter = {};
567-
568562
element = helper.compileInput(
569563
'<div ng-repeat="input in [0]">' +
570564
'<input type="text" ng-model="value" ng-maxlength="maxlength" validation-spy="maxlength" />' +
@@ -711,8 +705,6 @@ describe('validators', function() {
711705
$rootScope.$digest();
712706

713707
expect(helper.validationCounter.required).toBe(1);
714-
715-
helper.validationCounter = {};
716708
});
717709

718710

0 commit comments

Comments
 (0)