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

Commit

Permalink
fix(script): Incorrectly reading script text on ie
Browse files Browse the repository at this point in the history
IE deals with script tags in special way and .text() does not work. Reading the .text property directly fixes the issue.
  • Loading branch information
mhevery committed Apr 20, 2012
1 parent dc32ea6 commit 94dd685
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/ng/directive/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ var scriptDirective = ['$templateCache', function($templateCache) {
terminal: true,
compile: function(element, attr) {
if (attr.type == 'text/ng-template') {
var templateUrl = attr.id;
$templateCache.put(templateUrl, element.text());
var templateUrl = attr.id,
// IE is not consistent, in scripts we have to read .text but in other nodes we have to read .textContent
text = element[0].text;

$templateCache.put(templateUrl, text);
}
}
};
Expand Down
17 changes: 6 additions & 11 deletions test/ng/directive/scriptSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ describe('scriptDirective', function() {

it('should populate $templateCache with contents of a ng-template script element', inject(
function($compile, $templateCache) {
if (msie <=8) return;
// in ie8 it is not possible to create a script tag with the right content.
// it always comes up as empty. I was trying to set the text of the
// script tag, but that did not work either, so I gave up.
$compile('<div>foo' +
'<script id="/ignore">ignore me</script>' +
'<script type="text/ng-template" id="/myTemplate.html"><x>{{y}}</x></script>' +
Expand All @@ -26,19 +22,18 @@ describe('scriptDirective', function() {


it('should not compile scripts', inject(function($compile, $templateCache, $rootScope) {
if (msie <=8) return; // see above

var doc = jqLite('<div></div>');
// jQuery is too smart and removes
doc[0].innerHTML = '<script type="text/javascript">some {{binding}}</script>' +
'<script type="text/ng-template" id="/some">other {{binding}}</script>';
// jQuery is too smart and removes script tags
doc[0].innerHTML = 'foo' +
'<script type="text/javascript">some {{binding}}</script>' +
'<script type="text/ng-template" id="/some">other {{binding}}</script>';

$compile(doc)($rootScope);
$rootScope.$digest();

var scripts = doc.find('script');
expect(scripts.eq(0).text()).toBe('some {{binding}}');
expect(scripts.eq(1).text()).toBe('other {{binding}}');
expect(scripts.eq(0)[0].text).toBe('some {{binding}}');
expect(scripts.eq(1)[0].text).toBe('other {{binding}}');
dealoc(doc);
}));
});

0 comments on commit 94dd685

Please sign in to comment.