Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit dc75b01

Browse files
committed
fix(tagging-compiler): support top level comments
1 parent d22899a commit dc75b01

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

lib/core_dom/element_binder.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,11 @@ class TaggedElementBinder {
402402
final ElementBinder binder;
403403
int parentBinderOffset;
404404
var injector;
405+
bool isTopLevel;
405406

406407
List<TaggedTextBinder> textBinders;
407408

408-
TaggedElementBinder(this.binder, this.parentBinderOffset);
409+
TaggedElementBinder(this.binder, this.parentBinderOffset, this.isTopLevel);
409410

410411
void addText(TaggedTextBinder tagged) {
411412
if (textBinders == null) textBinders = [];

lib/core_dom/tagging_compiler.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class TaggingCompiler implements Compiler {
1919
DirectiveMap directives,
2020
int parentElementBinderOffset,
2121
TaggedElementBinder directParentElementBinder,
22-
List<TaggedElementBinder> elementBinders) {
22+
List<TaggedElementBinder> elementBinders,
23+
bool isTopLevel) {
2324
assert(parentElementBinderOffset != null);
2425
assert(parentElementBinderOffset < elementBinders.length);
2526
if (domCursor.current == null) return null;
@@ -55,7 +56,7 @@ class TaggingCompiler implements Compiler {
5556
int taggedElementBinderIndex = parentElementBinderOffset;
5657
if (elementBinder.hasDirectivesOrEvents || elementBinder.hasTemplate) {
5758
taggedElementBinder = _addBinder(elementBinders,
58-
new TaggedElementBinder(elementBinder, parentElementBinderOffset));
59+
new TaggedElementBinder(elementBinder, parentElementBinderOffset, isTopLevel));
5960
taggedElementBinderIndex = elementBinders.length - 1;
6061

6162
// TODO(deboer): Hack, this sucks.
@@ -72,11 +73,11 @@ class TaggingCompiler implements Compiler {
7273
addedDummy = true;
7374
// add a dummy to the list which may be removed later.
7475
taggedElementBinder = _addBinder(elementBinders,
75-
new TaggedElementBinder(null, parentElementBinderOffset));
76+
new TaggedElementBinder(null, parentElementBinderOffset, isTopLevel));
7677
}
7778

7879
_compileView(domCursor, templateCursor, null, directives,
79-
taggedElementBinderIndex, taggedElementBinder, elementBinders);
80+
taggedElementBinderIndex, taggedElementBinder, elementBinders, false);
8081

8182
if (addedDummy && !_isDummyBinder(taggedElementBinder)) {
8283
// We are keeping the element binder, so add the class
@@ -106,7 +107,7 @@ class TaggingCompiler implements Compiler {
106107
templateCursor.current.parentNode != null)) {
107108
// Always add an elementBinder for top-level text.
108109
_addBinder(elementBinders, new TaggedElementBinder(elementBinder,
109-
parentElementBinderOffset));
110+
parentElementBinderOffset, isTopLevel));
110111
}
111112
} else {
112113
throw "Unsupported node type for $node: [${node.nodeType}]";
@@ -130,7 +131,7 @@ class TaggingCompiler implements Compiler {
130131
var domCursorIndex = domCursor.index;
131132
var elementBinders = [];
132133
_compileView(domCursor, transcludeCursor, transcludedElementBinder,
133-
directives, -1, null, elementBinders);
134+
directives, -1, null, elementBinders, true);
134135

135136
viewFactory = new TaggingViewFactory(transcludeCursor.elements,
136137
_removeUnusedBinders(elementBinders), _perf, _expando);
@@ -160,7 +161,7 @@ class TaggingCompiler implements Compiler {
160161
final elementBinders = <TaggedElementBinder>[];
161162
_compileView(
162163
new NodeCursor(domElements), new NodeCursor(templateElements),
163-
null, directives, -1, null, elementBinders);
164+
null, directives, -1, null, elementBinders, true);
164165

165166
var viewFactory = new TaggingViewFactory(
166167
templateElements, _removeUnusedBinders(elementBinders), _perf, _expando);
@@ -170,7 +171,7 @@ class TaggingCompiler implements Compiler {
170171
}
171172

172173
_isDummyBinder(TaggedElementBinder binder) =>
173-
binder.binder == null && binder.textBinders == null;
174+
binder.binder == null && binder.textBinders == null && !binder.isTopLevel;
174175

175176
_removeUnusedBinders(List<TaggedElementBinder> binders) {
176177
// In order to support text nodes with directiveless parents, we

lib/core_dom/tagging_view_factory.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ class TaggingViewFactory implements ViewFactory {
7171
}
7272
} else if (node.nodeType == 3 || node.nodeType == 8) {
7373
TaggedElementBinder tagged = elementBinders[elementBinderIndex];
74-
assert(tagged.binder != null);
75-
76-
_bindTagged(tagged, rootInjector, elementBinders, view, node);
77-
74+
assert(tagged.binder != null || tagged.isTopLevel);
75+
if (tagged.binder != null) {
76+
_bindTagged(tagged, rootInjector, elementBinders, view, node);
77+
}
7878
elementBinderIndex++;
7979
} else {
8080
throw "nodeType sadness ${node.nodeType}}";

test/core_dom/compiler_spec.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ void main() {
5858
_.compile([]);
5959
});
6060

61+
it('should compile a comment on the top level', () {
62+
_.compile('<!-- comment -->');
63+
expect(_.rootElements[0]).toHaveHtml('<!-- comment -->');
64+
});
65+
6166
it('should compile a comment with no directives around', () {
6267
var element = _.compile('<div><!-- comment --></div>');
6368
expect(element).toHaveHtml('<!-- comment -->');

0 commit comments

Comments
 (0)