Comparing version 0.10.5 to 0.10.6
@@ -0,1 +1,10 @@ | ||
0.10.6 / 2013-01-29 | ||
================== | ||
* Added `$.contains(...)` (jugglinmike) | ||
* formatting cleanup (davidchambers) | ||
* Bug fix for `.children()` (jugglinmike & davidchambers) | ||
* Remove global `render` bug (wvl) | ||
0.10.5 / 2012-12-18 | ||
@@ -2,0 +11,0 @@ =================== |
@@ -12,6 +12,6 @@ var _ = require('underscore'), | ||
function setAttr(el, name, value) { | ||
if(typeof name === 'object') return _.extend(el.attribs, name); | ||
var setAttr = function(el, name, value) { | ||
if (typeof name === 'object') return _.extend(el.attribs, name); | ||
if(value === null) { | ||
if (value === null) { | ||
removeAttribute(el, name); | ||
@@ -23,3 +23,3 @@ } else { | ||
return el.attribs; | ||
} | ||
}; | ||
@@ -38,3 +38,3 @@ var attr = exports.attr = function(name, value) { | ||
if (!name) { | ||
for(var a in elem.attribs) { | ||
for (var a in elem.attribs) { | ||
elem.attribs[a] = decode(elem.attribs[a]); | ||
@@ -46,3 +46,3 @@ } | ||
// Set the value (with attr map support) | ||
if(typeof name === 'object' || value !== undefined) { | ||
if (typeof name === 'object' || value !== undefined) { | ||
this.each(function(i, el) { | ||
@@ -52,3 +52,3 @@ el.attribs = setAttr(el, name, value); | ||
return this; | ||
} else if(Object.hasOwnProperty.call(elem.attribs, name)) { | ||
} else if (Object.hasOwnProperty.call(elem.attribs, name)) { | ||
// Get the (decoded) attribute | ||
@@ -63,3 +63,3 @@ return decode(elem.attribs[name]); | ||
function removeAttribute(elem, name) { | ||
var removeAttribute = function(elem, name) { | ||
if (!isTag(elem.type) || !elem.attribs || !Object.hasOwnProperty.call(elem.attribs, name)) | ||
@@ -72,3 +72,3 @@ return; | ||
delete elem.attribs[name]; | ||
} | ||
}; | ||
@@ -136,8 +136,8 @@ | ||
var removeClass = exports.removeClass = function(value) { | ||
var split = function(className) { | ||
return className ? className.trim().split(rspace) : []; | ||
}; | ||
var classes = split(value); | ||
function split(className) { | ||
return !className ? [] : className.trim().split(rspace); | ||
} | ||
// Handle if value is a function | ||
@@ -151,3 +151,3 @@ if (_.isFunction(value)) { | ||
return this.each(function(i, el) { | ||
if(!isTag(el)) return; | ||
if (!isTag(el)) return; | ||
el.attribs['class'] = (!value) ? '' : _.reject( | ||
@@ -154,0 +154,0 @@ split(el.attribs['class']), |
@@ -157,3 +157,3 @@ var _ = require('underscore'), | ||
return $.html(this); | ||
} | ||
}; | ||
@@ -172,8 +172,8 @@ var text = exports.text = function(str) { | ||
var elem = { | ||
data : encode(str), | ||
type : 'text', | ||
parent : null, | ||
prev : null, | ||
next : null, | ||
children : [] | ||
data: encode(str), | ||
type: 'text', | ||
parent: null, | ||
prev: null, | ||
next: null, | ||
children: [] | ||
}; | ||
@@ -180,0 +180,0 @@ |
@@ -60,18 +60,24 @@ var _ = require('underscore'), | ||
var children = exports.children = function(selector) { | ||
if (!this[0] || !this[0].children) return this; | ||
var children = _.filter(this[0].children, function(elem) { | ||
return (isTag(elem)); | ||
}); | ||
var elems = _.reduce(this, function(memo, elem) { | ||
return memo.concat(_.filter(elem.children, isTag)); | ||
}, []); | ||
if (selector === undefined) return this.make(children); | ||
else if (_.isNumber(selector)) return this.make(children[selector]); | ||
if (selector === undefined) return this.make(elems); | ||
else if (_.isNumber(selector)) return this.make(elems[selector]); | ||
return this.make(children).filter(selector); | ||
return this.make(elems).filter(selector); | ||
}; | ||
var each = exports.each = function(fn) { | ||
_.each(this, function(el, i) { | ||
fn.call(this.make(el), i, el); | ||
}, this); | ||
var length = this.length, | ||
el, i; | ||
for (i = 0; i < length; ++i) { | ||
el = this[i]; | ||
if (fn.call(this.make(el), i, el) === false) { | ||
break; | ||
} | ||
} | ||
return this; | ||
@@ -78,0 +84,0 @@ }; |
@@ -36,9 +36,9 @@ /* | ||
var Cheerio = module.exports = function(selector, context, root) { | ||
if(!(this instanceof Cheerio)) return new Cheerio(selector, context, root); | ||
if (!(this instanceof Cheerio)) return new Cheerio(selector, context, root); | ||
// $(), $(null), $(undefined), $(false) | ||
if(!selector) return this; | ||
if (!selector) return this; | ||
if(root) { | ||
if(typeof root === 'string') root = parse(root); | ||
if (root) { | ||
if (typeof root === 'string') root = parse(root); | ||
this._root = this.make(root, this); | ||
@@ -48,10 +48,10 @@ } | ||
// $($) | ||
if(selector.cheerio) return selector; | ||
if (selector.cheerio) return selector; | ||
// $(dom) | ||
if(selector.name || isArray(selector)) | ||
if (selector.name || isArray(selector)) | ||
return this.make(selector, this); | ||
// $(<html>) | ||
if(typeof selector === 'string' && isHtml(selector)) { | ||
if (typeof selector === 'string' && isHtml(selector)) { | ||
return this.make(parse(selector).children); | ||
@@ -61,6 +61,6 @@ } | ||
// If we don't have a context, maybe we have a root, from loading | ||
if(!context) { | ||
if (!context) { | ||
context = this._root; | ||
} else if(typeof context === 'string') { | ||
if(isHtml(context)) { | ||
} else if (typeof context === 'string') { | ||
if (isHtml(context)) { | ||
// $('li', '<ul>...</ul>') | ||
@@ -77,3 +77,3 @@ context = parse(context); | ||
// If we still don't have a context, return | ||
if(!context) return this; | ||
if (!context) return this; | ||
@@ -101,5 +101,5 @@ // #id, .class, tag | ||
Cheerio.prototype.options = { | ||
ignoreWhitespace : false, | ||
xmlMode : false, | ||
lowerCaseTags : false | ||
ignoreWhitespace: false, | ||
xmlMode: false, | ||
lowerCaseTags: false | ||
}; | ||
@@ -117,5 +117,5 @@ | ||
*/ | ||
function isHtml(str) { | ||
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; | ||
if (str.charAt(0) === '<' && str.charAt(str.length - 1) === '>' && str.length >= 3) return true; | ||
@@ -125,3 +125,3 @@ // Run the regex | ||
return !!(match && match[1]); | ||
} | ||
}; | ||
@@ -133,5 +133,5 @@ /* | ||
Cheerio.prototype.make = function(dom, context) { | ||
if(dom.cheerio) return dom; | ||
if (dom.cheerio) return dom; | ||
dom = (_.isArray(dom)) ? dom : [dom]; | ||
return _.extend(context || new Cheerio(), dom, { length : dom.length }); | ||
return _.extend(context || new Cheerio(), dom, { length: dom.length }); | ||
}; | ||
@@ -138,0 +138,0 @@ |
@@ -17,8 +17,8 @@ /* | ||
var root = { | ||
type : 'root', | ||
name : 'root', | ||
parent : null, | ||
prev : null, | ||
next : null, | ||
children : [] | ||
type: 'root', | ||
name: 'root', | ||
parent: null, | ||
prev: null, | ||
next: null, | ||
children: [] | ||
}; | ||
@@ -88,4 +88,4 @@ | ||
for (var i = 0; i < arr.length; i++) { | ||
arr[i].prev = arr[i-1] || null; | ||
arr[i].next = arr[i+1] || null; | ||
arr[i].prev = arr[i - 1] || null; | ||
arr[i].next = arr[i + 1] || null; | ||
arr[i].parent = parent || null; | ||
@@ -92,0 +92,0 @@ } |
@@ -60,10 +60,10 @@ /* | ||
var tagType = { | ||
tag : 1, | ||
script : 1, | ||
link : 1, | ||
style : 1, | ||
template : 1 | ||
tag: 1, | ||
script: 1, | ||
link: 1, | ||
style: 1, | ||
template: 1 | ||
}; | ||
render = module.exports = function(dom, opts) { | ||
var render = module.exports = function(dom, opts) { | ||
dom = (isArray(dom) || dom.cheerio) ? dom : [dom]; | ||
@@ -70,0 +70,0 @@ opts = opts || {}; |
@@ -18,5 +18,5 @@ /** | ||
function initialize(selector, context, r) { | ||
var initialize = function(selector, context, r) { | ||
return new Cheerio(selector, context, r || root); | ||
} | ||
}; | ||
@@ -75,1 +75,23 @@ // Add in the static methods | ||
}; | ||
/** | ||
* $.contains() | ||
*/ | ||
var 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 descendents, 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; | ||
}; |
@@ -10,3 +10,3 @@ /** | ||
var tags = { tag : true, script : true, style : true }; | ||
var tags = { tag: true, script: true, style: true }; | ||
@@ -13,0 +13,0 @@ /** |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
"version": "0.10.5", | ||
"version": "0.10.6", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -249,3 +249,3 @@ # cheerio [![Build Status](https://secure.travis-ci.org/MatthewMueller/cheerio.png?branch=master)](http://travis-ci.org/MatthewMueller/cheerio) | ||
#### .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, which is equivalent to the function parameter `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`. To break out of the `each` loop early, return with `false`. | ||
@@ -274,3 +274,3 @@ ```js | ||
#### .filter( selector ) <br /> .filter( function(index) ) | ||
#### .filter( selector ) <br /> .filter( function(index) ) | ||
@@ -512,2 +512,5 @@ 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. | ||
#### $.contains( container, contained ) | ||
Checks to see if the `contained` DOM element is a descendent of the `container` DOM element. | ||
#### $.isArray( array ) | ||
@@ -560,21 +563,23 @@ Checks to see the passed argument is an array. | ||
project : cheerio | ||
repo age : 12 months ago | ||
commits : 369 | ||
active : 99 days | ||
repo age : 1 year, 4 months ago | ||
commits : 416 | ||
active : 118 days | ||
files : 26 | ||
authors : | ||
245 Matt Mueller 66.4% | ||
68 Matthew Mueller 18.4% | ||
24 David Chambers 6.5% | ||
15 Siddharth Mahendraker 4.1% | ||
4 ironchefpython 1.1% | ||
3 Jos Shepherd 0.8% | ||
authors : | ||
278 Matt Mueller 66.8% | ||
68 Matthew Mueller 16.3% | ||
27 David Chambers 6.5% | ||
15 Siddharth Mahendraker 3.6% | ||
7 ironchefpython 1.7% | ||
5 Jos Shepherd 1.2% | ||
5 Ben Sheldon 1.2% | ||
2 alexbardas 0.5% | ||
2 Rob Ashton 0.5% | ||
1 mattym 0.3% | ||
1 Chris O'Hara 0.3% | ||
1 Rob "Hurricane" Ashton 0.3% | ||
1 Sindre Sorhus 0.3% | ||
1 Wayne Larsen 0.3% | ||
1 Ben Atkin 0.3% | ||
1 mattym 0.2% | ||
1 Chris O'Hara 0.2% | ||
1 Mike Pennisi 0.2% | ||
1 Rob "Hurricane" Ashton 0.2% | ||
1 Sindre Sorhus 0.2% | ||
1 Wayne Larsen 0.2% | ||
1 Ben Atkin 0.2% | ||
``` | ||
@@ -581,0 +586,0 @@ |
var expect = require('expect.js'), | ||
$ = require('../'), | ||
food = require('./fixtures').food, | ||
fruits = require('./fixtures').fruits; | ||
@@ -43,2 +44,6 @@ | ||
it('() : should return children of all matched elements', function() { | ||
expect($('ul ul', food).children()).to.have.length(5); | ||
}); | ||
it('(selector) : should return children matching selector', function() { | ||
@@ -105,2 +110,13 @@ var cls = $('ul', fruits).children('.orange')[0].attribs['class']; | ||
it('( (i, elem) -> ) : should break iteration when the iterator function returns false', function() { | ||
var iterationCount = 0; | ||
$('li', fruits).each(function(idx, elem) { | ||
iterationCount++; | ||
return idx < 1; | ||
}); | ||
expect(iterationCount).to.equal(2); | ||
}); | ||
}); | ||
@@ -107,0 +123,0 @@ |
@@ -15,1 +15,8 @@ exports.fruits = [ | ||
].join(''); | ||
exports.food = [ | ||
'<ul id="food">', | ||
exports.fruits, | ||
exports.vegetables, | ||
'</ul>' | ||
].join(''); |
94796
26
1932
620