Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0e1fa2a

Browse files
mheveryIgorMinar
authored andcommitted
feat($interpolate): string interpolation function
1 parent 3d0ce0e commit 0e1fa2a

File tree

6 files changed

+105
-67
lines changed

6 files changed

+105
-67
lines changed

angularFiles.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ angularFiles = {
2424
'src/service/filter/limitTo.js',
2525
'src/service/filter/orderBy.js',
2626
'src/service/formFactory.js',
27+
'src/service/interpolate.js',
2728
'src/service/location.js',
2829
'src/service/log.js',
2930
'src/service/resource.js',

src/AngularPublic.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function ngModule($provide, $injector) {
7676
$provide.service('$document', $DocumentProvider);
7777
$provide.service('$exceptionHandler', $ExceptionHandlerProvider);
7878
$provide.service('$filter', $FilterProvider);
79+
$provide.service('$interpolate', $InterpolateProvider);
7980
$provide.service('$formFactory', $FormFactoryProvider);
8081
$provide.service('$http', $HttpProvider);
8182
$provide.service('$httpBackend', $HttpBackendProvider);

src/directives.js

-39
Original file line numberDiff line numberDiff line change
@@ -282,45 +282,6 @@ angularDirective("ng:bind", function(expression, element){
282282
}];
283283
});
284284

285-
var bindTemplateCache = {};
286-
function compileBindTemplate(template){
287-
var fn = bindTemplateCache[template];
288-
if (!fn) {
289-
var bindings = [];
290-
forEach(parseBindings(template), function(text){
291-
var exp = binding(text);
292-
bindings.push(exp
293-
? function(scope, element) { return scope.$eval(exp); }
294-
: function() { return text; });
295-
});
296-
bindTemplateCache[template] = fn = function(scope, element, prettyPrintJson) {
297-
var parts = [],
298-
hadOwnElement = scope.hasOwnProperty('$element'),
299-
oldElement = scope.$element;
300-
301-
// TODO(misko): get rid of $element
302-
scope.$element = element;
303-
try {
304-
for (var i = 0; i < bindings.length; i++) {
305-
var value = bindings[i](scope, element);
306-
if (isElement(value))
307-
value = '';
308-
else if (isObject(value))
309-
value = toJson(value, prettyPrintJson);
310-
parts.push(value);
311-
}
312-
return parts.join('');
313-
} finally {
314-
if (hadOwnElement) {
315-
scope.$element = oldElement;
316-
} else {
317-
delete scope.$element;
318-
}
319-
}
320-
};
321-
}
322-
return fn;
323-
}
324285

325286
/**
326287
* @ngdoc directive

src/markups.js

-28
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,6 @@
5151
* Understanding Angular Markup} in the Angular Developer Guide.
5252
*/
5353

54-
function parseBindings(string) {
55-
var results = [];
56-
var lastIndex = 0;
57-
var index;
58-
while((index = string.indexOf('{{', lastIndex)) > -1) {
59-
if (lastIndex < index)
60-
results.push(string.substr(lastIndex, index - lastIndex));
61-
lastIndex = index;
62-
63-
index = string.indexOf('}}', index);
64-
index = index < 0 ? string.length : index + 2;
65-
66-
results.push(string.substr(lastIndex, index - lastIndex));
67-
lastIndex = index;
68-
}
69-
if (lastIndex != string.length)
70-
results.push(string.substr(lastIndex, string.length - lastIndex));
71-
return results.length === 0 ? [ string ] : results;
72-
}
73-
74-
function binding(string) {
75-
var binding = string.replace(/\n/gm, ' ').match(/^\{\{(.*)\}\}$/);
76-
return binding ? binding[1] : null;
77-
}
78-
79-
function hasBindings(bindings) {
80-
return bindings.length > 1 || binding(bindings[0]) !== null;
81-
}
8254

8355
angularTextMarkup('{{}}', function(text, textNode, parentElement) {
8456
var bindings = parseBindings(text),

src/service/interpolate.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
function $InterpolateProvider(){
4+
this.$get = ['$parse', function($parse){
5+
return function(text, templateOnly) {
6+
var bindings = parseBindings(text);
7+
if (hasBindings(bindings) || !templateOnly) {
8+
return compileBindTemplate(text);
9+
}
10+
};
11+
}];
12+
}
13+
14+
var bindTemplateCache = {};
15+
function compileBindTemplate(template){
16+
var fn = bindTemplateCache[template];
17+
if (!fn) {
18+
var bindings = [];
19+
forEach(parseBindings(template), function(text){
20+
var exp = binding(text);
21+
bindings.push(exp
22+
? function(scope, element) { return scope.$eval(exp); }
23+
: function() { return text; });
24+
});
25+
bindTemplateCache[template] = fn = function(scope, element, prettyPrintJson) {
26+
var parts = [],
27+
hadOwnElement = scope.hasOwnProperty('$element'),
28+
oldElement = scope.$element;
29+
30+
// TODO(misko): get rid of $element
31+
scope.$element = element;
32+
try {
33+
for (var i = 0; i < bindings.length; i++) {
34+
var value = bindings[i](scope, element);
35+
if (isElement(value))
36+
value = '';
37+
else if (isObject(value))
38+
value = toJson(value, prettyPrintJson);
39+
parts.push(value);
40+
}
41+
return parts.join('');
42+
} finally {
43+
if (hadOwnElement) {
44+
scope.$element = oldElement;
45+
} else {
46+
delete scope.$element;
47+
}
48+
}
49+
};
50+
}
51+
return fn;
52+
}
53+
54+
55+
function parseBindings(string) {
56+
var results = [];
57+
var lastIndex = 0;
58+
var index;
59+
while((index = string.indexOf('{{', lastIndex)) > -1) {
60+
if (lastIndex < index)
61+
results.push(string.substr(lastIndex, index - lastIndex));
62+
lastIndex = index;
63+
64+
index = string.indexOf('}}', index);
65+
index = index < 0 ? string.length : index + 2;
66+
67+
results.push(string.substr(lastIndex, index - lastIndex));
68+
lastIndex = index;
69+
}
70+
if (lastIndex != string.length)
71+
results.push(string.substr(lastIndex, string.length - lastIndex));
72+
return results.length === 0 ? [ string ] : results;
73+
}
74+
75+
function binding(string) {
76+
var binding = string.replace(/\n/gm, ' ').match(/^\{\{(.*)\}\}$/);
77+
return binding ? binding[1] : null;
78+
}
79+
80+
function hasBindings(bindings) {
81+
return bindings.length > 1 || binding(bindings[0]) !== null;
82+
}

test/service/interpolateSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
describe('$interpolate', function() {
4+
5+
it('should return a function when there are no bindings and textOnly is undefined',
6+
inject(function($interpolate) {
7+
expect(typeof $interpolate('some text')).toBe('function');
8+
}));
9+
10+
11+
it('should return undefined when there are no bindings and textOnly is set to true',
12+
inject(function($interpolate) {
13+
expect($interpolate('some text', true)).toBeUndefined();
14+
}));
15+
16+
17+
it('should return interpolation function', inject(function($interpolate, $rootScope) {
18+
$rootScope.name = 'Misko';
19+
expect($interpolate('Hello {{name}}!')($rootScope)).toEqual('Hello Misko!');
20+
}));
21+
});

0 commit comments

Comments
 (0)