Skip to content

Commit 4f2f083

Browse files
committed
feat(compiler): allow ignoring element children
1 parent c141cbe commit 4f2f083

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

modules/core/src/annotations/annotations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class Component extends Directive {
7474
}
7575

7676
export class Decorator extends Directive {
77+
compileChildren: boolean;
7778
@CONST()
7879
constructor({
7980
selector,

modules/core/src/compiler/pipeline/compile_element.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class CompileElement {
3232
inheritedProtoView:ProtoView;
3333
inheritedProtoElementInjector:ProtoElementInjector;
3434
inheritedElementBinder:ElementBinder;
35+
compileChildren: boolean;
3536
constructor(element:Element) {
3637
this.element = element;
3738
this._attrs = null;
@@ -54,6 +55,7 @@ export class CompileElement {
5455
// inherited down to children if they don't have
5556
// an own elementBinder
5657
this.inheritedElementBinder = null;
58+
this.compileChildren = true;
5759
}
5860

5961
refreshAttrs() {

modules/core/src/compiler/pipeline/compile_pipeline.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ export class CompilePipeline {
2424

2525
_process(results, parent:CompileElement, current:CompileElement) {
2626
var additionalChildren = this._control.internalProcess(results, 0, parent, current);
27-
var node = DOM.templateAwareRoot(current.element).firstChild;
28-
while (isPresent(node)) {
29-
// compiliation can potentially move the node, so we need to store the
30-
// next sibling before recursing.
31-
var nextNode = DOM.nextSibling(node);
32-
if (node.nodeType === Node.ELEMENT_NODE) {
33-
this._process(results, current, new CompileElement(node));
27+
28+
if (current.compileChildren) {
29+
var node = DOM.templateAwareRoot(current.element).firstChild;
30+
while (isPresent(node)) {
31+
// compiliation can potentially move the node, so we need to store the
32+
// next sibling before recursing.
33+
var nextNode = DOM.nextSibling(node);
34+
if (node.nodeType === Node.ELEMENT_NODE) {
35+
this._process(results, current, new CompileElement(node));
36+
}
37+
node = nextNode;
3438
}
35-
node = nextNode;
3639
}
3740

3841
if (isPresent(additionalChildren)) {

modules/core/test/compiler/pipeline/pipeline_spec.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {describe, beforeEach, it, expect, iit, ddescribe} from 'test_lib/test_lib';
2-
import {ListWrapper, List} from 'facade/collection';
2+
import {ListWrapper, List, MapWrapper} from 'facade/collection';
33
import {DOM} from 'facade/dom';
44
import {isPresent, NumberWrapper, StringWrapper} from 'facade/lang';
55

@@ -10,14 +10,27 @@ import {CompileControl} from 'core/compiler/pipeline/compile_control';
1010

1111
export function main() {
1212
describe('compile_pipeline', () => {
13-
it('should walk the tree in depth first order including template contents', () => {
14-
var element = createElement('<div id="1"><template id="2"><span id="3"></span></template></div>');
13+
describe('children compilation', () => {
14+
it('should walk the tree in depth first order including template contents', () => {
15+
var element = createElement('<div id="1"><template id="2"><span id="3"></span></template></div>');
1516

16-
var step0Log = [];
17-
var results = new CompilePipeline([createLoggerStep(step0Log)]).process(element);
17+
var step0Log = [];
18+
var results = new CompilePipeline([createLoggerStep(step0Log)]).process(element);
19+
20+
expect(step0Log).toEqual(['1', '1<2', '2<3']);
21+
expect(resultIdLog(results)).toEqual(['1', '2', '3']);
22+
});
23+
24+
it('should stop walking the tree when compileChildren is false', () => {
25+
var element = createElement('<div id="1"><template id="2" ignore-children><span id="3"></span></template></div>');
26+
27+
var step0Log = [];
28+
var pipeline = new CompilePipeline([new IgnoreChildrenStep(), createLoggerStep(step0Log)]);
29+
var results = pipeline.process(element);
1830

19-
expect(step0Log).toEqual(['1', '1<2', '2<3']);
20-
expect(resultIdLog(results)).toEqual(['1', '2', '3']);
31+
expect(step0Log).toEqual(['1', '1<2']);
32+
expect(resultIdLog(results)).toEqual(['1', '2']);
33+
});
2134
});
2235

2336
describe('control.addParent', () => {
@@ -118,6 +131,15 @@ class MockStep extends CompileStep {
118131
}
119132
}
120133

134+
class IgnoreChildrenStep extends CompileStep {
135+
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
136+
var attributeMap = DOM.attributeMap(current.element);
137+
if (MapWrapper.contains(attributeMap, 'ignore-children')) {
138+
current.compileChildren = false;
139+
}
140+
}
141+
}
142+
121143
function logEntry(log, parent, current) {
122144
var parentId = '';
123145
if (isPresent(parent)) {

0 commit comments

Comments
 (0)