Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(tagging-compiler): support top level comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chirayuk committed Mar 25, 2014
1 parent d22899a commit dc75b01
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,11 @@ class TaggedElementBinder {
final ElementBinder binder;
int parentBinderOffset;
var injector;
bool isTopLevel;

List<TaggedTextBinder> textBinders;

TaggedElementBinder(this.binder, this.parentBinderOffset);
TaggedElementBinder(this.binder, this.parentBinderOffset, this.isTopLevel);

void addText(TaggedTextBinder tagged) {
if (textBinders == null) textBinders = [];
Expand Down
17 changes: 9 additions & 8 deletions lib/core_dom/tagging_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class TaggingCompiler implements Compiler {
DirectiveMap directives,
int parentElementBinderOffset,
TaggedElementBinder directParentElementBinder,
List<TaggedElementBinder> elementBinders) {
List<TaggedElementBinder> elementBinders,
bool isTopLevel) {
assert(parentElementBinderOffset != null);
assert(parentElementBinderOffset < elementBinders.length);
if (domCursor.current == null) return null;
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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}]";
Expand All @@ -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);
Expand Down Expand Up @@ -160,7 +161,7 @@ class TaggingCompiler implements Compiler {
final elementBinders = <TaggedElementBinder>[];
_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);
Expand All @@ -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<TaggedElementBinder> binders) {
// In order to support text nodes with directiveless parents, we
Expand Down
8 changes: 4 additions & 4 deletions lib/core_dom/tagging_view_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}}";
Expand Down
5 changes: 5 additions & 0 deletions test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ void main() {
_.compile([]);
});

it('should compile a comment on the top level', () {
_.compile('<!-- comment -->');
expect(_.rootElements[0]).toHaveHtml('<!-- comment -->');
});

it('should compile a comment with no directives around', () {
var element = _.compile('<div><!-- comment --></div>');
expect(element).toHaveHtml('<!-- comment -->');
Expand Down

0 comments on commit dc75b01

Please sign in to comment.