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 | 1 1 471 471 471 471 743 743 471 471 471 471 471 1 64 64 1 1 64 64 57 57 7 7 1 2 2 2 1 51 51 51 61 61 31 31 51 1 14 14 3 11 2 11 11 9 11 1 12 1 26 3 23 50 50 9 14 | /** * Module dependencies */ var select = require('CSSselect'), parse = require('./parse'), render = require('dom-serializer'), _ = require('lodash'); /** * $.load(str) */ exports.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) { 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 = Cheerio.prototype; // Add in the static methods _.merge(initialize, exports); // Add in the root initialize._root = root; // store options initialize._options = options; return initialize; }; /** * $.html([selector | dom]) */ exports.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); if (dom) { dom = (typeof dom === 'string') ? select(dom, this._root, options) : dom; return render(dom, options); } else Eif (this._root && this._root.children) { return render(this._root.children, options); } else { return ''; } }; /** * $.xml([selector | dom]) */ exports.xml = function(dom) { Iif (dom) { dom = (typeof dom === 'string') ? select(dom, this._root, this.options) : dom; return render(dom, { xmlMode: true }); } else Eif (this._root && this._root.children) { return render(this._root.children, { xmlMode: true }); } else { return ''; } }; /** * $.text(dom) */ exports.text = function(elems) { Iif (!elems) return ''; var ret = '', len = elems.length, elem; for (var i = 0; i < len; i ++) { elem = elems[i]; if (elem.type === 'text') ret += elem.data; else Eif (elem.children && elem.type !== 'comment') { ret += exports.text(elem.children); } } return ret; }; /** * $.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 compatibility with jQuery. */ exports.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(); } return parsed.root()[0].children; }; /** * $.root() */ exports.root = function() { return this(this._root); }; /** * $.contains() */ exports.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; }; |