contains = function (container, contained) { // According to the jQuery API, an element does not "contain" itself if (contained === container) { return false; } // Step up the descendants, stopping when the root element is reached // (signaled by `.parent` returning a reference to the same object) while (contained && contained !== contained.parent) { contained = contained.parent; if (contained === container) { return true; } } return false; }
...
Sometimes you need to work with the top-level root element. To query it, you can use `$.root()`.
```js
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
```
#### $.contains( container, contained )
Checks to see if the `contained` DOM element is a descendant of the `container` DOM element.
#### $.parseHTML( data [, context ] [, keepScripts ] )
Parses a string into an array of DOM nodes. The `context` argument has no meaning for Cheerio, but it is maintained for API compatability
.
#### $.load( html[, options ] )
Load in the HTML. (See the previous section titled "Loading" for more information.)
...
html = function (dom, options) { var Cheerio = require('./cheerio'); // be flexible about parameters, sometimes we call html(), // with options as only parameter // check dom argument for dom element specific properties // assume there is no 'length' or 'type' properties in the options object if (Object.prototype.toString.call(dom) === '[object Object]' && !options && !('length' in dom) && !('type' in dom)) { options = dom; dom = undefined; } // sometimes $.html() used without preloading html // so fallback non existing options to the default ones options = _.defaults(options || {}, this._options, Cheerio.prototype.options); return render(this, dom, options); }
...
```js
let cheerio = require('cheerio')
let $ = cheerio.load('<h2 class="title">Hello world</h2>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
```
## Installation
`npm install cheerio`
## Features
...
load = function (content, options) { var Cheerio = require('./cheerio'); options = _.defaults(options || {}, Cheerio.prototype.options); var root = parse(content, options); var initialize = function(selector, context, r, opts) { if (!(this instanceof initialize)) { return new initialize(selector, context, r, opts); } opts = _.defaults(opts || {}, options); return Cheerio.call(this, selector, context, r || root, opts); }; // Ensure that selections created by the "loaded" `initialize` function are // true Cheerio instances. initialize.prototype = Object.create(Cheerio.prototype); initialize.prototype.constructor = initialize; // Mimic jQuery's prototype alias for plugin authors. initialize.fn = initialize.prototype; // Keep a reference to the top-level scope so we can chain methods that implicitly // resolve selectors; e.g. $("<span>").(".bar"), which otherwise loses ._root initialize.prototype._originalRoot = root; // Add in the static methods _.merge(initialize, exports); // Add in the root initialize._root = root; // store options initialize._options = options; return initialize; }
...
</a>
</div>
<br />
```js
let cheerio = require('cheerio')
let $ = cheerio.load('<h2 class="title">Hello world</h2
>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
```
...
parse = function (content, options) { var dom = exports.evaluate(content, options), // Generic root element root = exports.evaluate('<root></root>', options)[0]; root.type = 'root'; // Update the dom using the root exports.update(dom, root); return root; }
n/a
parseHTML = function (data, context, keepScripts) { var parsed; if (!data || typeof data !== 'string') { return null; } if (typeof context === 'boolean') { keepScripts = context; } parsed = this.load(data); if (!keepScripts) { parsed('script').remove(); } // The `children` array is used by Cheerio internally to group elements that // share the same parents. When nodes created through `parseHTML` are // inserted into previously-existing DOM structures, they will be removed // from the `children` array. The results of `parseHTML` should remain // constant across these operations, so a shallow copy should be returned. return parsed.root()[0].children.slice(); }
...
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
```
#### $.contains( container, contained )
Checks to see if the `contained` DOM element is a descendant of the `container` DOM element.
#### $.parseHTML( data [, context ] [, keepScripts ] )
Parses a string into an array of DOM nodes. The `context` argument has no meaning for Cheerio, but it is maintained for API compatability
.
#### $.load( html[, options ] )
Load in the HTML. (See the previous section titled "Loading" for more information.)
### Plugins
...
root = function () { return this(this._root); }
...
var moreFruit = $('#fruits').clone()
```
### Utilities
#### $.root
Sometimes you need to work with the top-level root element. To query it, you can use `$.root
()`.
```js
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
```
#### $.contains( container, contained )
...
text = function (elems) { if (!elems) { elems = this.root(); } var ret = '', len = elems.length, elem; for (var i = 0; i < len; i++) { elem = elems[i]; if (elem.type === 'text') ret += elem.data; else if (elem.children && elem.type !== 'comment') { ret += exports.text(elem.children); } } return ret; }
...
<br />
```js
let cheerio = require('cheerio')
let $ = cheerio.load('<h2 class="title">Hello world</h2>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
```
## Installation
...
xml = function (dom) { var options = _.defaults({xmlMode: true}, this._options); return render(this, dom, options); }
...
```xml
$ = cheerio.load('<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50
" time="12:05:01.123"/>');
```
... and later want to render to XML. To do this, you can use the 'xml' utility function:
```js
$.xml()
//=> <media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time
="12:05:01.123"/>
```
You may also render the text content of a Cheerio object using the `text` static method:
```js
$ = cheerio.load('This is <em>content</em>.')
...
parse = function (content, options) { var dom = exports.evaluate(content, options), // Generic root element root = exports.evaluate('<root></root>', options)[0]; root.type = 'root'; // Update the dom using the root exports.update(dom, root); return root; }
n/a
evaluate = function (content, options) { // options = options || $.fn.options; var dom; if (typeof content === 'string' || Buffer.isBuffer(content)) { dom = htmlparser.parseDOM(content, options); } else { dom = content; } return dom; }
...
*/
var htmlparser = require('htmlparser2');
/*
Parser
*/
exports = module.exports = function(content, options) {
var dom = exports.evaluate(content, options),
// Generic root element
root = exports.evaluate('<root></root>', options)[0];
root.type = 'root';
// Update the dom using the root
exports.update(dom, root);
...
update = function (arr, parent) { // normalize if (!Array.isArray(arr)) arr = [arr]; // Update parent if (parent) { parent.children = arr; } else { parent = null; } // Update neighbors for (var i = 0; i < arr.length; i++) { var node = arr[i]; // Cleanly remove existing nodes from their previous structures. var oldParent = node.parent || node.root, oldSiblings = oldParent && oldParent.children; if (oldSiblings && oldSiblings !== arr) { oldSiblings.splice(oldSiblings.indexOf(node), 1); if (node.prev) { node.prev.next = node.next; } if (node.next) { node.next.prev = node.prev; } } if (parent) { node.prev = arr[i - 1] || null; node.next = arr[i + 1] || null; } else { node.prev = node.next = null; } if (parent && parent.type === 'root') { node.root = parent; node.parent = null; } else { node.root = null; node.parent = parent; } } return parent; }
...
var dom = exports.evaluate(content, options),
// Generic root element
root = exports.evaluate('<root></root>', options)[0];
root.type = 'root';
// Update the dom using the root
exports.update(dom, root);
return root;
};
exports.evaluate = function(content, options) {
// options = options || $.fn.options;
...
contains = function (container, contained) { // According to the jQuery API, an element does not "contain" itself if (contained === container) { return false; } // Step up the descendants, stopping when the root element is reached // (signaled by `.parent` returning a reference to the same object) while (contained && contained !== contained.parent) { contained = contained.parent; if (contained === container) { return true; } } return false; }
...
Sometimes you need to work with the top-level root element. To query it, you can use `$.root()`.
```js
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
```
#### $.contains( container, contained )
Checks to see if the `contained` DOM element is a descendant of the `container` DOM element.
#### $.parseHTML( data [, context ] [, keepScripts ] )
Parses a string into an array of DOM nodes. The `context` argument has no meaning for Cheerio, but it is maintained for API compatability
.
#### $.load( html[, options ] )
Load in the HTML. (See the previous section titled "Loading" for more information.)
...
html = function (dom, options) { var Cheerio = require('./cheerio'); // be flexible about parameters, sometimes we call html(), // with options as only parameter // check dom argument for dom element specific properties // assume there is no 'length' or 'type' properties in the options object if (Object.prototype.toString.call(dom) === '[object Object]' && !options && !('length' in dom) && !('type' in dom)) { options = dom; dom = undefined; } // sometimes $.html() used without preloading html // so fallback non existing options to the default ones options = _.defaults(options || {}, this._options, Cheerio.prototype.options); return render(this, dom, options); }
...
```js
let cheerio = require('cheerio')
let $ = cheerio.load('<h2 class="title">Hello world</h2>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
```
## Installation
`npm install cheerio`
## Features
...
load = function (content, options) { var Cheerio = require('./cheerio'); options = _.defaults(options || {}, Cheerio.prototype.options); var root = parse(content, options); var initialize = function(selector, context, r, opts) { if (!(this instanceof initialize)) { return new initialize(selector, context, r, opts); } opts = _.defaults(opts || {}, options); return Cheerio.call(this, selector, context, r || root, opts); }; // Ensure that selections created by the "loaded" `initialize` function are // true Cheerio instances. initialize.prototype = Object.create(Cheerio.prototype); initialize.prototype.constructor = initialize; // Mimic jQuery's prototype alias for plugin authors. initialize.fn = initialize.prototype; // Keep a reference to the top-level scope so we can chain methods that implicitly // resolve selectors; e.g. $("<span>").(".bar"), which otherwise loses ._root initialize.prototype._originalRoot = root; // Add in the static methods _.merge(initialize, exports); // Add in the root initialize._root = root; // store options initialize._options = options; return initialize; }
...
</a>
</div>
<br />
```js
let cheerio = require('cheerio')
let $ = cheerio.load('<h2 class="title">Hello world</h2
>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
```
...
parseHTML = function (data, context, keepScripts) { var parsed; if (!data || typeof data !== 'string') { return null; } if (typeof context === 'boolean') { keepScripts = context; } parsed = this.load(data); if (!keepScripts) { parsed('script').remove(); } // The `children` array is used by Cheerio internally to group elements that // share the same parents. When nodes created through `parseHTML` are // inserted into previously-existing DOM structures, they will be removed // from the `children` array. The results of `parseHTML` should remain // constant across these operations, so a shallow copy should be returned. return parsed.root()[0].children.slice(); }
...
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
```
#### $.contains( container, contained )
Checks to see if the `contained` DOM element is a descendant of the `container` DOM element.
#### $.parseHTML( data [, context ] [, keepScripts ] )
Parses a string into an array of DOM nodes. The `context` argument has no meaning for Cheerio, but it is maintained for API compatability
.
#### $.load( html[, options ] )
Load in the HTML. (See the previous section titled "Loading" for more information.)
### Plugins
...
root = function () { return this(this._root); }
...
var moreFruit = $('#fruits').clone()
```
### Utilities
#### $.root
Sometimes you need to work with the top-level root element. To query it, you can use `$.root
()`.
```js
$.root().append('<ul id="vegetables"></ul>').html();
//=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
```
#### $.contains( container, contained )
...
text = function (elems) { if (!elems) { elems = this.root(); } var ret = '', len = elems.length, elem; for (var i = 0; i < len; i++) { elem = elems[i]; if (elem.type === 'text') ret += elem.data; else if (elem.children && elem.type !== 'comment') { ret += exports.text(elem.children); } } return ret; }
...
<br />
```js
let cheerio = require('cheerio')
let $ = cheerio.load('<h2 class="title">Hello world</h2>')
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
//=> <h2 class="title welcome">Hello there!</h2>
```
## Installation
...
xml = function (dom) { var options = _.defaults({xmlMode: true}, this._options); return render(this, dom, options); }
...
```xml
$ = cheerio.load('<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50
" time="12:05:01.123"/>');
```
... and later want to render to XML. To do this, you can use the 'xml' utility function:
```js
$.xml()
//=> <media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time
="12:05:01.123"/>
```
You may also render the text content of a Cheerio object using the `text` static method:
```js
$ = cheerio.load('This is <em>content</em>.')
...
camelCase = function (str) { return str.replace(/[_.-](\w|$)/g, function(_, x) { return x.toUpperCase(); }); }
n/a
cloneDom = function (dom, options) { return parse(render(dom, options), options).children; }
n/a
cssCase = function (str) { return str.replace(/[A-Z]/g, '-$&').toLowerCase(); }
n/a
domEach = function (cheerio, fn) { var i = 0, len = cheerio.length; while (i < len && fn.call(cheerio, i, cheerio[i]) !== false) ++i; return cheerio; }
n/a
isHtml = function (str) { // Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML if (str.charAt(0) === '<' && str.charAt(str.length - 1) === '>' && str.length >= 3) return true; // Run the regex var match = quickExpr.exec(str); return !!(match && match[1]); }
n/a
isTag = function (type) { if (type.type) type = type.type; return tags[type] || false; }
n/a