* @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)
| 11735 | * - `context`: evaluation context for all expressions embedded in the interpolated text |
| 11736 | */ |
| 11737 | function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) { |
| 11738 | // Provide a quick exit and simplified result function for text with no interpolation |
| 11739 | if (!text.length || text.indexOf(startSymbol) === -1) { |
| 11740 | var constantInterp; |
| 11741 | if (!mustHaveExpression) { |
| 11742 | var unescapedText = unescapeText(text); |
| 11743 | constantInterp = valueFn(unescapedText); |
| 11744 | constantInterp.exp = text; |
| 11745 | constantInterp.expressions = []; |
| 11746 | constantInterp.$$watchDelegate = constantWatchDelegate; |
| 11747 | } |
| 11748 | return constantInterp; |
| 11749 | } |
| 11750 | |
| 11751 | allOrNothing = !!allOrNothing; |
| 11752 | var startIndex, |
| 11753 | endIndex, |
| 11754 | index = 0, |
| 11755 | expressions = [], |
| 11756 | parseFns = [], |
| 11757 | textLength = text.length, |
| 11758 | exp, |
| 11759 | concat = [], |
| 11760 | expressionPositions = []; |
| 11761 | |
| 11762 | while (index < textLength) { |
| 11763 | if (((startIndex = text.indexOf(startSymbol, index)) != -1) && |
| 11764 | ((endIndex = text.indexOf(endSymbol, startIndex + startSymbolLength)) != -1)) { |
| 11765 | if (index !== startIndex) { |
| 11766 | concat.push(unescapeText(text.substring(index, startIndex))); |
| 11767 | } |
| 11768 | exp = text.substring(startIndex + startSymbolLength, endIndex); |
| 11769 | expressions.push(exp); |
| 11770 | parseFns.push($parse(exp, parseStringifyInterceptor)); |
| 11771 | index = endIndex + endSymbolLength; |
| 11772 | expressionPositions.push(concat.length); |
| 11773 | concat.push(''); |
| 11774 | } else { |
| 11775 | // we did not find an interpolation, so we have to add the remainder to the separators array |
| 11776 | if (index !== textLength) { |
| 11777 | concat.push(unescapeText(text.substring(index))); |
| 11778 | } |
| 11779 | break; |
| 11780 | } |
| 11781 | } |
| 11782 | |
| 11783 | // Concatenating expressions makes it hard to reason about whether some combination of |
| 11784 | // concatenated values are unsafe to use and could easily lead to XSS. By requiring that a |
| 11785 | // single expression be used for iframe[src], object[src], etc., we ensure that the value |
| 11786 | // that's used is assigned or constructed by some JS code somewhere that is more testable or |
| 11787 | // make it obvious that you bound the value to some user controlled value. This helps reduce |
| 11788 | // the load when auditing for XSS issues. |
| 11789 | if (trustedContext && concat.length > 1) { |
| 11790 | $interpolateMinErr.throwNoconcat(text); |
| 11791 | } |
| 11792 | |
| 11793 | if (!mustHaveExpression || expressions.length) { |
| 11794 | var compute = function(values) { |
no test coverage detected