/
search-simple.js
214 lines (155 loc) · 6.26 KB
/
search-simple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Copyright (c) 2012 Andreas Madsen
* MIT License
*/
var assert = chai.assert;
suite('testing document search', ready(function(){
var iframe = document.getElementById('content');
var content = iframe.contentDocument || iframe.contentWindow.document;
var doc = domstream(content);
suite('when searching for a missing element using', function(){
suite('tagname',
expectNoResult(doc.find().elem('missing')));
suite('attribute name',
expectNoResult(doc.find().attr('missing')));
suite('attribute value',
expectNoResult(doc.find().attr('rel', 'missing')));
suite('attribute RegExp',
expectNoResult(doc.find().attr('rel', /missing/)));
suite('tagname followed by attribute value',
expectNoResult(doc.find().elem('link').attr('data-match', '1')));
suite('attribute followed by tagname value',
expectNoResult(doc.find().attr('data-match', '1').elem('missing')));
suite('multiply steps', expectNoResult(function () {
var search = doc.find();
// this will perform a real search for all <link> element
search.elem('link');
search.toArray();
// this will setup search for all <link data-match="1" *>
// note that such elements do not exist, but a <link *> element and <* data-match="1" *> do.
search.attr('data-match', '1');
return search;
}));
});
function expectNoResult(search) {
return function () {
if (search instanceof Function) {
search = search();
}
test('toValue returns false', function () {
assert.isFalse(search.toValue());
});
test('toArray returns empty array', function () {
assert.lengthOf(search.toArray(), 0);
});
};
}
suite('when searching with .only() using', function () {
var links = [
content.getElementsByTagName('link')[0],
content.getElementsByTagName('link')[1]
];
suite('tagname',
expectOneResult(doc.find().only().elem('link'), links[0]));
suite('attribute name',
expectOneResult(doc.find().only().attr('rel'), links[0]));
suite('attribute value',
expectOneResult(doc.find().only().attr('rel', 'stylesheet'), links[0]));
suite('attribute RegExp',
expectOneResult(doc.find().only().attr('href', /^file_(2|3)\.css$/), links[1]));
suite('tagname followed by attribute value',
expectOneResult(doc.find().only().elem('link').attr('rel', 'stylesheet'), links[0]));
suite('attribute followed by tagname value',
expectOneResult(doc.find().only().attr('rel', 'stylesheet').elem('link'), links[0]));
suite('multiply steps with only at start', function () {
var search = doc.find();
// this will perform a real search for the first <link> element
search.only().elem('link').toValue();
// since nodeList only contains one element, and futher search
// will be performed on nodeList, the use is not allowed t
// add more search criterias, since the will result in a wrong
// search result
var error = null;
try {
search.attr('href', 'file_2.css').toValue(); // throw
} catch (e) {
error = e;
} finally {
test('it should throw', function (error) {
assert.instanceOf(error, Error);
});
}
});
suite('multiply steps with only at middle', expectOneResult(function () {
var search = doc.find();
// this will perform a real search for all <link> element
search.elem('link').toValue();
// find second link element
search.only().attr('href', 'file_2.css'); // no throw
return search;
}, links[1]));
suite('multiply steps with only at end', expectOneResult(function () {
var search = doc.find();
// this will perform a real search for all <link> element
search.elem('link').toValue();
// find second link element
search.attr('href', 'file_2.css').only(); // no throw
return search;
}, links[1]));
});
function expectOneResult(search, result) {
return function () {
if (search instanceof Function) {
search = search();
}
test('toValue returns an element', function () {
assert.strictEqual(search.toValue().elem, result);
});
test('toArray returns array with one item', function () {
assert.lengthOf(search.toArray(), 1);
assert.strictEqual(search.toArray()[0].elem, result);
});
};
}
var items = content.getElementsByTagName('li');
suite('when searching using', function () {
suite('tagname',
expectResult(doc.find().elem('li'), items));
suite('attribute name',
expectResult(doc.find().attr('data-match'), items));
suite('attribute value',
expectResult(doc.find().attr('data-match', '1'), [ items[0], items[2], items[3] ]));
suite('attribute RegExp',
expectResult(doc.find().attr('data-match', /^(2|3)$/), [ items[1], items[4], items[5] ]));
suite('tagname followed by attribute value',
expectResult(doc.find().elem('li').attr('data-match', '1'), [ items[0], items[2], items[3] ]));
suite('attribute followed by tagname value',
expectResult(doc.find().attr('data-match', '1').elem('li'), [ items[0], items[2], items[3] ]));
suite('multiply steps', expectResult(function () {
var search = doc.find();
// this will perform a real search for all <li> element
search.elem('li');
search.toArray();
// this will setup search for all <li data-match="1" *>
search.attr('data-match', '1');
return search;
}, [ items[0], items[2], items[3] ]));
});
function expectResult(search, results) {
var length = results.length;
return function () {
test('toValue returns an element', function () {
assert.lengthOf(search.toValue(), length);
search.toValue().forEach(function (node, index) {
assert.strictEqual(node.elem, results[index]);
});
});
test('toArray returns array with one item', function () {
assert.lengthOf(search.toArray(), length);
search.toArray().forEach(function (node, index) {
assert.strictEqual(node.elem, results[index]);
});
});
};
}
}));