cheerio
Advanced tools
Comparing version 0.12.3 to 0.12.4
@@ -37,1 +37,12 @@ # Contributing to Cheerio | ||
- Terminate every statement with a semicolon | ||
- Private functionality (for re-using functionality that isn't part of the | ||
jQuery API) | ||
- *Static methods*: If the functionality does not require a reference to a | ||
Cheerio instance, simply define a named function within the module it is | ||
needed. | ||
- *Instance methods*: If the functionality requires a reference to a Cheerio | ||
instance, informally define the method as "private" using the following | ||
conventions: | ||
- Define the method as a function on the Cheerio prototype | ||
- Prefix the method name with an underscore (`_`) character | ||
- Include `@api private` in the code comment the documents the method |
0.12.3 / 2013-10-04 | ||
0.12.4 / 2013-11-12 | ||
================== | ||
* Coerce JSON values returned by `data` (@jugglinmike) | ||
* issue #284: when rendering HTML, use original data attributes (@Trott) | ||
* Introduce JSHint for automated code linting (@jugglinmike) | ||
* Prevent `find` from returning duplicate elements (@jugglinmike) | ||
* Implement function signature of `replaceWith` (@jugglinmike) | ||
* Implement function signature of `before` (@jugglinmike) | ||
* Implement function signature of `after` (@jugglinmike) | ||
* Implement function signature of `append`/`prepend` (@jugglinmike) | ||
* Extend iteration methods to accept nodes (@jugglinmike) | ||
* Improve `removeClass` (@jugglinmike) | ||
* Complete function signature of `addClass` (@jugglinmike) | ||
* Fix bug in `removeClass` (@jugglinmike) | ||
* Improve contributing.md (@jugglinmike) | ||
* Fix and document .css() (@jugglinmike) | ||
0.12.3 / 2013-10-04 | ||
=================== | ||
@@ -13,3 +31,3 @@ | ||
0.12.2 / 2013-09-04 | ||
0.12.2 / 2013-09-04 | ||
================== | ||
@@ -16,0 +34,0 @@ |
var _ = require('underscore'), | ||
utils = require('../utils'), | ||
isTag = utils.isTag, | ||
decode = utils.decode, | ||
encode = utils.encode, | ||
rspace = /\s+/, | ||
utils = require('../utils'), | ||
isTag = utils.isTag, | ||
decode = utils.decode, | ||
encode = utils.encode, | ||
hasOwn = Object.prototype.hasOwnProperty, | ||
rspace = /\s+/, | ||
// Attributes that are booleans | ||
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i; | ||
// Lookup table for coercing string data-* attributes to their corresponding | ||
// JavaScript primitives | ||
primitives = { | ||
null: null, | ||
true: true, | ||
false: false | ||
}, | ||
// Attributes that are booleans | ||
rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i, | ||
// Matches strings that look like JSON objects or arrays | ||
rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/; | ||
var setAttr = function(el, name, value) { | ||
@@ -31,3 +42,3 @@ if (typeof name === 'object') return _.extend(el.attribs, name); | ||
}); | ||
} | ||
} | ||
return this.each(function(i, el) { | ||
@@ -54,3 +65,3 @@ el.attribs = setAttr(el, name, value); | ||
if (Object.prototype.hasOwnProperty.call(elem.attribs, name)) { | ||
if (hasOwn.call(elem.attribs, name)) { | ||
// Get the (decoded) attribute | ||
@@ -100,5 +111,15 @@ return decode(elem.attribs[name]); | ||
return this; | ||
} else if (Object.hasOwnProperty.call(elem.data, name)) { | ||
} else if (hasOwn.call(elem.data, name)) { | ||
// Get the (decoded) data | ||
return decode(elem.data[name]); | ||
var val = decode(elem.data[name]); | ||
if (hasOwn.call(primitives, val)) { | ||
val = primitives[val]; | ||
} else if (val === String(Number(val))) { | ||
val = Number(val); | ||
} else if (rbrace.test(val)) { | ||
val = JSON.parse(val); | ||
} | ||
return val; | ||
} else if (typeof name === 'string' && value === undefined) { | ||
@@ -151,3 +172,4 @@ return undefined; | ||
}); | ||
} | ||
} | ||
return; | ||
case 'select': | ||
@@ -180,4 +202,4 @@ var option = this.find('option:selected'), | ||
if (!querying) { | ||
this.attr('value', value); | ||
return this; | ||
this.attr('value', value); | ||
return this; | ||
} | ||
@@ -193,9 +215,9 @@ return this.attr('value'); | ||
var removeAttribute = function(elem, name) { | ||
if (!isTag(elem.type) || !elem.attribs || !Object.hasOwnProperty.call(elem.attribs, name)) | ||
return; | ||
if (!isTag(elem.type) || !elem.attribs || !Object.hasOwnProperty.call(elem.attribs, name)) | ||
return; | ||
if (rboolean.test(elem.attribs[name])) | ||
elem.attribs[name] = false; | ||
else | ||
delete elem.attribs[name]; | ||
if (rboolean.test(elem.attribs[name])) | ||
elem.attribs[name] = false; | ||
else | ||
delete elem.attribs[name]; | ||
}; | ||
@@ -224,3 +246,3 @@ | ||
var className = this.attr('class') || ''; | ||
this.addClass(value.call(this, i, className)); | ||
this.addClass(value.call(this[0], i, className)); | ||
}); | ||
@@ -268,18 +290,19 @@ } | ||
}; | ||
var classes, removeAll; | ||
var classes = split(value); | ||
// Handle if value is a function | ||
if (_.isFunction(value)) { | ||
return this.each(function(i, el) { | ||
this.removeClass(value.call(this, i, el.attribs['class'] || '')); | ||
this.removeClass(value.call(this[0], i, el.attribs['class'] || '')); | ||
}); | ||
} | ||
classes = split(value); | ||
removeAll = arguments.length === 0; | ||
return this.each(function(i, el) { | ||
if (!isTag(el)) return; | ||
el.attribs['class'] = (!value) ? '' : _.reject( | ||
split(el.attribs['class']), | ||
function(name) { return _.contains(classes, name); } | ||
).join(' '); | ||
el.attribs.class = removeAll ? | ||
'' : | ||
_.difference(split(el.attribs.class), classes).join(' '); | ||
}); | ||
@@ -291,5 +314,5 @@ }; | ||
if (_.isFunction(value)) { | ||
return this.each(function(i, el) { | ||
this.toggleClass(value.call(this, i, el.attribs['class'] || '', stateVal), stateVal); | ||
}); | ||
return this.each(function(i, el) { | ||
this.toggleClass(value.call(this, i, el.attribs['class'] || '', stateVal), stateVal); | ||
}); | ||
} | ||
@@ -301,19 +324,19 @@ | ||
var classNames = value.split(rspace), | ||
numClasses = classNames.length, | ||
isBool = typeof stateVal === 'boolean', | ||
numElements = this.length, | ||
$elem, | ||
state; | ||
numClasses = classNames.length, | ||
isBool = typeof stateVal === 'boolean', | ||
numElements = this.length, | ||
$elem, | ||
state; | ||
for (var i = 0; i < numElements; i++) { | ||
$elem = this.make(this[i]); | ||
// If selected element isnt a tag, move on | ||
if (!isTag(this[i])) continue; | ||
$elem = this.make(this[i]); | ||
// If selected element isnt a tag, move on | ||
if (!isTag(this[i])) continue; | ||
// Check if class already exists | ||
for (var j = 0; j < numClasses; j++) { | ||
// check each className given, space separated list | ||
state = isBool ? stateVal : !$elem.hasClass(classNames[j]); | ||
$elem[state ? 'addClass' : 'removeClass'](classNames[j]); | ||
} | ||
// Check if class already exists | ||
for (var j = 0; j < numClasses; j++) { | ||
// check each className given, space separated list | ||
state = isBool ? stateVal : !$elem.hasClass(classNames[j]); | ||
$elem[state ? 'addClass' : 'removeClass'](classNames[j]); | ||
} | ||
} | ||
@@ -329,3 +352,3 @@ | ||
return false; | ||
} | ||
}; | ||
@@ -0,1 +1,3 @@ | ||
var _ = require('underscore'); | ||
var toString = Object.prototype.toString; | ||
@@ -11,11 +13,11 @@ /** | ||
exports.css = function(prop, val){ | ||
if (undefined == prop) return get(this); | ||
switch (arguments.length) { | ||
case 2: return set(this, prop, val); | ||
case 0: return get(this); | ||
case 1: return 'object' == typeof prop | ||
? set(this, prop) | ||
: get(this, prop); | ||
exports.css = function(prop, val) { | ||
if (arguments.length === 2 || | ||
// When `prop` is a "plain" object | ||
(toString.call(prop) === '[object Object]')) { | ||
return this.each(function(idx) { | ||
this._setCss(prop, val, idx); | ||
}); | ||
} else { | ||
return this._getCss(prop); | ||
} | ||
@@ -27,5 +29,5 @@ }; | ||
* | ||
* @param {Cheerio} self | ||
* @param {String|Object} prop | ||
* @param {String} val | ||
* @param {Number} idx - optional index within the selection | ||
* @return {self} | ||
@@ -35,14 +37,23 @@ * @api private | ||
function set(self, prop, val){ | ||
exports._setCss = function(prop, val, idx) { | ||
if ('string' == typeof prop) { | ||
var styles = get(self); | ||
styles[prop] = val; | ||
return self.attr('style', stringify(styles)); | ||
var styles = this._getCss(); | ||
if (_.isFunction(val)) { | ||
val = val.call(this[0], idx, this[0]); | ||
} | ||
if (val === '') { | ||
delete styles[prop]; | ||
} else if (val != null) { | ||
styles[prop] = val; | ||
} | ||
return this.attr('style', stringify(styles)); | ||
} else if ('object' == typeof prop) { | ||
Object.keys(prop).forEach(function(k){ | ||
set(self, k, prop[k]); | ||
}); | ||
return self; | ||
this._setCss(k, prop[k]); | ||
}, this); | ||
return this; | ||
} | ||
} | ||
}; | ||
@@ -52,3 +63,2 @@ /** | ||
* | ||
* @param {Cheerio} self | ||
* @param {String} prop | ||
@@ -59,9 +69,12 @@ * @return {Object} | ||
function get(self, prop){ | ||
var styles = parse(self.attr('style')); | ||
if (undefined == styles) throw new Error('undefined'); | ||
return 2 == arguments.length | ||
? styles[prop] | ||
: styles; | ||
} | ||
exports._getCss = function(prop) { | ||
var styles = parse(this.attr('style')); | ||
if (typeof prop === 'string') { | ||
return styles[prop]; | ||
} else if (_.isArray(prop)) { | ||
return _.pick(styles, prop); | ||
} else { | ||
return styles; | ||
} | ||
}; | ||
@@ -76,3 +89,3 @@ /** | ||
function stringify(obj){ | ||
function stringify(obj) { | ||
return Object.keys(obj || {}) | ||
@@ -97,6 +110,6 @@ .reduce(function(str, prop){ | ||
function parse(styles){ | ||
function parse(styles) { | ||
styles = (styles || '').trim(); | ||
if ('' == styles) return {}; | ||
if (!styles) return {}; | ||
@@ -107,3 +120,3 @@ return styles | ||
var parts = str.split(/\s*:\s*/); | ||
if ('' == parts[0]) return obj; | ||
if (!parts[0]) return obj; | ||
obj[parts[0]] = parts[1]; | ||
@@ -110,0 +123,0 @@ return obj; |
@@ -9,16 +9,16 @@ var _ = require('underscore'), | ||
/* | ||
Creates an array of cheerio objects, | ||
parsing strings if necessary | ||
*/ | ||
var makeCheerioArray = function(elems) { | ||
return _.chain(elems).map(function(elem) { | ||
if (elem.cheerio) { | ||
return elem.toArray(); | ||
} else if (!_.isArray(elem)) { | ||
return evaluate(elem); | ||
} else { | ||
return elem; | ||
} | ||
}).flatten().value(); | ||
// Create an array of nodes, recursing into arrays and parsing strings if | ||
// necessary | ||
var makeDomArray = function(elem) { | ||
if (elem == null) { | ||
return []; | ||
} else if (elem.cheerio) { | ||
return elem.toArray(); | ||
} else if (_.isArray(elem)) { | ||
return _.flatten(elem.map(makeDomArray)); | ||
} else if (_.isString(elem)) { | ||
return evaluate(elem); | ||
} else { | ||
return [elem]; | ||
} | ||
}; | ||
@@ -29,6 +29,8 @@ | ||
var elems = slice.call(arguments), | ||
dom = makeCheerioArray(elems); | ||
dom = makeDomArray(elems); | ||
return this.each(function(i, el) { | ||
if (_.isFunction(elems[0])) return el; // not yet supported | ||
if (_.isFunction(elems[0])) { | ||
dom = makeDomArray(elems[0].call(el, i, this.html())); | ||
} | ||
updateDOM(concatenator(dom, el.children || (el.children = [])), el); | ||
@@ -49,3 +51,3 @@ }); | ||
var elems = slice.call(arguments), | ||
dom = makeCheerioArray(elems); | ||
dom = makeDomArray(elems); | ||
@@ -59,2 +61,6 @@ this.each(function(i, el) { | ||
if (_.isFunction(elems[0])) { | ||
dom = makeDomArray(elems[0].call(el, i)); | ||
} | ||
// Add element after `this` element | ||
@@ -74,3 +80,3 @@ siblings.splice.apply(siblings, [++index, 0].concat(dom)); | ||
var elems = slice.call(arguments), | ||
dom = makeCheerioArray(elems); | ||
dom = makeDomArray(elems); | ||
@@ -84,2 +90,6 @@ this.each(function(i, el) { | ||
if (_.isFunction(elems[0])) { | ||
dom = makeDomArray(elems[0].call(el, i)); | ||
} | ||
// Add element before `el` element | ||
@@ -124,3 +134,3 @@ siblings.splice.apply(siblings, [index, 0].concat(dom)); | ||
var replaceWith = exports.replaceWith = function(content) { | ||
content = makeCheerioArray([content]); | ||
var dom = makeDomArray(content); | ||
@@ -133,4 +143,8 @@ this.each(function(i, el) { | ||
siblings.splice.apply(siblings, [index, 1].concat(content)); | ||
if (_.isFunction(content)) { | ||
dom = makeDomArray(content.call(el, i)); | ||
} | ||
siblings.splice.apply(siblings, [index, 1].concat(dom)); | ||
updateDOM(siblings, el.parent); | ||
@@ -137,0 +151,0 @@ el.parent.children = siblings; |
@@ -7,3 +7,6 @@ var _ = require('underscore'), | ||
var find = exports.find = function(selector) { | ||
return this.make(select(selector, [].slice.call(this.children()))); | ||
// TODO: Remove the call to _.uniq when the underlying bug in CSSselect has | ||
// been resolved: | ||
// https://github.com/fb55/CSSselect/issues/12 | ||
return this.make(_.uniq(select(selector, [].slice.call(this.children())))); | ||
}; | ||
@@ -24,3 +27,3 @@ | ||
$set = this.make(set) | ||
$set = this.make(set); | ||
@@ -120,3 +123,3 @@ if (arguments.length) { | ||
}, [])); | ||
} | ||
}; | ||
@@ -123,0 +126,0 @@ var each = exports.each = function(fn) { |
@@ -6,3 +6,3 @@ /* | ||
_ = require('underscore'), | ||
isTag = require('./utils').isTag; | ||
isTag = require('./utils').isTag, | ||
camelCase = require('./utils').camelCase; | ||
@@ -9,0 +9,0 @@ |
@@ -26,4 +26,2 @@ /* | ||
for (var key in attributes) { | ||
// Make sure it doesn't format data attributes. | ||
if (key.substr(0, 5) === 'data-') continue; | ||
value = attributes[key]; | ||
@@ -41,13 +39,2 @@ if (!value && (rboolean.test(key) || key === '/')) { | ||
/* | ||
Format data entries | ||
*/ | ||
var formatData = function(data) { | ||
if (!data) return ''; | ||
return Object.keys(data).map(function(key) { | ||
return 'data-' + key + '="' + data[key] + '"'; | ||
}).join(' '); | ||
}; | ||
/* | ||
Self-enclosing tags (stolen from node-htmlparser) | ||
@@ -123,3 +110,3 @@ */ | ||
return (xmlMode && (!elem.children || elem.children.length === 0)); | ||
} | ||
}; | ||
@@ -130,3 +117,2 @@ var renderTag = function(elem, xmlMode) { | ||
if (elem.attribs && _.size(elem.attribs)) { | ||
if (_.size(elem.data)) tag += ' ' + formatData(elem.data); | ||
tag += ' ' + formatAttrs(elem.attribs); | ||
@@ -138,3 +124,3 @@ } | ||
} | ||
return tag + '>'; | ||
@@ -141,0 +127,0 @@ }; |
@@ -5,5 +5,10 @@ { | ||
"description": "Tiny, fast, and elegant implementation of core jQuery designed specifically for the server", | ||
"keywords": ["htmlparser", "jquery", "selector", "scraper"], | ||
"version": "0.12.3", | ||
"repository": { | ||
"keywords": [ | ||
"htmlparser", | ||
"jquery", | ||
"selector", | ||
"scraper" | ||
], | ||
"version": "0.12.4", | ||
"repository": { | ||
"type": "git", | ||
@@ -17,14 +22,15 @@ "url": "git://github.com/MatthewMueller/cheerio.git" | ||
"dependencies": { | ||
"cheerio-select" : "*", | ||
"htmlparser2" : "3.1.4", | ||
"underscore" : "~1.4", | ||
"entities" : "0.x" | ||
"cheerio-select": "*", | ||
"htmlparser2": "3.1.4", | ||
"underscore": "~1.4", | ||
"entities": "0.x" | ||
}, | ||
"devDependencies": { | ||
"mocha" : "*", | ||
"expect.js" : "*" | ||
"mocha": "*", | ||
"expect.js": "*", | ||
"jshint": "~2.3.0" | ||
}, | ||
"scripts": { | ||
"test" : "make test" | ||
"test": "make test" | ||
} | ||
} |
@@ -147,3 +147,3 @@ # cheerio [![Build Status](https://secure.travis-ci.org/MatthewMueller/cheerio.png?branch=master)](http://travis-ci.org/MatthewMueller/cheerio) | ||
#### .data( name, value ) | ||
Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set. | ||
Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set. | ||
@@ -548,2 +548,6 @@ ```js | ||
#### .css( [propertName] ) <br /> .css( [ propertyNames] ) <br /> .css( [propertyName], [value] ) <br /> .css( [propertName], [function] ) <br /> .css( [properties] ) | ||
Get the value of a style property for the first element in the set of matched elements or set one or more CSS properties for every matched element. | ||
### Rendering | ||
@@ -644,36 +648,43 @@ When you're ready to render the document, you can use the `html` utility function: | ||
project : cheerio | ||
repo age : 1 year, 10 months | ||
active : 169 days | ||
commits : 513 | ||
files : 28 | ||
repo age : 2 years, 1 month | ||
active : 196 days | ||
commits : 591 | ||
files : 32 | ||
authors : | ||
292 Matt Mueller 56.9% | ||
86 Matthew Mueller 16.8% | ||
44 David Chambers 8.6% | ||
15 Siddharth Mahendraker 2.9% | ||
13 Mike Pennisi 2.5% | ||
11 Adam Bretz 2.1% | ||
7 ironchefpython 1.4% | ||
5 Ryan Schmukler 1.0% | ||
5 Ben Sheldon 1.0% | ||
5 Jos Shepherd 1.0% | ||
4 Maciej Adwent 0.8% | ||
3 jeremy.dentel@brandingbrand.com 0.6% | ||
2 Felix Böhm 0.4% | ||
2 Ali Farhadi 0.4% | ||
2 Chris Khoo 0.4% | ||
2 Wayne Larsen 0.4% | ||
2 alexbardas 0.4% | ||
2 Rob Ashton 0.4% | ||
1 nevermind 0.2% | ||
1 Felix Böhm 0.2% | ||
1 Jeremy Hubble 0.2% | ||
1 Manuel Alabor 0.2% | ||
1 Matt Liegey 0.2% | ||
1 Ben Atkin 0.2% | ||
1 Rob "Hurricane" Ashton 0.2% | ||
1 Simon Boudrias 0.2% | ||
1 Sindre Sorhus 0.2% | ||
1 mattym 0.2% | ||
1 Chris O'Hara 0.2% | ||
293 Matt Mueller 49.6% | ||
102 Matthew Mueller 17.3% | ||
52 Mike Pennisi 8.8% | ||
47 David Chambers 8.0% | ||
15 Siddharth Mahendraker 2.5% | ||
11 Adam Bretz 1.9% | ||
7 ironchefpython 1.2% | ||
6 Jarno Leppänen 1.0% | ||
5 Ben Sheldon 0.8% | ||
5 Ryan Schmukler 0.8% | ||
5 Jos Shepherd 0.8% | ||
4 Maciej Adwent 0.7% | ||
4 Amir Abu Shareb 0.7% | ||
3 Felix Böhm 0.5% | ||
3 jeremy.dentel@brandingbrand.com 0.5% | ||
3 Andi Neck 0.5% | ||
2 alexbardas 0.3% | ||
2 Ali Farhadi 0.3% | ||
2 Thomas Heymann 0.3% | ||
2 Wayne Larsen 0.3% | ||
2 Rob Ashton 0.3% | ||
2 Chris Khoo 0.3% | ||
1 xiaohwan 0.2% | ||
1 Chris O'Hara 0.2% | ||
1 Felix Böhm 0.2% | ||
1 Jeremy Hubble 0.2% | ||
1 Manuel Alabor 0.2% | ||
1 Matt Liegey 0.2% | ||
1 Ben Atkin 0.2% | ||
1 Rich Trott 0.2% | ||
1 Rob "Hurricane" Ashton 0.2% | ||
1 Simon Boudrias 0.2% | ||
1 Sindre Sorhus 0.2% | ||
1 Timm Preetz 0.2% | ||
1 mattym 0.2% | ||
1 nevermind 0.2% | ||
``` | ||
@@ -680,0 +691,0 @@ |
@@ -146,2 +146,35 @@ var expect = require('expect.js'); | ||
describe('(attr) : data-* attribute type coercion :', function() { | ||
it('boolean', function() { | ||
var $el = $('<div data-bool="true">'); | ||
expect($el.data('bool')).to.be(true); | ||
}); | ||
it('number', function() { | ||
var $el = $('<div data-number="23">'); | ||
expect($el.data('number')).to.be(23); | ||
}); | ||
it('number (scientific notation is not coerced)', function() { | ||
var $el = $('<div data-sci="1E10">'); | ||
expect($el.data('sci')).to.be('1E10'); | ||
}); | ||
it('null', function() { | ||
var $el = $('<div data-null="null">'); | ||
expect($el.data('null')).to.be(null); | ||
}); | ||
it('object', function() { | ||
var $el = $('<div data-obj=\'{ "a": 45 }\'>'); | ||
expect($el.data('obj')).to.eql({ a: 45 }); | ||
}); | ||
it('array', function() { | ||
var $el = $('<div data-array="[1, 2, 3]">'); | ||
expect($el.data('array')).to.eql([1, 2, 3]); | ||
}); | ||
}); | ||
}); | ||
@@ -151,50 +184,50 @@ | ||
describe('.val', function() { | ||
it('.val(): on select should get value', function() { | ||
var val = $('select#one', inputs).val(); | ||
expect(val).to.equal('option_selected'); | ||
}); | ||
it('.val(): on option should get value', function() { | ||
var val = $('select#one option', inputs).eq(0).val(); | ||
expect(val).to.equal('option_not_selected'); | ||
}); | ||
it('.val(): on text input should get value', function() { | ||
var val = $('input[type="text"]', inputs).val(); | ||
expect(val).to.equal('input_text'); | ||
}); | ||
it('.val(): on checked checkbox should get value', function() { | ||
var val = $('input[name="checkbox_on"]', inputs).val(); | ||
expect(val).to.equal('on'); | ||
}); | ||
it('.val(): on unchecked checkbox should get value', function() { | ||
var val = $('input[name="checkbox_off"]', inputs).val(); | ||
expect(val).to.equal('off'); | ||
}); | ||
it('.val(): on radio should get value', function() { | ||
var val = $('input[type="radio"]', inputs).val(); | ||
expect(val).to.equal('on'); | ||
}); | ||
it('.val(): on multiple select should get an array of values', function() { | ||
var val = $('select#multi', inputs).val(); | ||
expect(val).to.have.length(2); | ||
}); | ||
it('.val(value): on input text should set value', function() { | ||
var element = $('input[type="text"]', inputs).val('test'); | ||
expect(element.val()).to.equal('test'); | ||
}); | ||
it('.val(value): on select should set value', function() { | ||
var element = $('select#one', inputs).val('option_not_selected'); | ||
expect(element.val()).to.equal('option_not_selected'); | ||
}); | ||
it('.val(value): on option should set value', function() { | ||
var element = $('select#one option', inputs).eq(0).val('option_changed'); | ||
expect(element.val()).to.equal('option_changed'); | ||
}); | ||
it('.val(value): on radio should set value', function() { | ||
var element = $('input[name="radio"]', inputs).val('off'); | ||
expect(element.val()).to.equal('off'); | ||
}); | ||
it('.val(values): on multiple select should set multiple values', function() { | ||
var element = $('select#multi', inputs).val(['1', '3', '4']); | ||
expect(element.val()).to.have.length(3); | ||
}); | ||
it('.val(): on select should get value', function() { | ||
var val = $('select#one', inputs).val(); | ||
expect(val).to.equal('option_selected'); | ||
}); | ||
it('.val(): on option should get value', function() { | ||
var val = $('select#one option', inputs).eq(0).val(); | ||
expect(val).to.equal('option_not_selected'); | ||
}); | ||
it('.val(): on text input should get value', function() { | ||
var val = $('input[type="text"]', inputs).val(); | ||
expect(val).to.equal('input_text'); | ||
}); | ||
it('.val(): on checked checkbox should get value', function() { | ||
var val = $('input[name="checkbox_on"]', inputs).val(); | ||
expect(val).to.equal('on'); | ||
}); | ||
it('.val(): on unchecked checkbox should get value', function() { | ||
var val = $('input[name="checkbox_off"]', inputs).val(); | ||
expect(val).to.equal('off'); | ||
}); | ||
it('.val(): on radio should get value', function() { | ||
var val = $('input[type="radio"]', inputs).val(); | ||
expect(val).to.equal('on'); | ||
}); | ||
it('.val(): on multiple select should get an array of values', function() { | ||
var val = $('select#multi', inputs).val(); | ||
expect(val).to.have.length(2); | ||
}); | ||
it('.val(value): on input text should set value', function() { | ||
var element = $('input[type="text"]', inputs).val('test'); | ||
expect(element.val()).to.equal('test'); | ||
}); | ||
it('.val(value): on select should set value', function() { | ||
var element = $('select#one', inputs).val('option_not_selected'); | ||
expect(element.val()).to.equal('option_not_selected'); | ||
}); | ||
it('.val(value): on option should set value', function() { | ||
var element = $('select#one option', inputs).eq(0).val('option_changed'); | ||
expect(element.val()).to.equal('option_changed'); | ||
}); | ||
it('.val(value): on radio should set value', function() { | ||
var element = $('input[name="radio"]', inputs).val('off'); | ||
expect(element.val()).to.equal('off'); | ||
}); | ||
it('.val(values): on multiple select should set multiple values', function() { | ||
var element = $('select#multi', inputs).val(['1', '3', '4']); | ||
expect(element.val()).to.have.length(3); | ||
}); | ||
}); | ||
@@ -279,4 +312,30 @@ | ||
it('(fn) : should add classes returned from the function'); | ||
it('(fn) : should add classes returned from the function', function() { | ||
var $fruits = $(fruits).children(); | ||
var args = []; | ||
var thisVals = []; | ||
var toAdd = ['apple red', '', undefined]; | ||
$fruits.addClass(function(idx, currentClass) { | ||
args.push(arguments); | ||
thisVals.push(this); | ||
return toAdd[idx]; | ||
}); | ||
expect(args).to.eql([ | ||
[0, 'apple'], | ||
[1, 'orange'], | ||
[2, 'pear'] | ||
]); | ||
expect(thisVals).to.eql([ | ||
$fruits[0], | ||
$fruits[1], | ||
$fruits[2] | ||
]); | ||
expect($fruits.eq(0).hasClass('apple')).to.be.ok(); | ||
expect($fruits.eq(0).hasClass('red')).to.be.ok(); | ||
expect($fruits.eq(1).hasClass('orange')).to.be.ok(); | ||
expect($fruits.eq(2).hasClass('pear')).to.be.ok(); | ||
}); | ||
}); | ||
@@ -293,2 +352,8 @@ | ||
it('("") : should not modify class list', function() { | ||
var $fruits = $(fruits); | ||
$fruits.children().removeClass(''); | ||
expect($('.apple', $fruits)).to.have.length(1); | ||
}); | ||
it('(invalid class) : should not remove anything', function() { | ||
@@ -352,4 +417,30 @@ var $fruits = $(fruits); | ||
it('(fn) : should remove classes returned from the function'); | ||
it('(fn) : should remove classes returned from the function', function() { | ||
var $fruits = $(fruits).children(); | ||
var args = []; | ||
var thisVals = []; | ||
var toAdd = ['apple red', '', undefined]; | ||
$fruits.removeClass(function(idx, currentClass) { | ||
args.push(arguments); | ||
thisVals.push(this); | ||
return toAdd[idx]; | ||
}); | ||
expect(args).to.eql([ | ||
[0, 'apple'], | ||
[1, 'orange'], | ||
[2, 'pear'] | ||
]); | ||
expect(thisVals).to.eql([ | ||
$fruits[0], | ||
$fruits[1], | ||
$fruits[2] | ||
]); | ||
expect($fruits.eq(0).hasClass('apple')).to.not.be.ok(); | ||
expect($fruits.eq(0).hasClass('red')).to.not.be.ok(); | ||
expect($fruits.eq(1).hasClass('orange')).to.be.ok(); | ||
expect($fruits.eq(2).hasClass('pear')).to.be.ok(); | ||
}); | ||
}); | ||
@@ -356,0 +447,0 @@ |
@@ -1,43 +0,73 @@ | ||
var expect = require('expect.js'); | ||
var $ = require('..'); | ||
describe('$(...)', function(){ | ||
describe('$(...)', function() { | ||
describe('.css', function(){ | ||
it('(): should get all styles as object', function(){ | ||
var el = $('<li style="hai: there; wassup: 0;">'); | ||
expect(el.css()).to.eql({ hai: 'there', wassup: 0 }); | ||
}) | ||
it('(undefined): should retrun all styles as object', function(){ | ||
var el = $('<li style="color: white">'); | ||
expect(el.css()).to.eql({ color: 'white' }); | ||
}) | ||
it('(prop): should return a css property value', function(){ | ||
describe('.css', function() { | ||
it('(prop): should return a css property value', function() { | ||
var el = $('<li style="hai: there">'); | ||
expect(el.css('hai')).to.equal('there'); | ||
}) | ||
}); | ||
it('(prop, val): should set a css property', function(){ | ||
var el = $('<li>'); | ||
el.css('wassup', 0); | ||
expect(el.attr('style')).to.equal('wassup: 0;'); | ||
}) | ||
it('([prop1, prop2]): should return the specified property values as an object', function() { | ||
var el = $('<li style="margin: 1px; padding: 2px; color: blue;">'); | ||
expect(el.css(['margin', 'color'])).to.eql({ margin: '1px', color: 'blue' }); | ||
}); | ||
it('(obj): should set each key and val', function(){ | ||
var el = $('<li>'); | ||
it('(prop, val): should set a css property', function() { | ||
var el = $('<li style="margin: 0;"></li><li></li>'); | ||
el.css('color', 'red'); | ||
expect(el.attr('style')).to.equal('margin: 0; color: red;'); | ||
expect(el.eq(1).attr('style')).to.equal('color: red;'); | ||
}); | ||
it('(prop, ""): should unset a css property', function() { | ||
var el = $('<li style="padding: 1px; margin: 0;">'); | ||
el.css('padding', ''); | ||
expect(el.attr('style')).to.equal('margin: 0;'); | ||
}); | ||
describe('(prop, function):', function() { | ||
beforeEach(function() { | ||
this.$el = $('<div style="margin: 0;"></div><div style="margin: 0;"></div><div style="margin: 0;">'); | ||
}); | ||
it('should iterate over the selection', function() { | ||
var count = 0; | ||
var $el = this.$el; | ||
this.$el.css('margin', function(idx, elem) { | ||
expect(idx).to.equal(count); | ||
expect(elem).to.equal($el[count]); | ||
expect(this).to.equal($el[count]); | ||
count++; | ||
}); | ||
expect(count).to.equal(3); | ||
}); | ||
it('should set each attribute independently', function() { | ||
var values = ['4px', '', undefined]; | ||
this.$el.css('margin', function(idx) { | ||
return values[idx]; | ||
}); | ||
expect(this.$el.eq(0).attr('style')).to.equal('margin: 4px;'); | ||
expect(this.$el.eq(1).attr('style')).to.equal(''); | ||
expect(this.$el.eq(2).attr('style')).to.equal('margin: 0;'); | ||
}); | ||
}); | ||
it('(obj): should set each key and val', function() { | ||
var el = $('<li style="padding: 0;"></li><li></li>'); | ||
el.css({ foo: 0 }); | ||
expect(el.attr('style')).to.equal('foo: 0;'); | ||
}) | ||
expect(el.eq(0).attr('style')).to.equal('padding: 0; foo: 0;'); | ||
expect(el.eq(1).attr('style')).to.equal('foo: 0;'); | ||
}); | ||
describe('parser', function(){ | ||
it('should allow any whitespace between declarations', function(){ | ||
it('should allow any whitespace between declarations', function() { | ||
var el = $('<li style="one \t:\n 0;\n two \f\r:\v 1">'); | ||
expect(el.css()).to.eql({ one: 0, two: 1 }); | ||
}) | ||
}) | ||
}) | ||
expect(el.css(['one', 'two'])).to.eql({ one: 0, two: 1 }); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}); |
@@ -26,2 +26,9 @@ var expect = require('expect.js'), | ||
it('(Node) : should add element as last child', function() { | ||
var $fruits = $(fruits); | ||
var plum = $('<li class="plum">Plum</li>')[0]; | ||
$fruits.append(plum); | ||
expect($fruits.children(3).hasClass('plum')).to.be.ok(); | ||
}); | ||
it('($(...), html) : should add multiple elements as last children', function() { | ||
@@ -45,4 +52,75 @@ var $fruits = $(fruits); | ||
it('(fn) : should add returned element as last child'); | ||
it('(fn) : should invoke the callback with the correct argument and context', function() { | ||
var $fruits = $(fruits).children(); | ||
var args = []; | ||
var thisValues = []; | ||
$fruits.append(function() { | ||
args.push(arguments); | ||
thisValues.push(this); | ||
}); | ||
expect(args).to.eql([ | ||
[0, 'Apple'], | ||
[1, 'Orange'], | ||
[2, 'Pear'] | ||
]); | ||
expect(thisValues).to.eql([ | ||
$fruits[0], | ||
$fruits[1], | ||
$fruits[2] | ||
]); | ||
}); | ||
it('(fn) : should add returned string as last child', function() { | ||
var $fruits = $(fruits).children(); | ||
var $apple, $orange, $pear; | ||
$fruits.append(function() { | ||
return '<div class="first">'; | ||
}); | ||
$apple = $fruits.eq(0); | ||
$orange = $fruits.eq(1); | ||
$pear = $fruits.eq(2); | ||
expect($apple.find('.first')[0]).to.equal($apple.contents()[1]); | ||
expect($orange.find('.first')[0]).to.equal($orange.contents()[1]); | ||
expect($pear.find('.first')[0]).to.equal($pear.contents()[1]); | ||
}); | ||
it('(fn) : should add returned Cheerio object as last child', function() { | ||
var $fruits = $(fruits).children(); | ||
var $apple, $orange, $pear; | ||
$fruits.append(function() { | ||
return $('<div class="second">'); | ||
}); | ||
$apple = $fruits.eq(0); | ||
$orange = $fruits.eq(1); | ||
$pear = $fruits.eq(2); | ||
expect($apple.find('.second')[0]).to.equal($apple.contents()[1]); | ||
expect($orange.find('.second')[0]).to.equal($orange.contents()[1]); | ||
expect($pear.find('.second')[0]).to.equal($pear.contents()[1]); | ||
}); | ||
it('(fn) : should add returned Node as last child', function() { | ||
var $fruits = $(fruits).children(); | ||
var $apple, $orange, $pear; | ||
$fruits.append(function() { | ||
return $('<div class="third">')[0]; | ||
}); | ||
$apple = $fruits.eq(0); | ||
$orange = $fruits.eq(1); | ||
$pear = $fruits.eq(2); | ||
expect($apple.find('.third')[0]).to.equal($apple.contents()[1]); | ||
expect($orange.find('.third')[0]).to.equal($orange.contents()[1]); | ||
expect($pear.find('.third')[0]).to.equal($pear.contents()[1]); | ||
}); | ||
it('should maintain correct object state (Issue: #10)', function() { | ||
@@ -78,2 +156,9 @@ var $obj = $('<div></div>') | ||
it('(Node) : should add node as first child', function() { | ||
var $fruits = $(fruits); | ||
var plum = $('<li class="plum">Plum</li>')[0]; | ||
$fruits.prepend(plum); | ||
expect($fruits.children(0).hasClass('plum')).to.be.ok(); | ||
}); | ||
it('(Array) : should add all elements in the array as inital children', function() { | ||
@@ -97,4 +182,75 @@ var $fruits = $(fruits); | ||
it('(fn) : should add returned element as first child'); | ||
it('(fn) : should invoke the callback with the correct argument and context', function() { | ||
var $fruits = $(fruits).children(); | ||
var args = []; | ||
var thisValues = []; | ||
$fruits.prepend(function() { | ||
args.push(arguments); | ||
thisValues.push(this); | ||
}); | ||
expect(args).to.eql([ | ||
[0, 'Apple'], | ||
[1, 'Orange'], | ||
[2, 'Pear'] | ||
]); | ||
expect(thisValues).to.eql([ | ||
$fruits[0], | ||
$fruits[1], | ||
$fruits[2] | ||
]); | ||
}); | ||
it('(fn) : should add returned string as first child', function() { | ||
var $fruits = $(fruits).children(); | ||
var $apple, $orange, $pear; | ||
$fruits.prepend(function() { | ||
return '<div class="first">'; | ||
}); | ||
$apple = $fruits.eq(0); | ||
$orange = $fruits.eq(1); | ||
$pear = $fruits.eq(2); | ||
expect($apple.find('.first')[0]).to.equal($apple.contents()[0]); | ||
expect($orange.find('.first')[0]).to.equal($orange.contents()[0]); | ||
expect($pear.find('.first')[0]).to.equal($pear.contents()[0]); | ||
}); | ||
it('(fn) : should add returned Cheerio object as first child', function() { | ||
var $fruits = $(fruits).children(); | ||
var $apple, $orange, $pear; | ||
$fruits.prepend(function() { | ||
return $('<div class="second">'); | ||
}); | ||
$apple = $fruits.eq(0); | ||
$orange = $fruits.eq(1); | ||
$pear = $fruits.eq(2); | ||
expect($apple.find('.second')[0]).to.equal($apple.contents()[0]); | ||
expect($orange.find('.second')[0]).to.equal($orange.contents()[0]); | ||
expect($pear.find('.second')[0]).to.equal($pear.contents()[0]); | ||
}); | ||
it('(fn) : should add returned Node as first child', function() { | ||
var $fruits = $(fruits).children(); | ||
var $apple, $orange, $pear; | ||
$fruits.prepend(function() { | ||
return $('<div class="third">')[0]; | ||
}); | ||
$apple = $fruits.eq(0); | ||
$orange = $fruits.eq(1); | ||
$pear = $fruits.eq(2); | ||
expect($apple.find('.third')[0]).to.equal($apple.contents()[0]); | ||
expect($orange.find('.third')[0]).to.equal($orange.contents()[0]); | ||
expect($pear.find('.third')[0]).to.equal($pear.contents()[0]); | ||
}); | ||
}); | ||
@@ -131,2 +287,9 @@ | ||
it('(Node) : should add element as next sibling', function() { | ||
var $fruits = $(fruits); | ||
var plum = $('<li class="plum">Plum</li>')[0]; | ||
$('.apple', $fruits).after(plum); | ||
expect($('.apple', $fruits).next().hasClass('plum')).to.be.ok(); | ||
}); | ||
it('($(...), html) : should add multiple elements as next siblings', function() { | ||
@@ -141,4 +304,59 @@ var $fruits = $(fruits); | ||
it('(fn) : should add returned element as next sibling'); | ||
it('(fn) : should invoke the callback with the correct argument and context', function() { | ||
var $fruits = $(fruits).children(); | ||
var args = []; | ||
var thisValues = []; | ||
$fruits.after(function() { | ||
args.push(arguments); | ||
thisValues.push(this); | ||
}); | ||
expect(args).to.eql([[0], [1], [2]]); | ||
expect(thisValues).to.eql([ | ||
$fruits[0], | ||
$fruits[1], | ||
$fruits[2] | ||
]); | ||
}); | ||
it('(fn) : should add returned string as next sibling', function() { | ||
var $list = $(fruits); | ||
var $fruits = $list.children(); | ||
$fruits.after(function() { | ||
return '<li class="first">'; | ||
}); | ||
expect($list.find('.first')[0]).to.equal($list.contents()[1]); | ||
expect($list.find('.first')[1]).to.equal($list.contents()[3]); | ||
expect($list.find('.first')[2]).to.equal($list.contents()[5]); | ||
}); | ||
it('(fn) : should add returned Cheerio object as next sibling', function() { | ||
var $list = $(fruits); | ||
var $fruits = $list.children(); | ||
$fruits.after(function() { | ||
return $('<li class="second">'); | ||
}); | ||
expect($list.find('.second')[0]).to.equal($list.contents()[1]); | ||
expect($list.find('.second')[1]).to.equal($list.contents()[3]); | ||
expect($list.find('.second')[2]).to.equal($list.contents()[5]); | ||
}); | ||
it('(fn) : should add returned element as next sibling', function() { | ||
var $list = $(fruits); | ||
var $fruits = $list.children(); | ||
$fruits.after(function() { | ||
return $('<li class="third">')[0]; | ||
}); | ||
expect($list.find('.third')[0]).to.equal($list.contents()[1]); | ||
expect($list.find('.third')[1]).to.equal($list.contents()[3]); | ||
expect($list.find('.third')[2]).to.equal($list.contents()[5]); | ||
}); | ||
}); | ||
@@ -166,2 +384,9 @@ | ||
it('(Node) : should add element as previous sibling', function() { | ||
var $fruits = $(fruits); | ||
var plum = $('<li class="plum">Plum</li>'); | ||
$('.apple', $fruits).before(plum); | ||
expect($('.apple', $fruits).prev().hasClass('plum')).to.be.ok(); | ||
}); | ||
it('(Array) : should add all elements in the array as previous sibling', function() { | ||
@@ -185,4 +410,59 @@ var $fruits = $(fruits); | ||
it('(fn) : should add returned element as previous sibling'); | ||
it('(fn) : should invoke the callback with the correct argument and context', function() { | ||
var $fruits = $(fruits).children(); | ||
var args = []; | ||
var thisValues = []; | ||
$fruits.before(function() { | ||
args.push(arguments); | ||
thisValues.push(this); | ||
}); | ||
expect(args).to.eql([[0], [1], [2]]); | ||
expect(thisValues).to.eql([ | ||
$fruits[0], | ||
$fruits[1], | ||
$fruits[2] | ||
]); | ||
}); | ||
it('(fn) : should add returned string as previous sibling', function() { | ||
var $list = $(fruits); | ||
var $fruits = $list.children(); | ||
$fruits.before(function() { | ||
return '<li class="first">'; | ||
}); | ||
expect($list.find('.first')[0]).to.equal($list.contents()[0]); | ||
expect($list.find('.first')[1]).to.equal($list.contents()[2]); | ||
expect($list.find('.first')[2]).to.equal($list.contents()[4]); | ||
}); | ||
it('(fn) : should add returned Cheerio object as previous sibling', function() { | ||
var $list = $(fruits); | ||
var $fruits = $list.children(); | ||
$fruits.before(function() { | ||
return $('<li class="second">'); | ||
}); | ||
expect($list.find('.second')[0]).to.equal($list.contents()[0]); | ||
expect($list.find('.second')[1]).to.equal($list.contents()[2]); | ||
expect($list.find('.second')[2]).to.equal($list.contents()[4]); | ||
}); | ||
it('(fn) : should add returned Node as previous sibling', function() { | ||
var $list = $(fruits); | ||
var $fruits = $list.children(); | ||
$fruits.before(function() { | ||
return $('<li class="third">')[0]; | ||
}); | ||
expect($list.find('.third')[0]).to.equal($list.contents()[0]); | ||
expect($list.find('.third')[1]).to.equal($list.contents()[2]); | ||
expect($list.find('.third')[2]).to.equal($list.contents()[4]); | ||
}); | ||
}); | ||
@@ -235,2 +515,11 @@ | ||
it('(Node) : should replace the selected element with given element', function() { | ||
var $src = $('<ul></ul>'); | ||
var elem = $('<h2>hi <span>there</span></h2>'); | ||
var $replaced = $src.replaceWith(elem); | ||
expect($replaced[0].parent.type).to.equal('root'); | ||
expect($.html($replaced[0].parent)).to.equal('<h2>hi <span>there</span></h2>'); | ||
expect($.html($replaced)).to.equal('<ul></ul>'); | ||
}); | ||
it('(elem) : should replace the single selected element with given element', function() { | ||
@@ -253,2 +542,52 @@ var $src = $('<br/>'); | ||
it('(fn) : should invoke the callback with the correct argument and context', function() { | ||
var $fruits = $(fruits); | ||
var origChildren = $fruits.children().toArray(); | ||
var args = []; | ||
var thisValues = []; | ||
$fruits.children().replaceWith(function() { | ||
args.push(arguments); | ||
thisValues.push(this); | ||
return '<li class="first">'; | ||
}); | ||
expect(args).to.eql([[0], [1], [2]]); | ||
expect(thisValues).to.eql([ | ||
origChildren[0], | ||
origChildren[1], | ||
origChildren[2] | ||
]); | ||
}); | ||
it('(fn) : should replace the selected element with the returned string', function() { | ||
var $fruits = $(fruits); | ||
$fruits.children().replaceWith(function() { | ||
return '<li class="first">'; | ||
}); | ||
expect($fruits.find('.first')).to.have.length(3); | ||
}); | ||
it('(fn) : should replace the selected element with the returned Cheerio object', function() { | ||
var $fruits = $(fruits); | ||
$fruits.children().replaceWith(function() { | ||
return $('<li class="second">'); | ||
}); | ||
expect($fruits.find('.second')).to.have.length(3); | ||
}); | ||
it('(fn) : should replace the selected element with the returned node', function() { | ||
var $fruits = $(fruits); | ||
$fruits.children().replaceWith(function() { | ||
return $('<li class="third">')[0]; | ||
}); | ||
expect($fruits.find('.third')).to.have.length(3); | ||
}); | ||
}); | ||
@@ -302,3 +641,3 @@ | ||
}); | ||
describe('.toString', function() { | ||
@@ -309,3 +648,3 @@ it('() : should get the outerHTML for an element', function() { | ||
}); | ||
it('() : should return an html string for a set of elements', function() { | ||
@@ -315,3 +654,3 @@ var $fruits = $(fruits); | ||
}); | ||
it('() : should be called implicitly', function() { | ||
@@ -380,3 +719,3 @@ var string = [$("<foo>"), $("<bar>"), $("<baz>")].join(""); | ||
var $apple = $('.apple', fruits); | ||
$apple.text('blah <script>alert("XSS!")</script> blah'); | ||
@@ -383,0 +722,0 @@ expect($apple[0].children[0].data).to.equal('blah <script>alert("XSS!")</script> blah'); |
var expect = require('expect.js'), | ||
$ = require('../'), | ||
food = require('./fixtures').food, | ||
fruits = require('./fixtures').fruits; | ||
text = require('./fixtures').text; | ||
$ = require('../'), | ||
food = require('./fixtures').food, | ||
fruits = require('./fixtures').fruits, | ||
text = require('./fixtures').text; | ||
@@ -23,3 +23,5 @@ describe('$(...)', function() { | ||
it('(many) : should merge all selected elems with matching descendants'); | ||
it('(many) : should merge all selected elems with matching descendants', function() { | ||
expect($('#fruits, #food', food).find('.apple')).to.have.length(1); | ||
}); | ||
@@ -188,6 +190,6 @@ it('(invalid single) : should return empty if cant find', function() { | ||
expect(result[1].attribs.id).to.be('food'); | ||
result = $('#fruits', food).parents() | ||
result = $('#fruits', food).parents(); | ||
expect(result).to.have.length(1); | ||
expect(result[0].attribs.id).to.be('food'); | ||
}) | ||
}); | ||
it('(selector) : should get all of the parents that match the selector in logical order', function() { | ||
@@ -201,7 +203,7 @@ var result = $('.orange', food).parents('#fruits'); | ||
expect(result[1].attribs.id).to.be('food'); | ||
}) | ||
}); | ||
it('() : should not break if the selector does not have any results', function() { | ||
var result = $('.saladbar', food).parents(); | ||
expect(result).to.have.length(0); | ||
}) | ||
}); | ||
it('() : should return an empty set for top-level elements', function() { | ||
@@ -252,3 +254,3 @@ var result = $('#food', food).parents(); | ||
expect(result).to.be.a($); | ||
}) | ||
}); | ||
@@ -258,8 +260,8 @@ it('(selector) : should find the closest element that matches the selector, searching through its ancestors and itself', function() { | ||
var result = $('.orange', food).closest('#food'); | ||
expect(result[0].attribs['id']).to.be('food'); | ||
expect(result[0].attribs.id).to.be('food'); | ||
result = $('.orange', food).closest('ul'); | ||
expect(result[0].attribs['id']).to.be('fruits'); | ||
expect(result[0].attribs.id).to.be('fruits'); | ||
result = $('.orange', food).closest('li'); | ||
expect(result[0].attribs['class']).to.be('orange'); | ||
}) | ||
}); | ||
@@ -274,3 +276,3 @@ it('(selector) : should find the closest element of each item, removing duplicates', function() { | ||
expect(result).to.have.length(0); | ||
}) | ||
}); | ||
@@ -294,10 +296,9 @@ }); | ||
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; | ||
}); | ||
var iterationCount = 0; | ||
$('li', fruits).each(function(idx, elem) { | ||
iterationCount++; | ||
return idx < 1; | ||
}); | ||
expect(iterationCount).to.equal(2); | ||
expect(iterationCount).to.equal(2); | ||
}); | ||
@@ -304,0 +305,0 @@ |
@@ -0,1 +1,2 @@ | ||
/* jshint indent: false */ | ||
exports.fruits = [ | ||
@@ -21,3 +22,3 @@ '<ul id="fruits">', | ||
'<li class="cailler">Cailler</li>', | ||
'</ul>' | ||
'</ul>' | ||
].join(''); | ||
@@ -33,7 +34,7 @@ | ||
exports.inputs = [ | ||
'<select id="one"><option value="option_not_selected">Option not selected</option><option value="option_selected" selected>Option selected</option></select>', | ||
'<input type="text" value="input_text" />', | ||
'<input type="checkbox" name="checkbox_off" value="off" /><input type="checkbox" name="checkbox_on" value="on" checked />', | ||
'<input type="radio" value="off" name="radio" /><input type="radio" name="radio" value="on" checked />', | ||
'<select id="multi" multiple><option value="1">1</option><option value="2" selected>2</option><option value="3" selected>3</option><option value="4">4</option></select>' | ||
'<select id="one"><option value="option_not_selected">Option not selected</option><option value="option_selected" selected>Option selected</option></select>', | ||
'<input type="text" value="input_text" />', | ||
'<input type="checkbox" name="checkbox_off" value="off" /><input type="checkbox" name="checkbox_on" value="on" checked />', | ||
'<input type="radio" value="off" name="radio" /><input type="radio" name="radio" value="on" checked />', | ||
'<select id="multi" multiple><option value="1">1</option><option value="2" selected>2</option><option value="3" selected>3</option><option value="4">4</option></select>' | ||
].join(''); | ||
@@ -40,0 +41,0 @@ |
@@ -55,4 +55,9 @@ var expect = require('expect.js'), | ||
it('should preserve multiple hyphens in data attributes', function() { | ||
var str = '<div data-foo-bar-baz="value"></div>'; | ||
expect(html(str)).to.equal('<div data-foo-bar-baz="value"></div>'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
157477
31
3335
726
3