2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2018-12-27 11:54:16 +00:00
|
|
|
import {ng} from '../vendor';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
function stringify(value) {
|
|
|
|
if (value === null) { // null || undefined
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
switch (typeof value) {
|
|
|
|
case 'string':
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
value = String(value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = angular.toJson(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-12-27 11:54:16 +00:00
|
|
|
var $interpolateMinErr = ng.$interpolateMinErr = ng.$$minErr('$interpolate');
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
$interpolateMinErr.throwNoconcat = function(text) {
|
|
|
|
throw $interpolateMinErr('noconcat',
|
|
|
|
'Error while interpolating: {0}\nStrict Contextual Escaping disallows ' +
|
|
|
|
'interpolations that concatenate multiple expressions when a trusted value is ' +
|
|
|
|
'required. See http://docs.angularjs.org/api/ng.$sce', text);
|
|
|
|
};
|
|
|
|
|
|
|
|
$interpolateMinErr.interr = function(text, err) {
|
|
|
|
return $interpolateMinErr('interr', 'Can\'t interpolate: {0}\n{1}', text, err.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
function $get($parse, $exceptionHandler, $sce) {
|
2018-02-10 15:18:01 +00:00
|
|
|
let startSymbolLength = this._startSymbol.length;
|
|
|
|
let endSymbolLength = this._endSymbol.length;
|
|
|
|
let escapedStartRegexp = new RegExp(this._startSymbol.replace(/./g, escape), 'g');
|
|
|
|
let escapedEndRegexp = new RegExp(this._endSymbol.replace(/./g, escape), 'g');
|
|
|
|
let self = this;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
function escape(ch) {
|
|
|
|
return '\\\\\\' + ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unescapeText(text) {
|
|
|
|
return text.replace(escapedStartRegexp, self._startSymbol)
|
|
|
|
.replace(escapedEndRegexp, self._endSymbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is the same as the constantWatchDelegate in parse.js
|
|
|
|
function constantWatchDelegate(scope, listener, objectEquality, constantInterp) {
|
|
|
|
var unwatch = scope.$watch(function constantInterpolateWatch(scope) {
|
|
|
|
unwatch();
|
|
|
|
return constantInterp(scope);
|
|
|
|
}, listener, objectEquality);
|
|
|
|
return unwatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
function $interpolate(text, mustHaveExpression, trustedContext, allOrNothing) {
|
|
|
|
// Provide a quick exit and simplified result function for text with no interpolation
|
|
|
|
if (!text.length || text.indexOf(self._startSymbol) === -1) {
|
|
|
|
var constantInterp;
|
|
|
|
if (!mustHaveExpression) {
|
|
|
|
var unescapedText = unescapeText(text);
|
|
|
|
constantInterp = valueFn(unescapedText);
|
|
|
|
constantInterp.exp = text;
|
|
|
|
constantInterp.expressions = [];
|
|
|
|
constantInterp.$$watchDelegate = constantWatchDelegate;
|
|
|
|
}
|
|
|
|
return constantInterp;
|
|
|
|
}
|
|
|
|
|
|
|
|
allOrNothing = Boolean(allOrNothing);
|
2018-02-10 15:18:01 +00:00
|
|
|
let startIndex;
|
|
|
|
let endIndex;
|
|
|
|
let index = 0;
|
|
|
|
let expressions = [];
|
|
|
|
let parseFns = [];
|
|
|
|
let textLength = text.length;
|
|
|
|
let exp;
|
|
|
|
let concat = [];
|
|
|
|
let expressionPositions = [];
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
while (index < textLength) {
|
|
|
|
if (((startIndex = text.indexOf(self._startSymbol, index)) !== -1) &&
|
|
|
|
((endIndex = text.indexOf(self._endSymbol, startIndex + startSymbolLength)) !== -1)) {
|
2017-06-05 07:01:21 +00:00
|
|
|
if (index !== startIndex)
|
|
|
|
concat.push(unescapeText(text.substring(index, startIndex)));
|
2017-01-31 13:13:06 +00:00
|
|
|
exp = text.substring(startIndex + startSymbolLength, endIndex);
|
|
|
|
expressions.push(exp);
|
|
|
|
parseFns.push($parse(exp, parseStringifyInterceptor));
|
|
|
|
index = endIndex + endSymbolLength;
|
|
|
|
expressionPositions.push(concat.length);
|
|
|
|
concat.push('');
|
|
|
|
} else {
|
|
|
|
// we did not find an interpolation, so we have to add the remainder to the separators array
|
2017-06-05 07:01:21 +00:00
|
|
|
if (index !== textLength)
|
|
|
|
concat.push(unescapeText(text.substring(index)));
|
2017-01-31 13:13:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trustedContext && concat.length > 1) {
|
|
|
|
$interpolateMinErr.throwNoconcat(text);
|
|
|
|
}
|
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
var getValue = function(value) {
|
|
|
|
return trustedContext ?
|
|
|
|
$sce.getTrusted(trustedContext, value) :
|
|
|
|
$sce.valueOf(value);
|
|
|
|
};
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
if (!mustHaveExpression || expressions.length) {
|
|
|
|
var compute = function(values) {
|
|
|
|
for (var i = 0, ii = expressions.length; i < ii; i++) {
|
2017-06-05 07:01:21 +00:00
|
|
|
if (allOrNothing && isUndefined(values[i])) return;
|
|
|
|
concat[expressionPositions[i]] = values[i];
|
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
return concat.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
return angular.extend(function interpolationFn(context) {
|
|
|
|
var i = 0;
|
|
|
|
var ii = expressions.length;
|
|
|
|
var values = new Array(ii);
|
|
|
|
|
|
|
|
try {
|
2017-06-05 07:01:21 +00:00
|
|
|
for (; i < ii; i++) {
|
|
|
|
values[i] = parseFns[i](context);
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
2017-06-05 07:01:21 +00:00
|
|
|
|
|
|
|
return compute(values);
|
|
|
|
} catch (err) {
|
|
|
|
$exceptionHandler($interpolateMinErr.interr(text, err));
|
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
}, {
|
2017-06-05 07:01:21 +00:00
|
|
|
// all of these properties are undocumented for now
|
|
|
|
exp: text, // just for compatibility with regular watchers created via $watch
|
|
|
|
expressions: expressions
|
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseStringifyInterceptor(value) {
|
|
|
|
try {
|
|
|
|
value = getValue(value);
|
|
|
|
return allOrNothing && !isDefined(value) ? value : stringify(value);
|
|
|
|
} catch (err) {
|
|
|
|
$exceptionHandler($interpolateMinErr.interr(text, err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$interpolate.startSymbol = function() {
|
|
|
|
return startSymbol;
|
|
|
|
};
|
|
|
|
|
|
|
|
$interpolate.endSymbol = function() {
|
|
|
|
return endSymbol;
|
|
|
|
};
|
|
|
|
|
|
|
|
return $interpolate;
|
|
|
|
}
|
|
|
|
|
|
|
|
$get.$inject = ['$parse', '$exceptionHandler', '$sce'];
|
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
export class Interpolate {
|
2017-01-31 13:13:06 +00:00
|
|
|
constructor() {
|
|
|
|
this._startSymbol = '*[';
|
|
|
|
this._endSymbol = ']*';
|
|
|
|
}
|
|
|
|
set startSymbol(value) {
|
|
|
|
if (value) {
|
|
|
|
this._startSymbol = value;
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-05 07:01:21 +00:00
|
|
|
return this._startSymbol;
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
set endSymbol(value) {
|
|
|
|
if (value) {
|
|
|
|
this._endSymbol = value;
|
|
|
|
return this;
|
|
|
|
}
|
2017-06-05 07:01:21 +00:00
|
|
|
return this._endSymbol;
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Interpolate.prototype.$get = $get;
|
|
|
|
var interpolate = new Interpolate();
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.provider('vnInterpolate', () => interpolate);
|