-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrapAll.js
44 lines (39 loc) · 1.68 KB
/
wrapAll.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
/******************************/
/***CHEERIO wrapAll EXTENSION**/
/******************************/
var cheerio = require('cheerio');
var _ = require('lodash');
module.exports = (function() {
var extendCheerio = function extendCheerio($) {
_.extend($.prototype, {
wrapAll: function(wrapper) {
if (this.length < 1) {
return this;
}
if (this.length < 2 && this.wrap) { // wrap not defined in npm version,
return this.wrap(wrapper); // and git version fails testing.
}
var elems = this;
var section = $(wrapper);
var marker = $('<div>');
marker = marker.insertBefore(elems.first()); // in jQuery marker would remain current
elems.each(function(k, v) { // in Cheerio, we update with the output.
section.append($(v));
});
section.insertBefore(marker);
marker.remove();
return section; // This is what jQuery would return, IIRC.
},
});
};
if ("test") {
$ = cheerio.load("<html><body><div><p><span>This <em>is <i>test</p><span>More <em>test");
extendCheerio($);
$('span').wrapAll('<section>');
var passed = ($.html() === '<html><body><div><p><section><span>This <em>is <i>test</i></em>'+
'</span><span>More <em>test</em></span></section></p></div></body></html>');
//console.log("passed: " + (passed ? "yes" : "no"));
// console.log($.html() + "\n\n");
}
return extendCheerio;
})();