Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cheerio

Package Overview
Dependencies
Maintainers
4
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.19.0 to 0.20.0

41

History.md
0.20.0 / 2016-02-01
==================
* Add coveralls badge, remove link to old report (Felix Böhm)
* Update lodash dependeny to 4.1.0 (leif.hanack)
* Fix PR #726 adding 'appendTo()' and 'prependTo()' (Delgan)
* Added appendTo and prependTo with tests #641 (digihaven)
* Fix #780 by changing options context in '.find()' (Felix Böhm)
* Add an unit test checking the query of child (Delgan)
* fix #667: attr({foo: null}) removes attribute foo, like attr('foo', null) (Ray Waldin)
* Include reference to dedicated "Loading" section (Mike Pennisi)
* Added load method to $ (alanev)
* update css-select to 1.2.0 (Felix Böhm)
* Fixing Grammatical Error (Dan Corman)
* Test against node v0.12 --> v4.2 (Jason Kurian)
* Correct output in example (Felix Böhm)
* Fix npm files filter (Bogdan Chadkin)
* Enable setting data on all elements in selection (Mike Pennisi)
* Reinstate `$.fn.toArray` (Mike Pennisi)
* update css-select to 1.1.0 (Thomas Shafer)
* Complete implementation of `wrap` (Mike Pennisi)
* Correct name of unit test (Mike Pennisi)
* Correct grammar in test titles (Mike Pennisi)
* Normalize whitespace (Mike Pennisi)
* Insert omitted assertion (Mike Pennisi)
* Update invocation of `children` (Mike Pennisi)
* Begin implementation of `wrap` method (Dandlezzz)
* Update Readme.md (Sven Slootweg)
* fix document's mistake in Readme.md (exoticknight)
* Add tests for setting text and html as non-strings (Ryc O'Chet)
* Fix for passing non-string values to .html or .text (Ryc O'Chet)
* use a selector to filter form elements (fb55)
* fix README.md typo (Yutian Li)
* README: fix spelling (Chris Rebert)
* Added support for options without a `value` attribute. Fixes #633 (Todd Wolfson)
* responding to pull request feedback - remove item() method and related tests (Ray Waldin)
* add length property and item method to object returned by prop('style'), plus tests (Ray Waldin)
* Added .prop method to readme (Artem Burtsev)
* Added .prop method (Artem Burtsev)
* Added Gitter badge (The Gitter Badger)
0.19.0 / 2015-03-21

@@ -3,0 +44,0 @@ ==================

83

lib/api/attributes.js
var _ = require('lodash'),
$ = require('../static'),
utils = require('../utils'),

@@ -41,2 +42,7 @@ isTag = utils.isTag,

}
// Mimic the DOM and return text content as value for `option's`
if (elem.name === 'option' && name === 'value') {
return $.text(elem.children);
}
};

@@ -65,4 +71,4 @@

if (typeof name === 'object') {
_.each(name, function(name, key) {
el.attribs[key] = name+'';
_.each(name, function(value, name) {
setAttr(el, name, value);
});

@@ -78,3 +84,72 @@ } else {

var getProp = function (el, name) {
return el.hasOwnProperty(name)
? el[name]
: rboolean.test(name)
? getAttr(el, name) !== undefined
: getAttr(el, name);
};
var setProp = function (el, name, value) {
el[name] = rboolean.test(name) ? !!value : value;
};
exports.prop = function (name, value) {
var i = 0,
property;
if (typeof name === 'string' && value === undefined) {
switch (name) {
case 'style':
property = this.css();
_.each(property, function (v, p) {
property[i++] = p;
});
property.length = i;
break;
case 'tagName':
case 'nodeName':
property = this[0].name.toUpperCase();
break;
default:
property = getProp(this[0], name);
}
return property;
}
if (typeof name === 'object' || value !== undefined) {
if (typeof value === 'function') {
return domEach(this, function(i, el) {
setProp(el, name, value.call(el, i, getProp(el, name)));
});
}
return domEach(this, function(i, el) {
if (!isTag(el)) return;
if (typeof name === 'object') {
_.each(name, function(val, name) {
setProp(el, name, val);
});
} else {
setProp(el, name, value);
}
});
}
};
var setData = function(el, name, value) {
if (!el.data) {
el.data = {};
}
if (typeof name === 'object') return _.extend(el.data, name);

@@ -206,3 +281,3 @@ if (typeof name === 'string' && value !== undefined) {

domEach(option, function(i, el) {
returnValue.push(el.attribs.value);
returnValue.push(getAttr(el, 'value'));
});

@@ -241,3 +316,3 @@ }

exports.hasClass = function(className) {
return _.any(this, function(elem) {
return _.some(this, function(elem) {
var attrs = elem.attribs,

@@ -244,0 +319,0 @@ clazz = attrs && attrs['class'],

19

lib/api/forms.js

@@ -5,5 +5,3 @@ // https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js

submittableSelector = 'input,select,textarea,keygen',
rCRLF = /\r?\n/g,
rcheckableType = /^(?:checkbox|radio)$/i,
rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i;
rCRLF = /\r?\n/g;

@@ -21,14 +19,11 @@ exports.serializeArray = function() {

}
}).filter(function() {
var $elem = Cheerio(this);
var type = $elem.attr('type');
// Verify elements have a name (`attr.name`) and are not disabled (`:disabled`)
return $elem.attr('name') && !$elem.is(':disabled') &&
}).filter(
// Verify elements have a name (`attr.name`) and are not disabled (`:disabled`)
'[name!=""]:not(:disabled)'
// and cannot be clicked (`[type=submit]`) or are used in `x-www-form-urlencoded` (`[type=file]`)
!rsubmitterTypes.test(type) &&
+ ':not(:submit, :button, :image, :reset, :file)'
// and are either checked/don't have a checkable state
($elem.attr('checked') || !rcheckableType.test(type));
+ ':matches([checked], :not(:checkbox, :radio))'
// Convert each of the elements to its value(s)
}).map(function(i, elem) {
).map(function(i, elem) {
var $elem = Cheerio(elem);

@@ -35,0 +30,0 @@ var name = $elem.attr('name');

@@ -9,2 +9,3 @@ var _ = require('lodash'),

cloneDom = utils.cloneDom,
isHtml = utils.isHtml,
slice = Array.prototype.slice;

@@ -105,2 +106,22 @@

exports.appendTo = function(target) {
if (!target.cheerio) {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}
target.append(this);
return this;
};
exports.prependTo = function(target) {
if (!target.cheerio) {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}
target.prepend(this);
return this;
};
exports.append = _insert(function(dom, children, parent) {

@@ -114,2 +135,36 @@ uniqueSplice(children, children.length, 0, dom, parent);

exports.wrap = function(wrapper) {
var wrapperFn = typeof wrapper === 'function' && wrapper,
lastIdx = this.length - 1;
_.forEach(this, _.bind(function(el, i) {
var parent = el.parent || el.root,
siblings = parent.children,
dom, index;
if (!parent) {
return;
}
if (wrapperFn) {
wrapper = wrapperFn.call(el, i);
}
if (typeof wrapper === 'string' && !isHtml(wrapper)) {
wrapper = this.parents().last().find(wrapper).clone();
}
dom = this._makeDomArray(wrapper, i < lastIdx).slice(0, 1);
index = siblings.indexOf(el);
updateDOM([el], dom[0]);
// The previous operation removed the current element from the `siblings`
// array, so the `dom` array can be inserted without removing any
// additional elements.
uniqueSplice(siblings, index, 0, dom, parent);
}, this));
return this;
};
exports.after = function() {

@@ -322,3 +377,3 @@ var elems = slice.call(arguments),

var content = str.cheerio ? str.clone().get() : evaluate(str, opts);
var content = str.cheerio ? str.clone().get() : evaluate('' + str, opts);

@@ -354,3 +409,3 @@ updateDOM(content, el);

var elem = {
data: str,
data: '' + str,
type: 'text',

@@ -357,0 +412,0 @@ parent: el,

@@ -32,3 +32,5 @@ var _ = require('lodash'),

return this._make(select(selectorOrHaystack, elems, this.options));
var options = {__proto__: this.options, context: this.toArray()};
return this._make(select(selectorOrHaystack, elems, options));
};

@@ -263,4 +265,3 @@

parent ? parent.children() : this.siblingsAndMe(),
function(elem) { return isTag(elem) && !this.is(elem); },
this
_.bind(function(elem) { return isTag(elem) && !this.is(elem); }, this)
);

@@ -267,0 +268,0 @@

@@ -6,2 +6,3 @@ /*

var parse = require('./parse'),
isHtml = require('./utils').isHtml,
_ = require('lodash');

@@ -22,8 +23,2 @@

/*
* A simple way to check for HTML strings or ID strings
*/
var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
/*
* Instance of cheerio

@@ -54,5 +49,5 @@ */

if (Array.isArray(selector)) {
_.forEach(selector, function(elem, idx) {
_.forEach(selector, _.bind(function(elem, idx) {
this[idx] = elem;
}, this);
}, this));
this.length = selector.length;

@@ -123,14 +118,2 @@ return this;

/*
* Check if string is HTML
*/
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;
// Run the regex
var match = quickExpr.exec(str);
return !!(match && match[1]);
};
/*
* Make a cheerio object

@@ -149,4 +132,2 @@ *

* Turn a cheerio object into an array
*
* @deprecated
*/

@@ -153,0 +134,0 @@

@@ -66,1 +66,19 @@ var parse = require('./parse'),

};
/*
* A simple way to check for HTML strings or ID strings
*/
var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
/*
* Check if string is HTML
*/
exports.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;
// Run the regex
var match = quickExpr.exec(str);
return !!(match && match[1]);
};
{
"name": "cheerio",
"version": "0.19.0",
"version": "0.20.0",
"description": "Tiny, fast, and elegant implementation of core jQuery designed specifically for the server",

@@ -20,2 +20,6 @@ "author": "Matt Mueller <mattmuelle@gmail.com> (mat.io)",

"main": "./index.js",
"files": [
"index.js",
"lib"
],
"engines": {

@@ -25,7 +29,7 @@ "node": ">= 0.6"

"dependencies": {
"css-select": "~1.0.0",
"css-select": "~1.2.0",
"entities": "~1.1.1",
"htmlparser2": "~3.8.1",
"dom-serializer": "~0.1.0",
"lodash": "^3.2.0"
"lodash": "^4.1.0"
},

@@ -37,3 +41,2 @@ "devDependencies": {

"istanbul": "~0.2",
"jsdom": "~0.8.9",
"jshint": "~2.5.1",

@@ -45,3 +48,6 @@ "mocha": "*",

"test": "make test"
},
"optionalDependencies": {
"jsdom": "^7.0.2"
}
}

@@ -1,2 +0,2 @@

# cheerio [![Build Status](https://secure.travis-ci.org/cheeriojs/cheerio.svg?branch=master)](http://travis-ci.org/cheeriojs/cheerio) [![Gittask](https://gittask.com/cheeriojs/cheerio.svg)](https://gittask.com/cheeriojs/cheerio)
# cheerio [![Build Status](https://secure.travis-ci.org/cheeriojs/cheerio.svg?branch=master)](http://travis-ci.org/cheeriojs/cheerio) [![Gittask](https://gittask.com/cheeriojs/cheerio.svg)](https://gittask.com/cheeriojs/cheerio) [![Coverage](http://img.shields.io/coveralls/cheeriojs/cheerio.svg?branch=master&style=flat)](https://coveralls.io/r/cheeriojs/cheerio) [![Join the chat at https://gitter.im/cheeriojs/cheerio](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cheeriojs/cheerio?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

@@ -115,3 +115,3 @@ Fast, flexible, and lean implementation of core jQuery designed specifically for the server.

#### $( selector, [context], [root] )
`selector` searches within the `context` scope which searches within the `root` scope. `selector` and `context` can be an string expression, DOM Element, array of DOM elements, or cheerio object. `root` is typically the HTML document string.
`selector` searches within the `context` scope which searches within the `root` scope. `selector` and `context` can be a string expression, DOM Element, array of DOM elements, or cheerio object. `root` is typically the HTML document string.

@@ -128,3 +128,3 @@ This selector method is the starting point for traversing and manipulating the document. Like jQuery, it's the primary method for selecting elements in the document, but unlike jQuery it's built on top of the CSSSelect library, which implements most of the Sizzle selectors.

$('li[class=orange]').html()
//=> <li class="orange">Orange</li>
//=> Orange
```

@@ -148,2 +148,15 @@

#### .prop( name, value )
Method for getting and setting properties. Gets the property value for only the first element in the matched set.
```js
$('input[type="checkbox"]').prop('checked')
//=> false
$('input[type="checkbox"]').prop('checked', true).val()
//=> ok
```
> See http://api.jquery.com/prop/ for more information
#### .data( name, value )

@@ -156,3 +169,3 @@ Method for getting and setting data attributes. Gets or sets the data attribute value for only the first element in the matched set.

$('<div data-apple-color="red"></div>').data('data-apple-color')
$('<div data-apple-color="red"></div>').data('apple-color')
//=> 'red'

@@ -253,3 +266,3 @@

$('<form><input name="foo" value="bar" /></form>').serializeArray()
//=> [ { name: 'foo', valule: 'bar' } ]
//=> [ { name: 'foo', value: 'bar' } ]
```

@@ -372,3 +385,3 @@

#### .siblings( selector )
#### .siblings([selector])
Gets the first selected element's siblings, excluding itself.

@@ -385,3 +398,3 @@

#### .children( selector )
#### .children([selector])
Gets the children of the first selected element.

@@ -594,2 +607,16 @@

#### .appendTo( target )
Insert every element in the set of matched elements to the end of the target.
```js
$('<li class="plum">Plum</li>').appendTo('#fruits')
$.html()
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// <li class="plum">Plum</li>
// </ul>
```
#### .prepend( content, [content, ...] )

@@ -609,2 +636,16 @@ Inserts content as the *first* child of each of the selected elements.

#### .prependTo( target )
Insert every element in the set of matched elements to the beginning of the target.
```js
$('<li class="plum">Plum</li>').prependTo('#fruits')
$.html()
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
```
#### .after( content, [content, ...] )

@@ -624,3 +665,3 @@ Insert content next to each element in the set of matched elements.

#### .insertAfter( content )
#### .insertAfter( target )
Insert every element in the set of matched elements after the target.

@@ -653,3 +694,3 @@

#### .insertBefore( content )
#### .insertBefore( target )
Insert every element in the set of matched elements before the target.

@@ -727,2 +768,33 @@

#### .wrap( content )
The .wrap() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.
```js
var redFruit = $('<div class="red-fruit"></div>')
$('.apple').wrap(redFruit)
//=> <ul id="fruits">
// <div class="red-fruit">
// <li class="apple">Apple</li>
// </div>
// <li class="orange">Orange</li>
// <li class="plum">Plum</li>
// </ul>
var healthy = $('<div class="healthy"></div>')
$('li').wrap(healthy)
//=> <ul id="fruits">
// <div class="healthy">
// <li class="apple">Apple</li>
// </div>
// <div class="healthy">
// <li class="orange">Orange</li>
// </div>
// <div class="healthy">
// <li class="plum">Plum</li>
// </div>
// </ul>
```
#### .css( [propertName] ) <br /> .css( [ propertyNames] ) <br /> .css( [propertyName], [value] ) <br /> .css( [propertName], [function] ) <br /> .css( [properties] )

@@ -768,2 +840,10 @@

#### .toArray()
Retrieve all the DOM elements contained in the jQuery set as an array.
```js
$('li').toArray()
//=> [ {...}, {...}, {...} ]
```
#### .clone() ####

@@ -788,3 +868,3 @@ Clone the cheerio object.

#### $.contains( container, contained )
Checks to see if the `contained` DOM element is a descendent of the `container` DOM element.
Checks to see if the `contained` DOM element is a descendant of the `container` DOM element.

@@ -794,2 +874,5 @@ #### $.parseHTML( data [, context ] [, keepScripts ] )

#### $.load( html[, options ] )
Load in the HTML. (See the previous section titled "Loading" for more information.)
### Plugins

@@ -827,6 +910,2 @@

## Test Coverage
Cheerio has high-test coverage, you can view the report [here](https://s3.amazonaws.com/MattMueller/Coverage/cheerio.html).
## Testing

@@ -833,0 +912,0 @@

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