Skip to content

Commit

Permalink
feat($interpolate): add strict param to returned fn
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Apr 30, 2014
1 parent fcc3a7a commit 6bc2fac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/ng/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ function $InterpolateProvider() {
* expect(exp({name:'Angular'}).toEqual('Hello ANGULAR!');
* ```
*
* The function returned by `$interpolate` takes an optional second argument, `allOrNothing`.
* If `allOrNothing` is `true`, the interpolation function will return `undefined` unless all
* embedded expressions within the text are defined.
*
* ```js
* var $interpolate = ...; // injected
* var exp = $interpolate('{{greeting}} {{name}}!');
* var context = {greeting: 'Hey', name: undefined };
*
* // default "forgiving" mode
* expect(exp(context).toEqual('Hello !');
*
* // "allOrNothing" mode
* expect(exp(context, true).toBeUndefined();
* context.name = 'Angular';
* expect(exp(context, true).toEqual('Hello Angular!');
* ```
*
* `allOrNothing` is useful for interpolating URLs. `ngHref`, `ngSrc`, and `ngSrcset` use this
* behavior.
*
* @param {string} text The text with markup to interpolate.
* @param {boolean=} mustHaveExpression if set to true then the interpolation string must have
Expand All @@ -114,10 +134,11 @@ function $InterpolateProvider() {
* result through {@link ng.$sce#getTrusted $sce.getTrusted(interpolatedResult,
* trustedContext)} before returning it. Refer to the {@link ng.$sce $sce} service that
* provides Strict Contextual Escaping for details.
* @returns {function(context)} an interpolation function which is used to compute the
* @returns {function(context, allOrNothing)} an interpolation function which is used to compute the
* interpolated string. The function has these parameters:
*
* - `context`: evaluation context for all expressions embedded in the interpolated text
* - `allOrNothing`: if `true`, the undefined unless all embedded expressions are defined
*/
function $interpolate(text, mustHaveExpression, trustedContext) {
var startIndex,
Expand Down Expand Up @@ -182,23 +203,29 @@ function $InterpolateProvider() {
return concat.join('');
};

var stringify = function (value) {
var getValue = function (value) {
if (trustedContext) {
value = $sce.getTrusted(trustedContext, value);
} else {
value = $sce.valueOf(value);
}

if (value === null || isUndefined(value)) {
return value;
};

var stringify = function (value) {
if (isUndefined(value) || value === null) {
value = '';
} else if (typeof value != 'string') {
}
if (typeof value != 'string') {
value = toJson(value);
}

return value;
};

return extend(function interpolationFn(context) {
return extend(function interpolationFn(context, allOrNothing) {
allOrNothing = !!allOrNothing;
var scopeId = context.$id || 'notAScope';
var lastValues = lastValuesCache.values[scopeId];
var lastResult = lastValuesCache.results[scopeId];
Expand All @@ -225,7 +252,11 @@ function $InterpolateProvider() {

try {
for (; i < ii; i++) {
val = stringify(parseFns[i](context));
val = getValue(parseFns[i](context));
if (allOrNothing && isUndefined(val)) {
return;
}
val = stringify(val);
if (val !== lastValues[i]) {
inputsChanged = true;
}
Expand Down
5 changes: 5 additions & 0 deletions test/ng/interpolateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('$interpolate', function() {
expect($interpolate('some text', true)).toBeUndefined();
}));

it('should return undefined when there are bindings and strict is set to true',
inject(function($interpolate) {
expect($interpolate('test {{foo}}')({}, true)).toBeUndefined();
}));

it('should suppress falsy objects', inject(function($interpolate) {
expect($interpolate('{{undefined}}')({})).toEqual('');
expect($interpolate('{{null}}')({})).toEqual('');
Expand Down

0 comments on commit 6bc2fac

Please sign in to comment.