Comparing version 0.6.0 to 0.6.1
@@ -0,1 +1,11 @@ | ||
0.6.1 / 2012-02-12 | ||
================== | ||
* Added .first(), .last(), and .clone() commands. | ||
* Option to parse using whitespace added to `.load`. | ||
* Many bug fixes to make cheerio more aligned with jQuery. | ||
* Added $(':root') to select the highest level element. | ||
Many thanks to the contributors that made this release happen: @ironchefpython and @siddMahen | ||
0.6.0 / 2012-02-07 | ||
@@ -2,0 +12,0 @@ ================== |
@@ -150,3 +150,3 @@ var _ = require('underscore'), | ||
var replaceWith = exports.replaceWith = function(content) { | ||
var elems = parse.eval(content); | ||
var elems = (content.cheerio) ? content : parse.eval(content); | ||
@@ -159,3 +159,3 @@ this.each(function() { | ||
siblings.splice.apply(siblings, [index, 1].concat(elems)); | ||
siblings.splice.apply(siblings, [index, 1].concat(elems.toArray())); | ||
@@ -177,4 +177,4 @@ updateDOM(siblings, this.parent); | ||
var html = exports.html = function(str) { | ||
if(!str || typeof(str) === 'object') { | ||
if(!this[0] || !this[0].children) return ''; | ||
if(str === undefined || typeof(str) === 'object') { | ||
if(!this[0] || !this[0].children) return null; | ||
return $.html(this[0].children); | ||
@@ -222,2 +222,12 @@ } | ||
var clone = exports.clone = function() { | ||
var result = []; | ||
for (var i = 0; i < this.length; i++) { | ||
// TODO: This could be more efficient | ||
result = result.concat(parse.eval($.html(this[i]))); | ||
} | ||
return $(result); | ||
}; | ||
module.exports = $.fn.extend(exports); |
@@ -16,7 +16,7 @@ var _ = require("underscore"), | ||
else | ||
return null; | ||
return $(); | ||
}; | ||
var next = exports.next = function(elem) { | ||
if(!this[0]) return null; | ||
if(!this[0]) return $(); | ||
@@ -28,6 +28,7 @@ var nextSibling = this[0].next; | ||
} | ||
return $(); | ||
}; | ||
var prev = exports.prev = function(elem) { | ||
if(!this[0]) return null; | ||
if(!this[0]) return $(); | ||
@@ -39,6 +40,7 @@ var prevSibling = this[0].prev; | ||
} | ||
return $(); | ||
}; | ||
var siblings = exports.siblings = function(elem) { | ||
if(!this[0]) return null; | ||
if(!this[0]) return $(); | ||
@@ -57,3 +59,3 @@ var self = this, | ||
var children = exports.children = function(selector) { | ||
if(!this[0] || !this[0].children) return null; | ||
if(!this[0] || !this[0].children) return $(); | ||
@@ -74,2 +76,10 @@ var children = _.filter(this[0].children, function(elem) { | ||
var first = exports.first = function() { | ||
return this[0] ? $(this[0]) : $(); | ||
}; | ||
var last = exports.last = function() { | ||
return this[0] ? $(this[this.length - 1]) : $(); | ||
}; | ||
module.exports = $.fn.extend(exports); |
@@ -229,4 +229,4 @@ /* | ||
var load = exports.load = function(html) { | ||
var root = parse(html); | ||
var load = exports.load = function(html, opts) { | ||
var root = parse(html, opts); | ||
@@ -267,2 +267,2 @@ function fn(selector, context, r) { | ||
module.exports = $.extend(exports); | ||
module.exports = $.extend(exports); |
/* | ||
Module dependencies | ||
*/ | ||
var path = require('path'), | ||
soupselect = require('cheerio-soupselect'), | ||
filters = soupselect.filters, | ||
_ = require('underscore'), | ||
@@ -75,2 +77,3 @@ parse = require('./parse'); | ||
}, | ||
filters : filters, | ||
selector : '', | ||
@@ -97,5 +100,5 @@ sort : [].splice, | ||
*/ | ||
var api = 'core utils attributes traversing manipulation'; | ||
var api = 'selectors core utils attributes traversing manipulation'; | ||
api.split(' ').forEach(function(plugin) { | ||
require('./api/' + plugin); | ||
}); |
@@ -12,4 +12,4 @@ /* | ||
*/ | ||
var parser = exports = module.exports = function(content) { | ||
var dom = eval(content), | ||
var parser = exports = module.exports = function(content, opts) { | ||
var dom = eval(content, opts), | ||
root = { | ||
@@ -29,9 +29,8 @@ type : 'root', | ||
var eval = exports.eval = function(content) { | ||
var handler = new htmlparser.DefaultHandler({ | ||
ignoreWhitespace: true | ||
}), | ||
var eval = exports.eval = function(content, opts) { | ||
opts = opts || { ignoreWhitespace: true }; | ||
var handler = new htmlparser.DefaultHandler(opts), | ||
parser = new htmlparser.Parser(handler); | ||
parser.includeLocation = false; | ||
parser.parseComplete(content); | ||
@@ -38,0 +37,0 @@ |
@@ -43,3 +43,3 @@ /* | ||
output = output || []; | ||
dom = isArray(dom) ? dom : [dom]; | ||
dom = (isArray(dom) || dom.cheerio) ? dom : [dom]; | ||
@@ -46,0 +46,0 @@ var len = dom.length, |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -68,2 +68,6 @@ # cheerio | ||
By default, cheerio's parser will ignore whitespace, you can override this default by setting the second parameter: | ||
$ = cheerio.load('<ul id = "fruits">...</ul>', { ignoreWhitespace: false }); | ||
Optionally, you can also load in the HTML by passing the string as the context: | ||
@@ -207,3 +211,14 @@ | ||
#### .first() | ||
Will select the first element of a cheerio object | ||
$('#fruits').children().first().text() | ||
=> Apple | ||
#### .last() | ||
Will select the last element of a cheerio object | ||
$('#fruits').children().last().text() | ||
=> Pear | ||
### Manipulation | ||
@@ -336,2 +351,8 @@ Methods for modifying the DOM structure. | ||
=> [ {...}, {...}, {...} ] | ||
#### .clone() #### | ||
Clone the cheerio object. | ||
var moreFruit = $('#fruits').clone() | ||
### Utilities | ||
@@ -338,0 +359,0 @@ |
@@ -1,2 +0,2 @@ | ||
var cheerio = require('../'), | ||
var $ = require('../'), | ||
should = require('should'); | ||
@@ -13,11 +13,42 @@ | ||
it('(html) should return innerHTML of element', function() { | ||
var html = "<div><span>foo</span><span>bar</span></div>", | ||
$ = cheerio.load(html), | ||
span = $('div').children().get(1); | ||
it('() : should return innerHTML; $.html(obj) should return outerHTML', function() { | ||
var div = $('div', '<div><span>foo</span><span>bar</span></div>'), | ||
span = div.children().get(1); | ||
$(span).html().should.equal('bar'); | ||
$.html(span).should.equal('<span>bar</span>') | ||
$.html(span).should.equal('<span>bar</span>'); | ||
}); | ||
it('(<obj>) : should accept an object, an array, or a cheerio object', function() { | ||
var span = $("<span>foo</span>"); | ||
$.html(span.get(0)).should.equal('<span>foo</span>'); | ||
$.html(span.get()).should.equal('<span>foo</span>'); | ||
$.html(span).should.equal('<span>foo</span>'); | ||
}); | ||
it('(<value>) : should be able to set to an empty string', function() { | ||
var elem = $("<span>foo</span>"); | ||
elem.html(''); | ||
$.html(elem).should.equal('<span></span>'); | ||
}); | ||
it('() : of empty cheerio object should return null', function() { | ||
should.not.exist($().html()); | ||
}); | ||
}); | ||
describe('.clone', function() { | ||
it('() : should return a copy', function() { | ||
var src = $("<div><span>foo</span><span>bar</span><span>baz</span></div>").children(), | ||
elem = src.clone(); | ||
elem.length.should.equal(3); | ||
elem.parent().length.should.equal(0); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
75381
27
951
449