diff --git a/lib/core_dom/element_binder.dart b/lib/core_dom/element_binder.dart index ad14c2be5..98fa833bd 100644 --- a/lib/core_dom/element_binder.dart +++ b/lib/core_dom/element_binder.dart @@ -402,10 +402,11 @@ class TaggedElementBinder { final ElementBinder binder; int parentBinderOffset; var injector; + bool isTopLevel; List textBinders; - TaggedElementBinder(this.binder, this.parentBinderOffset); + TaggedElementBinder(this.binder, this.parentBinderOffset, this.isTopLevel); void addText(TaggedTextBinder tagged) { if (textBinders == null) textBinders = []; diff --git a/lib/core_dom/tagging_compiler.dart b/lib/core_dom/tagging_compiler.dart index bc400785a..8139213c9 100644 --- a/lib/core_dom/tagging_compiler.dart +++ b/lib/core_dom/tagging_compiler.dart @@ -19,7 +19,8 @@ class TaggingCompiler implements Compiler { DirectiveMap directives, int parentElementBinderOffset, TaggedElementBinder directParentElementBinder, - List elementBinders) { + List elementBinders, + bool isTopLevel) { assert(parentElementBinderOffset != null); assert(parentElementBinderOffset < elementBinders.length); if (domCursor.current == null) return null; @@ -55,7 +56,7 @@ class TaggingCompiler implements Compiler { int taggedElementBinderIndex = parentElementBinderOffset; if (elementBinder.hasDirectivesOrEvents || elementBinder.hasTemplate) { taggedElementBinder = _addBinder(elementBinders, - new TaggedElementBinder(elementBinder, parentElementBinderOffset)); + new TaggedElementBinder(elementBinder, parentElementBinderOffset, isTopLevel)); taggedElementBinderIndex = elementBinders.length - 1; // TODO(deboer): Hack, this sucks. @@ -72,11 +73,11 @@ class TaggingCompiler implements Compiler { addedDummy = true; // add a dummy to the list which may be removed later. taggedElementBinder = _addBinder(elementBinders, - new TaggedElementBinder(null, parentElementBinderOffset)); + new TaggedElementBinder(null, parentElementBinderOffset, isTopLevel)); } _compileView(domCursor, templateCursor, null, directives, - taggedElementBinderIndex, taggedElementBinder, elementBinders); + taggedElementBinderIndex, taggedElementBinder, elementBinders, false); if (addedDummy && !_isDummyBinder(taggedElementBinder)) { // We are keeping the element binder, so add the class @@ -106,7 +107,7 @@ class TaggingCompiler implements Compiler { templateCursor.current.parentNode != null)) { // Always add an elementBinder for top-level text. _addBinder(elementBinders, new TaggedElementBinder(elementBinder, - parentElementBinderOffset)); + parentElementBinderOffset, isTopLevel)); } } else { throw "Unsupported node type for $node: [${node.nodeType}]"; @@ -130,7 +131,7 @@ class TaggingCompiler implements Compiler { var domCursorIndex = domCursor.index; var elementBinders = []; _compileView(domCursor, transcludeCursor, transcludedElementBinder, - directives, -1, null, elementBinders); + directives, -1, null, elementBinders, true); viewFactory = new TaggingViewFactory(transcludeCursor.elements, _removeUnusedBinders(elementBinders), _perf, _expando); @@ -160,7 +161,7 @@ class TaggingCompiler implements Compiler { final elementBinders = []; _compileView( new NodeCursor(domElements), new NodeCursor(templateElements), - null, directives, -1, null, elementBinders); + null, directives, -1, null, elementBinders, true); var viewFactory = new TaggingViewFactory( templateElements, _removeUnusedBinders(elementBinders), _perf, _expando); @@ -170,7 +171,7 @@ class TaggingCompiler implements Compiler { } _isDummyBinder(TaggedElementBinder binder) => - binder.binder == null && binder.textBinders == null; + binder.binder == null && binder.textBinders == null && !binder.isTopLevel; _removeUnusedBinders(List binders) { // In order to support text nodes with directiveless parents, we diff --git a/lib/core_dom/tagging_view_factory.dart b/lib/core_dom/tagging_view_factory.dart index 065989327..1b7125584 100644 --- a/lib/core_dom/tagging_view_factory.dart +++ b/lib/core_dom/tagging_view_factory.dart @@ -71,10 +71,10 @@ class TaggingViewFactory implements ViewFactory { } } else if (node.nodeType == 3 || node.nodeType == 8) { TaggedElementBinder tagged = elementBinders[elementBinderIndex]; - assert(tagged.binder != null); - - _bindTagged(tagged, rootInjector, elementBinders, view, node); - + assert(tagged.binder != null || tagged.isTopLevel); + if (tagged.binder != null) { + _bindTagged(tagged, rootInjector, elementBinders, view, node); + } elementBinderIndex++; } else { throw "nodeType sadness ${node.nodeType}}"; diff --git a/test/core_dom/compiler_spec.dart b/test/core_dom/compiler_spec.dart index 3e7600b87..27783744b 100644 --- a/test/core_dom/compiler_spec.dart +++ b/test/core_dom/compiler_spec.dart @@ -58,6 +58,11 @@ void main() { _.compile([]); }); + it('should compile a comment on the top level', () { + _.compile(''); + expect(_.rootElements[0]).toHaveHtml(''); + }); + it('should compile a comment with no directives around', () { var element = _.compile('
'); expect(element).toHaveHtml('');