Socket
Socket
Sign inDemoInstall

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.18.0 to 0.19.0

lib/api/forms.js

33

History.md
0.19.0 / 2015-03-21
==================
* fixed allignment (fb55)
* added test case for malformed json in data attributes (fb55)
* fix: handle some extreme cases like `data-custom="{{templatevar}}"`. There is possibility error while parsing json . (Harish.K)
* Add missing optional selector doc for {prev,next}{All,Until} (Jérémie Astori)
* update to dom-serializer@0.1.0 (Felix Böhm)
* Document `Cheerio#serialzeArray` (Mike Pennisi)
* Fixed up `serializeArray()` and added multiple support (Todd Wolfson)
* Implement serializeArray() (Jarno Leppänen)
* recognize options in $.xml() (fb55)
* lib/static.js: text(): rm errant space before ++ (Chris Rebert)
* Do not expose internal `children` array (Mike Pennisi)
* Change lodash dependencies to ^3.1.0 (Samy Pessé)
* Update lodash@3.1.0 (Samy Pessé)
* Updates Readme.md: .not(function (index, elem)) (Patrick Ward)
* update to css-select@1.0.0 (fb55)
* Allow failures in Node.js v0.11 (Mike Pennisi)
* Added: Gittask badge (Matthew Mueller)
* Isolate prototypes of functions created via `load` (Mike Pennisi)
* Updates Readme.md: adds JS syntax highlighting (frankcash)
* #608 -- Add support for insertBefore/insertAfter syntax. Supports target types of: $, [$], selector (both single and multiple results) (Ben Cochran)
* Clone input nodes when inserting over a set (Mike Pennisi)
* Move unit test files (Mike Pennisi)
* remove unnecessarily tricky code (David Chambers)
* pass options to $.html in toString (fb55)
* add license info to package.json (Chris Rebert)
* xyz@~0.5.0 (David Chambers)
* Remove unofficial signature of `children` (Mike Pennisi)
* Fix bug in `css` method (Mike Pennisi)
* Correct bug in implementation of `Cheerio#val` (Mike Pennisi)
0.18.0 / 2014-11-06

@@ -3,0 +36,0 @@ ==================

21

lib/api/attributes.js

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

} else if (rbrace.test(value)) {
value = JSON.parse(value);
try {
value = JSON.parse(value);
} catch(e){ }
}

@@ -170,17 +172,6 @@

case 'radio':
var queryString = 'input[type=radio][name="' + this.attr('name') + '"]:checked';
var parentEl, root;
// Go up until we hit a form or root
parentEl = this.closest('form');
if (parentEl.length === 0) {
root = (this.parents().last()[0] || this[0]).root;
parentEl = this._make(root);
}
if (querying) {
return parentEl.find(queryString).attr('value');
return this.attr('value');
} else {
parentEl.find(':checked').removeAttr('checked');
parentEl.find('input[type=radio][value="' + value + '"]').attr('checked', '');
this.attr('value', value);
return this;

@@ -301,3 +292,3 @@ }

var appendClass = classNames[j] + ' ';
if (!~setClass.indexOf(' ' + appendClass))
if (setClass.indexOf(' ' + appendClass) < 0)
setClass += appendClass;

@@ -304,0 +295,0 @@ }

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

if (typeof val === 'function') {
val = val.call(el, idx, el);
val = val.call(el, idx, styles[prop]);
}

@@ -43,0 +43,0 @@

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

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

@@ -13,13 +14,15 @@

// necessary
exports._makeDomArray = function makeDomArray(elem) {
exports._makeDomArray = function makeDomArray(elem, clone) {
if (elem == null) {
return [];
} else if (elem.cheerio) {
return elem.get();
return clone ? cloneDom(elem.get(), elem.options) : elem.get();
} else if (Array.isArray(elem)) {
return _.flatten(elem.map(makeDomArray, this));
return _.flatten(elem.map(function(el) {
return this._makeDomArray(el, clone);
}, this));
} else if (typeof elem === 'string') {
return evaluate(elem, this.options);
} else {
return [elem];
return clone ? cloneDom([elem]) : [elem];
}

@@ -30,16 +33,17 @@ };

return function() {
var self = this,
elems = slice.call(arguments),
dom = this._makeDomArray(elems);
var elems = slice.call(arguments),
lastIdx = this.length - 1;
if (typeof elems[0] === 'function') {
return domEach(this, function(i, el) {
dom = self._makeDomArray(elems[0].call(el, i, $.html(el.children)));
concatenator(dom, el.children, el);
});
} else {
return domEach(this, function(i, el) {
concatenator(dom, el.children, el);
});
}
return domEach(this, function(i, el) {
var dom, domSrc;
if (typeof elems[0] === 'function') {
domSrc = elems[0].call(el, i, $.html(el.children));
} else {
domSrc = elems;
}
dom = this._makeDomArray(domSrc, i < lastIdx);
concatenator(dom, el.children, el);
});
};

@@ -100,3 +104,2 @@ };

}
return array.splice.apply(array, spliceArgs);

@@ -115,4 +118,3 @@ };

var elems = slice.call(arguments),
dom = this._makeDomArray(elems),
self = this;
lastIdx = this.length - 1;

@@ -126,13 +128,17 @@ domEach(this, function(i, el) {

var siblings = parent.children,
index = siblings.indexOf(el);
index = siblings.indexOf(el),
domSrc, dom;
// If not found, move on
if (!~index) return;
if (index < 0) return;
if (typeof elems[0] === 'function') {
dom = self._makeDomArray(elems[0].call(el, i, $.html(el.children)));
domSrc = elems[0].call(el, i, $.html(el.children));
} else {
domSrc = elems;
}
dom = this._makeDomArray(domSrc, i < lastIdx);
// Add element after `this` element
uniqueSplice(siblings, ++index, 0, dom, parent);
uniqueSplice(siblings, index + 1, 0, dom, parent);
});

@@ -143,6 +149,33 @@

exports.insertAfter = function(target) {
var clones = [],
self = this;
if (typeof target === 'string') {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}
target = this._makeDomArray(target);
self.remove();
domEach(target, function(i, el) {
var clonedSelf = self._makeDomArray(self.clone());
var parent = el.parent || el.root;
if (!parent) {
return;
}
var siblings = parent.children,
index = siblings.indexOf(el);
// If not found, move on
if (index < 0) return;
// Add cloned `this` element(s) after target element
uniqueSplice(siblings, index + 1, 0, clonedSelf, parent);
clones.push(clonedSelf);
});
return this.constructor.call(this.constructor, this._makeDomArray(clones));
};
exports.before = function() {
var elems = slice.call(arguments),
dom = this._makeDomArray(elems),
self = this;
lastIdx = this.length - 1;

@@ -156,11 +189,16 @@ domEach(this, function(i, el) {

var siblings = parent.children,
index = siblings.indexOf(el);
index = siblings.indexOf(el),
domSrc, dom;
// If not found, move on
if (!~index) return;
if (index < 0) return;
if (typeof elems[0] === 'function') {
dom = self._makeDomArray(elems[0].call(el, i, $.html(el.children)));
domSrc = elems[0].call(el, i, $.html(el.children));
} else {
domSrc = elems;
}
dom = this._makeDomArray(domSrc, i < lastIdx);
// Add element before `el` element

@@ -173,2 +211,30 @@ uniqueSplice(siblings, index, 0, dom, parent);

exports.insertBefore = function(target) {
var clones = [],
self = this;
if (typeof target === 'string') {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}
target = this._makeDomArray(target);
self.remove();
domEach(target, function(i, el) {
var clonedSelf = self._makeDomArray(self.clone());
var parent = el.parent || el.root;
if (!parent) {
return;
}
var siblings = parent.children,
index = siblings.indexOf(el);
// If not found, move on
if (index < 0) return;
// Add cloned `this` element(s) after target element
uniqueSplice(siblings, index, 0, clonedSelf, parent);
clones.push(clonedSelf);
});
return this.constructor.call(this.constructor, this._makeDomArray(clones));
};
/*

@@ -193,5 +259,4 @@ remove([selector])

if (!~index) return;
if (index < 0) return;
siblings.splice(index, 1);

@@ -273,3 +338,3 @@ if (el.prev) {

exports.toString = function() {
return $.html(this);
return $.html(this, this.options);
};

@@ -311,5 +376,3 @@

exports.clone = function() {
// Turn it into HTML, then recreate it,
// Seems to be the easiest way to reconnect everything correctly
return this._make($.html(this, this.options));
return this._make(cloneDom(this.get(), this.options));
};
var _ = require('lodash'),
select = require('CSSselect'),
select = require('css-select'),
utils = require('../utils'),

@@ -280,3 +280,2 @@ domEach = utils.domEach,

if (selector === undefined) return this._make(elems);
else if (typeof selector === 'number') return this._make(elems[selector]);

@@ -283,0 +282,0 @@ return exports.filter.call(elems, selector, this);

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

require('./api/manipulation'),
require('./api/css')
require('./api/css'),
require('./api/forms')
];

@@ -138,3 +139,3 @@

Cheerio.prototype._make = function(dom, context) {
var cheerio = new Cheerio(dom, context, this._root, this.options);
var cheerio = new this.constructor(dom, context, this._root, this.options);
cheerio.prevObject = this;

@@ -141,0 +142,0 @@ return cheerio;

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

var select = require('CSSselect'),
var select = require('css-select'),
parse = require('./parse'),
render = require('dom-serializer'),
serialize = require('dom-serializer'),
_ = require('lodash');

@@ -23,2 +23,5 @@

var initialize = function(selector, context, r, opts) {
if (!(this instanceof initialize)) {
return new initialize(selector, context, r, opts);
}
opts = _.defaults(opts || {}, options);

@@ -30,4 +33,12 @@ return Cheerio.call(this, selector, context, r || root, opts);

// true Cheerio instances.
initialize.prototype = Cheerio.prototype;
initialize.prototype = Object.create(Cheerio.prototype);
initialize.prototype.constructor = initialize;
// Mimic jQuery's prototype alias for plugin authors.
initialize.fn = initialize.prototype;
// Keep a reference to the top-level scope so we can chain methods that implicitly
// resolve selectors; e.g. $("<span>").(".bar"), which otherwise loses ._root
initialize.prototype._originalRoot = root;
// Add in the static methods

@@ -44,4 +55,22 @@ _.merge(initialize, exports);

/*
* Helper function
*/
function render(that, dom, options) {
if (!dom) {
if (that._root && that._root.children) {
dom = that._root.children;
} else {
return '';
}
} else if (typeof dom === 'string') {
dom = select(dom, that._root, options);
}
return serialize(dom, options);
}
/**
* $.html([selector | dom])
* $.html([selector | dom], [options])
*/

@@ -66,10 +95,3 @@

if (dom) {
dom = (typeof dom === 'string') ? select(dom, this._root, options) : dom;
return render(dom, options);
} else if (this._root && this._root.children) {
return render(this._root.children, options);
} else {
return '';
}
return render(this, dom, options);
};

@@ -82,10 +104,5 @@

exports.xml = function(dom) {
if (dom) {
dom = (typeof dom === 'string') ? select(dom, this._root, this.options) : dom;
return render(dom, { xmlMode: true });
} else if (this._root && this._root.children) {
return render(this._root.children, { xmlMode: true });
} else {
return '';
}
var options = _.defaults({xmlMode: true}, this._options);
return render(this, dom, options);
};

@@ -104,3 +121,3 @@

for (var i = 0; i < len; i ++) {
for (var i = 0; i < len; i++) {
elem = elems[i];

@@ -137,3 +154,8 @@ if (elem.type === 'text') ret += elem.data;

return parsed.root()[0].children;
// The `children` array is used by Cheerio internally to group elements that
// share the same parents. When nodes created through `parseHTML` are
// inserted into previously-existing DOM structures, they will be removed
// from the `children` array. The results of `parseHTML` should remain
// constant across these operations, so a shallow copy should be returned.
return parsed.root()[0].children.slice();
};

@@ -140,0 +162,0 @@

@@ -0,1 +1,4 @@

var parse = require('./parse'),
render = require('dom-serializer');
/**

@@ -49,4 +52,15 @@ * HTML Tags

var i = 0, len = cheerio.length;
while (i < len && fn(i, cheerio[i]) !== false) ++i;
while (i < len && fn.call(cheerio, i, cheerio[i]) !== false) ++i;
return cheerio;
};
/**
* Create a deep copy of the given DOM structure by first rendering it to a
* string and then parsing the resultant markup.
*
* @argument {Object} dom - The htmlparser2-compliant DOM structure
* @argument {Object} options - The parsing/rendering options
*/
exports.cloneDom = function(dom, options) {
return parse(render(dom, options), options).children;
};
{
"name": "cheerio",
"version": "0.18.0",
"version": "0.19.0",
"description": "Tiny, fast, and elegant implementation of core jQuery designed specifically for the server",
"author": "Matt Mueller <mattmuelle@gmail.com> (mat.io)",
"license": "MIT",
"keywords": [

@@ -23,7 +24,7 @@ "htmlparser",

"dependencies": {
"CSSselect": "~0.4.0",
"css-select": "~1.0.0",
"entities": "~1.1.1",
"htmlparser2": "~3.8.1",
"dom-serializer": "~0.0.0",
"lodash": "~2.4.1"
"dom-serializer": "~0.1.0",
"lodash": "^3.2.0"
},

@@ -38,3 +39,3 @@ "devDependencies": {

"mocha": "*",
"xyz": "~0.4.0"
"xyz": "~0.5.0"
},

@@ -41,0 +42,0 @@ "scripts": {

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

# cheerio [![Build Status](https://secure.travis-ci.org/cheeriojs/cheerio.svg?branch=master)](http://travis-ci.org/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)

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

### Forms
#### .serializeArray()
Encode a set of form elements as an array of names and values.
```js
$('<form><input name="foo" value="bar" /></form>').serializeArray()
//=> [ { name: 'foo', valule: 'bar' } ]
```
### Traversing

@@ -303,4 +313,4 @@

#### .nextAll()
Gets all the following siblings of the first selected element.
#### .nextAll([selector])
Gets all the following siblings of the first selected element, optionally filtered by a selector.

@@ -310,6 +320,8 @@ ```js

//=> [<li class="orange">Orange</li>, <li class="pear">Pear</li>]
$('.apple').nextAll('.orange')
//=> [<li class="orange">Orange</li>]
```
#### .nextUntil()
Gets all the following siblings up to but not including the element matched by the selector.
#### .nextUntil([selector], [filter])
Gets all the following siblings up to but not including the element matched by the selector, optionally filtered by another selector.

@@ -329,4 +341,4 @@ ```js

#### .prevAll()
Gets all the preceding siblings of the first selected element.
#### .prevAll([selector])
Gets all the preceding siblings of the first selected element, optionally filtered by a selector.

@@ -336,6 +348,8 @@ ```js

//=> [<li class="orange">Orange</li>, <li class="apple">Apple</li>]
$('.pear').prevAll('.orange')
//=> [<li class="orange">Orange</li>]
```
#### .prevUntil()
Gets all the preceding siblings up to but not including the element matched by the selector.
#### .prevUntil([selector], [filter])
Gets all the preceding siblings up to but not including the element matched by the selector, optionally filtered by another selector.

@@ -449,3 +463,3 @@ ```js

```js
$('li').filter(function(i, el) {
$('li').not(function(i, el) {
// this === el

@@ -524,3 +538,3 @@ return $(this).attr('class') === 'orange';

```
```js
$('.pear').index()

@@ -608,2 +622,16 @@ //=> 2

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

@@ -623,2 +651,16 @@ Insert content previous to each element in the set of matched elements.

#### .insertBefore( content )
Insert every element in the set of matched elements before the target.
```js
$('<li class="plum">Plum</li>').insertBefore('.apple')
$.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>
```
#### .remove( [selector] )

@@ -747,2 +789,15 @@ Removes the set of matched elements from the DOM and all their children. `selector` filters the set of matched elements to be removed.

### Plugins
Once you have loaded a document, you may extend the prototype or the equivalent `fn` property with custom plugin methods:
```js
var $ = cheerio.load('<html><body>Hello, <b>world</b>!</body></html>');
$.prototype.logHtml = function() {
console.log(this.html());
};
$('body').logHtml(); // logs "Hello, <b>world</b>!" to the console
```
### The "DOM Node" object

@@ -749,0 +804,0 @@

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

describe('prototype extensions', function() {
it('should honor extensions defined on `prototype` property', function() {
var $c = $.load('<div>');
var $div;
$c.prototype.myPlugin = function() {
return {
context: this,
args: arguments
};
};
$div = $c('div');
expect($div.myPlugin).to.be.a('function');
expect($div.myPlugin().context).to.be($div);
expect(Array.prototype.slice.call($div.myPlugin(1, 2, 3).args))
.to.eql([1, 2, 3]);
});
it('should honor extensions defined on `fn` property', function() {
var $c = $.load('<div>');
var $div;
$c.fn.myPlugin = function() {
return {
context: this,
args: arguments
};
};
$div = $c('div');
expect($div.myPlugin).to.be.a('function');
expect($div.myPlugin().context).to.be($div);
expect(Array.prototype.slice.call($div.myPlugin(1, 2, 3).args))
.to.eql([1, 2, 3]);
});
it('should isolate extensions between loaded functions', function() {
var $a = $.load('<div>');
var $b = $.load('<div>');
$a.prototype.foo = function() {};
expect($b('div').foo).to.be(undefined);
});
});
});
});

@@ -55,1 +55,12 @@ /* jshint indent: false */

].join('');
exports.forms = [
'<form id="simple"><input type="text" name="fruit" value="Apple" /></form>',
'<form id="nested"><div><input type="text" name="fruit" value="Apple" /></div><input type="text" name="vegetable" value="Carrot" /></form>',
'<form id="disabled"><input type="text" name="fruit" value="Apple" disabled /></form>',
'<form id="submit"><input type="text" name="fruit" value="Apple" /><input type="submit" name="submit" value="Submit" /></form>',
'<form id="select"><select name="fruit"><option value="Apple">Apple</option><option value="Orange" selected>Orange</option></select></form>',
'<form id="unnamed"><input type="text" name="fruit" value="Apple" /><input type="text" value="Carrot" /></form>',
'<form id="multiple"><select name="fruit" multiple><option value="Apple" selected>Apple</option><option value="Orange" selected>Orange</option><option value="Carrot">Carrot</option></select></form>',
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>'
].join('');

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

it('should escape entities', function(){
var str = '<tag attr="foo &amp; bar"/>';
expect(xml(str)).to.equal(str);
});
});

@@ -32,0 +37,0 @@

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