forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessageFormatCommon.js
67 lines (62 loc) · 2.21 KB
/
messageFormatCommon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
// NOTE: ADVANCED_OPTIMIZATIONS mode.
//
// This file is compiled with Closure compiler's ADVANCED_OPTIMIZATIONS flag! Be wary of using
// constructs incompatible with that mode.
/* global isFunction: false */
/* global noop: false */
/* global toJson: false */
/* global $$stringify: false */
// Convert an index into the string into line/column for use in error messages
// As such, this doesn't have to be efficient.
function indexToLineAndColumn(text, index) {
var lines = text.split(/\n/g);
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (index >= line.length) {
index -= line.length;
} else {
return { line: i + 1, column: index + 1 };
}
}
}
var PARSE_CACHE_FOR_TEXT_LITERALS = Object.create(null);
function parseTextLiteral(text) {
var cachedFn = PARSE_CACHE_FOR_TEXT_LITERALS[text];
if (cachedFn != null) {
return cachedFn;
}
function parsedFn(context) { return text; }
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
var unwatch = scope['$watch'](noop,
function textLiteralWatcher() {
if (isFunction(listener)) { listener(text, text, scope); }
unwatch();
},
objectEquality);
return unwatch;
};
PARSE_CACHE_FOR_TEXT_LITERALS[text] = parsedFn;
parsedFn['exp'] = text; // Needed to pretend to be $interpolate for tests copied from interpolateSpec.js
parsedFn['expressions'] = []; // Require this to call $compile.$$addBindingInfo() which allows Protractor to find elements by binding.
return parsedFn;
}
function subtractOffset(expressionFn, offset) {
if (offset === 0) {
return expressionFn;
}
function minusOffset(value) {
return (value == null) ? value : value - offset;
}
function parsedFn(context) { return minusOffset(expressionFn(context)); }
var unwatch;
parsedFn['$$watchDelegate'] = function watchDelegate(scope, listener, objectEquality) {
unwatch = scope['$watch'](expressionFn,
function pluralExpressionWatchListener(newValue, oldValue) {
if (isFunction(listener)) { listener(minusOffset(newValue), minusOffset(oldValue), scope); }
},
objectEquality);
return unwatch;
};
return parsedFn;
}