* @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 | // Provide a quick exit and simplified result function for text with no interpolation |
| 13315 | if (!text.length || text.indexOf(startSymbol) === -1) { |
| 13316 | var constantInterp; |
| 13317 | if (!mustHaveExpression) { |
| 13318 | var unescapedText = unescapeText(text); |
| 13319 | constantInterp = valueFn(unescapedText); |
| 13320 | constantInterp.exp = text; |
| 13321 | constantInterp.expressions = []; |
| 13322 | constantInterp.$$watchDelegate = constantWatchDelegate; |
| 13323 | } |
| 13324 | return constantInterp; |
| 13325 | } |
| 13326 | |
| 13327 | allOrNothing = !!allOrNothing; |
| 13328 | var startIndex, |
| 13329 | endIndex, |
| 13330 | index = 0, |
| 13331 | expressions = [], |
| 13332 | parseFns = [], |
| 13333 | textLength = text.length, |
| 13334 | exp, |
| 13335 | concat = [], |
| 13336 | expressionPositions = []; |
| 13337 | |
| 13338 | while (index < textLength) { |
| 13339 | if (((startIndex = text.indexOf(startSymbol, index)) !== -1) && |
| 13340 | ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) !== -1)) { |
| 13341 | if (index !== startIndex) { |
| 13342 | concat.push(unescapeText(text.substring(index, startIndex))); |
| 13343 | } |
| 13344 | exp = text.substring(startIndex + startSymbolLength, endIndex); |
| 13345 | expressions.push(exp); |
| 13346 | parseFns.push($parse(exp, parseStringifyInterceptor)); |
| 13347 | index = endIndex + endSymbolLength; |
| 13348 | expressionPositions.push(concat.length); |
| 13349 | concat.push(''); |
| 13350 | } else { |
| 13351 | // we did not find an interpolation, so we have to add the remainder to the separators array |
| 13352 | if (index !== textLength) { |
| 13353 | concat.push(unescapeText(text.substring(index))); |
| 13354 | } |
| 13355 | break; |
| 13356 | } |
| 13357 | } |
| 13358 | |
| 13359 | // Concatenating expressions makes it hard to reason about whether some combination of |
| 13360 | // concatenated values are unsafe to use and could easily lead to XSS. By requiring that a |
| 13361 | // single expression be used for iframe[src], object[src], etc., we ensure that the value |
| 13362 | // that's used is assigned or constructed by some JS code somewhere that is more testable or |
| 13363 | // make it obvious that you bound the value to some user controlled value. This helps reduce |
| 13364 | // the load when auditing for XSS issues. |
| 13365 | if (trustedContext && concat.length > 1) { |
| 13366 | $interpolateMinErr.throwNoconcat(text); |
| 13367 | } |
| 13368 | |
| 13369 | if (!mustHaveExpression || expressions.length) { |
| 13370 | var compute = function(values) { |
no test coverage detected