* @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)
| 12588 | * - `context`: evaluation context for all expressions embedded in the interpolated text |
| 12589 | */ |
| 12590 | function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) { |
| 12591 | // Provide a quick exit and simplified result function for text with no interpolation |
| 12592 | if (!text.length || text.indexOf(startSymbol) === -1) { |
| 12593 | var constantInterp; |
| 12594 | if (!mustHaveExpression) { |
| 12595 | var unescapedText = unescapeText(text); |
| 12596 | constantInterp = valueFn(unescapedText); |
| 12597 | constantInterp.exp = text; |
| 12598 | constantInterp.expressions = []; |
| 12599 | constantInterp.$$watchDelegate = constantWatchDelegate; |
| 12600 | } |
| 12601 | return constantInterp; |
| 12602 | } |
| 12603 | |
| 12604 | allOrNothing = !!allOrNothing; |
| 12605 | var startIndex, |
| 12606 | endIndex, |
| 12607 | index = 0, |
| 12608 | expressions = [], |
| 12609 | parseFns = [], |
| 12610 | textLength = text.length, |
| 12611 | exp, |
| 12612 | concat = [], |
| 12613 | expressionPositions = []; |
| 12614 | |
| 12615 | while (index < textLength) { |
| 12616 | if (((startIndex = text.indexOf(startSymbol, index)) !== -1) && |
| 12617 | ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) !== -1)) { |
| 12618 | if (index !== startIndex) { |
| 12619 | concat.push(unescapeText(text.substring(index, startIndex))); |
| 12620 | } |
| 12621 | exp = text.substring(startIndex + startSymbolLength, endIndex); |
| 12622 | expressions.push(exp); |
| 12623 | parseFns.push($parse(exp, parseStringifyInterceptor)); |
| 12624 | index = endIndex + endSymbolLength; |
| 12625 | expressionPositions.push(concat.length); |
| 12626 | concat.push(''); |
| 12627 | } else { |
| 12628 | // we did not find an interpolation, so we have to add the remainder to the separators array |
| 12629 | if (index !== textLength) { |
| 12630 | concat.push(unescapeText(text.substring(index))); |
| 12631 | } |
| 12632 | break; |
| 12633 | } |
| 12634 | } |
| 12635 | |
| 12636 | // Concatenating expressions makes it hard to reason about whether some combination of |
| 12637 | // concatenated values are unsafe to use and could easily lead to XSS. By requiring that a |
| 12638 | // single expression be used for iframe[src], object[src], etc., we ensure that the value |
| 12639 | // that's used is assigned or constructed by some JS code somewhere that is more testable or |
| 12640 | // make it obvious that you bound the value to some user controlled value. This helps reduce |
| 12641 | // the load when auditing for XSS issues. |
| 12642 | if (trustedContext && concat.length > 1) { |
| 12643 | $interpolateMinErr.throwNoconcat(text); |
| 12644 | } |
| 12645 | |
| 12646 | if (!mustHaveExpression || expressions.length) { |
| 12647 | var compute = function(values) { |
no test coverage detected