Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 04fae51

Browse files
committed
chore(selector): Remove the selector.match() function. Clean up tests.
1 parent 651c1d2 commit 04fae51

File tree

3 files changed

+139
-111
lines changed

3 files changed

+139
-111
lines changed

lib/core_dom/selector.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,8 @@ class DirectiveSelector {
351351
}
352352
return binder;
353353
}
354-
ElementBinder match(dom.Node node) {
355-
switch(node.nodeType) {
356-
case 1: // Element
357-
return matchElement(node);
358354

359-
case 3: // Text Node
360-
return matchText(node);
361-
}
362-
// TODO: This is wrong.
355+
ElementBinder matchComment(dom.Node node) {
363356
return _binderFactory.binder();
364357
}
365358
}

lib/core_dom/walking_compiler.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,25 @@ class WalkingCompiler implements Compiler {
1717
do {
1818
var subtrees, binder;
1919

20-
ElementBinder elementBinder = existingElementBinder == null
21-
? directives.selector.match(domCursor.current)
22-
: existingElementBinder;
20+
ElementBinder elementBinder;
21+
if (existingElementBinder != null) {
22+
elementBinder = existingElementBinder;
23+
} else {
24+
var node = domCursor.current;
25+
switch(node.nodeType) {
26+
case 1:
27+
elementBinder = directives.selector.matchElement(node);
28+
break;
29+
case 3:
30+
elementBinder = directives.selector.matchText(node);
31+
break;
32+
case 8:
33+
elementBinder = directives.selector.matchComment(node);
34+
break;
35+
default:
36+
throw "Unknown node type ${node.nodeType}";
37+
}
38+
}
2339

2440
if (elementBinder.hasTemplate) {
2541
elementBinder.templateViewFactory = _compileTransclusion(

test/core_dom/selector_spec.dart

Lines changed: 119 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ import '../_specs.dart';
3333

3434
main() {
3535
describe('Selector', () {
36-
//TODO(karma): throwing error here gets ignored
37-
// throw new Error();
38-
3936
var log;
4037
var selector;
4138
var element;
@@ -62,140 +59,162 @@ main() {
6259
..type(_OneOfTwoDirectives)
6360
..type(_TwoOfTwoDirectives);
6461
});
65-
beforeEach((DirectiveMap directives) {
66-
selector = (node) => directives.selector.match(node);
67-
});
6862

69-
it('should match directive on element', () {
70-
expect(
71-
selector(element = e('<b></b>')),
63+
describe('matchElement', () {
64+
beforeEach((DirectiveMap directives) {
65+
selector = (node) => directives.selector.matchElement(node);
66+
});
67+
68+
it('should match directive on element', () {
69+
expect(
70+
selector(element = e('<b></b>')),
71+
toEqualsDirectiveInfos([
72+
{ "selector": 'b', "value": null, "element": element}
73+
]));
74+
});
75+
76+
it('should match directive on class', () {
77+
expect(selector(element = e('<div class="a b c"></div>')),
7278
toEqualsDirectiveInfos([
73-
{ "selector": 'b', "value": null, "element": element}
79+
{ "selector": '.b', "value": null, "element": element}
7480
]));
75-
});
76-
77-
it('should match directive on class', () {
78-
expect(selector(element = e('<div class="a b c"></div>')),
79-
toEqualsDirectiveInfos([
80-
{ "selector": '.b', "value": null, "element": element}
81-
]));
82-
});
81+
});
8382

8483

85-
it('should match directive on [attribute]', () {
86-
expect(selector(element = e('<div directive=abc></div>')),
84+
it('should match directive on [attribute]', () {
85+
expect(selector(element = e('<div directive=abc></div>')),
8786
toEqualsDirectiveInfos([
88-
{ "selector": '[directive]', "value": 'abc', "element": element,
89-
"name": 'directive' }]));
87+
{ "selector": '[directive]', "value": 'abc', "element": element,
88+
"name": 'directive' }]));
9089

91-
expect(selector(element = e('<div directive></div>')),
90+
expect(selector(element = e('<div directive></div>')),
9291
toEqualsDirectiveInfos([
93-
{ "selector": '[directive]', "value": '', "element": element,
94-
"name": 'directive' }]));
95-
});
92+
{ "selector": '[directive]', "value": '', "element": element,
93+
"name": 'directive' }]));
94+
});
9695

9796

98-
it('should match directive on element[attribute]', () {
99-
expect(selector(element = e('<b directive=abc></b>')),
97+
it('should match directive on element[attribute]', () {
98+
expect(selector(element = e('<b directive=abc></b>')),
10099
toEqualsDirectiveInfos([
101-
{ "selector": 'b', "value": null, "element": element},
102-
{ "selector": '[directive]', "value": 'abc', "element": element},
103-
{ "selector": 'b[directive]', "value": 'abc', "element": element}
100+
{ "selector": 'b', "value": null, "element": element},
101+
{ "selector": '[directive]', "value": 'abc', "element": element},
102+
{ "selector": 'b[directive]', "value": 'abc', "element": element}
104103
]));
105-
});
104+
});
106105

107106

108-
it('should match directive on [attribute=value]', () {
109-
expect(selector(element = e('<div directive=value></div>')),
107+
it('should match directive on [attribute=value]', () {
108+
expect(selector(element = e('<div directive=value></div>')),
110109
toEqualsDirectiveInfos([
111-
{ "selector": '[directive]', "value": 'value', "element": element},
112-
{ "selector": '[directive=value]', "value": 'value', "element": element}
110+
{ "selector": '[directive]', "value": 'value', "element": element},
111+
{ "selector": '[directive=value]', "value": 'value', "element": element}
113112
]));
114-
});
113+
});
115114

116115

117-
it('should match directive on element[attribute=value]', () {
118-
expect(selector(element = e('<b directive=value></div>')),
116+
it('should match directive on element[attribute=value]', () {
117+
expect(selector(element = e('<b directive=value></div>')),
119118
toEqualsDirectiveInfos([
120-
{ "selector": 'b', "value": null, "element": element, "name": null},
121-
{ "selector": '[directive]', "value": 'value', "element": element},
122-
{ "selector": '[directive=value]', "value": 'value', "element": element},
123-
{ "selector": 'b[directive]', "value": 'value', "element": element},
124-
{ "selector": 'b[directive=value]', "value": 'value', "element": element}
119+
{ "selector": 'b', "value": null, "element": element, "name": null},
120+
{ "selector": '[directive]', "value": 'value', "element": element},
121+
{ "selector": '[directive=value]', "value": 'value', "element": element},
122+
{ "selector": 'b[directive]', "value": 'value', "element": element},
123+
{ "selector": 'b[directive=value]', "value": 'value', "element": element}
125124
]));
126-
});
125+
});
127126

128-
it('should match attributes', () {
129-
expect(selector(element = e('<div attr="before-xyz-after"></div>')),
127+
it('should match attributes', () {
128+
expect(selector(element = e('<div attr="before-xyz-after"></div>')),
130129
toEqualsDirectiveInfos([
131-
{ "selector": '[*=/xyz/]', "value": 'attr=before-xyz-after',
132-
"element": element, "name": 'attr'}
130+
{ "selector": '[*=/xyz/]', "value": 'attr=before-xyz-after',
131+
"element": element, "name": 'attr'}
133132
]));
134-
});
133+
});
135134

136-
it('should match attribute names', () {
137-
expect(selector(element = e('<div wildcard-match=ignored></div>')),
135+
it('should match attribute names', () {
136+
expect(selector(element = e('<div wildcard-match=ignored></div>')),
138137
toEqualsDirectiveInfos([
139-
{ "selector": '[wildcard-*]', "value": 'ignored',
140-
"element": element, "name": 'wildcard-match'}
138+
{ "selector": '[wildcard-*]', "value": 'ignored',
139+
"element": element, "name": 'wildcard-match'}
141140
]));
142-
});
141+
});
143142

144-
it('should match text', () {
145-
expect(selector(element = e('before-abc-after')),
143+
144+
145+
it('should sort by priority', () {
146+
expect(selector(element = e(
147+
'<component attribute ignore-children structural></component>')),
148+
toEqualsDirectiveInfos(
149+
[
150+
{ "selector": "[attribute]", "value": "", "element": element },
151+
{ "selector": "[ignore-children]", "value": "", "element": element }
152+
153+
],
154+
component: { "selector": "component", "value": null, "element": element },
155+
template: {"selector": "[structural]", "value": "", "element": element}));
156+
});
157+
158+
it('should match on multiple directives', () {
159+
expect(selector(element = e('<div directive="d" foo="f"></div>')),
146160
toEqualsDirectiveInfos([
147-
{ "selector": ':contains(/abc/)', "value": 'before-abc-after',
148-
"element": element, "name": '#text'}
161+
{ "selector": '[directive]', "value": 'd', "element": element},
162+
{ "selector": '[directive=d][foo=f]', "value": 'f', "element": element}
149163
]));
164+
});
165+
166+
it('should match ng-model + required on the same element', () {
167+
expect(
168+
selector(element = e('<input type="text" ng-model="val" probe="i" required="true" />')),
169+
toEqualsDirectiveInfos([
170+
{ "selector": '[ng-model]', "value": 'val', "element": element},
171+
{ "selector": '[probe]', "value": 'i', "element": element},
172+
{ "selector": '[ng-model][required]', "value": 'true', "element": element},
173+
{ "selector": 'input[type=text][ng-model]', "value": 'val', "element": element}
174+
]));
175+
});
176+
177+
it('should match two directives', () {
178+
expect(
179+
selector(element = e('<input type="text" my-model="val" required my-required />')),
180+
toEqualsDirectiveInfos([
181+
{ "selector": '[my-model][required]', "value": '', "element": element},
182+
{ "selector": '[my-model][my-required]', "value": '', "element": element}
183+
]));
184+
});
185+
186+
it('should match an two directives with the same selector', () {
187+
expect(selector(element = e('<div two-directives></div>')),
188+
toEqualsDirectiveInfos([
189+
{ "selector": '[two-directives]', "value": '', "element": element},
190+
{ "selector": '[two-directives]', "value": '', "element": element}
191+
]));
192+
});
150193
});
151194

152-
it('should sort by priority', () {
153-
expect(selector(element = e(
154-
'<component attribute ignore-children structural></component>')),
155-
toEqualsDirectiveInfos(
156-
[
157-
{ "selector": "[attribute]", "value": "", "element": element },
158-
{ "selector": "[ignore-children]", "value": "", "element": element }
159-
160-
],
161-
component: { "selector": "component", "value": null, "element": element },
162-
template: {"selector": "[structural]", "value": "", "element": element}));
163-
});
164-
165-
it('should match on multiple directives', () {
166-
expect(selector(element = e('<div directive="d" foo="f"></div>')),
167-
toEqualsDirectiveInfos([
168-
{ "selector": '[directive]', "value": 'd', "element": element},
169-
{ "selector": '[directive=d][foo=f]', "value": 'f', "element": element}
170-
]));
171-
});
195+
describe('matchText', () {
196+
beforeEach((DirectiveMap directives) {
197+
selector = (node) => directives.selector.matchText(node);
198+
});
172199

173-
it('should match ng-model + required on the same element', () {
174-
expect(
175-
selector(element = e('<input type="text" ng-model="val" probe="i" required="true" />')),
200+
it('should match text', () {
201+
expect(selector(element = e('before-abc-after')),
176202
toEqualsDirectiveInfos([
177-
{ "selector": '[ng-model]', "value": 'val', "element": element},
178-
{ "selector": '[probe]', "value": 'i', "element": element},
179-
{ "selector": '[ng-model][required]', "value": 'true', "element": element},
180-
{ "selector": 'input[type=text][ng-model]', "value": 'val', "element": element}
203+
{ "selector": ':contains(/abc/)', "value": 'before-abc-after',
204+
"element": element, "name": '#text'}
181205
]));
206+
});
182207
});
183208

184-
it('should match two directives', () {
185-
expect(
186-
selector(element = e('<input type="text" my-model="val" required my-required />')),
187-
toEqualsDirectiveInfos([
188-
{ "selector": '[my-model][required]', "value": '', "element": element},
189-
{ "selector": '[my-model][my-required]', "value": '', "element": element}
190-
]));
191-
});
209+
describe('matchComment', () {
210+
beforeEach((DirectiveMap directives) {
211+
selector = (node) => directives.selector.matchComment(node);
212+
});
192213

193-
it('should match an two directives with the same selector', () {
194-
expect(selector(element = e('<div two-directives></div>')),
195-
toEqualsDirectiveInfos([
196-
{ "selector": '[two-directives]', "value": '', "element": element},
197-
{ "selector": '[two-directives]', "value": '', "element": element}
198-
]));
214+
it('should match comments', () {
215+
expect(selector(element = e('<!-- nothing here -->')),
216+
toEqualsDirectiveInfos([]));
217+
});
199218
});
200219
});
201220
}

0 commit comments

Comments
 (0)