/* Module dependencies */ var parse = require('./parse'), isHtml = require('./utils').isHtml, _ = { extend: require('lodash.assignin'), bind: require('lodash.bind'), forEach: require('lodash.foreach'), defaults: require('lodash.defaults') }; /* * The API */ var api = [ require('./api/attributes'), require('./api/traversing'), require('./api/manipulation'), require('./api/css'), require('./api/forms') ]; /* * Instance of cheerio */ var Cheerio = module.exports = function(selector, context, root, options) { if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root, options); this.options = _.defaults(options || {}, this.options); // $(), $(null), $(undefined), $(false) if (!selector) return this; if (root) { if (typeof root === 'string') root = parse(root, this.options); this._root = Cheerio.call(this, root); } // $($) if (selector.cheerio) return selector; // $(dom) if (isNode(selector)) selector = [selector]; // $([dom]) if (Array.isArray(selector)) { _.forEach(selector, _.bind(function(elem, idx) { this[idx] = elem; }, this)); this.length = selector.length; return this; } // $() if (typeof selector === 'string' && isHtml(selector)) { return Cheerio.call(this, parse(selector, this.options).children); } // If we don't have a context, maybe we have a root, from loading if (!context) { context = this._root; } else if (typeof context === 'string') { if (isHtml(context)) { // $('li', '