Comparing version 0.10.1 to 0.10.2
@@ -0,1 +1,12 @@ | ||
0.10.2 / 2012-11-17 | ||
================== | ||
* Added a toString() method (@bensheldon) | ||
* use `_.each` and `_.map` to simplify cheerio namesakes (@davidchambers) | ||
* Added filter() with tests and updated readme (@bensheldon & @davidchambers) | ||
* Added spaces between attributes rewritten by removeClass (@jos3000) | ||
* updated docs to remove reference to size method (@ironchefpython) | ||
* removed tidy from cheerio | ||
0.10.1 / 2012-10-04 | ||
@@ -14,3 +25,3 @@ =================== | ||
0.9.2 / 2012-08-10 | ||
0.9.2 / 2012-08-10 | ||
================== | ||
@@ -27,3 +38,3 @@ | ||
0.9.0 / 2012-07-24 | ||
0.9.0 / 2012-07-24 | ||
================== | ||
@@ -30,0 +41,0 @@ |
@@ -6,11 +6,8 @@ var _ = require('underscore'), | ||
encode = utils.encode, | ||
rspace = /\s+/; | ||
rspace = /\s+/, | ||
/** | ||
* Attributes that are booleans | ||
*/ | ||
// Attributes that are booleans | ||
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i; | ||
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i; | ||
function setAttr(el, name, value) { | ||
@@ -29,4 +26,3 @@ if(typeof name === 'object') return _.extend(el.attribs, name); | ||
var attr = exports.attr = function(name, value) { | ||
var len = this.length, | ||
elem = this[0]; | ||
var elem = this[0]; | ||
@@ -153,4 +149,4 @@ if (!elem || !isTag(elem)) | ||
function(name) { return _.contains(classes, name); } | ||
).join(''); | ||
).join(' '); | ||
}); | ||
}; |
@@ -136,6 +136,2 @@ var _ = require('underscore'), | ||
var tidy = exports.tidy = function() { | ||
return $.tidy(this[0].children); | ||
}; | ||
/** | ||
@@ -160,2 +156,6 @@ * Set/Get the HTML | ||
var toString = exports.toString = function() { | ||
return $.html(this); | ||
} | ||
var text = exports.text = function(str) { | ||
@@ -162,0 +162,0 @@ // If `str` blank or an object |
@@ -73,12 +73,5 @@ var _ = require('underscore'), | ||
var each = exports.each = function(fn) { | ||
var len = this.length, | ||
el, | ||
$; | ||
for(var i = 0; i < len; i++) { | ||
el = this[i]; | ||
$ = this.make(el); | ||
fn.call($, i, el); | ||
} | ||
_.each(this, function(el, i) { | ||
fn.call(this.make(el), i, el); | ||
}, this); | ||
return this; | ||
@@ -88,22 +81,21 @@ }; | ||
var map = exports.map = function(fn) { | ||
var len = this.length, | ||
ret = [], | ||
el, | ||
$; | ||
return _.map(this, function(el, i) { | ||
return fn.call(this.make(el), i, el); | ||
}, this); | ||
}; | ||
for(var i = 0; i < len; i++) { | ||
el = this[i]; | ||
$ = this.make(el); | ||
ret[ret.length] = fn.call($, i, el); | ||
} | ||
return ret; | ||
var filter = exports.filter = function(match) { | ||
return this.make(_.filter(this, _.isString(match) ? | ||
function(el) { return select(match, el).length; } | ||
: function(el, i) { return match.call(this.make(el), i, el); } | ||
, this | ||
)); | ||
}; | ||
var first = exports.first = function() { | ||
return this[0] ? this.make(this[0]) : this; | ||
return this[0] ? this.make(this[0]) : this; | ||
}; | ||
var last = exports.last = function() { | ||
return this[0] ? this.make(this[this.length - 1]) : this; | ||
return this[0] ? this.make(this[this.length - 1]) : this; | ||
}; | ||
@@ -110,0 +102,0 @@ |
@@ -7,6 +7,2 @@ /* | ||
// Options affecting rendering | ||
// var xmlMode = $.fn.options.xmlMode, | ||
// ignoreWhitespace = $.fn.options.ignoreWhitespace; | ||
/* | ||
@@ -72,5 +68,2 @@ Boolean Attributes | ||
// keeps depth for pretty parsing | ||
var depth = 0; | ||
render = module.exports = function(dom, opts) { | ||
@@ -81,5 +74,3 @@ dom = (isArray(dom) || dom.cheerio) ? dom : [dom]; | ||
var output = [], | ||
tidy = !!opts.tidy; | ||
var xmlMode = opts.xmlMode || false, | ||
xmlMode = opts.xmlMode || false, | ||
ignoreWhitespace = opts.ignoreWhitespace || false; | ||
@@ -99,20 +90,13 @@ | ||
var spacing = ''; | ||
if (tidy) { | ||
spacing = Array(depth + 1).join(' '); | ||
output.push(spacing + pushVal + '\n'); | ||
} else { | ||
output.push(pushVal); | ||
} | ||
// Push rendered DOM node | ||
output.push(pushVal); | ||
depth++; | ||
if (elem.children) | ||
output.push(render(elem.children, { tidy: tidy })); | ||
depth--; | ||
output.push(render(elem.children, opts)); | ||
if ((!singleTag[elem.name] || xmlMode) && tagType[elem.type]) | ||
output.push(spacing + '</' + elem.name + '>'); | ||
output.push('</' + elem.name + '>'); | ||
}); | ||
return output.join(tidy ? '\n' : ''); | ||
return output.join(''); | ||
}; | ||
@@ -119,0 +103,0 @@ |
@@ -29,3 +29,3 @@ /** | ||
exports.encode = function(str) { return entities.encode(str, 0); }; | ||
exports.encode = function(str) { return entities.encode(String(str), 0); }; | ||
exports.decode = function(str) { return entities.decode(str, 2); }; |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
"version": "0.10.1", | ||
"version": "0.10.2", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -205,3 +205,3 @@ # cheerio [![Build Status](https://secure.travis-ci.org/MatthewMueller/cheerio.png?branch=master)](http://travis-ci.org/MatthewMueller/cheerio) | ||
```js | ||
$('#fruits').find('li').size() | ||
$('#fruits').find('li').length | ||
//=> 3 | ||
@@ -278,2 +278,23 @@ ``` | ||
#### .filter( selector ) <br /> .filter( function(index) ) | ||
Iterates over a cheerio object, reducing the set of selector elements to those that match the selector or pass the function's test. If using the function method, the function is executed in the context of the selected element, so `this` refers to the current element. | ||
Selector: | ||
```js | ||
$('li').filter('.orange').attr('class'); | ||
//=> orange | ||
``` | ||
Function: | ||
```js | ||
$('li').filter(function(i, el) { | ||
// this === el | ||
return $(this).attr('class') === 'orange'; | ||
}).attr('class') | ||
//=> orange | ||
``` | ||
#### .first() | ||
@@ -280,0 +301,0 @@ Will select the first element of a cheerio object |
@@ -55,2 +55,9 @@ var expect = require('expect.js'); | ||
}); | ||
it('(key, value) : should coerce values to a string', function() { | ||
var $apple = $('.apple', fruits); | ||
$apple.attr('data-test', 1); | ||
expect($apple[0].attribs['data-test']).to.equal('1'); | ||
expect($apple.attr('data-test')).to.equal('1'); | ||
}); | ||
}); | ||
@@ -172,2 +179,15 @@ | ||
it('(single class) : should remove a single class from multiple classes on the element', function() { | ||
var $fruits = $(fruits); | ||
$('.pear', $fruits).addClass('fruit green tasty'); | ||
expect($('.pear', $fruits).hasClass('fruit')).to.be.ok(); | ||
expect($('.pear', $fruits).hasClass('green')).to.be.ok(); | ||
expect($('.pear', $fruits).hasClass('tasty')).to.be.ok(); | ||
$('.pear', $fruits).removeClass('green'); | ||
expect($('.pear', $fruits).hasClass('fruit')).to.be.ok(); | ||
expect($('.pear', $fruits).hasClass('green')).to.not.be.ok(); | ||
expect($('.pear', $fruits).hasClass('tasty')).to.be.ok(); | ||
}); | ||
it('(class class class) : should remove multiple classes from the element', function() { | ||
@@ -174,0 +194,0 @@ var $fruits = $(fruits); |
@@ -246,2 +246,19 @@ var expect = require('expect.js'), | ||
}); | ||
describe('.toString', function() { | ||
it('() : should get the outerHTML for an element', function() { | ||
var $fruits = $(fruits); | ||
expect($fruits.toString()).to.equal(fruits); | ||
}); | ||
it('() : should return an html string for a set of elements', function() { | ||
var $fruits = $(fruits); | ||
expect($fruits.find('li').toString()).to.equal('<li class="apple">Apple</li><li class="orange">Orange</li><li class="pear">Pear</li>'); | ||
}); | ||
it('() : should be called implicitly', function() { | ||
var string = [$("<foo>"), $("<bar>"), $("<baz>")].join(""); | ||
expect(string).to.equal('<foo></foo><bar></bar><baz></baz>'); | ||
}); | ||
}); | ||
@@ -248,0 +265,0 @@ describe('.text', function() { |
@@ -114,3 +114,21 @@ var expect = require('expect.js'), | ||
}); | ||
describe('.filter', function() { | ||
it('(selector) : should reduce the set of matched elements to those that match the selector', function() { | ||
var pear = $('li', fruits).filter('.pear').text(); | ||
expect(pear).to.be('Pear'); | ||
}); | ||
it('(fn) : should reduce the set of matched elements to those that pass the function\'s test', function() { | ||
var orange = $('li', fruits).filter(function(i, el) { | ||
expect(this[0]).to.be(el); | ||
expect(el.name).to.be('li'); | ||
expect(i).to.be.a('number'); | ||
return this.attr('class') === 'orange'; | ||
}).text(); | ||
expect(orange).to.be('Orange'); | ||
}); | ||
}); | ||
describe('.first', function() { | ||
@@ -117,0 +135,0 @@ |
91331
1859
621