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 { export class Decorator extends Directive {
compileChildren: boolean;
@CONST() @CONST()
constructor({ constructor({
selector, selector,

View File

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

View File

@ -24,6 +24,8 @@ export class CompilePipeline {
_process(results, parent:CompileElement, current:CompileElement) { _process(results, parent:CompileElement, current:CompileElement) {
var additionalChildren = this._control.internalProcess(results, 0, parent, current); var additionalChildren = this._control.internalProcess(results, 0, parent, current);
if (current.compileChildren) {
var node = DOM.templateAwareRoot(current.element).firstChild; var node = DOM.templateAwareRoot(current.element).firstChild;
while (isPresent(node)) { while (isPresent(node)) {
// compiliation can potentially move the node, so we need to store the // compiliation can potentially move the node, so we need to store the
@ -34,6 +36,7 @@ export class CompilePipeline {
} }
node = nextNode; node = nextNode;
} }
}
if (isPresent(additionalChildren)) { if (isPresent(additionalChildren)) {
for (var i=0; i<additionalChildren.length; i++) { for (var i=0; i<additionalChildren.length; i++) {

View File

@ -1,5 +1,5 @@
import {describe, beforeEach, it, expect, iit, ddescribe} from 'test_lib/test_lib'; 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 {DOM} from 'facade/dom';
import {isPresent, NumberWrapper, StringWrapper} from 'facade/lang'; import {isPresent, NumberWrapper, StringWrapper} from 'facade/lang';
@ -10,6 +10,7 @@ import {CompileControl} from 'core/compiler/pipeline/compile_control';
export function main() { export function main() {
describe('compile_pipeline', () => { describe('compile_pipeline', () => {
describe('children compilation', () => {
it('should walk the tree in depth first order including template contents', () => { 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 element = createElement('<div id="1"><template id="2"><span id="3"></span></template></div>');
@ -20,6 +21,18 @@ export function main() {
expect(resultIdLog(results)).toEqual(['1', '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', () => { describe('control.addParent', () => {
it('should report the new parent to the following processor and the result', () => { it('should report the new parent to the following processor and the result', () => {
var element = createElement('<div id="1"><span wrap0="1" id="2"><b id="3"></b></span></div>'); var element = createElement('<div id="1"><span wrap0="1" id="2"><b id="3"></b></span></div>');
@ -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) { function logEntry(log, parent, current) {
var parentId = ''; var parentId = '';
if (isPresent(parent)) { if (isPresent(parent)) {