Socket
Socket
Sign inDemoInstall

cheerio

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cheerio - npm Package Compare versions

Comparing version 0.12.4 to 0.13.0

benchmark/.gitattributes

22

History.md
0.13.0 / 2013-12-30
==================
* Remove "root" node (@jugglinmike)
* Fix bug in `prevAll`, `prev`, `nextAll`, `next`, `prevUntil`, `nextUntil` (@jugglinmike)
* Fix `replaceWith` method (@jugglinmike)
* added nextUntil() and prevUntil() (@finspin)
* Remove internal `connect` function (@jugglinmike)
* Rename `Cheerio#make` to document private status (@jugginmike)
* Remove extraneous call to `_.uniq` (@jugglinmike)
* Use CSSselect library directly (@jugglinmike)
* Run CI against Node v0.11 as an allowed failure (@jugginmike)
* Correct bug in `Cheerio#parents` (@jugglinmike)
* Implement `$.fn.end` (@jugginmike)
* Ignore colons inside of url(.*) when parsing css (@Meekohi)
* Introduce rudimentary benchmark suite (@jugglinmike)
* Update HtmlParser2 version (@jugglinmike)
* Correct inconsistency in `$.fn.map` (@jugglinmike)
* fixed traversing tests (@finspin)
* Simplify `make` method (@jugglinmike)
* Avoid shadowing instance methods from arrays (@jugglinmike)
0.12.4 / 2013-11-12

@@ -3,0 +25,0 @@ ==================

8

lib/api/attributes.js

@@ -153,4 +153,4 @@ var _ = require('underscore'),

if (parentEl.length === 0) {
root = (this.parents().last()[0] || this[0]).parent;
parentEl = this.make(root);
root = (this.parents().last()[0] || this[0]).root;
parentEl = this._make(root);
}

@@ -256,3 +256,3 @@

for (var i = 0; i < numElements; i++) {
$elem = this.make(this[i]);
$elem = this._make(this[i]);
// If selected element isnt a tag, move on

@@ -324,3 +324,3 @@ if (!isTag(this[i])) continue;

for (var i = 0; i < numElements; i++) {
$elem = this.make(this[i]);
$elem = this._make(this[i]);
// If selected element isnt a tag, move on

@@ -327,0 +327,0 @@ if (!isTag(this[i])) continue;

@@ -110,9 +110,10 @@ var _ = require('underscore');

return styles
.split(/\s*;\s*/)
.split(';')
.reduce(function(obj, str){
var parts = str.split(/\s*:\s*/);
if (!parts[0]) return obj;
obj[parts[0]] = parts[1];
var n = str.indexOf(':');
// skip if there is no :, or if it is the first/last character
if (n < 1 || n === str.length-1) return obj;
obj[str.slice(0,n).trim()] = str.slice(n+1).trim();
return obj;
}, {});
}

@@ -52,3 +52,4 @@ var _ = require('underscore'),

this.each(function(i, el) {
var siblings = el.parent.children,
var parent = el.parent || el.root,
siblings = parent.children,
index = siblings.indexOf(el);

@@ -67,4 +68,4 @@

// Update next, prev, and parent pointers
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
parent.children = siblings;

@@ -81,3 +82,4 @@ });

this.each(function(i, el) {
var siblings = el.parent.children,
var parent = el.parent || el.root,
siblings = parent.children,
index = siblings.indexOf(el);

@@ -96,4 +98,4 @@

// Update next, prev, and parent pointers
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
parent.children = siblings;

@@ -116,3 +118,4 @@ });

elems.each(function(i, el) {
var siblings = el.parent.children,
var parent = el.parent || el.root,
siblings = parent.children,
index = siblings.indexOf(el);

@@ -125,4 +128,4 @@

// Update next, prev, and parent pointers
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
parent.children = siblings;
});

@@ -137,7 +140,6 @@

this.each(function(i, el) {
var siblings = el.parent.children,
index = siblings.indexOf(el);
var parent = el.parent || el.root,
siblings = parent.children,
index;
if (!~index) return;
if (_.isFunction(content)) {

@@ -147,6 +149,13 @@ dom = makeDomArray(content.call(el, i));

// In the case that `dom` contains nodes that already exist in other
// structures, ensure those nodes are properly removed.
updateDOM(dom, null);
index = siblings.indexOf(el);
// Completely remove old element
siblings.splice.apply(siblings, [index, 1].concat(dom));
el.parent = el.prev = el.next = null;
updateDOM(siblings, el.parent);
el.parent.children = siblings;
updateDOM(siblings, parent);
});

@@ -219,3 +228,3 @@

// Seems to be the easiest way to reconnect everything correctly
return this.constructor($.html(this));
return this._make($.html(this));
};
var _ = require('underscore'),
select = require('cheerio-select'),
select = require('CSSselect'),
utils = require('../utils'),

@@ -7,6 +7,3 @@ isTag = utils.isTag;

var find = exports.find = function(selector) {
// 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()))));
return this._make(select(selector, [].slice.call(this.children())));
};

@@ -22,3 +19,3 @@

var parentElem = elem.parent;
if (set.indexOf(parentElem) < 0 && parentElem.type !== 'root') {
if (parentElem && set.indexOf(parentElem) < 0) {
set.push(parentElem);

@@ -28,3 +25,3 @@ }

$set = this.make(set);
$set = this._make(set);

@@ -39,6 +36,18 @@ if (arguments.length) {

var parents = exports.parents = function(selector) {
if (this[0] && this[0].parent) {
return traverseParents(this, this[0].parent, selector, Infinity);
}
return this;
var parentNodes = [];
// When multiple DOM elements are in the original set, the resulting set will
// be in *reverse* order of the original elements as well, with duplicates
// removed.
this.toArray().reverse().forEach(function(elem) {
traverseParents(this, elem.parent, selector, Infinity)
.forEach(function(node) {
if (parentNodes.indexOf(node) === -1) {
parentNodes.push(node);
}
}
);
}, this);
return this._make(parentNodes);
};

@@ -53,3 +62,3 @@

if (!selector) {
return this.make(set);
return this._make(set);
}

@@ -66,3 +75,3 @@

return this.make(set);
return this._make(set);
};

@@ -72,5 +81,14 @@

if (!this[0]) { return this; }
var elem = this[0];
while ((elem = elem.next)) if (isTag(elem)) return this.make(elem);
return this.make([]);
var elems = [];
_.forEach(this, function(elem) {
while ((elem = elem.next)) {
if (isTag(elem)) {
elems.push(elem);
return;
}
}
});
return this._make(elems);
};

@@ -80,12 +98,58 @@

if (!this[0]) { return this; }
var elems = [], elem = this[0];
while ((elem = elem.next)) if (isTag(elem)) elems.push(elem);
return this.make(selector ? select(selector, elems) : elems);
var elems = [];
_.forEach(this, function(elem) {
while ((elem = elem.next)) {
if (isTag(elem) && elems.indexOf(elem) === -1) {
elems.push(elem);
}
}
});
return this._make(selector ? select(selector, elems) : elems);
};
var nextUntil = exports.nextUntil = function(selector, filter) {
if (!this[0]) { return this; }
var elems = [], untilNode, untilNodes;
if (typeof selector === 'string') {
untilNode = select(selector, this.nextAll().toArray())[0];
} else if (selector && selector.cheerio) {
untilNodes = selector.toArray();
} else if (selector) {
untilNode = selector;
}
_.forEach(this, function(elem) {
while ((elem = elem.next)) {
if ((untilNode && elem !== untilNode) ||
(untilNodes && untilNodes.indexOf(elem) === -1) ||
(!untilNode && !untilNodes)) {
if (isTag(elem) && elems.indexOf(elem) === -1) {
elems.push(elem);
}
} else {
break;
}
}
});
return this._make(filter ? select(filter, elems) : elems);
};
var prev = exports.prev = function() {
if (!this[0]) { return this; }
var elem = this[0];
while ((elem = elem.prev)) if (isTag(elem)) return this.make(elem);
return this.make([]);
var elems = [];
_.forEach(this, function(elem) {
while ((elem = elem.prev)) {
if (isTag(elem)) {
elems.push(elem);
return;
}
}
});
return this._make(elems);
};

@@ -95,7 +159,44 @@

if (!this[0]) { return this; }
var elems = [], elem = this[0];
while ((elem = elem.prev)) if (isTag(elem)) elems.push(elem);
return this.make(selector ? select(selector, elems) : elems);
var elems = [];
_.forEach(this, function(elem) {
while ((elem = elem.prev)) {
if (isTag(elem) && elems.indexOf(elem) === -1) {
elems.push(elem);
}
}
});
return this._make(selector ? select(selector, elems) : elems);
};
var prevUntil = exports.prevUntil = function(selector, filter) {
if (!this[0]) { return this; }
var elems = [], untilNode, untilNodes;
if (typeof selector === 'string') {
untilNode = select(selector, this.prevAll().toArray())[0];
} else if (selector && selector.cheerio) {
untilNodes = selector.toArray();
} else if (selector) {
untilNode = selector;
}
_.forEach(this, function(elem) {
while ((elem = elem.prev)) {
if ((untilNode && elem !== untilNode) ||
(untilNodes && untilNodes.indexOf(elem) === -1) ||
(!untilNode && !untilNodes)) {
if (isTag(elem) && elems.indexOf(elem) === -1) {
elems.push(elem);
}
} else {
break;
}
}
});
return this._make(filter ? select(filter, elems) : elems);
};
var siblings = exports.siblings = function(selector) {

@@ -108,5 +209,5 @@ var elems = _.filter(

if (selector !== undefined) {
elems = this.make(select(selector, elems));
elems = this._make(select(selector, elems));
}
return this.make(elems);
return this._make(elems);
};

@@ -120,10 +221,10 @@

if (selector === undefined) return this.make(elems);
else if (_.isNumber(selector)) return this.make(elems[selector]);
if (selector === undefined) return this._make(elems);
else if (_.isNumber(selector)) return this._make(elems[selector]);
return this.make(elems).filter(selector);
return this._make(elems).filter(selector);
};
var contents = exports.contents = function() {
return this.make(_.reduce(this, function(all, elem) {
return this._make(_.reduce(this, function(all, elem) {
all.push.apply(all, elem.children);

@@ -136,3 +237,3 @@ return all;

var i = 0, len = this.length;
while (i < len && fn.call(this.make(this[i]), i, this[i]) !== false) ++i;
while (i < len && fn.call(this._make(this[i]), i, this[i]) !== false) ++i;
return this;

@@ -142,9 +243,10 @@ };

var map = exports.map = function(fn) {
return _.map(this, function(el, i) {
return fn.call(this.make(el), i, el);
}, this);
return this._make(_.reduce(this, function(memo, el, i) {
var val = fn.call(el, i, el);
return val == null ? memo : memo.concat(val);
}, []));
};
var filter = exports.filter = function(match) {
var make = _.bind(this.make, this);
var make = _.bind(this._make, this);
var filterFn;

@@ -154,3 +256,3 @@

filterFn = function(el) {
return select(match, el)[0] === el;
return select(match, [el])[0] === el;
};

@@ -173,7 +275,7 @@ } else if (_.isFunction(match)) {

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;
};

@@ -185,7 +287,7 @@

if (i < 0) i = this.length + i;
return this[i] ? this.make(this[i]) : this.make([]);
return this[i] ? this._make(this[i]) : this._make([]);
};
var slice = exports.slice = function() {
return this.make([].slice.apply(this, arguments));
return this._make([].slice.apply(this, arguments));
};

@@ -195,4 +297,4 @@

var elems = [];
while (elems.length < limit && elem.type !== 'root') {
if (!selector || self.make(elem).filter(selector).length) {
while (elem && elems.length < limit) {
if (!selector || self._make(elem).filter(selector).length) {
elems.push(elem);

@@ -202,3 +304,9 @@ }

}
return self.make(elems);
return elems;
}
// End the most recent filtering operation in the current chain and return the
// set of matched elements to its previous state.
var end = exports.end = function() {
return this.prevObject || this._make([]);
};

@@ -6,6 +6,4 @@ /*

var path = require('path'),
select = require('cheerio-select'),
parse = require('./parse'),
evaluate = parse.evaluate,
updateDOM = parse.update,
_ = require('underscore');

@@ -43,3 +41,3 @@

if (typeof root === 'string') root = parse(root);
this._root = this.make(root, this);
this._root = Cheerio.call(this, root);
}

@@ -51,8 +49,15 @@

// $(dom)
if (selector.name || Array.isArray(selector))
return this.make(selector, this);
if (selector.name || selector.type === 'text' || selector.type === 'comment')
selector = [selector];
// $([dom])
if (Array.isArray(selector)) {
_.defaults(this, selector);
this.length = selector.length;
return this;
}
// $(<html>)
if (typeof selector === 'string' && isHtml(selector)) {
return this.make(parse(selector).children);
return Cheerio.call(this, parse(selector).children);
}

@@ -67,3 +72,3 @@

context = parse(context);
context = this.make(context, this);
context = Cheerio.call(this, context);
} else {

@@ -84,6 +89,6 @@ // $('li', 'ul')

/**
* Inherit from `static`
* Mix in `static`
*/
Cheerio.__proto__ = require('./static');
_.extend(Cheerio, require('./static'));

@@ -101,3 +106,3 @@ /*

Cheerio.prototype.options = {
ignoreWhitespace: false,
normalizeWhitespace: false,
xmlMode: false,

@@ -112,3 +117,3 @@ lowerCaseTags: false

Cheerio.prototype.length = 0;
Cheerio.prototype.sort = [].splice;
Cheerio.prototype.splice = Array.prototype.splice;

@@ -129,8 +134,10 @@ /*

* Make a cheerio object
*
* @api private
*/
Cheerio.prototype.make = function(dom, context) {
if (dom.cheerio) return dom;
dom = (Array.isArray(dom)) ? dom : [dom];
return _.extend(context || new Cheerio(), dom, { length: dom.length });
Cheerio.prototype._make = function(dom) {
var cheerio = new Cheerio(dom);
cheerio.prevObject = this;
return cheerio;
};

@@ -137,0 +144,0 @@

@@ -40,43 +40,5 @@ /*

return connect(handler.dom);
};
_.forEach(handler.dom, parseData);
var connect = exports.connect = function(dom, parent) {
parent = parent || null;
var prevElem = null;
_.each(dom, function(elem) {
if (isTag(elem.type)) {
// If tag and no attributes, add empty object
if (elem.attribs === undefined) {
elem.attribs = {};
} else {
// If there are already attributes, add them to the data list.
elem.data = parseData(elem);
}
}
// Set parent
elem.parent = parent;
// Previous Sibling
elem.prev = prevElem;
// Next sibling
elem.next = null;
if (prevElem) prevElem.next = elem;
// Run through the children
if (elem.children)
connect(elem.children, elem);
else if (isTag(elem.type))
elem.children = [];
// Get ready for next element
prevElem = elem;
});
return dom;
return handler.dom;
};

@@ -86,4 +48,2 @@

Update the dom structure, for one changed layer
* Much faster than reconnecting
*/

@@ -94,12 +54,37 @@ var update = exports.update = function(arr, parent) {

// Update parent
if (parent) {
parent.children = arr;
} else {
parent = null;
}
// Update neighbors
for (var i = 0; i < arr.length; i++) {
arr[i].prev = arr[i - 1] || null;
arr[i].next = arr[i + 1] || null;
arr[i].parent = parent || null;
var node = arr[i];
// Cleanly remove existing nodes from their previous structures.
var oldSiblings = node.parent && node.parent.children;
if (oldSiblings && oldSiblings !== arr) {
oldSiblings.splice(oldSiblings.indexOf(node), 1);
if (node.prev) {
node.prev.next = node.next;
}
if (node.next) {
node.next.prev = node.prev;
}
}
node.prev = arr[i - 1] || null;
node.next = arr[i + 1] || null;
if (parent && parent.type === 'root') {
node.root = parent;
node.parent = null;
} else {
delete node.root;
node.parent = parent;
}
}
// Update parent
parent.children = arr;
return parent;

@@ -109,5 +94,7 @@ };

/**
* Extract data by using element attributes.
* Extract element data according to `data-*` element attributes and store in
* a key-value hash on the element's `data` attribute. Repeat for any and all
* descendant elements.
*
* @param {Object} elem Element
* @return {Object} `element.data` object
*/

@@ -125,5 +112,6 @@ var parseData = exports.parseData = function(elem) {

}
return elem.data;
_.forEach(elem.children, parseData);
};
// module.exports = $.extend(exports);

@@ -75,4 +75,3 @@ /*

var output = [],
xmlMode = opts.xmlMode || false,
ignoreWhitespace = opts.ignoreWhitespace || false;
xmlMode = opts.xmlMode || false;

@@ -79,0 +78,0 @@ _.each(dom, function(elem) {

@@ -5,3 +5,3 @@ /**

var select = require('cheerio-select'),
var select = require('CSSselect'),
parse = require('./parse'),

@@ -8,0 +8,0 @@ render = require('./render'),

@@ -11,3 +11,3 @@ {

],
"version": "0.12.4",
"version": "0.13.0",
"repository": {

@@ -22,6 +22,6 @@ "type": "git",

"dependencies": {
"cheerio-select": "*",
"htmlparser2": "3.1.4",
"htmlparser2": "~3.4.0",
"underscore": "~1.4",
"entities": "0.x"
"entities": "0.x",
"CSSselect": "~0.4.0"
},

@@ -31,3 +31,5 @@ "devDependencies": {

"expect.js": "*",
"jshint": "~2.3.0"
"jshint": "~2.3.0",
"benchmark": "~1.0.0",
"jsdom": "~0.8.9"
},

@@ -34,0 +36,0 @@ "scripts": {

@@ -91,3 +91,3 @@ # cheerio [![Build Status](https://secure.travis-ci.org/MatthewMueller/cheerio.png?branch=master)](http://travis-ci.org/MatthewMueller/cheerio)

$ = cheerio.load('<ul id="fruits">...</ul>', {
ignoreWhitespace: true,
normalizeWhitespace: true,
xmlMode: true

@@ -102,3 +102,3 @@ });

{
ignoreWhitespace: false,
normalizeWhitespace: false,
xmlMode: false,

@@ -300,2 +300,10 @@ lowerCaseTags: false

#### .nextUntil()
Gets all the following siblings up to but not including the element matched by the selector.
```js
$('.apple').nextUntil('.pear')
//=> [<li class="orange">Orange</li>]
```
#### .prev()

@@ -317,2 +325,10 @@ Gets the previous sibling of the first selected element.

#### .prevUntil()
Gets all the preceding siblings up to but not including the element matched by the selector.
```js
$('.pear').prevUntil('.apple')
//=> [<li class="orange">Orange</li>]
```
#### .slice( start, [end] )

@@ -375,3 +391,3 @@ Gets the elements matching the specified range

#### .map( function(index, element) )
Iterates over a cheerio object, executing a function for each selected element. Map will return an `array` of return values from each of the functions it iterated over. 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`.
Pass each element in the current matched set through a function, producing a new Cheerio object containing the return values. The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set. If the function returns null or undefined, no element will be inserted.

@@ -381,5 +397,5 @@ ```js

// this === el
return $(this).attr('class');
}).join(', ');
//=> apple, orange, pear
return $('<div>').text($(this).text());
}).html();
//=> <div>apple</div><div>orange</div><div>pear</div>
```

@@ -435,2 +451,10 @@

#### .end()
End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
```js
$('li').eq(0).end().length
//=> 3
```
### Manipulation

@@ -437,0 +461,0 @@ Methods for modifying the DOM structure.

@@ -30,2 +30,17 @@ var expect = require('expect.js');

it('(prop): should not mangle embedded urls', function() {
var el = $('<li style="background-image:url(http://example.com/img.png);">');
expect(el.css('background-image')).to.equal('url(http://example.com/img.png)');
});
it('(prop): should ignore blank properties', function() {
var el = $('<li style=":#ccc;color:#aaa;">');
expect(el.css()).to.eql({color:'#aaa'});
});
it('(prop): should ignore blank values', function() {
var el = $('<li style="color:;position:absolute;">');
expect(el.css()).to.eql({position:'absolute'});
});
describe('(prop, function):', function() {

@@ -32,0 +47,0 @@ beforeEach(function() {

@@ -496,37 +496,37 @@ var expect = require('expect.js'),

expect($fruits.children(3).hasClass('grape')).to.be.ok();
expect($fruits.children()).to.have.length(4);
});
it('(elem) : 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('(Node) : should replace the selected element with given node', function() {
var $src = $('<h2>hi <span>there</span></h2>');
var $new = $('<ul></ul>');
var $replaced = $src.find('span').replaceWith($new[0]);
expect($new[0].parent).to.equal($src[0]);
expect($replaced[0].parent).to.equal(null);
expect($.html($src)).to.equal('<h2>hi <ul></ul></h2>');
});
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('(existing element) : should remove element from its previous location', function() {
var $fruits = $(fruits);
$('.pear', $fruits).replaceWith($('.apple', $fruits));
expect($fruits.children()).to.have.length(2);
expect($fruits.children()[0]).to.equal($('.orange', $fruits)[0]);
expect($fruits.children()[1]).to.equal($('.apple', $fruits)[0]);
});
it('(elem) : should replace the single selected element with given element', function() {
var $src = $('<br/>');
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('<br>');
var $src = $('<h2>hi <span>there</span></h2>');
var $new = $('<div>here</div>');
var $replaced = $src.find('span').replaceWith($new);
expect($new[0].parent).to.equal($src[0]);
expect($replaced[0].parent).to.equal(null);
expect($.html($src)).to.equal('<h2>hi <div>here</div></h2>');
});
it('(str) : should accept strings', function() {
var $src = $('<br/>');
var $replaced = $src.replaceWith('<h2>hi <span>there</span></h2>');
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('<br>');
var $src = $('<h2>hi <span>there</span></h2>');
var newStr = '<div>here</div>';
var $replaced = $src.find('span').replaceWith(newStr);
expect($replaced[0].parent).to.equal(null);
expect($.html($src)).to.equal('<h2>hi <div>here</div></h2>');
});

@@ -645,3 +645,3 @@

it('() : should be called implicitly', function() {
var string = [$("<foo>"), $("<bar>"), $("<baz>")].join("");
var string = [$('<foo>'), $('<bar>'), $('<baz>')].join('');
expect(string).to.equal('<foo></foo><bar></bar><baz></baz>');

@@ -648,0 +648,0 @@ });

@@ -5,2 +5,3 @@ var expect = require('expect.js'),

fruits = require('./fixtures').fruits,
drinks = require('./fixtures').drinks,
text = require('./fixtures').text;

@@ -69,3 +70,5 @@

it('should only match immediate children, not ancestors');
it('should only match immediate children, not ancestors', function() {
expect($(food).children('li')).to.have.length(0);
});

@@ -105,2 +108,6 @@ });

it('() : should operate over all elements in the selection', function() {
expect($('.apple, .orange', food).next()).to.have.length(2);
});
});

@@ -122,7 +129,81 @@

it('(nextAll on empty object) : should return empty', function() {
expect($('.banana', fruits).next()).to.have.length(0);
expect($('.banana', fruits).nextAll()).to.have.length(0);
});
it('() : should operate over all elements in the selection', function() {
expect($('.apple, .carrot', food).nextAll()).to.have.length(3);
});
it('() : should not contain duplicate elements', function() {
var elems = $('.apple, .orange', food);
expect(elems.nextAll()).to.have.length(2);
});
});
describe('.nextUntil', function() {
it('() : should return all following siblings if no selector specified', function() {
var elems = $('.apple', food).nextUntil();
expect(elems).to.have.length(2);
expect(elems[0].attribs['class']).to.equal('orange');
expect(elems[1].attribs['class']).to.equal('pear');
});
it('() : should filter out non-element nodes', function() {
var elems = $('<div><div></div><!-- comment -->text<div></div></div>');
var div = elems.children().eq(0);
expect(div.nextUntil()).to.have.length(1);
});
it('() : should operate over all elements in the selection', function() {
var elems = $('.apple, .carrot', food);
expect(elems.nextUntil()).to.have.length(3);
});
it('() : should not contain duplicate elements', function() {
var elems = $('.apple, .orange', food);
expect(elems.nextUntil()).to.have.length(2);
});
it('(selector) : should return all following siblings until selector', function() {
var elems = $('.apple', food).nextUntil('.pear');
expect(elems).to.have.length(1);
expect(elems[0].attribs['class']).to.equal('orange');
});
it('(selector not sibling) : should return all following siblings', function() {
var elems = $('.apple', fruits).nextUntil('#vegetables');
expect(elems).to.have.length(2);
});
it('(selector, filterString) : should return all following siblings until selector, filtered by filter', function() {
var elems = $('.beer', drinks).nextUntil('.water', '.milk');
expect(elems).to.have.length(1);
expect(elems[0].attribs['class']).to.equal('milk');
});
it('() : should return an empty object for last child', function() {
expect($('.pear', fruits).nextUntil()).to.have.length(0);
});
it('() : should return an empty object when called on an empty object', function() {
expect($('.banana', fruits).nextUntil()).to.have.length(0);
});
it('(node) : should return all following siblings until the node', function() {
var $fruits = $(fruits).children();
var elems = $fruits.eq(0).nextUntil($fruits[2]);
expect(elems).to.have.length(1);
});
it('(cheerio object) : should return all following siblings until any member of the cheerio object', function() {
var $drinks = $(drinks).children();
var $until = $([$drinks[4], $drinks[3]]);
var elems = $drinks.eq(0).nextUntil($until);
expect(elems).to.have.length(2);
});
});
describe('.prev', function() {

@@ -140,5 +221,9 @@

it('(prev on empty object) : should return empty', function() {
expect($('.banana', fruits).next()).to.have.length(0);
expect($('.banana', fruits).prev()).to.have.length(0);
});
it('() : should operate over all elements in the selection', function() {
expect($('.orange, .pear', food).prev()).to.have.length(2);
});
});

@@ -160,7 +245,82 @@

it('(prevAll on empty object) : should return empty', function() {
expect($('.banana', fruits).next()).to.have.length(0);
expect($('.banana', fruits).prevAll()).to.have.length(0);
});
it('() : should operate over all elements in the selection', function() {
expect($('.orange, .sweetcorn', food).prevAll()).to.have.length(2);
});
it('() : should not contain duplicate elements', function() {
var elems = $('.orange, .pear', food);
expect(elems.prevAll()).to.have.length(2);
});
});
describe('.prevUntil', function() {
it('() : should return all preceding siblings if no selector specified', function() {
var elems = $('.pear', fruits).prevUntil();
expect(elems).to.have.length(2);
expect(elems[0].attribs['class']).to.equal('orange');
expect(elems[1].attribs['class']).to.equal('apple');
});
it('() : should filter out non-element nodes', function() {
var elems = $('<div class="1"><div class="2"></div><!-- comment -->text<div class="3"></div></div>');
var div = elems.children().last();
expect(div.prevUntil()).to.have.length(1);
});
it('() : should operate over all elements in the selection', function() {
var elems = $('.pear, .sweetcorn', food);
expect(elems.prevUntil()).to.have.length(3);
});
it('() : should not contain duplicate elements', function() {
var elems = $('.orange, .pear', food);
expect(elems.prevUntil()).to.have.length(2);
});
it('(selector) : should return all preceding siblings until selector', function() {
var elems = $('.pear', fruits).prevUntil('.apple');
expect(elems).to.have.length(1);
expect(elems[0].attribs['class']).to.equal('orange');
});
it('(selector not sibling) : should return all preceding siblings', function() {
var elems = $('.sweetcorn', food).prevUntil('#fruits');
expect(elems).to.have.length(1);
expect(elems[0].attribs['class']).to.equal('carrot');
});
it('(selector, filterString) : should return all preceding siblings until selector, filtered by filter', function() {
var elems = $('.cider', drinks).prevUntil('.juice', '.water');
expect(elems).to.have.length(1);
expect(elems[0].attribs['class']).to.equal('water');
});
it('() : should return an empty object for first child', function() {
expect($('.apple', fruits).prevUntil()).to.have.length(0);
});
it('() : should return an empty object when called on an empty object', function() {
expect($('.banana', fruits).prevUntil()).to.have.length(0);
});
it('(node) : should return all previous siblings until the node', function() {
var $fruits = $(fruits).children();
var elems = $fruits.eq(2).prevUntil($fruits[0]);
expect(elems).to.have.length(1);
});
it('(cheerio object) : should return all previous siblings until any member of the cheerio object', function() {
var $drinks = $(drinks).children();
var $until = $([$drinks[0], $drinks[1]]);
var elems = $drinks.eq(4).prevUntil($until);
expect(elems).to.have.length(2);
});
});
describe('.siblings', function() {

@@ -216,2 +376,11 @@

});
it('() : should return the parents of every element in the *reveresed* collection, omitting duplicates', function() {
var $food = $(food);
var $parents = $food.find('li').parents();
expect($parents).to.have.length(3);
expect($parents[0]).to.be($food.find('#vegetables')[0]);
expect($parents[1]).to.be($food[0]);
expect($parents[2]).to.be($food.find('#fruits')[0]);
});
});

@@ -309,12 +478,75 @@

describe('.map', function() {
it('(fn) : should return an array of mapped items', function() {
var classes = $('li', fruits).map(function(i, el) {
expect(this[0]).to.be(el);
expect(el.name).to.be('li');
expect(i).to.be.a('number');
return el.attribs['class'];
}).join(', ');
it('(fn) : should be invoked with the correct arguments and context', function() {
var $fruits = $('li', fruits);
var args = [];
var thisVals = [];
expect(classes).to.be('apple, orange, pear');
$fruits.map(function() {
args.push(arguments);
thisVals.push(this);
});
expect(args).to.eql([
[0, $fruits[0]],
[1, $fruits[1]],
[2, $fruits[2]]
]);
expect(thisVals).to.eql([
$fruits[0],
$fruits[1],
$fruits[2]
]);
});
it('(fn) : should return an Cheerio object wrapping the returned items', function() {
var $fruits = $('li', fruits);
var $mapped = $fruits.map(function(i, el) {
return $fruits[2 - i];
});
expect($mapped).to.have.length(3);
expect($mapped[0]).to.be($fruits[2]);
expect($mapped[1]).to.be($fruits[1]);
expect($mapped[2]).to.be($fruits[0]);
});
it('(fn) : should ignore `null` and `undefined` returned by iterator', function() {
var $fruits = $('li', fruits);
var retVals = [null, undefined, $fruits[1]];
var $mapped = $fruits.map(function(i, el) {
return retVals[i];
});
expect($mapped).to.have.length(1);
expect($mapped[0]).to.be($fruits[1]);
});
it('(fn) : should preform a shallow merge on arrays returned by iterator', function() {
var $fruits = $('li', fruits);
var $mapped = $fruits.map(function(i, el) {
return [1, [3, 4]];
});
expect($mapped.toArray()).to.eql([
1, [3, 4],
1, [3, 4],
1, [3, 4]
]);
});
it('(fn) : should tolerate `null` and `undefined` when flattening arrays returned by iterator', function() {
var $fruits = $('li', fruits);
var $mapped = $fruits.map(function(i, el) {
return [null, undefined];
});
expect($mapped.toArray()).to.eql([
null, undefined,
null, undefined,
null, undefined,
]);
});
});

@@ -453,2 +685,65 @@

describe('.end() :', function() {
var $fruits = $(fruits).children();
it('returns an empty object at the end of the chain', function() {
expect($fruits.end().end()).to.be.ok();
expect($fruits.end().end()).to.have.length(0);
});
it('find', function() {
expect($fruits.find('.apple').end()).to.be($fruits);
});
it('filter', function() {
expect($fruits.filter('.apple').end()).to.be($fruits);
});
it('map', function() {
expect($fruits.map(function() { return this; }).end()).to.be($fruits);
});
it('contents', function() {
expect($fruits.contents().end()).to.be($fruits);
});
it('eq', function() {
expect($fruits.eq(1).end()).to.be($fruits);
});
it('first', function() {
expect($fruits.first().end()).to.be($fruits);
});
it('last', function() {
expect($fruits.last().end()).to.be($fruits);
});
it('slice', function() {
expect($fruits.slice(1).end()).to.be($fruits);
});
it('children', function() {
expect($fruits.children().end()).to.be($fruits);
});
it('parent', function() {
expect($fruits.parent().end()).to.be($fruits);
});
it('parents', function() {
expect($fruits.parents().end()).to.be($fruits);
});
it('closest', function() {
expect($fruits.closest('ul').end()).to.be($fruits);
});
it('siblings', function() {
expect($fruits.siblings().end()).to.be($fruits);
});
it('next', function() {
expect($fruits.next().end()).to.be($fruits);
});
it('nextAll', function() {
expect($fruits.nextAll().end()).to.be($fruits);
});
it('prev', function() {
expect($fruits.prev().end()).to.be($fruits);
});
it('prevAll', function() {
expect($fruits.prevAll().end()).to.be($fruits);
});
it('clone', function() {
expect($fruits.clone().end()).to.be($fruits);
});
});
});

@@ -54,5 +54,5 @@ var expect = require('expect.js'),

it('(html) : should handle the ignore whitepace option', function() {
var $html = $.load('<body><a href="http://yahoo.com">Yahoo</a> <a href="http://google.com">Google</a></body>', { ignoreWhitespace : true });
expect($html.html()).to.be('<body><a href="http://yahoo.com">Yahoo</a><a href="http://google.com">Google</a></body>');
it('(html) : should handle the `normalizeWhitepace` option', function() {
var $html = $.load('<body><b>foo</b> <b>bar</b></body>', { normalizeWhitespace : true });
expect($html.html()).to.be('<body><b>foo</b> <b>bar</b></body>');
});

@@ -59,0 +59,0 @@

@@ -130,2 +130,7 @@ var expect = require('expect.js'),

it('should not create a top-level node', function() {
var $elem = $('* div', '<div>');
expect($elem).to.have.length(0);
});
it('should be able to select multiple elements: $(".apple, #fruits")', function() {

@@ -202,2 +207,12 @@ var $elems = $('.apple, #fruits', fruits);

it('(extended Array) should not interfere with prototype methods (issue #119)', function() {
var extended = [];
var custom = extended.find = extended.children = extended.each = function() {};
var $empty = $(extended);
expect($empty.find).to.be($.prototype.find);
expect($empty.children).to.be($.prototype.children);
expect($empty.each).to.be($.prototype.each);
});
});

@@ -25,2 +25,12 @@ /* jshint indent: false */

exports.drinks = [
'<ul id="drinks">',
'<li class="beer">Beer</li>',
'<li class="juice">Juice</li>',
'<li class="milk">Milk</li>',
'<li class="water">Water</li>',
'<li class="cider">Cider</li>',
'</ul>'
].join('');
exports.food = [

@@ -27,0 +37,0 @@ '<ul id="food">',

@@ -149,121 +149,2 @@ var expect = require('expect.js'),

describe('.connect', function() {
var create = function(html) {
var dom = parse.evaluate(html);
return parse.connect(dom);
};
it('should fill in empty attributes: ' + basic, function() {
var tag = create(basic)[0];
// Should exist but be null
expect(tag.parent).to.be(null);
expect(tag.next).to.be(null);
expect(tag.prev).to.be(null);
// Should exist but be empty
expect(tag.children).to.be.empty();
expect(tag.attribs).to.be.ok();
});
it('should should fill in empty attributes for scripts: ' + scriptEmpty, function() {
var script = create(scriptEmpty)[0];
// Should exist but be null
expect(script.parent).to.be(null);
expect(script.next).to.be(null);
expect(script.prev).to.be(null);
// Should exist but be empty
expect(script.children).to.be.empty();
expect(script.attribs).to.be.ok();
});
it('should should fill in empty attributes for styles: ' + styleEmpty, function() {
var style = create(styleEmpty)[0];
// Should exist but be null
expect(style.parent).to.be(null);
expect(style.next).to.be(null);
expect(style.prev).to.be(null);
// Should exist but be empty
expect(style.children).to.be.empty();
expect(style.attribs).to.be.ok();
});
it('should have next and prev siblings: ' + siblings, function() {
var dom = create(siblings),
h2 = dom[0],
p = dom[1];
// No parents
expect(h2.parent).to.be(null);
expect(p.parent).to.be(null);
// Neighbors
expect(h2.next.name).to.equal('p');
expect(p.prev.name).to.equal('h2');
// Should exist but be empty
expect(h2.children).to.be.empty();
expect(h2.attribs).to.be.ok();
expect(p.children).to.be.empty();
expect(p.attribs).to.be.ok();
});
it('should connect child with parent: ' + children, function() {
var html = create(children)[0],
br = html.children[0];
// html has 1 child and it's <br>
expect(html.children).to.have.length(1);
expect(html.children[0].name).to.equal('br');
// br's parent is html
expect(br.parent.name).to.equal('html');
});
it('should fill in some empty attributes for comments: ' + comment, function() {
var elem = create(comment)[0];
// Should exist but be null
expect(elem.parent).to.be(null);
expect(elem.next).to.be(null);
expect(elem.prev).to.be(null);
// Should not exist at all
expect(elem.children).to.not.be.ok();
expect(elem.attribs).to.not.be.ok();
});
it('should fill in some empty attributes for text: ' + text, function() {
var text = create(text)[0];
// Should exist but be null
expect(text.parent).to.be(null);
expect(text.next).to.be(null);
expect(text.prev).to.be(null);
// Should not exist at all
expect(text.children).to.not.be.ok();
expect(text.attribs).to.not.be.ok();
});
it('should fill in some empty attributes for directives: ' + directive, function() {
var elem = create(directive)[0];
// Should exist but be null
expect(elem.parent).to.be(null);
expect(elem.next).to.be(null);
expect(elem.prev).to.be(null);
// Should not exist at all
expect(elem.children).to.not.be.ok();
expect(elem.attribs).to.not.be.ok();
});
});
describe('.parse', function() {

@@ -281,3 +162,3 @@

var child = root.children[0];
expect(child.parent).to.equal(root);
expect(child.parent).to.be(null);
}

@@ -298,3 +179,3 @@

expect(root.children[1].name).to.equal('p');
expect(root.children[1].parent.name).to.equal('root');
expect(root.children[1].parent).to.equal(null);
});

@@ -301,0 +182,0 @@

@@ -21,3 +21,3 @@ var expect = require('expect.js'),

it('should handle double quotes within single quoted attributes properly', function() {
var str = "<hr class='an \"edge\" case' />";
var str = '<hr class=\'an "edge" case\' />';
expect(html(str)).to.equal('<hr class="an &quot;edge&quot; case">');

@@ -51,5 +51,5 @@ });

it('should ignore whitespace if specified', function() {
it('should normalize whitespace if specified', function() {
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>';
expect(html(str, {ignoreWhitespace: true})).to.equal('<a href="./haha.html">hi</a><a href="./blah.html">blah </a>');
expect(html(str, { normalizeWhitespace: true })).to.equal('<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>');
});

@@ -56,0 +56,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc