Skip to content

Commit 48e5012

Browse files
committed
feat(compiler): handle compileChildren from @decorator
1 parent 9cacde9 commit 48e5012

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

modules/core/src/annotations/annotations.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ export class Decorator extends Directive {
8080
selector,
8181
bind,
8282
lightDomServices,
83-
implementsTypes
83+
implementsTypes,
84+
compileChildren = true
8485
}:{
8586
selector:string,
8687
bind:any,
8788
lightDomServices:List,
88-
implementsTypes:List
89+
implementsTypes:List,
90+
compileChildren:boolean
8991
}={})
9092
{
93+
this.compileChildren = compileChildren;
9194
super({
9295
selector: selector,
9396
bind: bind,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ export class CompileElement {
119119
this.decoratorDirectives = ListWrapper.create();
120120
}
121121
ListWrapper.push(this.decoratorDirectives, directive);
122+
if (!annotation.compileChildren) {
123+
this.compileChildren = false;
124+
}
122125
} else if (annotation instanceof Template) {
123126
this.templateDirective = directive;
124127
} else if (annotation instanceof Component) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export function createDefaultSteps(parser:Parser, compiledComponent: DirectiveMe
2323

2424
return [
2525
new ViewSplitter(parser, compilationUnit),
26-
new TextInterpolationParser(parser, compilationUnit),
2726
new PropertyBindingParser(parser, compilationUnit),
2827
new DirectiveParser(directives),
28+
new TextInterpolationParser(parser, compilationUnit),
2929
new ElementBindingMarker(),
3030
new ProtoViewBuilder(),
3131
new ProtoElementInjectorBuilder(),
3232
new ElementBinderBuilder()
3333
];
34-
}
34+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export class TextInterpolationParser extends CompileStep {
5454
}
5555

5656
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
57+
if (!current.compileChildren) {
58+
return;
59+
}
5760
var element = current.element;
5861
var childNodes = DOM.templateAwareRoot(element).childNodes;
5962
for (var i=0; i<childNodes.length; i++) {

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ export function main() {
2020

2121
beforeEach( () => {
2222
reader = new DirectiveMetadataReader();
23-
directives = [SomeDecorator, SomeTemplate, SomeTemplate2, SomeComponent, SomeComponent2];
23+
directives = [
24+
SomeDecorator,
25+
SomeDecoratorIgnoringChildren,
26+
SomeTemplate,
27+
SomeTemplate2,
28+
SomeComponent,
29+
SomeComponent2
30+
];
2431
});
2532

2633
function createPipeline({propertyBindings, variableBindings}={}) {
@@ -141,6 +148,16 @@ export function main() {
141148
expect(results[0].decoratorDirectives).toEqual([reader.read(SomeDecorator)]);
142149
});
143150

151+
it('should compile children by default', () => {
152+
var results = createPipeline().process(createElement('<div some-decor></div>'));
153+
expect(results[0].compileChildren).toEqual(true);
154+
});
155+
156+
it('should stop compiling children when specified in the decorator config', () => {
157+
var results = createPipeline().process(createElement('<div some-decor-ignoring-children></div>'));
158+
expect(results[0].compileChildren).toEqual(false);
159+
});
160+
144161
it('should detect them in variable bindings', () => {
145162
var pipeline = createPipeline({variableBindings: {
146163
'some-decor': 'someExpr'
@@ -176,6 +193,13 @@ class MockStep extends CompileStep {
176193
})
177194
class SomeDecorator {}
178195

196+
@Decorator({
197+
selector: '[some-decor-ignoring-children]',
198+
compileChildren: false
199+
})
200+
class SomeDecoratorIgnoringChildren {
201+
}
202+
179203
@Template({
180204
selector: '[some-templ]'
181205
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class MockStep extends CompileStep {
131131
}
132132
}
133133

134-
class IgnoreChildrenStep extends CompileStep {
134+
export class IgnoreChildrenStep extends CompileStep {
135135
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
136136
var attributeMap = DOM.attributeMap(current.element);
137137
if (MapWrapper.contains(attributeMap, 'ignore-children')) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import {DOM} from 'facade/dom';
55
import {MapWrapper} from 'facade/collection';
66

77
import {Lexer, Parser} from 'change_detection/change_detection';
8+
import {IgnoreChildrenStep} from './pipeline_spec';
89

910
export function main() {
1011
describe('TextInterpolationParser', () => {
1112
function createPipeline() {
12-
return new CompilePipeline([new TextInterpolationParser(new Parser(new Lexer()), null)]);
13+
return new CompilePipeline([
14+
new IgnoreChildrenStep(),
15+
new TextInterpolationParser(new Parser(new Lexer()), null)
16+
]);
1317
}
1418

1519
it('should find text interpolation in normal elements', () => {
@@ -32,6 +36,13 @@ export function main() {
3236
expect(MapWrapper.get(bindings, 0).source).toEqual("(expr1)+(expr2)");
3337
});
3438

39+
it('should not interpolate when compileChildren is false', () => {
40+
var results = createPipeline().process(createElement('<div>{{included}}<span ignore-children>{{excluded}}</span></div>'));
41+
var bindings = results[0].textNodeBindings;
42+
expect(MapWrapper.get(bindings, 0).source).toEqual("(included)");
43+
expect(results[1].textNodeBindings).toBe(null);
44+
});
45+
3546
it('should allow fixed text before, in between and after expressions', () => {
3647
var results = createPipeline().process(createElement('<div>a{{expr1}}b{{expr2}}c</div>'));
3748
var bindings = results[0].textNodeBindings;

0 commit comments

Comments
 (0)