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

WIP: detect accidental interpolation #8262

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
17 changes: 17 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,23 @@ function assertNotHasOwnProperty(name, context) {
}
}


/**
* Throw an error when an interpolation is detected in an attribute value that should be an
* expression.
*
* @param attrs $attrs object
* @param attrName attribute name
*/
function assertNoInterpolation(attrs, attrName) {
if (/\{\{/.test(attrs[attrName])) {
throw new Error('Unexpected interpolation found in attribute ' + attrName +
' of element ' + startingTag(attrs.$$element));
}
};



/**
* Return the value accessible from the object by path. Any undefined traversals are ignored
* @param {Object} obj starting object
Expand Down
1 change: 1 addition & 0 deletions src/ng/directive/ngBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ var ngBindTemplateDirective = ['$interpolate', function($interpolate) {
var ngBindHtmlDirective = ['$sce', '$parse', function($sce, $parse) {
return {
compile: function (tElement, tAttrs) {
assertNoInterpolation(tAttrs, 'ngBindHtml');
tElement.addClass('ng-binding');

return function (scope, element, attr) {
Expand Down
7 changes: 7 additions & 0 deletions test/ng/directive/ngBindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ describe('ngBind*', function() {
}));


it('should complain about accidental use of interpolation', inject(function($compile) {
expect(function() {
$compile('<div ng-bind-html="{{myHtml}}"></div>');
}).toThrow('Unexpected interpolation found in attribute ngBindHtml of element <div ng-bind-html="{{myHtml}}">');
}));


describe('SCE disabled', function() {
beforeEach(function() {
module(function($sceProvider) { $sceProvider.enabled(false); });
Expand Down