feat(compiler): allow ignoring element children
This commit is contained in:
parent
c141cbe865
commit
4f2f083b16
@ -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,
|
||||||
|
@ -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() {
|
||||||
|
@ -24,15 +24,18 @@ 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);
|
||||||
var node = DOM.templateAwareRoot(current.element).firstChild;
|
|
||||||
while (isPresent(node)) {
|
if (current.compileChildren) {
|
||||||
// compiliation can potentially move the node, so we need to store the
|
var node = DOM.templateAwareRoot(current.element).firstChild;
|
||||||
// next sibling before recursing.
|
while (isPresent(node)) {
|
||||||
var nextNode = DOM.nextSibling(node);
|
// compiliation can potentially move the node, so we need to store the
|
||||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
// next sibling before recursing.
|
||||||
this._process(results, current, new CompileElement(node));
|
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)) {
|
if (isPresent(additionalChildren)) {
|
||||||
|
@ -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,14 +10,27 @@ import {CompileControl} from 'core/compiler/pipeline/compile_control';
|
|||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('compile_pipeline', () => {
|
describe('compile_pipeline', () => {
|
||||||
it('should walk the tree in depth first order including template contents', () => {
|
describe('children compilation', () => {
|
||||||
var element = createElement('<div id="1"><template id="2"><span id="3"></span></template></div>');
|
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 step0Log = [];
|
||||||
var results = new CompilePipeline([createLoggerStep(step0Log)]).process(element);
|
var results = new CompilePipeline([createLoggerStep(step0Log)]).process(element);
|
||||||
|
|
||||||
expect(step0Log).toEqual(['1', '1<2', '2<3']);
|
expect(step0Log).toEqual(['1', '1<2', '2<3']);
|
||||||
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', () => {
|
||||||
@ -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)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user