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

feat(attrs): trigger observers for specific ng-attributes #7758

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ module.exports = function(grunt) {
'src/**/*.js',
'test/**/*.js',
'!test/ngScenario/DescribeSpec.js',
'!src/ng/directive/booleanAttrs.js', // legitimate xit here
'!src/ng/directive/attrs.js', // legitimate xit here
'!src/ngScenario/**/*.js'
]
},
Expand Down
2 changes: 1 addition & 1 deletion angularFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ angularFiles = {

'src/ng/directive/directives.js',
'src/ng/directive/a.js',
'src/ng/directive/booleanAttrs.js',
'src/ng/directive/attrs.js',
'src/ng/directive/form.js',
'src/ng/directive/input.js',
'src/ng/directive/ngBind.js',
Expand Down
3 changes: 3 additions & 0 deletions src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"nodeName_": false,
"uid": false,

"REGEX_STRING_REGEXP" : false,
"lowercase": false,
"uppercase": false,
"manualLowercase": false,
Expand Down Expand Up @@ -117,6 +118,7 @@

/* jqLite.js */
"BOOLEAN_ATTR": false,
"ALIASED_ATTR": false,
"jqNextId": false,
"camelCase": false,
"jqLitePatchJQueryRemove": false,
Expand All @@ -134,6 +136,7 @@
"jqLiteController": false,
"jqLiteInheritedData": false,
"getBooleanAttrName": false,
"getAliasedAttrName": false,
"createEventHandler": false,
"JQLitePrototype": false,
"addEventListenerFn": false,
Expand Down
3 changes: 3 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
-angularModule,
-nodeName_,
-uid,
-REGEX_STRING_REGEXP,

-lowercase,
-uppercase,
Expand Down Expand Up @@ -102,6 +103,8 @@
* <div doc-module-components="ng"></div>
*/

var REGEX_STRING_REGEXP = /^\/(.+)\/([a-z]*)$/;

/**
* @ngdoc function
* @name angular.lowercase
Expand Down
13 changes: 12 additions & 1 deletion src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
-JQLitePrototype,
-addEventListenerFn,
-removeEventListenerFn,
-BOOLEAN_ATTR
-BOOLEAN_ATTR,
-ALIASED_ATTR
*/

//////////////////////////////////
Expand Down Expand Up @@ -463,6 +464,11 @@ var BOOLEAN_ELEMENTS = {};
forEach('input,select,option,textarea,button,form,details'.split(','), function(value) {
BOOLEAN_ELEMENTS[uppercase(value)] = true;
});
var ALIASED_ATTR = {
'ngMinlength' : 'minlength',
'ngMaxlength' : 'maxlength',
'ngPattern' : 'pattern'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want other attributes like min/max?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not yet since I don't want to refactor the date, week and time validators in this commit.

};

function getBooleanAttrName(element, name) {
// check dom last since we will most likely fail on name
Expand All @@ -472,6 +478,11 @@ function getBooleanAttrName(element, name) {
return booleanAttr && BOOLEAN_ELEMENTS[element.nodeName] && booleanAttr;
}

function getAliasedAttrName(element, name) {
var nodeName = element.nodeName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we still need it for IE9, but there is _nodename helper method to get name in a cross browser happy way

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or check for nodeType instead.

return (nodeName === 'INPUT' || nodeName === 'TEXTAREA') && ALIASED_ATTR[name];
}

forEach({
data: jqLiteData,
inheritedData: jqLiteInheritedData,
Expand Down
10 changes: 8 additions & 2 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,19 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
//is set through this function since it may cause $updateClass to
//become unstable.

var booleanKey = getBooleanAttrName(this.$$element[0], key),
var node = this.$$element[0],
booleanKey = getBooleanAttrName(node, key),
aliasedKey = getAliasedAttrName(node, key),
observer = key,
normalizedVal,
nodeName;

if (booleanKey) {
this.$$element.prop(key, value);
attrName = booleanKey;
} else if(aliasedKey) {
this[aliasedKey] = value;
observer = aliasedKey;
}

this[key] = value;
Expand Down Expand Up @@ -768,7 +774,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

// fire observers
var $$observers = this.$$observers;
$$observers && forEach($$observers[key], function(fn) {
$$observers && forEach($$observers[observer], function(fn) {
try {
fn(value);
} catch (e) {
Expand Down
23 changes: 23 additions & 0 deletions src/ng/directive/booleanAttrs.js → src/ng/directive/attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,29 @@ forEach(BOOLEAN_ATTR, function(propName, attrName) {
};
});

// aliased input attrs are evaluated
forEach(ALIASED_ATTR, function(htmlAttr, ngAttr) {
ngAttributeAliasDirectives[ngAttr] = function() {
return {
priority: 100,
link: function(scope, element, attr) {
//special case ngPattern when a literal regular expression value
//is used as the expression (this way we don't have to watch anything).
if (ngAttr === "ngPattern" && attr.ngPattern.charAt(0) == "/") {
var match = attr.ngPattern.match(REGEX_STRING_REGEXP);
if (match) {
attr.$set("ngPattern", new RegExp(match[1], match[2]));
return;
}
}

scope.$watch(attr[ngAttr], function ngAttrAliasWatchAction(value) {
attr.$set(ngAttr, value);
});
}
};
};
});

// ng-src, ng-srcset, ng-href are interpolated
forEach(['src', 'srcset', 'href'], function(attrName) {
Expand Down
44 changes: 20 additions & 24 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,32 +975,28 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
};

// pattern validator
var pattern = attr.ngPattern,
patternValidator,
match;
if (attr.ngPattern) {
var regexp, patternExp = attr.ngPattern;
attr.$observe('pattern', function(regex) {
if(isString(regex)) {
var match = regex.match(REGEX_STRING_REGEXP);
if(match) {
regex = new RegExp(match[1], match[2]);
}
}

if (pattern) {
var validateRegex = function(regexp, value) {
return validate(ctrl, 'pattern', ctrl.$isEmpty(value) || regexp.test(value), value);
};
match = pattern.match(/^\/(.*)\/([gim]*)$/);
if (match) {
pattern = new RegExp(match[1], match[2]);
patternValidator = function(value) {
return validateRegex(pattern, value);
};
} else {
patternValidator = function(value) {
var patternObj = scope.$eval(pattern);
if (regex && !regex.test) {
throw minErr('ngPattern')('noregexp',
'Expected {0} to be a RegExp but was {1}. Element: {2}', patternExp,
regex, startingTag(element));
}

if (!patternObj || !patternObj.test) {
throw minErr('ngPattern')('noregexp',
'Expected {0} to be a RegExp but was {1}. Element: {2}', pattern,
patternObj, startingTag(element));
}
return validateRegex(patternObj, value);
};
}
regexp = regex || undefined;
});

var patternValidator = function(value) {
return validate(ctrl, 'pattern', ctrl.$isEmpty(value) || isUndefined(regexp) || regexp.test(value), value);
};

ctrl.$formatters.push(patternValidator);
ctrl.$parsers.push(patternValidator);
Expand Down
60 changes: 57 additions & 3 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ describe('input', function() {
scope.$digest();
}

var attrs;
beforeEach(module(function($compileProvider) {
$compileProvider.directive('attrCapture', function() {
return function(scope, element, $attrs) {
attrs = $attrs;
};
});
}));

beforeEach(inject(function($injector, _$sniffer_, _$browser_) {
$sniffer = _$sniffer_;
$browser = _$browser_;
Expand Down Expand Up @@ -1073,6 +1082,19 @@ describe('input', function() {
expect(inputElm).toBeInvalid();
});

it('should listen on ng-pattern when pattern is observed', function() {
var value, patternVal = /^\w+$/;
compileInput('<input type="text" ng-model="value" ng-pattern="pat" attr-capture />');
attrs.$observe('pattern', function(v) {
value = attrs.pattern;
});

scope.$apply(function() {
scope.pat = patternVal;
});

expect(value).toBe(patternVal);
});

it('should validate in-lined pattern with modifiers', function() {
compileInput('<input type="text" ng-model="value" ng-pattern="/^abc?$/i" />');
Expand Down Expand Up @@ -1104,7 +1126,9 @@ describe('input', function() {
changeInputValueTo('x');
expect(inputElm).toBeInvalid();

scope.regexp = /abc?/;
scope.$apply(function() {
scope.regexp = /abc?/;
});

changeInputValueTo('ab');
expect(inputElm).toBeValid();
Expand All @@ -1114,10 +1138,12 @@ describe('input', function() {
});


it('should throw an error when scope pattern can\'t be found', function() {
it('should throw an error when scope pattern is invalid', function() {
expect(function() {
compileInput('<input type="text" ng-model="foo" ng-pattern="fooRegexp" />');
scope.$apply();
scope.$apply(function() {
scope.fooRegexp = '/...';
});
}).toThrowMatching(/^\[ngPattern:noregexp\] Expected fooRegexp to be a RegExp but was/);
});
});
Expand All @@ -1134,6 +1160,20 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});

it('should listen on ng-minlength when minlength is observed', function() {
var value = 0;
compileInput('<input type="text" ng-model="value" ng-minlength="min" attr-capture />');
attrs.$observe('minlength', function(v) {
value = int(attrs.minlength);
});

scope.$apply(function() {
scope.min = 5;
});

expect(value).toBe(5);
});
});


Expand All @@ -1148,6 +1188,20 @@ describe('input', function() {
changeInputValueTo('aaa');
expect(scope.value).toBe('aaa');
});

it('should listen on ng-maxlength when maxlength is observed', function() {
var value = 0;
compileInput('<input type="text" ng-model="value" ng-maxlength="max" attr-capture />');
attrs.$observe('maxlength', function(v) {
value = int(attrs.maxlength);
});

scope.$apply(function() {
scope.max = 10;
});

expect(value).toBe(10);
});
});


Expand Down
1 change: 0 additions & 1 deletion test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,6 @@ describe('parser', function() {
}));
});


describe('one-time binding', function() {
it('should only use the cache when it is not a one-time binding', inject(function($parse) {
expect($parse('foo')).toBe($parse('foo'));
Expand Down