From 04a62e83bcd4067749fa5e2eb0181bc43500169c Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 6 May 2011 13:29:51 -0700 Subject: Throw error when compiling multiple roots Closes #338 --- src/Compiler.js | 5 +++++ test/BinderSpec.js | 4 ++-- test/CompilerSpec.js | 9 +++++++++ test/service/invalidWidgetsSpec.js | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Compiler.js b/src/Compiler.js index 08c76eea..f8d1e0f0 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -191,6 +191,11 @@ Compiler.prototype = { var index = 0, template, parent = templateElement.parent(); + if (templateElement.length > 1) { + // https://github.com/angular/angular.js/issues/338 + throw Error("Cannot compile multiple element roots: " + + jqLite('
').append(templateElement.clone()).html()); + } if (parent && parent[0]) { parent = parent[0]; for(var i = 0; i < parent.childNodes.length; i++) { diff --git a/test/BinderSpec.js b/test/BinderSpec.js index 15a1b5c7..d78573bb 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -40,8 +40,8 @@ describe('Binder', function(){ }); it('ChangingRadioUpdatesModel', function(){ - var scope = this.compile('' + - ''); + var scope = this.compile('
' + + '
'); scope.$eval(); assertEquals(scope.model.price, 'A'); }); diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js index b9505192..ff576581 100644 --- a/test/CompilerSpec.js +++ b/test/CompilerSpec.js @@ -35,6 +35,15 @@ describe('compiler', function(){ dealoc(scope); }); + it('should not allow compilation of multiple roots', function(){ + expect(function(){ + compiler.compile('
A
'); + }).toThrow("Cannot compile multiple element roots: " + ie("
A
")); + function ie(text) { + return msie ? uppercase(text) : text; + } + }); + it('should recognize a directive', function(){ var e = jqLite('
'); directives.directive = function(expression, element){ diff --git a/test/service/invalidWidgetsSpec.js b/test/service/invalidWidgetsSpec.js index 4bf16a6e..6b965ef8 100644 --- a/test/service/invalidWidgetsSpec.js +++ b/test/service/invalidWidgetsSpec.js @@ -12,7 +12,7 @@ describe('$invalidWidgets', function() { it("should count number of invalid widgets", function(){ - var element = jqLite(''); + var element = jqLite(''); jqLite(document.body).append(element); scope = compile(element)(); var $invalidWidgets = scope.$service('$invalidWidgets'); -- cgit v1.2.3