Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8092864

Browse files
committed
Throw error when compiling multiple roots
Closes angular#338
1 parent e87d58c commit 8092864

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/Compiler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ Compiler.prototype = {
169169
var index = 0,
170170
template,
171171
parent = templateElement.parent();
172+
if (templateElement.length > 1) {
173+
// https://github.com/angular/angular.js/issues/338
174+
throw Error("Cannot compile multiple element roots: " +
175+
jqLite('<div>').append(templateElement.clone()).html());
176+
}
172177
if (parent && parent[0]) {
173178
parent = parent[0];
174179
for(var i = 0; i < parent.childNodes.length; i++) {

test/BinderSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ describe('Binder', function(){
4040
});
4141

4242
it('ChangingRadioUpdatesModel', function(){
43-
var scope = this.compile('<input type="radio" name="model.price" value="A" checked>' +
44-
'<input type="radio" name="model.price" value="B">');
43+
var scope = this.compile('<div><input type="radio" name="model.price" value="A" checked>' +
44+
'<input type="radio" name="model.price" value="B"></div>');
4545
scope.$apply();
4646
assertEquals(scope.model.price, 'A');
4747
});

test/CompilerSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ describe('compiler', function(){
3535
dealoc(scope);
3636
});
3737

38+
it('should not allow compilation of multiple roots', function(){
39+
expect(function(){
40+
compiler.compile('<div>A</div><span></span>');
41+
}).toThrow("Cannot compile multiple element roots: " + ie("<div>A</div><span></span>"));
42+
function ie(text) {
43+
return msie ? uppercase(text) : text;
44+
}
45+
});
46+
3847
it('should recognize a directive', function(){
3948
var e = jqLite('<div directive="expr" ignore="me"></div>');
4049
directives.directive = function(expression, element){

test/service/invalidWidgetsSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('$invalidWidgets', function() {
1212

1313

1414
it("should count number of invalid widgets", function(){
15-
var element = jqLite('<input name="price" ng:required ng:validate="number"></input>');
15+
var element = jqLite('<input name="price" ng:required ng:validate="number">');
1616
jqLite(document.body).append(element);
1717
scope = compile(element)();
1818
var $invalidWidgets = scope.$service('$invalidWidgets');

0 commit comments

Comments
 (0)