feat(compiler): allow ignoring element children

This commit is contained in:
Victor Berchet 2015-01-07 17:32:46 +01:00
parent c141cbe865
commit 4f2f083b16
4 changed files with 43 additions and 15 deletions

View File

@ -74,6 +74,7 @@ export class Component extends Directive {
}
export class Decorator extends Directive {
compileChildren: boolean;
@CONST()
constructor({
selector,

View File

@ -32,6 +32,7 @@ export class CompileElement {
inheritedProtoView:ProtoView;
inheritedProtoElementInjector:ProtoElementInjector;
inheritedElementBinder:ElementBinder;
compileChildren: boolean;
constructor(element:Element) {
this.element = element;
this._attrs = null;
@ -54,6 +55,7 @@ export class CompileElement {
// inherited down to children if they don't have
// an own elementBinder
this.inheritedElementBinder = null;
this.compileChildren = true;
}
refreshAttrs() {

View File

@ -24,15 +24,18 @@ export class CompilePipeline {
_process(results, parent:CompileElement, current:CompileElement) {
var additionalChildren = this._control.internalProcess(results, 0, parent, current);
var node = DOM.templateAwareRoot(current.element).firstChild;
while (isPresent(node)) {
// compiliation can potentially move the node, so we need to store the
// next sibling before recursing.
var nextNode = DOM.nextSibling(node);
if (node.nodeType === Node.ELEMENT_NODE) {
this._process(results, current, new CompileElement(node));
if (current.compileChildren) {
var node = DOM.templateAwareRoot(current.element).firstChild;
while (isPresent(node)) {
// compiliation can potentially move the node, so we need to store the
// next sibling before recursing.
var nextNode = DOM.nextSibling(node);
if (node.nodeType === Node.ELEMENT_NODE) {
this._process(results, current, new CompileElement(node));
}
node = nextNode;
}
node = nextNode;
}
if (isPresent(additionalChildren)) {

View File

@ -1,5 +1,5 @@
import {describe, beforeEach, it, expect, iit, ddescribe} from 'test_lib/test_lib';
import {ListWrapper, List} from 'facade/collection';
import {ListWrapper, List, MapWrapper} from 'facade/collection';
import {DOM} from 'facade/dom';
import {isPresent, NumberWrapper, StringWrapper} from 'facade/lang';
@ -10,14 +10,27 @@ import {CompileControl} from 'core/compiler/pipeline/compile_control';
export function main() {
describe('compile_pipeline', () => {
it('should walk the tree in depth first order including template contents', () => {
var element = createElement('<div id="1"><template id="2"><span id="3"></span></template></div>');
describe('children compilation', () => {
it('should walk the tree in depth first order including template contents', () => {
var element = createElement('<div id="1"><template id="2"><span id="3"></span></template></div>');
var step0Log = [];
var results = new CompilePipeline([createLoggerStep(step0Log)]).process(element);
var step0Log = [];
var results = new CompilePipeline([createLoggerStep(step0Log)]).process(element);
expect(step0Log).toEqual(['1', '1<2', '2<3']);
expect(resultIdLog(results)).toEqual(['1', '2', '3']);
expect(step0Log).toEqual(['1', '1<2', '2<3']);
expect(resultIdLog(results)).toEqual(['1', '2', '3']);
});
it('should stop walking the tree when compileChildren is false', () => {
var element = createElement('<div id="1"><template id="2" ignore-children><span id="3"></span></template></div>');
var step0Log = [];
var pipeline = new CompilePipeline([new IgnoreChildrenStep(), createLoggerStep(step0Log)]);
var results = pipeline.process(element);
expect(step0Log).toEqual(['1', '1<2']);
expect(resultIdLog(results)).toEqual(['1', '2']);
});
});
describe('control.addParent', () => {
@ -118,6 +131,15 @@ class MockStep extends CompileStep {
}
}
class IgnoreChildrenStep extends CompileStep {
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
var attributeMap = DOM.attributeMap(current.element);
if (MapWrapper.contains(attributeMap, 'ignore-children')) {
current.compileChildren = false;
}
}
}
function logEntry(log, parent, current) {
var parentId = '';
if (isPresent(parent)) {