Skip to content

Commit b2e804c

Browse files
Suguru Inatomimhevery
authored andcommitted
fix(metadata): Allow spacing in multiple selectors (#7418)
1 parent 85ce184 commit b2e804c

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

modules/@angular/core/src/metadata/di.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {stringify, isString, Type} from '../../src/facade/lang';
1+
import {stringify, isString, Type, StringWrapper} from '../../src/facade/lang';
22
import {DependencyMetadata} from '../di/metadata';
33
import {resolveForwardRef} from '../di/forward_ref';
44

@@ -184,7 +184,7 @@ export class QueryMetadata extends DependencyMetadata {
184184
* returns a list of variable bindings this is querying for.
185185
* Only applicable if this is a variable bindings query.
186186
*/
187-
get varBindings(): string[] { return this.selector.split(','); }
187+
get varBindings(): string[] { return StringWrapper.split(this.selector, /\s*,\s*/g); }
188188

189189
toString(): string { return `@Query(${stringify(this.selector)})`; }
190190
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {
2+
AsyncTestCompleter,
3+
beforeEach,
4+
ddescribe,
5+
describe,
6+
expect,
7+
iit,
8+
inject,
9+
it,
10+
xit,
11+
} from '@angular/core/testing/testing_internal';
12+
import { TestComponentBuilder } from '@angular/compiler/testing';
13+
14+
import {
15+
Component,
16+
ViewMetadata,
17+
Input,
18+
Directive,
19+
ViewChild,
20+
ViewChildren,
21+
QueryList,
22+
ElementRef
23+
} from '@angular/core';
24+
25+
export function main() {
26+
describe('ViewChild', () => {
27+
it('should support type selector',
28+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
29+
tcb.overrideView(ViewChildTypeSelectorComponent,
30+
new ViewMetadata(
31+
{template: `<simple [marker]="'1'"></simple><simple [marker]="'2'"></simple>`, directives: [Simple]}))
32+
.createAsync(ViewChildTypeSelectorComponent)
33+
.then((view) => {
34+
view.detectChanges();
35+
expect(view.componentInstance.child).toBeDefined();
36+
expect(view.componentInstance.child.marker).toBe("1");
37+
async.done();
38+
});
39+
}));
40+
it('should support string selector',
41+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
42+
tcb.overrideView(
43+
ViewChildStringSelectorComponent,
44+
new ViewMetadata({template: `<simple #child></simple>`, directives: [Simple]}))
45+
.createAsync(ViewChildStringSelectorComponent)
46+
.then((view) => {
47+
view.detectChanges();
48+
expect(view.componentInstance.child).toBeDefined();
49+
async.done();
50+
});
51+
}));
52+
});
53+
describe('ViewChildren', () => {
54+
it('should support type selector',
55+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
56+
tcb.overrideView(ViewChildrenTypeSelectorComponent,
57+
new ViewMetadata({template: `<simple></simple><simple></simple>`, directives: [Simple]}))
58+
.createAsync(ViewChildrenTypeSelectorComponent)
59+
.then((view) => {
60+
view.detectChanges();
61+
expect(view.componentInstance.children).toBeDefined();
62+
expect(view.componentInstance.children.length).toBe(2);
63+
async.done();
64+
});
65+
}));
66+
it('should support string selector',
67+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
68+
tcb.overrideView(
69+
ViewChildrenStringSelectorComponent,
70+
new ViewMetadata({template: `<simple #child1></simple><simple #child2></simple>`, directives: [Simple]}))
71+
.createAsync(ViewChildrenStringSelectorComponent)
72+
.then((view) => {
73+
view.detectChanges();
74+
expect(view.componentInstance.children).toBeDefined();
75+
expect(view.componentInstance.children.length).toBe(2);
76+
async.done();
77+
});
78+
}));
79+
});
80+
}
81+
82+
83+
@Directive({selector: "simple"})
84+
class Simple {
85+
@Input() marker: string;
86+
}
87+
88+
@Component({selector: 'view-child-type-selector'})
89+
class ViewChildTypeSelectorComponent {
90+
@ViewChild(Simple) child: Simple;
91+
}
92+
93+
@Component({selector: 'view-child-string-selector'})
94+
class ViewChildStringSelectorComponent {
95+
@ViewChild("child") child: ElementRef;
96+
}
97+
98+
@Component({selector: 'view-children-type-selector'})
99+
class ViewChildrenTypeSelectorComponent {
100+
@ViewChildren(Simple) children: QueryList<Simple>;
101+
}
102+
103+
@Component({selector: 'view-child-string-selector'})
104+
class ViewChildrenStringSelectorComponent {
105+
// Allow comma separated selector (with spaces).
106+
@ViewChildren("child1 , child2") children: QueryList<ElementRef>;
107+
}

0 commit comments

Comments
 (0)