* @ngdoc service * @name $interpolate * @kind function * * @requires $parse * @requires $sce * * @description * * Compiles a string with markup into an interpolation function. This service is used by the * HTML ng.$compile $compile service fo
(text, mustHaveExpression, trustedContext, allOrNothing)
| 13311 | * - `context`: evaluation context for all expressions embedded in the interpolated text |
| 13312 | */ |
| 13313 | function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) { |
| 13314 | var contextAllowsConcatenation = trustedContext === $sce.URL || trustedContext === $sce.MEDIA_URL; |
| 13315 | |
| 13316 | // Provide a quick exit and simplified result function for text with no interpolation |
| 13317 | if (!text.length || text.indexOf(startSymbol) === -1) { |
| 13318 | if (mustHaveExpression && !contextAllowsConcatenation) return; |
| 13319 | |
| 13320 | var unescapedText = unescapeText(text); |
| 13321 | if (contextAllowsConcatenation) { |
| 13322 | unescapedText = $sce.getTrusted(trustedContext, unescapedText); |
| 13323 | } |
| 13324 | var constantInterp = valueFn(unescapedText); |
| 13325 | constantInterp.exp = text; |
| 13326 | constantInterp.expressions = []; |
| 13327 | constantInterp.$$watchDelegate = constantWatchDelegate; |
| 13328 | |
| 13329 | return constantInterp; |
| 13330 | } |
| 13331 | |
| 13332 | allOrNothing = !!allOrNothing; |
| 13333 | var startIndex, |
| 13334 | endIndex, |
| 13335 | index = 0, |
| 13336 | expressions = [], |
| 13337 | parseFns, |
| 13338 | textLength = text.length, |
| 13339 | exp, |
| 13340 | concat = [], |
| 13341 | expressionPositions = [], |
| 13342 | singleExpression; |
| 13343 | |
| 13344 | |
| 13345 | while (index < textLength) { |
| 13346 | if (((startIndex = text.indexOf(startSymbol, index)) !== -1) && |
| 13347 | ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) !== -1)) { |
| 13348 | if (index !== startIndex) { |
| 13349 | concat.push(unescapeText(text.substring(index, startIndex))); |
| 13350 | } |
| 13351 | exp = text.substring(startIndex + startSymbolLength, endIndex); |
| 13352 | expressions.push(exp); |
| 13353 | index = endIndex + endSymbolLength; |
| 13354 | expressionPositions.push(concat.length); |
| 13355 | concat.push(''); // Placeholder that will get replaced with the evaluated expression. |
| 13356 | } else { |
| 13357 | // we did not find an interpolation, so we have to add the remainder to the separators array |
| 13358 | if (index !== textLength) { |
| 13359 | concat.push(unescapeText(text.substring(index))); |
| 13360 | } |
| 13361 | break; |
| 13362 | } |
| 13363 | } |
| 13364 | |
| 13365 | singleExpression = concat.length === 1 && expressionPositions.length === 1; |
| 13366 | // Intercept expression if we need to stringify concatenated inputs, which may be SCE trusted |
| 13367 | // objects rather than simple strings |
| 13368 | // (we don't modify the expression if the input consists of only a single trusted input) |
| 13369 | var interceptor = contextAllowsConcatenation && singleExpression ? undefined : parseStringifyInterceptor; |
| 13370 | parseFns = expressions.map(function(exp) { return $parse(exp, interceptor); }); |
no test coverage detected