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): a text child after a directive child
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Mar 18, 2014
1 parent 364d9ff commit 81030dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class TaggedTextBinder {
// Used for the tagging compiler
class TaggedElementBinder {
final ElementBinder binder;
final int parentBinderOffset;
int parentBinderOffset;
var injector;

List<TaggedTextBinder> textBinders;
Expand Down
65 changes: 51 additions & 14 deletions lib/core_dom/tagging_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class TaggingCompiler implements Compiler {
}

node = domCursor.current;

if (node.nodeType == 1) {
var templateNode = templateCursor.current as dom.Element;

var taggedElementBinder = null;
int taggedElementBinderIndex = parentElementBinderOffset;
Expand All @@ -57,16 +59,35 @@ class TaggingCompiler implements Compiler {
taggedElementBinderIndex = elementBinders.length - 1;

// TODO(deboer): Hack, this sucks.
(templateCursor.current as dom.Element).classes.add('ng-binding');
templateNode.classes.add('ng-binding');
node.classes.add('ng-binding');
}

if (elementBinder.shouldCompileChildren) {
if (domCursor.descend()) {
templateCursor.descend();

var addedDummy = false;
if (taggedElementBinder == null) {
addedDummy = true;
// add a dummy to the list which may be removed later.
taggedElementBinder = _addBinder(elementBinders,
new TaggedElementBinder(null, parentElementBinderOffset));
}

_compileView(domCursor, templateCursor, null, directives,
taggedElementBinderIndex, taggedElementBinder, elementBinders);

if (addedDummy && !_isDummyBinder(taggedElementBinder)) {
// We are keeping the element binder, so add the class
// to the DOM node as well.
//
// To avoid array chrun, we remove all dummy binders at the
// end of the compilation process.
templateNode.classes.add('ng-binding');
node.classes.add('ng-binding');
}

domCursor.ascend();
templateCursor.ascend();
}
Expand All @@ -79,17 +100,6 @@ class TaggingCompiler implements Compiler {
if (elementBinder != null &&
elementBinder.hasDirectivesOrEvents &&
(node.parentNode != null && templateCursor.current.parentNode != null)) {
if (directParentElementBinder == null) {

directParentElementBinder = _addBinder(elementBinders,
new TaggedElementBinder(null, parentElementBinderOffset));

assert(templateCursor.current.parentNode is dom.Element);
assert(node.parentNode is dom.Element);

(node.parentNode as dom.Element).classes.add('ng-binding');
(templateCursor.current.parentNode as dom.Element).classes.add('ng-binding');
}
directParentElementBinder.addText(
new TaggedTextBinder(elementBinder, domCursor.index));
} else if(!(node.parentNode != null &&
Expand Down Expand Up @@ -123,7 +133,7 @@ class TaggingCompiler implements Compiler {
directives, -1, null, elementBinders);

viewFactory = new TaggingViewFactory(transcludeCursor.elements,
elementBinders, _perf, _expando);
_removeUnusedBinders(elementBinders), _perf, _expando);
domCursor.index = domCursorIndex;

if (domCursor.isInstance) {
Expand Down Expand Up @@ -153,9 +163,36 @@ class TaggingCompiler implements Compiler {
null, directives, -1, null, elementBinders);

var viewFactory = new TaggingViewFactory(
templateElements, elementBinders, _perf, _expando);
templateElements, _removeUnusedBinders(elementBinders), _perf, _expando);

assert(_perf.stopTimer(timerId) != false);
return viewFactory;
}

_isDummyBinder(TaggedElementBinder binder) =>
binder.binder == null && binder.textBinders == null;

_removeUnusedBinders(List<TaggedElementBinder> binders) {
// In order to support text nodes with directiveless parents, we
// add dummy ElementBinders to the list. After the entire template
// has been compiled, we remove the dummies and update the offset indices
final output = [];
final List<int> offsetMap = [];
int outputIndex = 0;

for (var i = 0, ii = binders.length; i < ii; i++) {
TaggedElementBinder binder = binders[i];
if (_isDummyBinder(binder)) {
offsetMap.add(-2);
} else {
if (binder.parentBinderOffset != -1) {
binder.parentBinderOffset = offsetMap[binder.parentBinderOffset];
}
assert(binder.parentBinderOffset != -2);
output.add(binder);
offsetMap.add(outputIndex++);
}
}
return output;
}
}
4 changes: 4 additions & 0 deletions test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ void main() {
expect(log).toEqual(['Ignore', 'Ignore']);
});

it('should compile a text child after a directive child', () {
_.compile('<div><span ng-show="true">hi</span>{{hello}}</div>');
});


describe("interpolation", () {
it('should interpolate attribute nodes', () {
Expand Down

0 comments on commit 81030dd

Please sign in to comment.