diff --git a/modules/core/src/compiler/pipeline/compile_pipeline.js b/modules/core/src/compiler/pipeline/compile_pipeline.js index 1eca885b13..7d23f9ba23 100644 --- a/modules/core/src/compiler/pipeline/compile_pipeline.js +++ b/modules/core/src/compiler/pipeline/compile_pipeline.js @@ -1,5 +1,5 @@ import {List, ListWrapper} from 'facade/collection'; -import {Element, TemplateElement, Node} from 'facade/dom'; +import {Element, Node, DOM} from 'facade/dom'; import {CompileElement} from './compile_element'; import {CompileControl} from './compile_control'; import {CompileStep} from './compile_step'; @@ -23,12 +23,7 @@ export class CompilePipeline { _process(results, parent:CompileElement, element:Element) { var current = new CompileElement(element); this._control.internalProcess(results, 0, parent, current); - var childNodes; - if (element instanceof TemplateElement) { - childNodes = element.content.childNodes; - } else { - childNodes = element.childNodes; - } + var childNodes = DOM.templateAwareRoot(element).childNodes; for (var i=0; i { + function createCollectDomNodesTestCases(useTemplateElement:boolean) { - it('should use the ProtoView element if it is no TemplateElement', () => { - var pv = new ProtoView(createElement('
'), new ProtoWatchGroup()); + function templateAwareCreateElement(html) { + return createElement(useTemplateElement ? `` : html); + } + + it('should collect the root node in the ProtoView element', () => { + var pv = new ProtoView(templateAwareCreateElement('
'), new ProtoWatchGroup()); var view = pv.instantiate(null, null); expect(view.nodes.length).toBe(1); expect(view.nodes[0].getAttribute('id')).toEqual('1'); }); - it('should use the ProtoView elements children if it is a TemplateElement', () => { - var pv = new ProtoView(createElement(''), - new ProtoWatchGroup()); - var view = pv.instantiate(null, null); - expect(view.nodes.length).toBe(1); - expect(view.nodes[0].getAttribute('id')).toEqual('1'); + describe('collect elements with property bindings', () => { + + it('should collect property bindings on the root element if it has the ng-binding class', () => { + var pv = new ProtoView(templateAwareCreateElement('
'), new ProtoWatchGroup()); + pv.bindElement(null); + pv.bindElementProperty('prop', parser.parseBinding('a')); + + var view = pv.instantiate(null, null); + expect(view.bindElements.length).toEqual(1); + expect(view.bindElements[0]).toBe(view.nodes[0]); + }); + + it('should collect property bindings on child elements with ng-binding class', () => { + var pv = new ProtoView(templateAwareCreateElement('
'), + new ProtoWatchGroup()); + pv.bindElement(null); + pv.bindElementProperty('a', parser.parseBinding('b')); + + var view = pv.instantiate(null, null); + expect(view.bindElements.length).toEqual(1); + expect(view.bindElements[0]).toBe(view.nodes[0].childNodes[1]); + }); + }); + describe('collect text nodes with bindings', () => { + + it('should collect text nodes under the root element', () => { + var pv = new ProtoView(templateAwareCreateElement('
{{}}{{}}
'), new ProtoWatchGroup()); + pv.bindElement(null); + pv.bindTextNode(0, parser.parseBinding('a')); + pv.bindTextNode(2, parser.parseBinding('b')); + + var view = pv.instantiate(null, null); + expect(view.textNodes.length).toEqual(2); + expect(view.textNodes[0]).toBe(view.nodes[0].childNodes[0]); + expect(view.textNodes[1]).toBe(view.nodes[0].childNodes[2]); + }); + + it('should collect text nodes with bindings on child elements with ng-binding class', () => { + var pv = new ProtoView(templateAwareCreateElement('
{{}}
'), + new ProtoWatchGroup()); + pv.bindElement(null); + pv.bindTextNode(0, parser.parseBinding('b')); + + var view = pv.instantiate(null, null); + expect(view.textNodes.length).toEqual(1); + expect(view.textNodes[0]).toBe(view.nodes[0].childNodes[1].childNodes[0]); + }); + + }); + } + + describe('collect dom nodes with a regular element as root', () => { + createCollectDomNodesTestCases(false); }); - describe('collect elements with property bindings', () => { - - it('should collect property bindings on the root element if it has the ng-binding class', () => { - var pv = new ProtoView(createElement('
'), new ProtoWatchGroup()); - pv.bindElement(null); - pv.bindElementProperty('prop', parser.parseBinding('a')); - - var view = pv.instantiate(null, null); - expect(view.bindElements.length).toEqual(1); - expect(view.bindElements[0]).toBe(view.nodes[0]); - }); - - it('should collect property bindings on child elements with ng-binding class', () => { - var pv = new ProtoView(createElement('
'), - new ProtoWatchGroup()); - pv.bindElement(null); - pv.bindElementProperty('a', parser.parseBinding('b')); - - var view = pv.instantiate(null, null); - expect(view.bindElements.length).toEqual(1); - expect(view.bindElements[0]).toBe(view.nodes[0].childNodes[1]); - }); - - }); - - describe('collect text nodes with bindings', () => { - - it('should collect text nodes under the root element', () => { - var pv = new ProtoView(createElement('
{{}}{{}}
'), new ProtoWatchGroup()); - pv.bindElement(null); - pv.bindTextNode(0, parser.parseBinding('a')); - pv.bindTextNode(2, parser.parseBinding('b')); - - var view = pv.instantiate(null, null); - expect(view.textNodes.length).toEqual(2); - expect(view.textNodes[0]).toBe(view.nodes[0].childNodes[0]); - expect(view.textNodes[1]).toBe(view.nodes[0].childNodes[2]); - }); - - it('should collect text nodes with bindings on child elements with ng-binding class', () => { - var pv = new ProtoView(createElement('
{{}}
'), - new ProtoWatchGroup()); - pv.bindElement(null); - pv.bindTextNode(0, parser.parseBinding('b')); - - var view = pv.instantiate(null, null); - expect(view.textNodes.length).toEqual(1); - expect(view.textNodes[0]).toBe(view.nodes[0].childNodes[1].childNodes[0]); - }); - + describe('collect dom nodes with a template element as root', () => { + createCollectDomNodesTestCases(true); }); describe('create ElementInjectors', () => { diff --git a/modules/facade/src/dom.dart b/modules/facade/src/dom.dart index 01ae174e76..4a08921ec8 100644 --- a/modules/facade/src/dom.dart +++ b/modules/facade/src/dom.dart @@ -79,4 +79,7 @@ class DOM { static attributeMap(Element element) { return element.attributes; } + static Node templateAwareRoot(Element el) { + return el is TemplateElement ? el.content : el; + } } diff --git a/modules/facade/src/dom.es6 b/modules/facade/src/dom.es6 index d7fcde9610..c5c722f983 100644 --- a/modules/facade/src/dom.es6 +++ b/modules/facade/src/dom.es6 @@ -78,5 +78,7 @@ export class DOM { } return res; } - + static templateAwareRoot(el:Element):Node { + return el instanceof TemplateElement ? el.content : el; + } }