X Tutup
Skip to content

Commit 46502e4

Browse files
committed
fix(projection): allow more bound render elements than app elements.
Fixes #3236 Closes #3247
1 parent b44b06c commit 46502e4

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

modules/angular2/src/core/compiler/view.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export class AppProtoViewMergeMapping {
3737
this.renderProtoViewRef = renderProtoViewMergeMapping.mergedProtoViewRef;
3838
this.renderFragmentCount = renderProtoViewMergeMapping.fragmentCount;
3939
this.renderElementIndices = renderProtoViewMergeMapping.mappedElementIndices;
40-
this.renderInverseElementIndices =
41-
inverseIndexMapping(this.renderElementIndices, this.renderElementIndices.length);
40+
this.renderInverseElementIndices = inverseIndexMapping(
41+
this.renderElementIndices, renderProtoViewMergeMapping.mappedElementCount);
4242
this.renderTextIndices = renderProtoViewMergeMapping.mappedTextIndices;
4343
this.hostElementIndicesByViewIndex = renderProtoViewMergeMapping.hostElementIndicesByViewIndex;
4444
this.nestedViewIndicesByElementIndex =
@@ -48,7 +48,7 @@ export class AppProtoViewMergeMapping {
4848
}
4949

5050
function inverseIndexMapping(input: number[], resultLength: number): number[] {
51-
var result = ListWrapper.createFixedSize(resultLength);
51+
var result = ListWrapper.createGrowableSize(resultLength);
5252
for (var i = 0; i < input.length; i++) {
5353
var value = input[i];
5454
if (isPresent(value)) {

modules/angular2/src/render/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ export class RenderProtoViewMergeMapping {
307307
// Mappings of nested ProtoViews are in depth first order, with all
308308
// indices for one ProtoView in a consecuitve block.
309309
public mappedElementIndices: number[],
310+
// Number of bound render element.
311+
// Note: This could be more than the original ones
312+
// as we might have bound a new element for projecting bound text nodes.
313+
public mappedElementCount: number,
310314
// Mapping from app text index to render text index.
311315
// Mappings of nested ProtoViews are in depth first order, with all
312316
// indices for one ProtoView in a consecuitve block.

modules/angular2/src/render/dom/view/proto_view_merger.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ export function mergeProtoViewsRecursively(protoViewRefs: List<RenderProtoViewRe
5656
var mergedProtoView =
5757
DomProtoView.create(mainProtoView.original.type, rootElement, fragmentsRootNodeCount,
5858
rootTextNodeIndices, mergedElementBinders);
59-
return new RenderProtoViewMergeMapping(
60-
new DomProtoViewRef(mergedProtoView), fragmentsRootNodeCount.length, mappedElementIndices,
61-
mappedTextIndices, hostElementIndicesByViewIndex, nestedViewCounts);
59+
return new RenderProtoViewMergeMapping(new DomProtoViewRef(mergedProtoView),
60+
fragmentsRootNodeCount.length, mappedElementIndices,
61+
mergedBoundElements.length, mappedTextIndices,
62+
hostElementIndicesByViewIndex, nestedViewCounts);
6263
}
6364

6465
function cloneProtoViews(protoViewRefs: List<RenderProtoViewRef | List<any>>,

modules/angular2/test/core/compiler/compiler_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function main() {
7474
renderCompiler.spy('mergeProtoViewsRecursively')
7575
.andCallFake((protoViewRefs: List<renderApi.RenderProtoViewRef | List<any>>) => {
7676
return PromiseWrapper.resolve(new renderApi.RenderProtoViewMergeMapping(
77-
new MergedRenderProtoViewRef(protoViewRefs), 1, [], [], [], [null]));
77+
new MergedRenderProtoViewRef(protoViewRefs), 1, [], 0, [], [], [null]));
7878
});
7979
// TODO spy on .compile and return RenderProtoViewRef, same for compileHost
8080
rootProtoView = createRootProtoView(directiveResolver, MainComponent);

modules/angular2/test/core/compiler/projection_integration_spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ export function main() {
9292
});
9393
}));
9494

95+
96+
it('should support projecting text interpolation to a non bound element with other bound elements after it',
97+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
98+
tcb.overrideView(Simple, new viewAnn.View({
99+
template: 'SIMPLE(<div><ng-content></ng-content></div><div [tab-index]="0">EL</div>)',
100+
directives: []
101+
}))
102+
.overrideView(
103+
MainComp,
104+
new viewAnn.View({template: '<simple>{{text}}</simple>', directives: [Simple]}))
105+
.createAsync(MainComp)
106+
.then((main) => {
107+
108+
main.componentInstance.text = 'A';
109+
main.detectChanges();
110+
expect(main.nativeElement).toHaveText('SIMPLE(AEL)');
111+
async.done();
112+
});
113+
}));
114+
95115
it('should not show the light dom even if there is no content tag',
96116
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
97117
tcb.overrideView(MainComp,

modules/angular2/test/core/compiler/view_manager_utils_spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ function _createProtoView(type: ViewType, binders: ElementBinder[] = null) {
324324
}
325325
var hostElementIndicesByViewIndex = calcHostElementIndicesByViewIndex(res);
326326
if (type === ViewType.EMBEDDED || type === ViewType.HOST) {
327-
res.mergeMapping = new AppProtoViewMergeMapping(new RenderProtoViewMergeMapping(
328-
null, hostElementIndicesByViewIndex.length, mappedElementIndices, [],
329-
hostElementIndicesByViewIndex, countNestedProtoViews(res)));
327+
res.mergeMapping = new AppProtoViewMergeMapping(
328+
new RenderProtoViewMergeMapping(null, hostElementIndicesByViewIndex.length,
329+
mappedElementIndices, mappedElementIndices.length, [],
330+
hostElementIndicesByViewIndex, countNestedProtoViews(res)));
330331
}
331332
return res;
332333
}

0 commit comments

Comments
 (0)
X Tutup