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 | 1 1 1 1 5147 4319 4319 4311 1990 1990 4311 3048 935 3048 2245 2758 2245 2245 803 144 659 597 62 54 51 51 3 3 8 3 659 656 1 1 1 1 1 1 857 662 662 1 1303 1303 1303 1 16 1 4 1 3048 | /* Module dependencies */ var parse = require('./parse'), _ = require('lodash'); /* * The API */ var api = [ require('./api/attributes'), require('./api/traversing'), require('./api/manipulation'), require('./api/css') ]; /* * A simple way to check for HTML strings or ID strings */ var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/; /* * 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, function(elem, idx) { this[idx] = elem; }, this); this.length = selector.length; return this; } // $(<html>) 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', '<ul>...</ul>') context = parse(context, this.options); context = Cheerio.call(this, context); } else { // $('li', 'ul') selector = [context, selector].join(' '); context = this._root; } // $('li', node), $('li', [nodes]) } else if (!context.cheerio) { context = Cheerio.call(this, context); } // If we still don't have a context, return if (!context) return this; // #id, .class, tag return context.find(selector); }; /** * Mix in `static` */ _.extend(Cheerio, require('./static')); /* * Set a signature of the object */ Cheerio.prototype.cheerio = '[cheerio object]'; /* * Cheerio default options */ Cheerio.prototype.options = { withDomLvl1: true, normalizeWhitespace: false, xmlMode: false, decodeEntities: true }; /* * Make cheerio an array-like object */ Cheerio.prototype.length = 0; Cheerio.prototype.splice = Array.prototype.splice; /* * Check if string is HTML */ var 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]); }; /* * Make a cheerio object * * @api private */ Cheerio.prototype._make = function(dom, context) { var cheerio = new Cheerio(dom, context, this._root, this.options); cheerio.prevObject = this; return cheerio; }; /** * Turn a cheerio object into an array * * @deprecated */ Cheerio.prototype.toArray = function() { return this.get(); }; /** * Plug in the API */ api.forEach(function(mod) { _.extend(Cheerio.prototype, mod); }); var isNode = function(obj) { return obj.name || obj.type === 'text' || obj.type === 'comment'; }; |