/
cheerio.js
181 lines (151 loc) · 5.97 KB
/
cheerio.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
var expect = require('expect.js'),
_ = require('underscore'),
$ = require('../'),
fruits = require('./fixtures').fruits;
// HTML
var script = '<script src="script.js" type="text/javascript"></script>',
multiclass = '<p><a class="btn primary" href="#">Save</a></p>';
describe('cheerio', function() {
it('should get the version', function() {
expect(/\d\.\d\.\d/.test($.version)).to.be.ok();
});
it('$(null) should return be empty', function() {
expect($(null)).to.be.empty();
});
it('$(undefined) should be empty', function() {
expect($(undefined)).to.be.empty();
});
it('$(null) should be empty', function() {
expect($('')).to.be.empty();
});
it('$(selector) with no context or root should be empty', function() {
expect($('.h2')).to.be.empty();
expect($('#fruits')).to.be.empty();
});
it('should be able to create html without a root or context', function() {
var $h2 = $('<h2>');
expect($h2).to.not.be.empty();
expect($h2).to.have.length(1);
expect($h2[0].name).to.equal('h2');
});
it('should be able to create complicated html', function() {
var $script = $(script);
expect($script).to.not.be.empty();
expect($script).to.have.length(1);
expect($script[0].attribs.src).to.equal('script.js');
expect($script[0].attribs.type).to.equal('text/javascript');
expect($script[0].children).to.be.empty();
});
var testAppleSelect = function($apple) {
expect($apple).to.have.length(1);
$apple = $apple[0];
expect($apple.parent.name).to.equal('ul');
expect($apple.prev).to.be(null);
expect($apple.next.attribs['class']).to.equal('orange');
expect($apple.children).to.have.length(1);
expect($apple.children[0].data).to.equal('Apple');
};
it('should be able to select .apple with only a context', function() {
var $apple = $('.apple', fruits);
testAppleSelect($apple);
});
it('should be able to select .apple with only a root', function() {
var $apple = $('.apple', null, fruits);
testAppleSelect($apple);
});
it('should be able to select an id', function() {
var $fruits = $('#fruits', null, fruits);
expect($fruits).to.have.length(1);
expect($fruits[0].attribs.id).to.equal('fruits');
});
it('should be able to select a tag', function() {
var $ul = $('ul', fruits);
expect($ul).to.have.length(1);
expect($ul[0].name).to.equal('ul');
});
it('should be able to select multiple tags', function() {
var $fruits = $('li', null, fruits);
expect($fruits).to.have.length(3);
var classes = ['apple', 'orange', 'pear'];
$fruits.each(function(idx, $fruit) {
expect($fruit.attribs['class']).to.equal(classes[idx]);
});
});
it('should be able to do: $("#fruits .apple")', function() {
var $apple = $('#fruits .apple', fruits);
testAppleSelect($apple);
});
it('should be able to do: $("li.apple")', function() {
var $apple = $('li.apple', fruits);
testAppleSelect($apple);
});
it('should be able to select by attributes', function() {
var $apple = $('li[class=apple]', fruits);
testAppleSelect($apple);
});
it('should be able to select multiple classes: $(".btn.primary")', function() {
var $a = $('.btn.primary', multiclass);
expect($a).to.have.length(1);
expect($a[0].children[0].data).to.equal('Save');
});
it('should be able to select multiple elements: $(".apple, #fruits")', function() {
var $elems = $('.apple, #fruits', fruits);
expect($elems).to.have.length(2);
var $apple = _($elems).filter(function(elem) {
return elem.attribs['class'] === 'apple';
});
var $fruits = _($elems).filter(function(elem) {
return elem.attribs.id === 'fruits';
});
testAppleSelect($($apple));
expect($fruits[0].attribs.id).to.equal('fruits');
});
it('should select first element $(:first)');
// var $elem = $(':first', fruits);
// var $h2 = $('<h2>fruits</h2>');
// console.log($elem.before('hi'));
// console.log($elem.before($h2));
it('should be able to select immediate children: $("#fruits > .pear")', function() {
var $fruitsWithMorePear = $('.pear', fruits).append('<li class="pear">Another Pear!</li>');
expect($('#fruits .pear', $fruitsWithMorePear)).to.have.length(2);
var $elem = $('#fruits > .pear', $fruitsWithMorePear);
expect($elem).to.have.length(1);
expect($elem.attr('class')).to.equal('pear');
});
it('should be able to select immediate children: $(".apple + .pear")', function() {
var $elem = $('.apple + li', fruits);
expect($elem).to.have.length(1);
$elem = $('.apple + .pear', fruits);
expect($elem).to.have.length(0);
$elem = $('.apple + .orange', fruits);
expect($elem).to.have.length(1);
expect($elem.attr('class')).to.equal('orange');
});
it('should be able to select immediate children: $(".apple ~ .pear")', function() {
var $elem = $('.apple ~ li', fruits);
expect($elem).to.have.length(2);
$elem = $('.apple ~ .pear', fruits);
expect($elem.attr('class')).to.equal('pear');
});
it('should handle wildcards on attributes: $("li[class*=r]")', function() {
var $elem = $('li[class*=r]', fruits);
expect($elem).to.have.length(2);
expect($elem.eq(0).attr('class')).to.equal('orange');
expect($elem.eq(1).attr('class')).to.equal('pear');
});
it('should handle beginning of attr selectors: $("li[class^=o]")', function() {
var $elem = $('li[class^=o]', fruits);
expect($elem).to.have.length(1);
expect($elem.eq(0).attr('class')).to.equal('orange');
});
it('should handle beginning of attr selectors: $("li[class$=e]")', function() {
var $elem = $('li[class$=e]', fruits);
expect($elem).to.have.length(2);
expect($elem.eq(0).attr('class')).to.equal('apple');
expect($elem.eq(1).attr('class')).to.equal('orange');
});
it('should gracefully degrade on complex, unmatched queries', function() {
var $elem = $('Eastern States Cup #8-fin <br>Downhill ');
expect($elem).to.have.length(0); // []
});
});