Comparing version 0.2.0 to 0.2.1
@@ -25,6 +25,6 @@ (function() { | ||
ret = this.constructor(); | ||
if (cheerio.isArray(elems)) { | ||
if ($.isArray(elems)) { | ||
push.apply(ret, elems); | ||
} else { | ||
cheerio.merge(ret, elems); | ||
$.merge(ret, elems); | ||
} | ||
@@ -31,0 +31,0 @@ ret.prevObject = this; |
(function() { | ||
var $, after, append, before, dom, empty, html, parser, prepend, remove, removeChild, text, updateArray, _; | ||
var $, after, append, before, dom, empty, html, parser, prepend, remove, removeChild, text, _; | ||
var __slice = Array.prototype.slice; | ||
@@ -52,9 +52,2 @@ _ = require('underscore'); | ||
}; | ||
updateArray = function(arr) { | ||
arr.forEach(function(elem, i) { | ||
arr[i].prev = arr[i - 1] || null; | ||
return arr[i].next = arr[i + 1] || null; | ||
}); | ||
return arr; | ||
}; | ||
after = exports.after = function() { | ||
@@ -69,9 +62,9 @@ var doms, elem, elems, _i, _len; | ||
this.each(function() { | ||
var parentsChildren, pos; | ||
parentsChildren = this.parent.children; | ||
pos = $.inArray(this, parentsChildren); | ||
var pos, siblings; | ||
siblings = $.siblingsAndMe(this); | ||
pos = $.inArray(this, siblings); | ||
if (pos >= 0) { | ||
parentsChildren.splice.apply(parentsChildren, [pos + 1, 0].concat(doms)); | ||
siblings.splice.apply(siblings, [pos + 1, 0].concat(doms)); | ||
} | ||
return $.updateDOM(parentsChildren, this.parent); | ||
return $.updateDOM(siblings, this.parent); | ||
}); | ||
@@ -89,9 +82,9 @@ return this; | ||
this.each(function() { | ||
var parentsChildren, pos; | ||
parentsChildren = this.parent.children; | ||
pos = $.inArray(this, parentsChildren); | ||
var pos, siblings; | ||
siblings = $.siblingsAndMe(this); | ||
pos = $.inArray(this, siblings); | ||
if (pos >= 0) { | ||
parentsChildren.splice.apply(parentsChildren, [pos, 0].concat(doms)); | ||
siblings.splice.apply(siblings, [pos, 0].concat(doms)); | ||
} | ||
return $.updateDOM(parentsChildren, this.parent); | ||
return $.updateDOM(siblings, this.parent); | ||
}); | ||
@@ -98,0 +91,0 @@ return this; |
(function() { | ||
var $, children, each, next, parent, prev, siblings, _; | ||
var $, children, each, find, next, parent, prev, siblings, soupselect, _; | ||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
_ = require("underscore"); | ||
soupselect = require("soupselect"); | ||
$ = require("../cheerio"); | ||
@@ -11,2 +12,7 @@ /* | ||
*/ | ||
find = exports.find = function(selector) { | ||
var elem; | ||
elem = soupselect.select(this.toArray(), selector); | ||
return $(elem); | ||
}; | ||
parent = exports.parent = function(elem) { | ||
@@ -48,3 +54,9 @@ if (this[0] && this[0].parent) { | ||
siblings = exports.siblings = function(elem) { | ||
siblings = _.filter(this.parent().children(), __bind(function(elem) { | ||
var sibs; | ||
if (this.parent()) { | ||
sibs = this.parent().children(); | ||
} else { | ||
sibs = $.siblingsAndMe(this); | ||
} | ||
siblings = _.filter(sibs, __bind(function(elem) { | ||
return elem !== this[0] && elem.type === "tag"; | ||
@@ -51,0 +63,0 @@ }, this)); |
(function() { | ||
var $, access, attr, class2type, each, hasOwn, inArray, indexOf, isArray, makeArray, merge, push, rboolean, removeAttr, slice, text, toString, trim, type, updateDOM, _; | ||
var $, access, attr, class2type, dom, each, html, inArray, indexOf, isArray, load, makeArray, merge, parser, push, rboolean, removeAttr, renderer, siblingsAndMe, text, toString, type, updateDOM, _; | ||
_ = require("underscore"); | ||
$ = require("../cheerio"); | ||
parser = require("../parser"); | ||
renderer = require("../renderer"); | ||
class2type = {}; | ||
@@ -20,9 +22,9 @@ _.each("Boolean Number String Function Array Date Regex Object".split(" "), function(name, i) { | ||
toString = Object.prototype.toString; | ||
hasOwn = Object.prototype.hasOwnProperty; | ||
push = Array.prototype.push; | ||
slice = Array.prototype.slice; | ||
trim = String.prototype.trim; | ||
indexOf = Array.prototype.indexOf; | ||
updateDOM = exports.updateDOM = function(arr, parent) { | ||
var elem, i, _len; | ||
if (parent) { | ||
parent.children = $(arr).get(); | ||
} | ||
for (i = 0, _len = arr.length; i < _len; i++) { | ||
@@ -83,2 +85,19 @@ elem = arr[i]; | ||
}; | ||
siblingsAndMe = exports.siblingsAndMe = function(elem) { | ||
var element, raw, siblings; | ||
siblings = []; | ||
raw = $(elem)[0]; | ||
element = raw; | ||
while (element.prev) { | ||
element = element.prev; | ||
} | ||
siblings.push(element); | ||
while (element.next) { | ||
element = element.next; | ||
if (element.type === "tag") { | ||
siblings.push(element); | ||
} | ||
} | ||
return $(null).pushStack(siblings); | ||
}; | ||
each = exports.each = function(object, callback, args) { | ||
@@ -189,3 +208,32 @@ var i, isObj, length, name; | ||
}; | ||
load = exports.load = function(html) { | ||
var fn, root; | ||
root = parser.parse(html); | ||
$.extend({ | ||
'root': root | ||
}); | ||
fn = function(selector, context) { | ||
return $(selector, context, root); | ||
}; | ||
return _(fn).extend($); | ||
}; | ||
html = exports.html = function(dom) { | ||
if (dom !== void 0 && dom.type) { | ||
return renderer.render(dom); | ||
} else if (this.root) { | ||
return renderer.render(this.root); | ||
} else { | ||
return ""; | ||
} | ||
}; | ||
dom = exports.dom = function(dom) { | ||
if (dom !== void 0 && dom.type) { | ||
return dom; | ||
} else if (this.root) { | ||
return this.root; | ||
} else { | ||
return ""; | ||
} | ||
}; | ||
module.exports = $.extend(exports); | ||
}).call(this); |
(function() { | ||
var api, cheerio, parser, path, plugin, renderer, soupselect, _, _i, _len; | ||
var api, cheerio, parser, path, plugin, soupselect, _, _i, _len; | ||
path = require("path"); | ||
@@ -7,3 +7,2 @@ soupselect = require("soupselect"); | ||
parser = require("./parser"); | ||
renderer = require("./renderer"); | ||
cheerio = (function() { | ||
@@ -18,2 +17,3 @@ var quickExpr, trimLeft, trimRight; | ||
cheerio.fn = cheerio.prototype = { | ||
cheerio: "0.2.1", | ||
constructor: cheerio, | ||
@@ -58,3 +58,2 @@ init: function(selector, context, root) { | ||
}, | ||
cheerio: "0.0.1", | ||
selector: "", | ||
@@ -70,40 +69,2 @@ sort: [].sort, | ||
}; | ||
cheerio.extend({ | ||
load: function(html) { | ||
var fn, root; | ||
root = parser.parse(html); | ||
cheerio.extend({ | ||
'root': root | ||
}); | ||
fn = function(selector, context) { | ||
return cheerio(selector, context, root); | ||
}; | ||
return _(fn).extend(cheerio); | ||
}, | ||
html: function(dom) { | ||
if (dom !== void 0 && dom.type) { | ||
return renderer.render(dom); | ||
} else if (this.root) { | ||
return renderer.render(this.root); | ||
} else { | ||
return ""; | ||
} | ||
}, | ||
dom: function(dom) { | ||
if (dom !== void 0 && dom.type) { | ||
return dom; | ||
} else if (this.root) { | ||
return this.root; | ||
} else { | ||
return ""; | ||
} | ||
} | ||
}); | ||
cheerio.fn.extend({ | ||
find: function(selector) { | ||
var elem; | ||
elem = soupselect.select(this.toArray(), selector); | ||
return cheerio(elem); | ||
} | ||
}); | ||
return cheerio; | ||
@@ -110,0 +71,0 @@ })(); |
(function() { | ||
var render, renderDirective, renderTag, renderText, singleTag, tagType, utils, _; | ||
var render, renderComment, renderDirective, renderTag, renderText, singleTag, tagType, utils, _; | ||
_ = require("underscore"); | ||
@@ -50,2 +50,4 @@ utils = require("./utils"); | ||
output.push(renderDirective(elem)); | ||
} else if (elem.type === "comment") { | ||
output.push(renderComment(elem)); | ||
} else { | ||
@@ -82,3 +84,6 @@ output.push(renderText(elem)); | ||
}; | ||
renderComment = function(elem) { | ||
return '<!--' + elem.raw + '-->'; | ||
}; | ||
module.exports = exports; | ||
}).call(this); |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -9,8 +9,8 @@ # cheerio | ||
var cheerio = require("cheerio"), | ||
$ = cheerio.load("<h2 class = 'title'>Hello world</h2>") | ||
$ = cheerio.load("<h2 class = 'title'>Hello world</h2>"); | ||
$('h2.title').text('Hello there!') | ||
$('h2').addClass('welcome') | ||
$('h2.title').text('Hello there!'); | ||
$('h2').addClass('welcome'); | ||
$.html() | ||
$.html(); | ||
=> <h2 class = "title welcome">Hello there!</h2> | ||
@@ -28,9 +28,9 @@ | ||
__❤ Familiar syntax:__ | ||
Cheerio implements a subset of core jQuery. Cheerio removes all the DOM inconsistencies and browser cruft from the jQuery library, revealing a truly gorgeous API. | ||
Cheerio implements a subset of core jQuery. Cheerio removes all the DOM inconsistencies and browser cruft from the jQuery library, revealing its truly gorgeous API. | ||
__ϟ Blazingly fast:__ | ||
Cheerio works with a very simple, consistent DOM model. As a result parsing, manipulating, and rendering are incredibly efficient. Preliminary end-to-end benchmarks suggest that cheerio is about 8x faster than JSDOM. | ||
Cheerio works with a very simple, consistent DOM model. As a result parsing, manipulating, and rendering are incredibly efficient. Preliminary end-to-end benchmarks suggest that cheerio is about __8x__ faster than JSDOM. | ||
__❁ Insanely flexible:__ | ||
Cheerio wraps around @tautologistics forgiving htmlparser. Cheerio can parse nearly any page or chunk of html or xml. | ||
Cheerio wraps around @tautologistics forgiving htmlparser. Cheerio can parse nearly any HTML or XML document. | ||
@@ -52,3 +52,2 @@ ## What about JSDOM? | ||
### Markup example we'll be using: | ||
This is the HTML markup we will be selecting, traversing, and manipulating in all of the API examples. | ||
@@ -61,2 +60,3 @@ <ul id = "fruits"> | ||
This is the HTML markup we will be using in all of the API examples. | ||
@@ -67,16 +67,16 @@ | ||
This is the preferred method: | ||
This is the _preferred_ method: | ||
var cheerio = require('cheerio'), | ||
$ = cheerio.load( '<ul id = "fruits">...</ul>' ) | ||
$ = cheerio.load('<ul id = "fruits">...</ul>'); | ||
Optionally, you can also load in the HTML by passing the string as the context: | ||
$ = require('cheerio') | ||
$('ul', '<ul id = "fruits">...</ul>') | ||
$ = require('cheerio'); | ||
$('ul', '<ul id = "fruits">...</ul>'); | ||
Or as the root: | ||
$ = require('cheerio') | ||
$('li', 'ul', '<ul id = "fruits">...</ul>') | ||
$ = require('cheerio'); | ||
$('li', 'ul', '<ul id = "fruits">...</ul>'); | ||
### Selectors | ||
@@ -91,4 +91,2 @@ | ||
__Examples:__ | ||
$(".apple", '#fruits').text() | ||
@@ -103,2 +101,3 @@ => Apple | ||
> See https://github.com/harryf/node-soupselect for all available selectors | ||
@@ -111,3 +110,3 @@ > See http://api.jquery.com/jQuery/ for more information | ||
#### .attr( name, value ) | ||
Method for getting and setting attributes. May also use a `map` and `function` like jQuery. Gets the attribute value for only the first element in the matched set. If you set an attribute's value to `null`, you remove that attribute. | ||
Method for getting and setting attributes. Gets the attribute value for only the first element in the matched set. If you set an attribute's value to `null`, you remove that attribute. You may also pass a `map` and `function` like jQuery. | ||
@@ -129,3 +128,3 @@ $('ul').attr('id') | ||
#### .hasClass( className ) | ||
Check to see if *any* of the matched elements have a given `className`. | ||
Check to see if *any* of the matched elements have the given `className`. | ||
@@ -166,2 +165,8 @@ $('.pear').hasClass('pear') | ||
#### .find(selector) | ||
Get a set of descendants filtered by `selector` of each element in the current set of matched elements. | ||
$('#fruits').find('li').size() | ||
=> 3 | ||
#### .parent() | ||
@@ -186,3 +191,3 @@ Gets the parent of the first selected element. | ||
#### .siblings() | ||
Gets the first selected element's siblings. Excludes itself. | ||
Gets the first selected element's siblings, excluding itself. | ||
@@ -199,3 +204,3 @@ $('.pear').siblings().length | ||
#### .each( function(index, element) ) | ||
Iterates over a cheerio object, executing a function for each matched element. When the callback is fired, the function is fired in the context of the DOM element, so `this` refers to the current element, same as `element`. | ||
Iterates over a cheerio object, executing a function for each matched element. When the callback is fired, the function is fired in the context of the DOM element, so `this` refers to the current element, which is equivalent to the function parameter `element`. | ||
@@ -269,3 +274,2 @@ var fruits = []; | ||
=> <ul id = "fruits"> | ||
<li class = "plum">Plum</li> | ||
<li class = "apple">Apple</li> | ||
@@ -288,3 +292,3 @@ <li class = "orange">Orange</li> | ||
$('ul#fruits').html('<li class = "mango">Mango</li>').html() | ||
$('#fruits').html('<li class = "mango">Mango</li>').html() | ||
=> <ul id="fruits"> | ||
@@ -383,3 +387,10 @@ <li class="mango">Mango</li> | ||
## Testing | ||
To run the test suite, download the repository, then within the cheerio directory, run: | ||
npm install | ||
npm test | ||
This will download the development packages and run the test suite. | ||
## Special Thanks | ||
@@ -390,6 +401,6 @@ | ||
__• @tautologistics' node-htmlparser:__ | ||
This HTML parser can parse anything! The parser produces really consistent results, even when the HTML string has errors. This man is a genius. | ||
This HTML parser can parse anything and produces really consistent results, even when the HTML string has errors. This man is a genius. | ||
__• @harryf's node-soupselect:__ | ||
What an incredibly fast and precise CSS selector engine. Never really liked the feature-rich selector engines – this engine strikes a great balance. | ||
What an incredibly fast and precise CSS selector engine. I never really liked the feature-rich selector engines — I think this engine strikes a great balance. | ||
@@ -396,0 +407,0 @@ __• @jQuery team:__ |
Sorry, the diff of this file is not supported yet
39691
16
904
419