forked from cheeriojs/cheerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
233 lines (177 loc) · 8.45 KB
/
utils.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var expect = require('expect.js'),
fixtures = require('../fixtures'),
cheerio = require('../..');
describe('cheerio', function() {
describe('.html', function() {
it('() : should return innerHTML; $.html(obj) should return outerHTML', function() {
var $div = cheerio('div', '<div><span>foo</span><span>bar</span></div>');
var span = $div.children()[1];
expect(cheerio(span).html()).to.equal('bar');
expect(cheerio.html(span)).to.equal('<span>bar</span>');
});
it('(<obj>) : should accept an object, an array, or a cheerio object', function() {
var $span = cheerio('<span>foo</span>');
expect(cheerio.html($span[0])).to.equal('<span>foo</span>');
expect(cheerio.html($span)).to.equal('<span>foo</span>');
});
it('(<value>) : should be able to set to an empty string', function() {
var $elem = cheerio('<span>foo</span>').html('');
expect(cheerio.html($elem)).to.equal('<span></span>');
});
it('() : of empty cheerio object should return null', function() {
expect(cheerio().html()).to.be(null);
});
it('(selector) : should return the outerHTML of the selected element', function() {
var $ = cheerio.load(fixtures.fruits);
expect($.html('.pear')).to.equal('<li class="pear">Pear</li>');
});
});
describe('.text', function() {
it('(cheerio object) : should return the text contents of the specified elements', function() {
var $ = cheerio.load('<a>This is <em>content</em>.</a>');
expect($.text($('a'))).to.equal('This is content.');
});
it('(cheerio object) : should omit comment nodes', function() {
var $ = cheerio.load('<a>This is <!-- a comment --> not a comment.</a>');
expect($.text($('a'))).to.equal('This is not a comment.');
});
it('(cheerio object) : should include text contents of children recursively', function() {
var $ = cheerio.load('<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>');
expect($.text($('a'))).to.equal('This is a child with another child and not a comment followed by one last child and some final text.');
});
it('() : should return the rendered text content of the root', function() {
var $ = cheerio.load('<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>');
expect($.text()).to.equal('This is a child with another child and not a comment followed by one last child and some final text.');
});
});
describe('.load', function() {
it('(html) : should retain original root after creating a new node', function() {
var $html = cheerio.load('<body><ul id="fruits"></ul></body>');
expect($html('body')).to.have.length(1);
$html('<script>');
expect($html('body')).to.have.length(1);
});
it('(html) : should handle lowercase tag options', function() {
var $html = cheerio.load('<BODY><ul id="fruits"></ul></BODY>', { lowerCaseTags : true });
expect($html.html()).to.be('<body><ul id="fruits"></ul></body>');
});
it('(html) : should handle the `normalizeWhitepace` option', function() {
var $html = cheerio.load('<body><b>foo</b> <b>bar</b></body>', { normalizeWhitespace : true });
expect($html.html()).to.be('<body><b>foo</b> <b>bar</b></body>');
});
// TODO:
// it('(html) : should handle xml tag option', function() {
// var $html = $.load('<body><script>oh hai</script></body>', { xmlMode : true });
// console.log($html('script')[0].type);
// expect($html('script')[0].type).to.be('tag');
// });
it('(buffer) : should accept a buffer', function() {
var $html = cheerio.load(new Buffer('<div>foo</div>'));
expect($html.html()).to.be('<div>foo</div>');
});
});
describe('.clone', function() {
it('() : should return a copy', function() {
var $src = cheerio('<div><span>foo</span><span>bar</span><span>baz</span></div>').children();
var $elem = $src.clone();
expect($elem.length).to.equal(3);
expect($elem.parent()).to.have.length(0);
expect($elem.text()).to.equal($src.text());
$src.text('rofl');
expect($elem.text()).to.not.equal($src.text());
});
it('() : should preserve parsing options', function() {
var $ = cheerio.load('<div>π</div>', { decodeEntities: false });
var $div = $('div');
expect($div.text()).to.equal($div.clone().text());
});
});
describe('.parseHTML', function() {
it('() : returns null', function() {
expect(cheerio.parseHTML()).to.equal(null);
});
it('(null) : returns null', function() {
expect(cheerio.parseHTML(null)).to.equal(null);
});
it('("") : returns null', function() {
expect(cheerio.parseHTML('')).to.equal(null);
});
it('(largeHtmlString) : parses large HTML strings', function() {
var html = new Array(10).join('<div></div>');
var nodes = cheerio.parseHTML(html);
expect(nodes.length).to.be.greaterThan(4);
expect(nodes).to.be.an('array');
});
it('("<script>") : ignores scripts by default', function() {
var html = '<script>undefined()</script>';
expect(cheerio.parseHTML(html)).to.have.length(0);
});
it('("<script>", true) : preserves scripts when requested', function() {
var html = '<script>undefined()</script>';
expect(cheerio.parseHTML(html, true)[0].tagName).to.match(/script/i);
});
it('("scriptAndNonScript) : preserves non-script nodes', function() {
var html = '<script>undefined()</script><div></div>';
expect(cheerio.parseHTML(html)[0].tagName).to.match(/div/i);
});
it('(scriptAndNonScript, true) : Preserves script position', function() {
var html = '<script>undefined()</script><div></div>';
expect(cheerio.parseHTML(html, true)[0].tagName).to.match(/script/i);
});
it('(text) : returns a text node', function() {
expect(cheerio.parseHTML('text')[0].type).to.be('text');
});
it('(\\ttext) : preserves leading whitespace', function() {
expect(cheerio.parseHTML('\t<div></div>')[0].data).to.equal('\t');
});
it('( text) : Leading spaces are treated as text nodes', function() {
expect(cheerio.parseHTML(' <div/> ')[0].type).to.be('text');
});
it('(html) : should preserve content', function() {
var html = '<div>test div</div>';
expect(cheerio(cheerio.parseHTML(html)[0]).html()).to.equal('test div');
});
it('(malformedHtml) : should not break', function() {
expect(cheerio.parseHTML('<span><span>')).to.have.length(1);
});
it('(garbageInput) : should not cause an error', function() {
expect(cheerio.parseHTML('<#if><tr><p>This is a test.</p></tr><#/if>') || true).to.be.ok();
});
it('(text) : should return an array that is not effected by DOM manipulation methods', function() {
var $ = cheerio.load('<div>');
var elems = $.parseHTML('<b></b><i></i>');
$('div').append(elems);
expect(elems).to.have.length(2);
});
});
describe('.contains', function() {
var $;
beforeEach(function() {
$ = cheerio.load(fixtures.food);
});
it('(container, contained) : should correctly detect the provided element', function() {
var $food = $('#food');
var $fruits = $('#fruits');
var $apple = $('.apple');
expect($.contains($food[0], $fruits[0])).to.equal(true);
expect($.contains($food[0], $apple[0])).to.equal(true);
});
it('(container, other) : should not detect elements that are not contained', function() {
var $fruits = $('#fruits');
var $vegetables = $('#vegetables');
var $apple = $('.apple');
expect($.contains($vegetables[0], $apple[0])).to.equal(false);
expect($.contains($fruits[0], $vegetables[0])).to.equal(false);
expect($.contains($vegetables[0], $fruits[0])).to.equal(false);
expect($.contains($fruits[0], $fruits[0])).to.equal(false);
expect($.contains($vegetables[0], $vegetables[0])).to.equal(false);
});
});
describe('.root', function() {
it('() : should return a cheerio-wrapped root object', function() {
var $html = cheerio.load('<div><span>foo</span><span>bar</span></div>');
$html.root().append('<div id="test"></div>');
expect($html.html()).to.equal('<div><span>foo</span><span>bar</span></div><div id="test"></div>');
});
});
});