Skip to content

Commit 5aa7877

Browse files
committed
fix(script): parse template as JSON if content-type attr matches 'json'
This change is needed so that $http can avoid parsing JSON if an inline template is in use. BREAKING CHANGE: inline templates which were previously parsed as JSON by default, now require the attribute `content-type` to contain the text 'json'. Additionally, the data pushed into $templateCache by the script directive is now always an array, with the following items: [ status (200), data (script text or parsed JSON), headers (the empty object) ] BEFORE: ``` <script type="text/ng-template" id="famous-foods.json"> { "elvis presley": "peanut butter, banana & bacon sandwich", "shirley temple": "whipped potatoes & roast beef", "marilyn monroe": "grilled steak" } </script> ``` AFTER: ``` <script type="text/ng-template" id="famous-foods.json" content-type="application/json"> { "elvis presley": "peanut butter, banana & bacon sandwich", "shirley temple": "whipped potatoes & roast beef", "marilyn monroe": "grilled steak" } </script>
1 parent 34fee06 commit 5aa7877

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

docs/src/example.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ exports.Example.prototype.toHtmlTabs = function() {
115115
htmlTabs(this.html);
116116
htmlTabs(this.css);
117117
htmlTabs(this.js);
118-
htmlTabs(this.json);
118+
htmlTabs(this.json, 'application/json');
119119
htmlTabs(this.unit);
120120
htmlTabs(this.scenario);
121121
htmlTabs(this.protractorTest);
122122
out.push('</div>');
123123
return out.join('');
124124

125-
function htmlTabs(sources) {
125+
function htmlTabs(sources, contentType) {
126126
sources.forEach(function(source) {
127127
var wrap = '',
128128
isCss = source.name.match(/\.css$/),
@@ -139,7 +139,9 @@ exports.Example.prototype.toHtmlTabs = function() {
139139
'<pre class="prettyprint linenums" ng-set-text="' + source.id + '"' + wrap + '></pre>\n' +
140140
(isCss
141141
? ('<style type="text/css" id="' + source.id + '">' + source.content + '</style>\n')
142-
: ('<script type="text/ng-template" id="' + source.id + '">' + source.content + '</script>\n') ) +
142+
: ('<script type="text/ng-template"' + (' id="' + source.id + '"') +
143+
(contentType ? ' content-type="' + contentType + '"' : '') + '>' +
144+
source.content + '</script>\n') ) +
143145
'</div>\n');
144146
});
145147
}

src/ng/directive/script.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
</doc:example>
3535
*/
3636
var scriptDirective = ['$templateCache', function($templateCache) {
37+
var JSON_CONTENT_TYPE = /json/;
3738
return {
3839
restrict: 'E',
3940
terminal: true,
@@ -43,7 +44,11 @@ var scriptDirective = ['$templateCache', function($templateCache) {
4344
// IE is not consistent, in scripts we have to read .text but in other nodes we have to read .textContent
4445
text = element[0].text;
4546

46-
$templateCache.put(templateUrl, text);
47+
if (JSON_CONTENT_TYPE.test(attr.contentType || '')) {
48+
text = JSON.parse(text);
49+
}
50+
51+
$templateCache.put(templateUrl, [200, text, {}]);
4752
}
4853
}
4954
};

test/ng/directive/scriptSpec.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('scriptDirective', function() {
1515
'<script id="/ignore">ignore me</script>' +
1616
'<script type="text/ng-template" id="/myTemplate.html"><x>{{y}}</x></script>' +
1717
'</div>' );
18-
expect($templateCache.get('/myTemplate.html')).toBe('<x>{{y}}</x>');
18+
expect($templateCache.get('/myTemplate.html')[1]).toBe('<x>{{y}}</x>');
1919
expect($templateCache.get('/ignore')).toBeUndefined();
2020
}
2121
));
@@ -36,4 +36,11 @@ describe('scriptDirective', function() {
3636
expect(scripts.eq(1)[0].text).toBe('other {{binding}}');
3737
dealoc(doc);
3838
}));
39+
40+
41+
it('should parse JSON if attribute `content-type` matches \'json\'', inject(function($compile, $templateCache, $rootScope) {
42+
var doc = '<div><script type="text/ng-template" id="file.json" content-type="application/json">{"foo":"bar"}</script></div>';
43+
element = $compile(doc)($rootScope);
44+
expect($templateCache.get('file.json')[1]).toEqual({"foo": "bar"});
45+
}));
3946
});

0 commit comments

Comments
 (0)