mirror of
https://github.com/cheeriojs/cheerio.git
synced 2026-02-25 23:25:17 +00:00
Upgrade dependencies
This commit is contained in:
@@ -26,12 +26,12 @@ Please make sure commit hooks are run, which will enforce the code style.
|
||||
|
||||
When implementing private functionality that isn't part of the jQuery API, please opt for:
|
||||
|
||||
* _Static methods_: If the functionality does not require a reference to a
|
||||
- _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 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
|
||||
- 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
|
||||
|
||||
1065
History.md
1065
History.md
File diff suppressed because it is too large
Load Diff
@@ -171,8 +171,8 @@ object to `.load()`:
|
||||
```js
|
||||
const $ = cheerio.load('<ul id="fruits">...</ul>', {
|
||||
xml: {
|
||||
normalizeWhitespace: true
|
||||
}
|
||||
normalizeWhitespace: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
@@ -293,7 +293,7 @@ Once you have loaded a document, you may extend the prototype or the equivalent
|
||||
|
||||
```js
|
||||
const $ = cheerio.load('<html><body>Hello, <b>world</b>!</body></html>');
|
||||
$.prototype.logHtml = function() {
|
||||
$.prototype.logHtml = function () {
|
||||
console.log(this.html());
|
||||
};
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
theme: jekyll-theme-cayman
|
||||
theme: jekyll-theme-cayman
|
||||
|
||||
@@ -16,101 +16,101 @@ if (process.argv.indexOf('--cheerio-only') >= 0) {
|
||||
}
|
||||
|
||||
suites.add('Select all', 'jquery.html', {
|
||||
test: function($) {
|
||||
test: function ($) {
|
||||
$('*').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('Select some', 'jquery.html', {
|
||||
test: function($) {
|
||||
test: function ($) {
|
||||
$('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
/*
|
||||
* Manipulation Tests
|
||||
*/
|
||||
suites.add('manipulation - append', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $body) {
|
||||
test: function ($, $body) {
|
||||
$body.append(new Array(50).join('<div>'));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// These tests run out of memory in jsdom
|
||||
suites.add('manipulation - prepend - highmem', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $body) {
|
||||
test: function ($, $body) {
|
||||
$body.prepend(new Array(50).join('<div>'));
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('manipulation - after - highmem', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $body) {
|
||||
test: function ($, $body) {
|
||||
$body.after(new Array(50).join('<div>'));
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('manipulation - before - highmem', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $body) {
|
||||
test: function ($, $body) {
|
||||
$body.before(new Array(50).join('<div>'));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
suites.add('manipulation - remove', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
var child = $('<div>');
|
||||
$lis.append(child);
|
||||
child.remove();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
suites.add('manipulation - replaceWith', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
$('body').append('<div id="foo">');
|
||||
},
|
||||
test: function($) {
|
||||
test: function ($) {
|
||||
$('#foo').replaceWith('<div id="foo">');
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
suites.add('manipulation - empty', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.empty();
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('manipulation - html', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.html();
|
||||
$lis.html('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('manipulation - html render', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.html();
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('manipulation - html independent', 'jquery.html', {
|
||||
setup: function() {
|
||||
setup: function () {
|
||||
return (
|
||||
'<div class="foo"><div id="bar">bat<hr>baz</div> </div>' +
|
||||
'<div class="foo"><div id="bar">bat<hr>baz</div> </div>' +
|
||||
@@ -120,218 +120,218 @@ suites.add('manipulation - html independent', 'jquery.html', {
|
||||
'<div class="foo"><div id="bar">bat<hr>baz</div> </div>'
|
||||
);
|
||||
},
|
||||
test: function($, content) {
|
||||
test: function ($, content) {
|
||||
$(content).html();
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('manipulation - text', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.text();
|
||||
$lis.text('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
/*
|
||||
* Traversing Tests
|
||||
*/
|
||||
suites.add('traversing - Find', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.find('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Parent', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.parent('div').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Parents', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.parents('div').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Closest', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.closest('div').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - next', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.next().length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - nextAll', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.nextAll('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - nextUntil', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.nextUntil('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - prev', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.prev().length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - prevAll', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.prevAll('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - prevUntil', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.prevUntil('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - siblings', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.siblings('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Children', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.children('a').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Filter', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.filter('li').length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - First', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.first().first().length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Last', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.last().last().length;
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('traversing - Eq', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.eq(0).eq(0).length;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
/*
|
||||
* Attributes Tests
|
||||
*/
|
||||
suites.add('attributes - Attributes', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.attr('foo', 'bar');
|
||||
$lis.attr('foo');
|
||||
$lis.removeAttr('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('attributes - Single Attribute', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('body');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.attr('foo', 'bar');
|
||||
$lis.attr('foo');
|
||||
$lis.removeAttr('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('attributes - Data', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.data('foo', 'bar');
|
||||
$lis.data('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('attributes - Val', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('select,input,textarea,option');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
$lis.each(function() {
|
||||
test: function ($, $lis) {
|
||||
$lis.each(function () {
|
||||
$(this).val();
|
||||
$(this).val('foo');
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
suites.add('attributes - Has class', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.hasClass('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('attributes - Toggle class', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.toggleClass('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
suites.add('attributes - Add Remove class', 'jquery.html', {
|
||||
setup: function($) {
|
||||
setup: function ($) {
|
||||
return $('li');
|
||||
},
|
||||
test: function($, $lis) {
|
||||
test: function ($, $lis) {
|
||||
$lis.addClass('foo');
|
||||
$lis.removeClass('foo');
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,17 +14,17 @@ var jQueryScript = new Script(jQuerySrc);
|
||||
var filterRe = /./;
|
||||
var cheerioOnly = false;
|
||||
|
||||
var Suites = (module.exports = function() {});
|
||||
var Suites = (module.exports = function () {});
|
||||
|
||||
Suites.prototype.filter = function(str) {
|
||||
Suites.prototype.filter = function (str) {
|
||||
filterRe = new RegExp(str, 'i');
|
||||
};
|
||||
|
||||
Suites.prototype.cheerioOnly = function() {
|
||||
Suites.prototype.cheerioOnly = function () {
|
||||
cheerioOnly = true;
|
||||
};
|
||||
|
||||
Suites.prototype.add = function(name, fileName, options) {
|
||||
Suites.prototype.add = function (name, fileName, options) {
|
||||
var markup, suite;
|
||||
if (!filterRe.test(name)) {
|
||||
return;
|
||||
@@ -32,21 +32,21 @@ Suites.prototype.add = function(name, fileName, options) {
|
||||
markup = fs.readFileSync(path.join(documentDir, fileName), 'utf8');
|
||||
suite = new Benchmark.Suite(name);
|
||||
|
||||
suite.on('start', function() {
|
||||
suite.on('start', function () {
|
||||
console.log('Test: ' + name + ' (file: ' + fileName + ')');
|
||||
});
|
||||
suite.on('cycle', function(event) {
|
||||
suite.on('cycle', function (event) {
|
||||
if (event.target.error) {
|
||||
return;
|
||||
}
|
||||
console.log('\t' + String(event.target));
|
||||
});
|
||||
suite.on('error', function(event) {
|
||||
suite.on('error', function (event) {
|
||||
console.log('*** Error in ' + event.target.name + ': ***');
|
||||
console.log('\t' + event.target.error);
|
||||
console.log('*** Test invalidated. ***');
|
||||
});
|
||||
suite.on('complete', function(event) {
|
||||
suite.on('complete', function (event) {
|
||||
if (event.target.error) {
|
||||
console.log();
|
||||
return;
|
||||
@@ -62,7 +62,7 @@ Suites.prototype.add = function(name, fileName, options) {
|
||||
}
|
||||
};
|
||||
|
||||
Suites.prototype._benchJsDom = function(suite, markup, options) {
|
||||
Suites.prototype._benchJsDom = function (suite, markup, options) {
|
||||
var testFn = options.test;
|
||||
|
||||
var dom = new JSDOM(markup, { runScripts: 'outside-only' });
|
||||
@@ -73,20 +73,20 @@ Suites.prototype._benchJsDom = function(suite, markup, options) {
|
||||
if (options.setup) {
|
||||
setupData = options.setup.call(null, dom.window.$);
|
||||
}
|
||||
suite.add('jsdom', function() {
|
||||
suite.add('jsdom', function () {
|
||||
testFn.call(null, dom.window.$, setupData);
|
||||
});
|
||||
suite.run();
|
||||
};
|
||||
|
||||
Suites.prototype._benchCheerio = function(suite, markup, options) {
|
||||
Suites.prototype._benchCheerio = function (suite, markup, options) {
|
||||
var $ = cheerio.load(markup);
|
||||
var testFn = options.test;
|
||||
var setupData;
|
||||
if (options.setup) {
|
||||
setupData = options.setup.call(null, $);
|
||||
}
|
||||
suite.add('cheerio', function() {
|
||||
suite.add('cheerio', function () {
|
||||
testFn.call(null, $, setupData);
|
||||
});
|
||||
};
|
||||
|
||||
23
index.js
23
index.js
@@ -23,25 +23,27 @@ exports.xml = staticMethods.xml;
|
||||
|
||||
/**
|
||||
* In order to promote consistency with the jQuery library, users are
|
||||
* encouraged to instead use the static method of the same name. For example:
|
||||
* encouraged to instead use the static method of the same name.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('<div><p></p></div>');
|
||||
* $.contains($('div').get(0), $('p').get(0)); // true
|
||||
* $.contains($('p').get(0), $('div').get(0)); // false
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
* @deprecated
|
||||
*/
|
||||
exports.contains = staticMethods.contains;
|
||||
|
||||
/**
|
||||
* In order to promote consistency with the jQuery library, users are
|
||||
* encouraged to instead use the static method of the same name. For example:
|
||||
* encouraged to instead use the static method of the same name.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('');
|
||||
* $.merge([1, 2], [3, 4]) // [1, 2, 3, 4]
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
* @deprecated
|
||||
*/
|
||||
exports.merge = staticMethods.merge;
|
||||
@@ -49,25 +51,26 @@ exports.merge = staticMethods.merge;
|
||||
/**
|
||||
* In order to promote consistency with the jQuery library, users are
|
||||
* encouraged to instead use the static method of the same name as it is
|
||||
* defined on the "loaded" Cheerio factory function. For example:
|
||||
* defined on the "loaded" Cheerio factory function.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('');
|
||||
* $.parseHTML('<b>markup</b>');
|
||||
*
|
||||
* @method
|
||||
* @deprecated see {@link static/parseHTML}
|
||||
* @function
|
||||
* @deprecated See {@link static/parseHTML}.
|
||||
*/
|
||||
exports.parseHTML = staticMethods.parseHTML;
|
||||
|
||||
/**
|
||||
* Users seeking to access the top-level element of a parsed document should
|
||||
* instead use the `root` static method of a "loaded" Cheerio function. For
|
||||
* example:
|
||||
* instead use the `root` static method of a "loaded" Cheerio function.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('');
|
||||
* $.root();
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
* @deprecated
|
||||
*/
|
||||
exports.root = staticMethods.root;
|
||||
|
||||
@@ -19,21 +19,21 @@ var text = require('../static').text,
|
||||
_ = {
|
||||
forEach: require('lodash/forEach'),
|
||||
extend: require('lodash/assignIn'),
|
||||
some: require('lodash/some')
|
||||
some: require('lodash/some'),
|
||||
},
|
||||
// Lookup table for coercing string data-* attributes to their corresponding
|
||||
// JavaScript primitives
|
||||
primitives = {
|
||||
null: null,
|
||||
true: true,
|
||||
false: false
|
||||
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 getAttr = function(elem, name) {
|
||||
var getAttr = function (elem, name) {
|
||||
if (!elem || !isTag(elem)) return;
|
||||
|
||||
if (!elem.attribs) {
|
||||
@@ -65,7 +65,7 @@ var getAttr = function(elem, name) {
|
||||
}
|
||||
};
|
||||
|
||||
var setAttr = function(el, name, value) {
|
||||
var setAttr = function (el, name, value) {
|
||||
if (value === null) {
|
||||
removeAttribute(el, name);
|
||||
} else {
|
||||
@@ -92,19 +92,19 @@ var setAttr = function(el, name, value) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/attr/}
|
||||
*/
|
||||
exports.attr = function(name, value) {
|
||||
exports.attr = function (name, value) {
|
||||
// Set the value (with attr map support)
|
||||
if (typeof name === 'object' || value !== undefined) {
|
||||
if (typeof value === 'function') {
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
setAttr(el, name, value.call(el, i, el.attribs[name]));
|
||||
});
|
||||
}
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
if (!isTag(el)) return;
|
||||
|
||||
if (typeof name === 'object') {
|
||||
_.forEach(name, function(objValue, objName) {
|
||||
_.forEach(name, function (objValue, objName) {
|
||||
setAttr(el, objName, objValue);
|
||||
});
|
||||
} else {
|
||||
@@ -116,7 +116,7 @@ exports.attr = function(name, value) {
|
||||
return getAttr(this[0], name);
|
||||
};
|
||||
|
||||
var getProp = function(el, name) {
|
||||
var getProp = function (el, name) {
|
||||
if (!el || !isTag(el)) return;
|
||||
|
||||
return name in el
|
||||
@@ -126,7 +126,7 @@ var getProp = function(el, name) {
|
||||
: getAttr(el, name);
|
||||
};
|
||||
|
||||
var setProp = function(el, name, value) {
|
||||
var setProp = function (el, name, value) {
|
||||
el[name] = rboolean.test(name) ? !!value : value;
|
||||
};
|
||||
|
||||
@@ -147,7 +147,7 @@ var setProp = function(el, name, value) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/prop/}
|
||||
*/
|
||||
exports.prop = function(name, value) {
|
||||
exports.prop = function (name, value) {
|
||||
var i = 0,
|
||||
property;
|
||||
|
||||
@@ -156,7 +156,7 @@ exports.prop = function(name, value) {
|
||||
case 'style':
|
||||
property = this.css();
|
||||
|
||||
_.forEach(property, function(v, p) {
|
||||
_.forEach(property, function (v, p) {
|
||||
property[i++] = p;
|
||||
});
|
||||
|
||||
@@ -168,10 +168,7 @@ exports.prop = function(name, value) {
|
||||
property = this[0].name.toUpperCase();
|
||||
break;
|
||||
case 'outerHTML':
|
||||
property = this.clone()
|
||||
.wrap('<container />')
|
||||
.parent()
|
||||
.html();
|
||||
property = this.clone().wrap('<container />').parent().html();
|
||||
break;
|
||||
default:
|
||||
property = getProp(this[0], name);
|
||||
@@ -182,16 +179,16 @@ exports.prop = function(name, value) {
|
||||
|
||||
if (typeof name === 'object' || value !== undefined) {
|
||||
if (typeof value === 'function') {
|
||||
return domEach(this, function(j, el) {
|
||||
return domEach(this, function (j, el) {
|
||||
setProp(el, name, value.call(el, j, getProp(el, name)));
|
||||
});
|
||||
}
|
||||
|
||||
return domEach(this, function(__, el) {
|
||||
return domEach(this, function (__, el) {
|
||||
if (!isTag(el)) return;
|
||||
|
||||
if (typeof name === 'object') {
|
||||
_.forEach(name, function(val, key) {
|
||||
_.forEach(name, function (val, key) {
|
||||
setProp(el, key, val);
|
||||
});
|
||||
} else {
|
||||
@@ -201,7 +198,7 @@ exports.prop = function(name, value) {
|
||||
}
|
||||
};
|
||||
|
||||
var setData = function(el, name, value) {
|
||||
var setData = function (el, name, value) {
|
||||
if (!el.data) {
|
||||
el.data = {};
|
||||
}
|
||||
@@ -216,15 +213,15 @@ var setData = function(el, name, value) {
|
||||
// and (if present) cache the value in the node's internal data store. If no
|
||||
// attribute name is specified, read *all* HTML5 `data-*` attributes in this
|
||||
// manner.
|
||||
var readData = function(el, name) {
|
||||
var readData = function (el, name) {
|
||||
var readAll = arguments.length === 1;
|
||||
var domNames, domName, jsNames, jsName, value, idx, length;
|
||||
|
||||
if (readAll) {
|
||||
domNames = Object.keys(el.attribs).filter(function(attrName) {
|
||||
domNames = Object.keys(el.attribs).filter(function (attrName) {
|
||||
return attrName.slice(0, dataAttrPrefix.length) === dataAttrPrefix;
|
||||
});
|
||||
jsNames = domNames.map(function(_domName) {
|
||||
jsNames = domNames.map(function (_domName) {
|
||||
return camelCase(_domName.slice(dataAttrPrefix.length));
|
||||
});
|
||||
} else {
|
||||
@@ -278,7 +275,7 @@ var readData = function(el, name) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/data/}
|
||||
*/
|
||||
exports.data = function(name, value) {
|
||||
exports.data = function (name, value) {
|
||||
var elem = this[0];
|
||||
|
||||
if (!elem || !isTag(elem)) return;
|
||||
@@ -294,7 +291,7 @@ exports.data = function(name, value) {
|
||||
|
||||
// Set the value (with attr map support)
|
||||
if (typeof name === 'object' || value !== undefined) {
|
||||
domEach(this, function(i, el) {
|
||||
domEach(this, function (i, el) {
|
||||
setData(el, name, value);
|
||||
});
|
||||
return this;
|
||||
@@ -321,7 +318,7 @@ exports.data = function(name, value) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/val/}
|
||||
*/
|
||||
exports.val = function(value) {
|
||||
exports.val = function (value) {
|
||||
var querying = arguments.length === 0,
|
||||
element = this[0];
|
||||
|
||||
@@ -361,7 +358,7 @@ exports.val = function(value) {
|
||||
returnValue = option.attr('value');
|
||||
if (hasOwn.call(this.attr(), 'multiple')) {
|
||||
returnValue = [];
|
||||
domEach(option, function(__, el) {
|
||||
domEach(option, function (__, el) {
|
||||
returnValue.push(getAttr(el, 'value'));
|
||||
});
|
||||
}
|
||||
@@ -382,7 +379,7 @@ exports.val = function(value) {
|
||||
* @param {node} elem - Node to remove attribute from.
|
||||
* @param {string} name - Name of the attribute to remove.
|
||||
*/
|
||||
var removeAttribute = function(elem, name) {
|
||||
var removeAttribute = function (elem, name) {
|
||||
if (!elem.attribs || !hasOwn.call(elem.attribs, name)) return;
|
||||
|
||||
delete elem.attribs[name];
|
||||
@@ -400,8 +397,8 @@ var removeAttribute = function(elem, name) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/removeAttr/}
|
||||
*/
|
||||
exports.removeAttr = function(name) {
|
||||
domEach(this, function(i, elem) {
|
||||
exports.removeAttr = function (name) {
|
||||
domEach(this, function (i, elem) {
|
||||
removeAttribute(elem, name);
|
||||
});
|
||||
|
||||
@@ -426,8 +423,8 @@ exports.removeAttr = function(name) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/hasClass/}
|
||||
*/
|
||||
exports.hasClass = function(className) {
|
||||
return _.some(this, function(elem) {
|
||||
exports.hasClass = function (className) {
|
||||
return _.some(this, function (elem) {
|
||||
var attrs = elem.attribs,
|
||||
clazz = attrs && attrs['class'],
|
||||
idx = -1,
|
||||
@@ -464,10 +461,10 @@ exports.hasClass = function(className) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/addClass/}
|
||||
*/
|
||||
exports.addClass = function(value) {
|
||||
exports.addClass = function (value) {
|
||||
// Support functions
|
||||
if (typeof value === 'function') {
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
var className = el.attribs['class'] || '';
|
||||
exports.addClass.call([el], value.call(el, i, className));
|
||||
});
|
||||
@@ -507,7 +504,7 @@ exports.addClass = function(value) {
|
||||
return this;
|
||||
};
|
||||
|
||||
var splitClass = function(className) {
|
||||
var splitClass = function (className) {
|
||||
return className ? className.trim().split(rspace) : [];
|
||||
};
|
||||
|
||||
@@ -527,12 +524,12 @@ var splitClass = function(className) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/removeClass/}
|
||||
*/
|
||||
exports.removeClass = function(value) {
|
||||
exports.removeClass = function (value) {
|
||||
var classes, numClasses, removeAll;
|
||||
|
||||
// Handle if value is a function
|
||||
if (typeof value === 'function') {
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
exports.removeClass.call(
|
||||
[el],
|
||||
value.call(el, i, el.attribs['class'] || '')
|
||||
@@ -544,7 +541,7 @@ exports.removeClass = function(value) {
|
||||
numClasses = classes.length;
|
||||
removeAll = arguments.length === 0;
|
||||
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
if (!isTag(el)) return;
|
||||
|
||||
if (removeAll) {
|
||||
@@ -592,10 +589,10 @@ exports.removeClass = function(value) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/toggleClass/}
|
||||
*/
|
||||
exports.toggleClass = function(value, stateVal) {
|
||||
exports.toggleClass = function (value, stateVal) {
|
||||
// Support functions
|
||||
if (typeof value === 'function') {
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
exports.toggleClass.call(
|
||||
[el],
|
||||
value.call(el, i, el.attribs['class'] || '', stateVal),
|
||||
@@ -651,7 +648,7 @@ exports.toggleClass = function(value, stateVal) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/is/}
|
||||
*/
|
||||
exports.is = function(selector) {
|
||||
exports.is = function (selector) {
|
||||
if (selector) {
|
||||
return this.filter(selector).length > 0;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ exports = exports; // eslint-disable-line no-self-assign
|
||||
|
||||
var domEach = require('../utils').domEach,
|
||||
_ = {
|
||||
pick: require('lodash/pick')
|
||||
pick: require('lodash/pick'),
|
||||
};
|
||||
|
||||
var toString = Object.prototype.toString;
|
||||
@@ -23,13 +23,13 @@ var toString = Object.prototype.toString;
|
||||
*
|
||||
* @see {@link http://api.jquery.com/css/}
|
||||
*/
|
||||
exports.css = function(prop, val) {
|
||||
exports.css = function (prop, val) {
|
||||
if (
|
||||
arguments.length === 2 ||
|
||||
// When `prop` is a "plain" object
|
||||
toString.call(prop) === '[object Object]'
|
||||
) {
|
||||
return domEach(this, function(idx, el) {
|
||||
return domEach(this, function (idx, el) {
|
||||
setCss(el, prop, val, idx);
|
||||
});
|
||||
} else {
|
||||
@@ -62,7 +62,7 @@ function setCss(el, prop, val, idx) {
|
||||
|
||||
el.attribs.style = stringify(styles);
|
||||
} else if ('object' == typeof prop) {
|
||||
Object.keys(prop).forEach(function(k) {
|
||||
Object.keys(prop).forEach(function (k) {
|
||||
setCss(el, k, prop[k]);
|
||||
});
|
||||
}
|
||||
@@ -99,7 +99,7 @@ function getCss(el, prop) {
|
||||
* @private
|
||||
*/
|
||||
function stringify(obj) {
|
||||
return Object.keys(obj || {}).reduce(function(str, prop) {
|
||||
return Object.keys(obj || {}).reduce(function (str, prop) {
|
||||
return (str += '' + (str ? ' ' : '') + prop + ': ' + obj[prop] + ';');
|
||||
}, '');
|
||||
}
|
||||
@@ -116,7 +116,7 @@ function parse(styles) {
|
||||
|
||||
if (!styles) return {};
|
||||
|
||||
return styles.split(';').reduce(function(obj, str) {
|
||||
return styles.split(';').reduce(function (obj, str) {
|
||||
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;
|
||||
|
||||
@@ -11,7 +11,7 @@ var submittableSelector = 'input,select,textarea,keygen',
|
||||
r20 = /%20/g,
|
||||
rCRLF = /\r?\n/g,
|
||||
_ = {
|
||||
map: require('lodash/map')
|
||||
map: require('lodash/map'),
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -19,12 +19,12 @@ var submittableSelector = 'input,select,textarea,keygen',
|
||||
*
|
||||
* @see {@link http://api.jquery.com/serialize/}
|
||||
*/
|
||||
exports.serialize = function() {
|
||||
exports.serialize = function () {
|
||||
// Convert form elements into name/value objects
|
||||
var arr = this.serializeArray();
|
||||
|
||||
// Serialize each element into a key/value string
|
||||
var retArr = _.map(arr, function(data) {
|
||||
var retArr = _.map(arr, function (data) {
|
||||
return encodeURIComponent(data.name) + '=' + encodeURIComponent(data.value);
|
||||
});
|
||||
|
||||
@@ -41,10 +41,10 @@ exports.serialize = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/serializeArray/}
|
||||
*/
|
||||
exports.serializeArray = function() {
|
||||
exports.serializeArray = function () {
|
||||
// Resolve all form elements from either forms or collections of form elements
|
||||
var Cheerio = this.constructor;
|
||||
return this.map(function() {
|
||||
return this.map(function () {
|
||||
var elem = this;
|
||||
var $elem = Cheerio(elem);
|
||||
if (elem.name === 'form') {
|
||||
@@ -62,7 +62,7 @@ exports.serializeArray = function() {
|
||||
':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);
|
||||
var name = $elem.attr('name');
|
||||
var value = $elem.val();
|
||||
@@ -74,7 +74,7 @@ exports.serializeArray = function() {
|
||||
|
||||
// If we have an array of values (e.g. `<select multiple>`), return an array of key/value pairs
|
||||
if (Array.isArray(value)) {
|
||||
return _.map(value, function(val) {
|
||||
return _.map(value, function (val) {
|
||||
// We trim replace any line endings (e.g. `\r` or `\r\n` with `\r\n`) to guarantee consistency across platforms
|
||||
// These can occur inside of `<textarea>'s`
|
||||
return { name: name, value: val.replace(rCRLF, '\r\n') };
|
||||
|
||||
@@ -20,7 +20,7 @@ var parse = require('../parse'),
|
||||
_ = {
|
||||
flatten: require('lodash/flatten'),
|
||||
bind: require('lodash/bind'),
|
||||
forEach: require('lodash/forEach')
|
||||
forEach: require('lodash/forEach'),
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -38,7 +38,7 @@ exports._makeDomArray = function makeDomArray(elem, clone) {
|
||||
return clone ? cloneDom(elem.get(), elem.options) : elem.get();
|
||||
} else if (Array.isArray(elem)) {
|
||||
return _.flatten(
|
||||
elem.map(function(el) {
|
||||
elem.map(function (el) {
|
||||
return this._makeDomArray(el, clone);
|
||||
}, this)
|
||||
);
|
||||
@@ -49,12 +49,12 @@ exports._makeDomArray = function makeDomArray(elem, clone) {
|
||||
}
|
||||
};
|
||||
|
||||
var _insert = function(concatenator) {
|
||||
return function() {
|
||||
var _insert = function (concatenator) {
|
||||
return function () {
|
||||
var elems = slice.call(arguments),
|
||||
lastIdx = this.length - 1;
|
||||
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
var dom, domSrc;
|
||||
|
||||
if (typeof elems[0] === 'function') {
|
||||
@@ -80,7 +80,7 @@ var _insert = function(concatenator) {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) {
|
||||
var uniqueSplice = function (array, spliceIdx, spliceCount, newElems, parent) {
|
||||
var spliceArgs = [spliceIdx, spliceCount].concat(newElems),
|
||||
prev = array[spliceIdx - 1] || null,
|
||||
next = array[spliceIdx] || null;
|
||||
@@ -143,7 +143,7 @@ var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/appendTo/}
|
||||
*/
|
||||
exports.appendTo = function(target) {
|
||||
exports.appendTo = function (target) {
|
||||
if (!target.cheerio) {
|
||||
target = this.constructor.call(
|
||||
this.constructor,
|
||||
@@ -177,7 +177,7 @@ exports.appendTo = function(target) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/prependTo/}
|
||||
*/
|
||||
exports.prependTo = function(target) {
|
||||
exports.prependTo = function (target) {
|
||||
if (!target.cheerio) {
|
||||
target = this.constructor.call(
|
||||
this.constructor,
|
||||
@@ -195,7 +195,7 @@ exports.prependTo = function(target) {
|
||||
/**
|
||||
* Inserts content as the *last* child of each of the selected elements.
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
@@ -210,14 +210,14 @@ exports.prependTo = function(target) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/append/}
|
||||
*/
|
||||
exports.append = _insert(function(dom, children, parent) {
|
||||
exports.append = _insert(function (dom, children, parent) {
|
||||
uniqueSplice(children, children.length, 0, dom, parent);
|
||||
});
|
||||
|
||||
/**
|
||||
* Inserts content as the *first* child of each of the selected elements.
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
@@ -232,7 +232,7 @@ exports.append = _insert(function(dom, children, parent) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/prepend/}
|
||||
*/
|
||||
exports.prepend = _insert(function(dom, children, parent) {
|
||||
exports.prepend = _insert(function (dom, children, parent) {
|
||||
uniqueSplice(children, 0, 0, dom, parent);
|
||||
});
|
||||
|
||||
@@ -276,13 +276,13 @@ exports.prepend = _insert(function(dom, children, parent) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/wrap/}
|
||||
*/
|
||||
exports.wrap = function(wrapper) {
|
||||
exports.wrap = function (wrapper) {
|
||||
var wrapperFn = typeof wrapper === 'function' && wrapper,
|
||||
lastIdx = this.length - 1;
|
||||
|
||||
_.forEach(
|
||||
this,
|
||||
_.bind(function(el, i) {
|
||||
_.bind(function (el, i) {
|
||||
var parent = el.parent || el.root,
|
||||
siblings = parent.children,
|
||||
wrapperDom,
|
||||
@@ -299,10 +299,7 @@ exports.wrap = function(wrapper) {
|
||||
}
|
||||
|
||||
if (typeof wrapper === 'string' && !isHtml(wrapper)) {
|
||||
wrapper = this.parents()
|
||||
.last()
|
||||
.find(wrapper)
|
||||
.clone();
|
||||
wrapper = this.parents().last().find(wrapper).clone();
|
||||
}
|
||||
|
||||
wrapperDom = this._makeDomArray(wrapper, i < lastIdx).slice(0, 1);
|
||||
@@ -352,11 +349,11 @@ exports.wrap = function(wrapper) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/after/}
|
||||
*/
|
||||
exports.after = function() {
|
||||
exports.after = function () {
|
||||
var elems = slice.call(arguments),
|
||||
lastIdx = this.length - 1;
|
||||
|
||||
domEach(this, function(i, el) {
|
||||
domEach(this, function (i, el) {
|
||||
var parent = el.parent || el.root;
|
||||
if (!parent) {
|
||||
return;
|
||||
@@ -402,7 +399,7 @@ exports.after = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/insertAfter/}
|
||||
*/
|
||||
exports.insertAfter = function(target) {
|
||||
exports.insertAfter = function (target) {
|
||||
var clones = [],
|
||||
self = this;
|
||||
if (typeof target === 'string') {
|
||||
@@ -415,7 +412,7 @@ exports.insertAfter = function(target) {
|
||||
}
|
||||
target = this._makeDomArray(target);
|
||||
self.remove();
|
||||
domEach(target, function(i, el) {
|
||||
domEach(target, function (i, el) {
|
||||
var clonedSelf = self._makeDomArray(self.clone());
|
||||
var parent = el.parent || el.root;
|
||||
if (!parent) {
|
||||
@@ -451,11 +448,11 @@ exports.insertAfter = function(target) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/before/}
|
||||
*/
|
||||
exports.before = function() {
|
||||
exports.before = function () {
|
||||
var elems = slice.call(arguments),
|
||||
lastIdx = this.length - 1;
|
||||
|
||||
domEach(this, function(i, el) {
|
||||
domEach(this, function (i, el) {
|
||||
var parent = el.parent || el.root;
|
||||
if (!parent) {
|
||||
return;
|
||||
@@ -502,7 +499,7 @@ exports.before = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/insertBefore/}
|
||||
*/
|
||||
exports.insertBefore = function(target) {
|
||||
exports.insertBefore = function (target) {
|
||||
var clones = [],
|
||||
self = this;
|
||||
if (typeof target === 'string') {
|
||||
@@ -515,7 +512,7 @@ exports.insertBefore = function(target) {
|
||||
}
|
||||
target = this._makeDomArray(target);
|
||||
self.remove();
|
||||
domEach(target, function(i, el) {
|
||||
domEach(target, function (i, el) {
|
||||
var clonedSelf = self._makeDomArray(self.clone());
|
||||
var parent = el.parent || el.root;
|
||||
if (!parent) {
|
||||
@@ -552,13 +549,13 @@ exports.insertBefore = function(target) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/remove/}
|
||||
*/
|
||||
exports.remove = function(selector) {
|
||||
exports.remove = function (selector) {
|
||||
var elems = this;
|
||||
|
||||
// Filter if we have selector
|
||||
if (selector) elems = elems.filter(selector);
|
||||
|
||||
domEach(elems, function(i, el) {
|
||||
domEach(elems, function (i, el) {
|
||||
var parent = el.parent || el.root;
|
||||
if (!parent) {
|
||||
return;
|
||||
@@ -600,10 +597,10 @@ exports.remove = function(selector) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/replaceWith/}
|
||||
*/
|
||||
exports.replaceWith = function(content) {
|
||||
exports.replaceWith = function (content) {
|
||||
var self = this;
|
||||
|
||||
domEach(this, function(i, el) {
|
||||
domEach(this, function (i, el) {
|
||||
var parent = el.parent || el.root;
|
||||
if (!parent) {
|
||||
return;
|
||||
@@ -640,9 +637,9 @@ exports.replaceWith = function(content) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/empty/}
|
||||
*/
|
||||
exports.empty = function() {
|
||||
domEach(this, function(i, el) {
|
||||
_.forEach(el.children, function(child) {
|
||||
exports.empty = function () {
|
||||
domEach(this, function (i, el) {
|
||||
_.forEach(el.children, function (child) {
|
||||
child.next = child.prev = child.parent = null;
|
||||
});
|
||||
|
||||
@@ -668,7 +665,7 @@ exports.empty = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/html/}
|
||||
*/
|
||||
exports.html = function(str) {
|
||||
exports.html = function (str) {
|
||||
if (str === undefined) {
|
||||
if (!this[0] || !this[0].children) return null;
|
||||
return html(this[0].children, this.options);
|
||||
@@ -676,8 +673,8 @@ exports.html = function(str) {
|
||||
|
||||
var opts = this.options;
|
||||
|
||||
domEach(this, function(i, el) {
|
||||
_.forEach(el.children, function(child) {
|
||||
domEach(this, function (i, el) {
|
||||
_.forEach(el.children, function (child) {
|
||||
child.next = child.prev = child.parent = null;
|
||||
});
|
||||
|
||||
@@ -691,7 +688,7 @@ exports.html = function(str) {
|
||||
return this;
|
||||
};
|
||||
|
||||
exports.toString = function() {
|
||||
exports.toString = function () {
|
||||
return html(this, this.options);
|
||||
};
|
||||
|
||||
@@ -714,14 +711,14 @@ exports.toString = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/text/}
|
||||
*/
|
||||
exports.text = function(str) {
|
||||
exports.text = function (str) {
|
||||
// If `str` is undefined, act as a "getter"
|
||||
if (str === undefined) {
|
||||
return text(this);
|
||||
} else if (typeof str === 'function') {
|
||||
// Function support
|
||||
var self = this;
|
||||
return domEach(this, function(i, el) {
|
||||
return domEach(this, function (i, el) {
|
||||
return exports.text.call(self._make(el), str.call(el, i, text([el])));
|
||||
});
|
||||
}
|
||||
@@ -729,8 +726,8 @@ exports.text = function(str) {
|
||||
var opts = this.options;
|
||||
|
||||
// Append text node to each selected elements
|
||||
domEach(this, function(i, el) {
|
||||
_.forEach(el.children, function(child) {
|
||||
domEach(this, function (i, el) {
|
||||
_.forEach(el.children, function (child) {
|
||||
child.next = child.prev = child.parent = null;
|
||||
});
|
||||
|
||||
@@ -752,6 +749,6 @@ exports.text = function(str) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/clone/}
|
||||
*/
|
||||
exports.clone = function() {
|
||||
exports.clone = function () {
|
||||
return this._make(cloneDom(this.get(), this.options));
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ var select = require('css-select'),
|
||||
forEach: require('lodash/forEach'),
|
||||
reject: require('lodash/reject'),
|
||||
filter: require('lodash/filter'),
|
||||
reduce: require('lodash/reduce')
|
||||
reduce: require('lodash/reduce'),
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -31,14 +31,14 @@ var select = require('css-select'),
|
||||
* $('#fruits').find($('.apple')).length
|
||||
* //=> 1
|
||||
*
|
||||
* @param {string|cheerio|node} selectorOrHaystack - Element to look for.
|
||||
* @param {string|cheerio|node} selectorOrHaystack - - Element to look for.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/find/}
|
||||
*/
|
||||
exports.find = function(selectorOrHaystack) {
|
||||
exports.find = function (selectorOrHaystack) {
|
||||
var elems = _.reduce(
|
||||
this,
|
||||
function(memo, elem) {
|
||||
function (memo, elem) {
|
||||
return memo.concat(_.filter(elem.children, isTag));
|
||||
},
|
||||
[]
|
||||
@@ -54,7 +54,7 @@ exports.find = function(selectorOrHaystack) {
|
||||
}
|
||||
|
||||
return this._make(
|
||||
haystack.filter(function(elem) {
|
||||
haystack.filter(function (elem) {
|
||||
var idx, len;
|
||||
for (idx = 0, len = this.length; idx < len; ++idx) {
|
||||
if (contains(this[idx], elem)) {
|
||||
@@ -83,10 +83,10 @@ exports.find = function(selectorOrHaystack) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/parent/}
|
||||
*/
|
||||
exports.parent = function(selector) {
|
||||
exports.parent = function (selector) {
|
||||
var set = [];
|
||||
|
||||
domEach(this, function(idx, elem) {
|
||||
domEach(this, function (idx, elem) {
|
||||
var parentElem = elem.parent;
|
||||
if (parentElem && set.indexOf(parentElem) < 0) {
|
||||
set.push(parentElem);
|
||||
@@ -115,7 +115,7 @@ exports.parent = function(selector) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/parents/}
|
||||
*/
|
||||
exports.parents = function(selector) {
|
||||
exports.parents = function (selector) {
|
||||
var parentNodes = [];
|
||||
|
||||
// When multiple DOM elements are in the original set, the resulting set will
|
||||
@@ -123,8 +123,8 @@ exports.parents = function(selector) {
|
||||
// removed.
|
||||
this.get()
|
||||
.reverse()
|
||||
.forEach(function(elem) {
|
||||
traverseParents(this, elem.parent, selector, Infinity).forEach(function(
|
||||
.forEach(function (elem) {
|
||||
traverseParents(this, elem.parent, selector, Infinity).forEach(function (
|
||||
node
|
||||
) {
|
||||
if (parentNodes.indexOf(node) === -1) {
|
||||
@@ -151,7 +151,7 @@ exports.parents = function(selector) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/parentsUntil/}
|
||||
*/
|
||||
exports.parentsUntil = function(selector, filter) {
|
||||
exports.parentsUntil = function (selector, filter) {
|
||||
var parentNodes = [],
|
||||
untilNode,
|
||||
untilNodes;
|
||||
@@ -170,7 +170,7 @@ exports.parentsUntil = function(selector, filter) {
|
||||
|
||||
this.toArray()
|
||||
.reverse()
|
||||
.forEach(function(elem) {
|
||||
.forEach(function (elem) {
|
||||
while ((elem = elem.parent)) {
|
||||
if (
|
||||
(untilNode && elem !== untilNode) ||
|
||||
@@ -211,7 +211,7 @@ exports.parentsUntil = function(selector, filter) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/closest/}
|
||||
*/
|
||||
exports.closest = function(selector) {
|
||||
exports.closest = function (selector) {
|
||||
var set = [];
|
||||
|
||||
if (!selector) {
|
||||
@@ -220,7 +220,7 @@ exports.closest = function(selector) {
|
||||
|
||||
domEach(
|
||||
this,
|
||||
function(idx, elem) {
|
||||
function (idx, elem) {
|
||||
var closestElem = traverseParents(this, elem, selector, 1)[0];
|
||||
|
||||
// Do not add duplicate elements to the set
|
||||
@@ -246,13 +246,13 @@ exports.closest = function(selector) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/next/}
|
||||
*/
|
||||
exports.next = function(selector) {
|
||||
exports.next = function (selector) {
|
||||
if (!this[0]) {
|
||||
return this;
|
||||
}
|
||||
var elems = [];
|
||||
|
||||
_.forEach(this, function(elem) {
|
||||
_.forEach(this, function (elem) {
|
||||
while ((elem = elem.next)) {
|
||||
if (isTag(elem)) {
|
||||
elems.push(elem);
|
||||
@@ -281,13 +281,13 @@ exports.next = function(selector) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/nextAll/}
|
||||
*/
|
||||
exports.nextAll = function(selector) {
|
||||
exports.nextAll = function (selector) {
|
||||
if (!this[0]) {
|
||||
return this;
|
||||
}
|
||||
var elems = [];
|
||||
|
||||
_.forEach(this, function(elem) {
|
||||
_.forEach(this, function (elem) {
|
||||
while ((elem = elem.next)) {
|
||||
if (isTag(elem) && elems.indexOf(elem) === -1) {
|
||||
elems.push(elem);
|
||||
@@ -310,11 +310,11 @@ exports.nextAll = function(selector) {
|
||||
* //=> [<li class="orange">Orange</li>]
|
||||
*
|
||||
* @param {string|cheerio|node} selector - Selector for element to stop at.
|
||||
* @param {string} [filterSelector] - If specified filter for siblings.
|
||||
* @param {string} [filterSelector] - - If specified filter for siblings.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/nextUntil/}
|
||||
*/
|
||||
exports.nextUntil = function(selector, filterSelector) {
|
||||
exports.nextUntil = function (selector, filterSelector) {
|
||||
if (!this[0]) {
|
||||
return this;
|
||||
}
|
||||
@@ -330,7 +330,7 @@ exports.nextUntil = function(selector, filterSelector) {
|
||||
untilNode = selector;
|
||||
}
|
||||
|
||||
_.forEach(this, function(elem) {
|
||||
_.forEach(this, function (elem) {
|
||||
while ((elem = elem.next)) {
|
||||
if (
|
||||
(untilNode && elem !== untilNode) ||
|
||||
@@ -360,17 +360,17 @@ exports.nextUntil = function(selector, filterSelector) {
|
||||
* $('.orange').prev().hasClass('apple')
|
||||
* //=> true
|
||||
*
|
||||
* @param {string} [selector] - If specified filter for siblings.
|
||||
* @param {string} [selector] - - If specified filter for siblings.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/prev/}
|
||||
*/
|
||||
exports.prev = function(selector) {
|
||||
exports.prev = function (selector) {
|
||||
if (!this[0]) {
|
||||
return this;
|
||||
}
|
||||
var elems = [];
|
||||
|
||||
_.forEach(this, function(elem) {
|
||||
_.forEach(this, function (elem) {
|
||||
while ((elem = elem.prev)) {
|
||||
if (isTag(elem)) {
|
||||
elems.push(elem);
|
||||
@@ -395,17 +395,17 @@ exports.prev = function(selector) {
|
||||
* $('.pear').prevAll('.orange')
|
||||
* //=> [<li class="orange">Orange</li>]
|
||||
*
|
||||
* @param {string} [selector] - If specified filter for siblings.
|
||||
* @param {string} [selector] - - If specified filter for siblings.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/prevAll/}
|
||||
*/
|
||||
exports.prevAll = function(selector) {
|
||||
exports.prevAll = function (selector) {
|
||||
if (!this[0]) {
|
||||
return this;
|
||||
}
|
||||
var elems = [];
|
||||
|
||||
_.forEach(this, function(elem) {
|
||||
_.forEach(this, function (elem) {
|
||||
while ((elem = elem.prev)) {
|
||||
if (isTag(elem) && elems.indexOf(elem) === -1) {
|
||||
elems.push(elem);
|
||||
@@ -428,11 +428,11 @@ exports.prevAll = function(selector) {
|
||||
* //=> [<li class="orange">Orange</li>]
|
||||
*
|
||||
* @param {string|cheerio|node} selector - Selector for element to stop at.
|
||||
* @param {string} [filterSelector] - If specified filter for siblings.
|
||||
* @param {string} [filterSelector] - - If specified filter for siblings.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/prevUntil/}
|
||||
*/
|
||||
exports.prevUntil = function(selector, filterSelector) {
|
||||
exports.prevUntil = function (selector, filterSelector) {
|
||||
if (!this[0]) {
|
||||
return this;
|
||||
}
|
||||
@@ -448,7 +448,7 @@ exports.prevUntil = function(selector, filterSelector) {
|
||||
untilNode = selector;
|
||||
}
|
||||
|
||||
_.forEach(this, function(elem) {
|
||||
_.forEach(this, function (elem) {
|
||||
while ((elem = elem.prev)) {
|
||||
if (
|
||||
(untilNode && elem !== untilNode) ||
|
||||
@@ -480,16 +480,16 @@ exports.prevUntil = function(selector, filterSelector) {
|
||||
* $('.pear').siblings('.orange').length
|
||||
* //=> 1
|
||||
*
|
||||
* @param {string} [selector] - If specified filter for siblings.
|
||||
* @param {string} [selector] - - If specified filter for siblings.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/siblings/}
|
||||
*/
|
||||
exports.siblings = function(selector) {
|
||||
exports.siblings = function (selector) {
|
||||
var parent = this.parent();
|
||||
|
||||
var elems = _.filter(
|
||||
parent ? parent.children() : this.siblingsAndMe(),
|
||||
_.bind(function(elem) {
|
||||
_.bind(function (elem) {
|
||||
return isTag(elem) && !this.is(elem);
|
||||
}, this)
|
||||
);
|
||||
@@ -512,14 +512,14 @@ exports.siblings = function(selector) {
|
||||
* $('#fruits').children('.pear').text()
|
||||
* //=> Pear
|
||||
*
|
||||
* @param {string} [selector] - If specified filter for children.
|
||||
* @param {string} [selector] - - If specified filter for children.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/children/}
|
||||
*/
|
||||
exports.children = function(selector) {
|
||||
exports.children = function (selector) {
|
||||
var elems = _.reduce(
|
||||
this,
|
||||
function(memo, elem) {
|
||||
function (memo, elem) {
|
||||
return memo.concat(_.filter(elem.children, isTag));
|
||||
},
|
||||
[]
|
||||
@@ -541,11 +541,11 @@ exports.children = function(selector) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/contents/}
|
||||
*/
|
||||
exports.contents = function() {
|
||||
exports.contents = function () {
|
||||
return this._make(
|
||||
_.reduce(
|
||||
this,
|
||||
function(all, elem) {
|
||||
function (all, elem) {
|
||||
all.push.apply(all, elem.children);
|
||||
return all;
|
||||
},
|
||||
@@ -572,11 +572,11 @@ exports.contents = function() {
|
||||
* fruits.join(', ');
|
||||
* //=> Apple, Orange, Pear
|
||||
*
|
||||
* @param {Function} fn - Function to execute.
|
||||
* @param {Function} fn - - Function to execute.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/each/}
|
||||
*/
|
||||
exports.each = function(fn) {
|
||||
exports.each = function (fn) {
|
||||
var i = 0,
|
||||
len = this.length;
|
||||
while (i < len && fn.call(this[i], i, this[i]) !== false) ++i;
|
||||
@@ -599,15 +599,15 @@ exports.each = function(fn) {
|
||||
* }).get().join(' ');
|
||||
* //=> "apple orange pear"
|
||||
*
|
||||
* @param {Function} fn - Function to execute.
|
||||
* @param {Function} fn - - Function to execute.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/map/}
|
||||
*/
|
||||
exports.map = function(fn) {
|
||||
exports.map = function (fn) {
|
||||
return this._make(
|
||||
_.reduce(
|
||||
this,
|
||||
function(memo, el, i) {
|
||||
function (memo, el, i) {
|
||||
var val = fn.call(el, i, el);
|
||||
return val == null ? memo : memo.concat(val);
|
||||
},
|
||||
@@ -616,21 +616,21 @@ exports.map = function(fn) {
|
||||
);
|
||||
};
|
||||
|
||||
var makeFilterMethod = function(filterFn) {
|
||||
return function(match, container) {
|
||||
var makeFilterMethod = function (filterFn) {
|
||||
return function (match, container) {
|
||||
var testFn;
|
||||
container = container || this;
|
||||
|
||||
if (typeof match === 'string') {
|
||||
testFn = select.compile(match, container.options);
|
||||
} else if (typeof match === 'function') {
|
||||
testFn = function(el, i) {
|
||||
testFn = function (el, i) {
|
||||
return match.call(el, i, el);
|
||||
};
|
||||
} else if (match.cheerio) {
|
||||
testFn = match.is.bind(match);
|
||||
} else {
|
||||
testFn = function(el) {
|
||||
testFn = function (el) {
|
||||
return match === el;
|
||||
};
|
||||
}
|
||||
@@ -648,7 +648,7 @@ var makeFilterMethod = function(filterFn) {
|
||||
* function is executed in the context of the selected element, so `this`
|
||||
* refers to the current element.
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
*
|
||||
* @example <caption>Selector</caption>
|
||||
*
|
||||
@@ -677,7 +677,7 @@ exports.filter = makeFilterMethod(_.filter);
|
||||
* function returns true are excluded from the filtered set; all other elements
|
||||
* are included.
|
||||
*
|
||||
* @method
|
||||
* @function
|
||||
*
|
||||
* @example <caption>Selector</caption>
|
||||
*
|
||||
@@ -711,13 +711,13 @@ exports.not = makeFilterMethod(_.reject);
|
||||
* $('ul').has($('.pear')[0]).attr('id');
|
||||
* //=> fruits
|
||||
*
|
||||
* @param {string|cheerio|node} selectorOrHaystack - Element to look for.
|
||||
* @param {string|cheerio|node} selectorOrHaystack - - Element to look for.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/has/}
|
||||
*/
|
||||
exports.has = function(selectorOrHaystack) {
|
||||
exports.has = function (selectorOrHaystack) {
|
||||
var that = this;
|
||||
return exports.filter.call(this, function() {
|
||||
return exports.filter.call(this, function () {
|
||||
return that._make(this).find(selectorOrHaystack).length > 0;
|
||||
});
|
||||
};
|
||||
@@ -732,7 +732,7 @@ exports.has = function(selectorOrHaystack) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/first/}
|
||||
*/
|
||||
exports.first = function() {
|
||||
exports.first = function () {
|
||||
return this.length > 1 ? this._make(this[0]) : this;
|
||||
};
|
||||
|
||||
@@ -746,7 +746,7 @@ exports.first = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/last/}
|
||||
*/
|
||||
exports.last = function() {
|
||||
exports.last = function () {
|
||||
return this.length > 1 ? this._make(this[this.length - 1]) : this;
|
||||
};
|
||||
|
||||
@@ -766,7 +766,7 @@ exports.last = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/eq/}
|
||||
*/
|
||||
exports.eq = function(i) {
|
||||
exports.eq = function (i) {
|
||||
i = +i;
|
||||
|
||||
// Use the first identity optimization if possible
|
||||
@@ -796,7 +796,7 @@ exports.eq = function(i) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/get/}
|
||||
*/
|
||||
exports.get = function(i) {
|
||||
exports.get = function (i) {
|
||||
if (i == null) {
|
||||
return Array.prototype.slice.call(this);
|
||||
} else {
|
||||
@@ -816,11 +816,11 @@ exports.get = function(i) {
|
||||
* $('.apple').index($('#fruit, li'))
|
||||
* //=> 1
|
||||
*
|
||||
* @param {string|cheerio|node} [selectorOrNeedle] - Element to look for.
|
||||
* @param {string|cheerio|node} [selectorOrNeedle] - - Element to look for.
|
||||
*
|
||||
* @see {@link http://api.jquery.com/index/}
|
||||
*/
|
||||
exports.index = function(selectorOrNeedle) {
|
||||
exports.index = function (selectorOrNeedle) {
|
||||
var $haystack, needle;
|
||||
|
||||
if (arguments.length === 0) {
|
||||
@@ -850,7 +850,7 @@ exports.index = function(selectorOrNeedle) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/slice/}
|
||||
*/
|
||||
exports.slice = function() {
|
||||
exports.slice = function () {
|
||||
return this._make([].slice.apply(this, arguments));
|
||||
};
|
||||
|
||||
@@ -876,7 +876,7 @@ function traverseParents(self, elem, selector, limit) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/end/}
|
||||
*/
|
||||
exports.end = function() {
|
||||
exports.end = function () {
|
||||
return this.prevObject || this._make([]);
|
||||
};
|
||||
|
||||
@@ -893,7 +893,7 @@ exports.end = function() {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/add/}
|
||||
*/
|
||||
exports.add = function(other, context) {
|
||||
exports.add = function (other, context) {
|
||||
var selection = this._make(other, context);
|
||||
var contents = uniqueSort(selection.get().concat(this.get()));
|
||||
|
||||
@@ -918,7 +918,7 @@ exports.add = function(other, context) {
|
||||
*
|
||||
* @see {@link http://api.jquery.com/addBack/}
|
||||
*/
|
||||
exports.addBack = function(selector) {
|
||||
exports.addBack = function (selector) {
|
||||
return this.add(
|
||||
arguments.length ? this.prevObject.filter(selector) : this.prevObject
|
||||
);
|
||||
|
||||
@@ -11,7 +11,7 @@ var parse = require('./parse'),
|
||||
extend: require('lodash/assignIn'),
|
||||
bind: require('lodash/bind'),
|
||||
forEach: require('lodash/forEach'),
|
||||
defaults: require('lodash/defaults')
|
||||
defaults: require('lodash/defaults'),
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -22,7 +22,7 @@ var api = [
|
||||
require('./api/traversing'),
|
||||
require('./api/manipulation'),
|
||||
require('./api/css'),
|
||||
require('./api/forms')
|
||||
require('./api/forms'),
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -42,7 +42,7 @@ var api = [
|
||||
* @mixes manipulation
|
||||
* @mixes traversing
|
||||
*/
|
||||
var Cheerio = (module.exports = function(selector, context, root, options) {
|
||||
var Cheerio = (module.exports = function (selector, context, root, options) {
|
||||
if (!(this instanceof Cheerio))
|
||||
return new Cheerio(selector, context, root, options);
|
||||
|
||||
@@ -70,7 +70,7 @@ var Cheerio = (module.exports = function(selector, context, root, options) {
|
||||
if (Array.isArray(selector)) {
|
||||
_.forEach(
|
||||
selector,
|
||||
_.bind(function(elem, idx) {
|
||||
_.bind(function (elem, idx) {
|
||||
this[idx] = elem;
|
||||
}, this)
|
||||
);
|
||||
@@ -124,7 +124,7 @@ Cheerio.prototype.splice = Array.prototype.splice;
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
Cheerio.prototype._make = function(dom, context) {
|
||||
Cheerio.prototype._make = function (dom, context) {
|
||||
var cheerio = new this.constructor(dom, context, this._root, this.options);
|
||||
cheerio.prevObject = this;
|
||||
return cheerio;
|
||||
@@ -137,7 +137,7 @@ Cheerio.prototype._make = function(dom, context) {
|
||||
* $('li').toArray()
|
||||
* //=> [ {...}, {...}, {...} ]
|
||||
*/
|
||||
Cheerio.prototype.toArray = function() {
|
||||
Cheerio.prototype.toArray = function () {
|
||||
return this.get();
|
||||
};
|
||||
|
||||
@@ -147,10 +147,10 @@ if (typeof Symbol !== 'undefined') {
|
||||
}
|
||||
|
||||
// Plug in the API
|
||||
api.forEach(function(mod) {
|
||||
api.forEach(function (mod) {
|
||||
_.extend(Cheerio.prototype, mod);
|
||||
});
|
||||
|
||||
var isNode = function(obj) {
|
||||
var isNode = function (obj) {
|
||||
return obj.name || obj.type === 'text' || obj.type === 'comment';
|
||||
};
|
||||
|
||||
@@ -8,10 +8,10 @@ exports.default = {
|
||||
withDomLvl1: true,
|
||||
normalizeWhitespace: false,
|
||||
xml: false,
|
||||
decodeEntities: true
|
||||
decodeEntities: true,
|
||||
};
|
||||
|
||||
exports.flatten = function(options) {
|
||||
exports.flatten = function (options) {
|
||||
return options && options.xml
|
||||
? assign({ xmlMode: true }, options.xml)
|
||||
: options;
|
||||
|
||||
@@ -8,7 +8,7 @@ var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
|
||||
/*
|
||||
Parser
|
||||
*/
|
||||
exports = module.exports = function(content, options, isDocument) {
|
||||
exports = module.exports = function (content, options, isDocument) {
|
||||
var dom = exports.evaluate(content, options, isDocument);
|
||||
// Generic root element
|
||||
var root = exports.evaluate('<root></root>', options, false)[0];
|
||||
@@ -26,13 +26,13 @@ function parseWithParse5(content, options, isDocument) {
|
||||
var parse = isDocument ? parse5.parse : parse5.parseFragment;
|
||||
var root = parse(content, {
|
||||
treeAdapter: htmlparser2Adapter,
|
||||
sourceCodeLocationInfo: options.sourceCodeLocationInfo
|
||||
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
|
||||
});
|
||||
|
||||
return root.children;
|
||||
}
|
||||
|
||||
exports.evaluate = function(content, options, isDocument) {
|
||||
exports.evaluate = function (content, options, isDocument) {
|
||||
// options = options || $.fn.options;
|
||||
|
||||
var dom;
|
||||
@@ -55,7 +55,7 @@ exports.evaluate = function(content, options, isDocument) {
|
||||
/*
|
||||
Update the dom structure, for one changed layer
|
||||
*/
|
||||
exports.update = function(arr, parent) {
|
||||
exports.update = function (arr, parent) {
|
||||
// normalize
|
||||
if (!Array.isArray(arr)) arr = [arr];
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
|
||||
exports = exports; // eslint-disable-line no-self-assign
|
||||
// The preceeding statement is necessary for proper documentation generation.
|
||||
|
||||
var serialize = require('dom-serializer');
|
||||
var serialize = require('dom-serializer').default;
|
||||
var defaultOptions = require('./options').default;
|
||||
var flattenOptions = require('./options').flatten;
|
||||
var select = require('css-select');
|
||||
@@ -16,7 +16,7 @@ var parse5 = require('parse5');
|
||||
var parse = require('./parse');
|
||||
var _ = {
|
||||
merge: require('lodash/merge'),
|
||||
defaults: require('lodash/defaults')
|
||||
defaults: require('lodash/defaults'),
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -30,7 +30,7 @@ var _ = {
|
||||
* @param {boolean} [isDocument] - Allows parser to be switched to fragment mode.
|
||||
*
|
||||
*/
|
||||
exports.load = function(content, options, isDocument) {
|
||||
exports.load = function (content, options, isDocument) {
|
||||
if (content === null || content === undefined) {
|
||||
throw new Error('cheerio.load() expects a string');
|
||||
}
|
||||
@@ -43,7 +43,7 @@ exports.load = function(content, options, isDocument) {
|
||||
|
||||
var root = parse(content, options, isDocument);
|
||||
|
||||
var initialize = function(selector, context, r, opts) {
|
||||
var initialize = function (selector, context, r, opts) {
|
||||
if (!(this instanceof initialize)) {
|
||||
return new initialize(selector, context, r, opts);
|
||||
}
|
||||
@@ -115,7 +115,7 @@ function render(that, dom, options) {
|
||||
* @param {string|cheerio|node} [dom] - Element to render.
|
||||
* @param {object} [options] - Options for the renderer.
|
||||
*/
|
||||
exports.html = function(dom, options) {
|
||||
exports.html = function (dom, options) {
|
||||
// be flexible about parameters, sometimes we call html(),
|
||||
// with options as only parameter
|
||||
// check dom argument for dom element specific properties
|
||||
@@ -146,7 +146,7 @@ exports.html = function(dom, options) {
|
||||
*
|
||||
* @param {string|cheerio|node} [dom] - Element to render.
|
||||
*/
|
||||
exports.xml = function(dom) {
|
||||
exports.xml = function (dom) {
|
||||
var options = _.defaults({ xml: true }, this._options);
|
||||
|
||||
return render(this, dom, options);
|
||||
@@ -157,14 +157,14 @@ exports.xml = function(dom) {
|
||||
*
|
||||
* @param {string|cheerio|node} [elems] - Elements to render.
|
||||
*/
|
||||
exports.text = function(elems) {
|
||||
exports.text = function (elems) {
|
||||
if (!elems) {
|
||||
elems = this.root();
|
||||
}
|
||||
|
||||
var ret = '',
|
||||
len = elems.length,
|
||||
elem;
|
||||
len = elems.length,
|
||||
elem;
|
||||
|
||||
for (var i = 0; i < len; i++) {
|
||||
elem = elems[i];
|
||||
@@ -193,7 +193,7 @@ exports.text = function(elems) {
|
||||
* @alias Cheerio.parseHTML
|
||||
* @see {@link https://api.jquery.com/jQuery.parseHTML/}
|
||||
*/
|
||||
exports.parseHTML = function(data, context, keepScripts) {
|
||||
exports.parseHTML = function (data, context, keepScripts) {
|
||||
var parsed;
|
||||
|
||||
if (!data || typeof data !== 'string') {
|
||||
@@ -229,7 +229,7 @@ exports.parseHTML = function(data, context, keepScripts) {
|
||||
* //=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
|
||||
* ```
|
||||
*/
|
||||
exports.root = function() {
|
||||
exports.root = function () {
|
||||
return this(this._root);
|
||||
};
|
||||
|
||||
@@ -243,7 +243,7 @@ exports.root = function() {
|
||||
* @alias Cheerio.contains
|
||||
* @see {@link https://api.jquery.com/jQuery.contains}
|
||||
*/
|
||||
exports.contains = function(container, contained) {
|
||||
exports.contains = function (container, contained) {
|
||||
// According to the jQuery API, an element does not "contain" itself
|
||||
if (contained === container) {
|
||||
return false;
|
||||
@@ -270,7 +270,7 @@ exports.contains = function(container, contained) {
|
||||
* @alias Cheerio.merge
|
||||
* @see {@link https://api.jquery.com/jQuery.merge}
|
||||
*/
|
||||
exports.merge = function(arr1, arr2) {
|
||||
exports.merge = function (arr1, arr2) {
|
||||
if (!(isArrayLike(arr1) && isArrayLike(arr2))) {
|
||||
return;
|
||||
}
|
||||
|
||||
16
lib/utils.js
16
lib/utils.js
@@ -12,7 +12,7 @@ var tags = { tag: true, script: true, style: true };
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
exports.isTag = function(type) {
|
||||
exports.isTag = function (type) {
|
||||
if (type.type) type = type.type;
|
||||
return tags[type] || false;
|
||||
};
|
||||
@@ -25,8 +25,8 @@ exports.isTag = function(type) {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
exports.camelCase = function(str) {
|
||||
return str.replace(/[_.-](\w|$)/g, function(_, x) {
|
||||
exports.camelCase = function (str) {
|
||||
return str.replace(/[_.-](\w|$)/g, function (_, x) {
|
||||
return x.toUpperCase();
|
||||
});
|
||||
};
|
||||
@@ -40,7 +40,7 @@ exports.camelCase = function(str) {
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
exports.cssCase = function(str) {
|
||||
exports.cssCase = function (str) {
|
||||
return str.replace(/[A-Z]/g, '-$&').toLowerCase();
|
||||
};
|
||||
|
||||
@@ -49,7 +49,7 @@ exports.cssCase = function(str) {
|
||||
//
|
||||
// This is indented for use internally to avoid otherwise unnecessary memory
|
||||
// pressure introduced by _make.
|
||||
exports.domEach = function(cheerio, fn) {
|
||||
exports.domEach = function (cheerio, fn) {
|
||||
var i = 0,
|
||||
len = cheerio.length;
|
||||
while (i < len && fn.call(cheerio, i, cheerio[i]) !== false) ++i;
|
||||
@@ -63,10 +63,10 @@ exports.domEach = function(cheerio, fn) {
|
||||
* @param {object} dom - The htmlparser2-compliant DOM structure.
|
||||
* @private
|
||||
*/
|
||||
exports.cloneDom = function(dom) {
|
||||
exports.cloneDom = function (dom) {
|
||||
var parents =
|
||||
'length' in dom
|
||||
? Array.prototype.map.call(dom, function(el) {
|
||||
? Array.prototype.map.call(dom, function (el) {
|
||||
return el.parent;
|
||||
})
|
||||
: [dom.parent];
|
||||
@@ -92,7 +92,7 @@ var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w-]*)$)/;
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
exports.isHtml = function(str) {
|
||||
exports.isHtml = function (str) {
|
||||
// Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML
|
||||
if (
|
||||
str.charAt(0) === '<' &&
|
||||
|
||||
4222
package-lock.json
generated
4222
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
43
package.json
43
package.json
@@ -25,49 +25,48 @@
|
||||
"node": ">= 0.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"css-select": "~2.0.0",
|
||||
"dom-serializer": "~0.1.1",
|
||||
"entities": "~1.1.1",
|
||||
"htmlparser2": "^3.9.1",
|
||||
"css-select": "~2.1.0",
|
||||
"dom-serializer": "~1.0.1",
|
||||
"entities": "~2.0.2",
|
||||
"htmlparser2": "^4.1.0",
|
||||
"lodash": "^4.17.11",
|
||||
"parse5": "^5.1.0",
|
||||
"parse5-htmlparser2-tree-adapter": "^5.1.0"
|
||||
"parse5": "^6.0.0",
|
||||
"parse5-htmlparser2-tree-adapter": "^6.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"benchmark": "^2.1.4",
|
||||
"coveralls": "^3.0.2",
|
||||
"eslint": "^6.0.1",
|
||||
"eslint": "^7.0.0",
|
||||
"eslint-config-prettier": "^6.0.0",
|
||||
"eslint-plugin-jsdoc": "^8.7.0",
|
||||
"eslint-plugin-jsdoc": "^25.0.1",
|
||||
"expect.js": "~0.3.1",
|
||||
"husky": "^0.14.3",
|
||||
"husky": "^4.2.5",
|
||||
"jquery": "^3.0.0",
|
||||
"jsdoc": "https://github.com/cheeriojs/jsdoc.git#exports-mixin",
|
||||
"jsdom": "^15.1.1",
|
||||
"lint-staged": "^8.2.1",
|
||||
"mocha": "^6.1.4",
|
||||
"nyc": "^14.1.1",
|
||||
"prettier": "^1.11.0",
|
||||
"xyz": "~3.0.0"
|
||||
"jsdom": "^16.2.2",
|
||||
"lint-staged": "^10.2.2",
|
||||
"mocha": "^7.1.2",
|
||||
"nyc": "^15.0.1",
|
||||
"prettier": "^2.0.5",
|
||||
"xyz": "~4.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm run test:lint && npm run test:mocha",
|
||||
"test:mocha": "mocha --recursive --reporter dot",
|
||||
"test:lint": "eslint --ignore-pattern docs --ignore-path .gitignore .",
|
||||
"precommit": "lint-staged"
|
||||
"pre-commit": "lint-staged"
|
||||
},
|
||||
"prettier": {
|
||||
"singleQuote": true
|
||||
"singleQuote": true,
|
||||
"tabWidth": 2
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.js": [
|
||||
"prettier --write",
|
||||
"npm run test:lint -- --fix",
|
||||
"git add"
|
||||
"npm run test:lint -- --fix"
|
||||
],
|
||||
"*.json": [
|
||||
"prettier --write",
|
||||
"git add"
|
||||
"*.{json,md}": [
|
||||
"prettier --write"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,41 @@ var chocolates = require('../fixtures').chocolates;
|
||||
var inputs = require('../fixtures').inputs;
|
||||
var toArray = Function.call.bind(Array.prototype.slice);
|
||||
|
||||
describe('$(...)', function() {
|
||||
describe('$(...)', function () {
|
||||
var $;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
$ = cheerio.load(fruits);
|
||||
});
|
||||
|
||||
describe('.attr', function() {
|
||||
it('() : should get all the attributes', function() {
|
||||
describe('.attr', function () {
|
||||
it('() : should get all the attributes', function () {
|
||||
var attrs = $('ul').attr();
|
||||
expect(attrs.id).to.equal('fruits');
|
||||
});
|
||||
|
||||
it('(invalid key) : invalid attr should get undefined', function() {
|
||||
it('(invalid key) : invalid attr should get undefined', function () {
|
||||
var attr = $('.apple').attr('lol');
|
||||
expect(attr).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(valid key) : valid attr should get value', function() {
|
||||
it('(valid key) : valid attr should get value', function () {
|
||||
var cls = $('.apple').attr('class');
|
||||
expect(cls).to.equal('apple');
|
||||
});
|
||||
|
||||
it('(valid key) : valid attr should get name when boolean', function() {
|
||||
it('(valid key) : valid attr should get name when boolean', function () {
|
||||
var attr = $('<input name=email autofocus>').attr('autofocus');
|
||||
expect(attr).to.equal('autofocus');
|
||||
});
|
||||
|
||||
it('(key, value) : should set attr', function() {
|
||||
it('(key, value) : should set attr', function () {
|
||||
var $pear = $('.pear').attr('id', 'pear');
|
||||
expect($('#pear')).to.have.length(1);
|
||||
expect($pear).to.be.a($);
|
||||
});
|
||||
|
||||
it('(key, value) : should set attr', function() {
|
||||
it('(key, value) : should set attr', function () {
|
||||
var $el = cheerio('<div></div> <div></div>').attr('class', 'pear');
|
||||
|
||||
expect($el[0].attribs['class']).to.equal('pear');
|
||||
@@ -50,17 +50,17 @@ describe('$(...)', function() {
|
||||
expect($el[2].attribs['class']).to.equal('pear');
|
||||
});
|
||||
|
||||
it('(key, value) : should return an empty object for an empty object', function() {
|
||||
it('(key, value) : should return an empty object for an empty object', function () {
|
||||
var $src = $().attr('key', 'value');
|
||||
expect($src.length).to.equal(0);
|
||||
expect($src[0]).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(map) : object map should set multiple attributes', function() {
|
||||
it('(map) : object map should set multiple attributes', function () {
|
||||
$('.apple').attr({
|
||||
id: 'apple',
|
||||
style: 'color:red;',
|
||||
'data-url': 'http://apple.com'
|
||||
'data-url': 'http://apple.com',
|
||||
});
|
||||
var attrs = $('.apple').attr();
|
||||
expect(attrs.id).to.equal('apple');
|
||||
@@ -68,9 +68,9 @@ describe('$(...)', function() {
|
||||
expect(attrs['data-url']).to.equal('http://apple.com');
|
||||
});
|
||||
|
||||
it('(key, function) : should call the function and update the attribute with the return value', function() {
|
||||
it('(key, function) : should call the function and update the attribute with the return value', function () {
|
||||
var $fruits = $('#fruits');
|
||||
$fruits.attr('id', function(index, value) {
|
||||
$fruits.attr('id', function (index, value) {
|
||||
expect(index).to.equal(0);
|
||||
expect(value).to.equal('fruits');
|
||||
return 'ninja';
|
||||
@@ -79,7 +79,7 @@ describe('$(...)', function() {
|
||||
expect(attrs.id).to.equal('ninja');
|
||||
});
|
||||
|
||||
it('(key, value) : should correctly encode then decode unsafe values', function() {
|
||||
it('(key, value) : should correctly encode then decode unsafe values', function () {
|
||||
var $apple = $('.apple');
|
||||
$apple.attr(
|
||||
'href',
|
||||
@@ -96,14 +96,14 @@ describe('$(...)', function() {
|
||||
expect($apple.html()).to.not.contain('<script>alert("XSS!")</script>');
|
||||
});
|
||||
|
||||
it('(key, value) : should coerce values to a string', function() {
|
||||
it('(key, value) : should coerce values to a string', function () {
|
||||
var $apple = $('.apple');
|
||||
$apple.attr('data-test', 1);
|
||||
expect($apple[0].attribs['data-test']).to.equal('1');
|
||||
expect($apple.attr('data-test')).to.equal('1');
|
||||
});
|
||||
|
||||
it('(key, value) : handle removed boolean attributes', function() {
|
||||
it('(key, value) : handle removed boolean attributes', function () {
|
||||
var $apple = $('.apple');
|
||||
$apple.attr('autofocus', 'autofocus');
|
||||
expect($apple.attr('autofocus')).to.equal('autofocus');
|
||||
@@ -111,7 +111,7 @@ describe('$(...)', function() {
|
||||
expect($apple.attr('autofocus')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(key, value) : should remove non-boolean attributes with names or values similar to boolean ones', function() {
|
||||
it('(key, value) : should remove non-boolean attributes with names or values similar to boolean ones', function () {
|
||||
var $apple = $('.apple');
|
||||
$apple.attr('data-autofocus', 'autofocus');
|
||||
expect($apple.attr('data-autofocus')).to.equal('autofocus');
|
||||
@@ -119,17 +119,17 @@ describe('$(...)', function() {
|
||||
expect($apple.attr('data-autofocus')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(key, value) : should remove attributes when called with null value', function() {
|
||||
it('(key, value) : should remove attributes when called with null value', function () {
|
||||
var $pear = $('.pear').attr('autofocus', 'autofocus');
|
||||
expect($pear.attr('autofocus')).to.equal('autofocus');
|
||||
$pear.attr('autofocus', null);
|
||||
expect($pear.attr('autofocus')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(map) : should remove attributes with null values', function() {
|
||||
it('(map) : should remove attributes with null values', function () {
|
||||
var $pear = $('.pear').attr({
|
||||
autofocus: 'autofocus',
|
||||
style: 'color:red'
|
||||
style: 'color:red',
|
||||
});
|
||||
expect($pear.attr('autofocus')).to.equal('autofocus');
|
||||
expect($pear.attr('style')).to.equal('color:red');
|
||||
@@ -139,16 +139,16 @@ describe('$(...)', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.prop', function() {
|
||||
describe('.prop', function () {
|
||||
var checkbox, selectMenu;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
$ = cheerio.load(inputs);
|
||||
selectMenu = $('select');
|
||||
checkbox = $('input[name=checkbox_on]');
|
||||
});
|
||||
|
||||
it('(valid key) : valid prop should get value', function() {
|
||||
it('(valid key) : valid prop should get value', function () {
|
||||
expect(checkbox.prop('checked')).to.equal(true);
|
||||
checkbox.css('display', 'none');
|
||||
expect(checkbox.prop('style').display).to.equal('none');
|
||||
@@ -158,12 +158,12 @@ describe('$(...)', function() {
|
||||
expect(checkbox.prop('nodeName')).to.equal('INPUT');
|
||||
});
|
||||
|
||||
it('(invalid key) : invalid prop should get undefined', function() {
|
||||
it('(invalid key) : invalid prop should get undefined', function () {
|
||||
var attr = checkbox.prop('lol');
|
||||
expect(attr).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(key, value) : should set prop', function() {
|
||||
it('(key, value) : should set prop', function () {
|
||||
expect(checkbox.prop('checked')).to.equal(true);
|
||||
checkbox.prop('checked', false);
|
||||
expect(checkbox.prop('checked')).to.equal(false);
|
||||
@@ -171,17 +171,17 @@ describe('$(...)', function() {
|
||||
expect(checkbox.prop('checked')).to.equal(true);
|
||||
});
|
||||
|
||||
it('(map) : object map should set multiple props', function() {
|
||||
it('(map) : object map should set multiple props', function () {
|
||||
checkbox.prop({
|
||||
id: 'check',
|
||||
checked: false
|
||||
checked: false,
|
||||
});
|
||||
expect(checkbox.prop('id')).to.equal('check');
|
||||
expect(checkbox.prop('checked')).to.equal(false);
|
||||
});
|
||||
|
||||
it('(key, function) : should call the function and update the prop with the return value', function() {
|
||||
checkbox.prop('checked', function(index, value) {
|
||||
it('(key, function) : should call the function and update the prop with the return value', function () {
|
||||
checkbox.prop('checked', function (index, value) {
|
||||
expect(index).to.equal(0);
|
||||
expect(value).to.equal(true);
|
||||
return false;
|
||||
@@ -189,72 +189,72 @@ describe('$(...)', function() {
|
||||
expect(checkbox.prop('checked')).to.equal(false);
|
||||
});
|
||||
|
||||
it('(key, value) : should support chaining after setting props', function() {
|
||||
it('(key, value) : should support chaining after setting props', function () {
|
||||
expect(checkbox.prop('checked', false)).to.equal(checkbox);
|
||||
});
|
||||
|
||||
it('(invalid element/tag) : prop should return undefined', function() {
|
||||
it('(invalid element/tag) : prop should return undefined', function () {
|
||||
expect($(undefined).prop('prop')).to.be(undefined);
|
||||
expect($(null).prop('prop')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(inherited properties) : prop should support inherited properties', function() {
|
||||
expect(selectMenu.prop('childNodes')).to.equal(selectMenu[0].childNodes);
|
||||
});
|
||||
|
||||
it('("outerHTML") : should render properly', function() {
|
||||
it('("outerHTML") : should render properly', function () {
|
||||
var outerHtml = '<div><a></a></div>';
|
||||
var $a = $(outerHtml);
|
||||
|
||||
expect($a.prop('outerHTML')).to.be(outerHtml);
|
||||
});
|
||||
|
||||
it('(inherited properties) : prop should support inherited properties', function () {
|
||||
expect(selectMenu.prop('childNodes')).to.equal(selectMenu[0].childNodes);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.data', function() {
|
||||
beforeEach(function() {
|
||||
describe('.data', function () {
|
||||
beforeEach(function () {
|
||||
$ = cheerio.load(chocolates);
|
||||
});
|
||||
|
||||
it('() : should get all data attributes initially declared in the markup', function() {
|
||||
it('() : should get all data attributes initially declared in the markup', function () {
|
||||
var data = $('.linth').data();
|
||||
expect(data).to.eql({
|
||||
highlight: 'Lindor',
|
||||
origin: 'swiss'
|
||||
origin: 'swiss',
|
||||
});
|
||||
});
|
||||
|
||||
it('() : should get all data set via `data`', function() {
|
||||
it('() : should get all data set via `data`', function () {
|
||||
var $el = cheerio('<div>');
|
||||
$el.data('a', 1);
|
||||
$el.data('b', 2);
|
||||
|
||||
expect($el.data()).to.eql({
|
||||
a: 1,
|
||||
b: 2
|
||||
b: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('() : should get all data attributes initially declared in the markup merged with all data additionally set via `data`', function() {
|
||||
it('() : should get all data attributes initially declared in the markup merged with all data additionally set via `data`', function () {
|
||||
var $el = cheerio('<div data-a="a">');
|
||||
$el.data('b', 'b');
|
||||
|
||||
expect($el.data()).to.eql({
|
||||
a: 'a',
|
||||
b: 'b'
|
||||
b: 'b',
|
||||
});
|
||||
});
|
||||
|
||||
it('() : no data attribute should return an empty object', function() {
|
||||
it('() : no data attribute should return an empty object', function () {
|
||||
var data = $('.cailler').data();
|
||||
expect(data).to.be.empty();
|
||||
});
|
||||
|
||||
it('(invalid key) : invalid data attribute should return `undefined` ', function() {
|
||||
it('(invalid key) : invalid data attribute should return `undefined` ', function () {
|
||||
var data = $('.frey').data('lol');
|
||||
expect(data).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(valid key) : valid data attribute should get value', function() {
|
||||
it('(valid key) : valid data attribute should get value', function () {
|
||||
var highlight = $('.linth').data('highlight');
|
||||
var origin = $('.linth').data('origin');
|
||||
|
||||
@@ -262,7 +262,7 @@ describe('$(...)', function() {
|
||||
expect(origin).to.equal('swiss');
|
||||
});
|
||||
|
||||
it('(key) : should translate camel-cased key values to hyphen-separated versions', function() {
|
||||
it('(key) : should translate camel-cased key values to hyphen-separated versions', function () {
|
||||
var $el = cheerio(
|
||||
'<div data--three-word-attribute="a" data-foo-Bar_BAZ-="b">'
|
||||
);
|
||||
@@ -271,7 +271,7 @@ describe('$(...)', function() {
|
||||
expect($el.data('fooBar_baz-')).to.be('b');
|
||||
});
|
||||
|
||||
it('(key) : should retrieve object values', function() {
|
||||
it('(key) : should retrieve object values', function () {
|
||||
var data = {};
|
||||
var $el = cheerio('<div>');
|
||||
|
||||
@@ -280,13 +280,13 @@ describe('$(...)', function() {
|
||||
expect($el.data('test')).to.be(data);
|
||||
});
|
||||
|
||||
it('(key) : should parse JSON data derived from the markup', function() {
|
||||
it('(key) : should parse JSON data derived from the markup', function () {
|
||||
var $el = cheerio('<div data-json="[1, 2, 3]">');
|
||||
|
||||
expect($el.data('json')).to.eql([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('(key) : should not parse JSON data set via the `data` API', function() {
|
||||
it('(key) : should not parse JSON data set via the `data` API', function () {
|
||||
var $el = cheerio('<div>');
|
||||
$el.data('json', '[1, 2, 3]');
|
||||
|
||||
@@ -294,7 +294,7 @@ describe('$(...)', function() {
|
||||
});
|
||||
|
||||
// See http://api.jquery.com/data/ and http://bugs.jquery.com/ticket/14523
|
||||
it('(key) : should ignore the markup value after the first access', function() {
|
||||
it('(key) : should ignore the markup value after the first access', function () {
|
||||
var $el = cheerio('<div data-test="a">');
|
||||
|
||||
expect($el.data('test')).to.be('a');
|
||||
@@ -304,24 +304,24 @@ describe('$(...)', function() {
|
||||
expect($el.data('test')).to.be('a');
|
||||
});
|
||||
|
||||
it('(key) : should recover from malformed JSON', function() {
|
||||
it('(key) : should recover from malformed JSON', function () {
|
||||
var $el = cheerio('<div data-custom="{{templatevar}}">');
|
||||
|
||||
expect($el.data('custom')).to.be('{{templatevar}}');
|
||||
});
|
||||
|
||||
it('(hyphen key) : data addribute with hyphen should be camelized ;-)', function() {
|
||||
it('(hyphen key) : data addribute with hyphen should be camelized ;-)', function () {
|
||||
var data = $('.frey').data();
|
||||
expect(data).to.eql({
|
||||
taste: 'sweet',
|
||||
bestCollection: 'Mahony'
|
||||
bestCollection: 'Mahony',
|
||||
});
|
||||
});
|
||||
|
||||
it('(key, value) : should set data attribute', function() {
|
||||
it('(key, value) : should set data attribute', function () {
|
||||
// Adding as object.
|
||||
var a = $('.frey').data({
|
||||
balls: 'giandor'
|
||||
balls: 'giandor',
|
||||
});
|
||||
// Adding as string.
|
||||
var b = $('.linth').data('snack', 'chocoletti');
|
||||
@@ -330,32 +330,20 @@ describe('$(...)', function() {
|
||||
expect(b.data('snack')).to.eql('chocoletti');
|
||||
});
|
||||
|
||||
it('(key, value) : should set data for all elements in the selection', function() {
|
||||
it('(key, value) : should set data for all elements in the selection', function () {
|
||||
$('li').data('foo', 'bar');
|
||||
|
||||
expect(
|
||||
$('li')
|
||||
.eq(0)
|
||||
.data('foo')
|
||||
).to.eql('bar');
|
||||
expect(
|
||||
$('li')
|
||||
.eq(1)
|
||||
.data('foo')
|
||||
).to.eql('bar');
|
||||
expect(
|
||||
$('li')
|
||||
.eq(2)
|
||||
.data('foo')
|
||||
).to.eql('bar');
|
||||
expect($('li').eq(0).data('foo')).to.eql('bar');
|
||||
expect($('li').eq(1).data('foo')).to.eql('bar');
|
||||
expect($('li').eq(2).data('foo')).to.eql('bar');
|
||||
});
|
||||
|
||||
it('(map) : object map should set multiple data attributes', function() {
|
||||
it('(map) : object map should set multiple data attributes', function () {
|
||||
var data = $('.linth').data({
|
||||
id: 'Cailler',
|
||||
flop: 'Pippilotti Rist',
|
||||
top: 'Frigor',
|
||||
url: 'http://www.cailler.ch/'
|
||||
url: 'http://www.cailler.ch/',
|
||||
})['0'].data;
|
||||
|
||||
expect(data.id).to.equal('Cailler');
|
||||
@@ -364,154 +352,150 @@ describe('$(...)', function() {
|
||||
expect(data.url).to.equal('http://www.cailler.ch/');
|
||||
});
|
||||
|
||||
describe('(attr) : data-* attribute type coercion :', function() {
|
||||
it('boolean', function() {
|
||||
describe('(attr) : data-* attribute type coercion :', function () {
|
||||
it('boolean', function () {
|
||||
var $el = cheerio('<div data-bool="true">');
|
||||
expect($el.data('bool')).to.be(true);
|
||||
});
|
||||
|
||||
it('number', function() {
|
||||
it('number', function () {
|
||||
var $el = cheerio('<div data-number="23">');
|
||||
expect($el.data('number')).to.be(23);
|
||||
});
|
||||
|
||||
it('number (scientific notation is not coerced)', function() {
|
||||
it('number (scientific notation is not coerced)', function () {
|
||||
var $el = cheerio('<div data-sci="1E10">');
|
||||
expect($el.data('sci')).to.be('1E10');
|
||||
});
|
||||
|
||||
it('null', function() {
|
||||
it('null', function () {
|
||||
var $el = cheerio('<div data-null="null">');
|
||||
expect($el.data('null')).to.be(null);
|
||||
});
|
||||
|
||||
it('object', function() {
|
||||
it('object', function () {
|
||||
var $el = cheerio('<div data-obj=\'{ "a": 45 }\'>');
|
||||
expect($el.data('obj')).to.eql({ a: 45 });
|
||||
});
|
||||
|
||||
it('array', function() {
|
||||
it('array', function () {
|
||||
var $el = cheerio('<div data-array="[1, 2, 3]">');
|
||||
expect($el.data('array')).to.eql([1, 2, 3]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.val', function() {
|
||||
beforeEach(function() {
|
||||
describe('.val', function () {
|
||||
beforeEach(function () {
|
||||
$ = cheerio.load(inputs);
|
||||
});
|
||||
|
||||
it('(): on select should get value', function() {
|
||||
it('(): on select should get value', function () {
|
||||
var val = $('select#one').val();
|
||||
expect(val).to.equal('option_selected');
|
||||
});
|
||||
it('(): on select with no value should get text', function() {
|
||||
it('(): on select with no value should get text', function () {
|
||||
var val = $('select#one-valueless').val();
|
||||
expect(val).to.equal('Option selected');
|
||||
});
|
||||
it('(): on select with no value should get converted HTML', function() {
|
||||
it('(): on select with no value should get converted HTML', function () {
|
||||
var val = $('select#one-html-entity').val();
|
||||
expect(val).to.equal('Option <selected>');
|
||||
});
|
||||
it('(): on select with no value should get text content', function() {
|
||||
it('(): on select with no value should get text content', function () {
|
||||
var val = $('select#one-nested').val();
|
||||
expect(val).to.equal('Option selected');
|
||||
});
|
||||
it('(): on option should get value', function() {
|
||||
var val = $('select#one option')
|
||||
.eq(0)
|
||||
.val();
|
||||
it('(): on option should get value', function () {
|
||||
var val = $('select#one option').eq(0).val();
|
||||
expect(val).to.equal('option_not_selected');
|
||||
});
|
||||
it('(): on text input should get value', function() {
|
||||
it('(): on text input should get value', function () {
|
||||
var val = $('input[type="text"]').val();
|
||||
expect(val).to.equal('input_text');
|
||||
});
|
||||
it('(): on checked checkbox should get value', function() {
|
||||
it('(): on checked checkbox should get value', function () {
|
||||
var val = $('input[name="checkbox_on"]').val();
|
||||
expect(val).to.equal('on');
|
||||
});
|
||||
it('(): on unchecked checkbox should get value', function() {
|
||||
it('(): on unchecked checkbox should get value', function () {
|
||||
var val = $('input[name="checkbox_off"]').val();
|
||||
expect(val).to.equal('off');
|
||||
});
|
||||
it('(): on valueless checkbox should get value', function() {
|
||||
it('(): on valueless checkbox should get value', function () {
|
||||
var val = $('input[name="checkbox_valueless"]').val();
|
||||
expect(val).to.equal('on');
|
||||
});
|
||||
it('(): on radio should get value', function() {
|
||||
it('(): on radio should get value', function () {
|
||||
var val = $('input[type="radio"]').val();
|
||||
expect(val).to.equal('off');
|
||||
});
|
||||
it('(): on valueless radio should get value', function() {
|
||||
it('(): on valueless radio should get value', function () {
|
||||
var val = $('input[name="radio_valueless"]').val();
|
||||
expect(val).to.equal('on');
|
||||
});
|
||||
it('(): on multiple select should get an array of values', function() {
|
||||
it('(): on multiple select should get an array of values', function () {
|
||||
var val = $('select#multi').val();
|
||||
expect(val).to.eql(['2', '3']);
|
||||
});
|
||||
it('(): on multiple select with no value attribute should get an array of text content', function() {
|
||||
it('(): on multiple select with no value attribute should get an array of text content', function () {
|
||||
var val = $('select#multi-valueless').val();
|
||||
expect(val).to.eql(['2', '3']);
|
||||
});
|
||||
it('(): with no selector matches should return nothing', function() {
|
||||
it('(): with no selector matches should return nothing', function () {
|
||||
var val = $('.nasty').val();
|
||||
expect(val).to.equal(undefined);
|
||||
});
|
||||
it('(invalid value): should only handle arrays when it has the attribute multiple', function() {
|
||||
it('(invalid value): should only handle arrays when it has the attribute multiple', function () {
|
||||
var val = $('select#one').val([]);
|
||||
expect(val).not.to.equal(undefined);
|
||||
});
|
||||
it('(value): on input text should set value', function() {
|
||||
it('(value): on input text should set value', function () {
|
||||
var element = $('input[type="text"]').val('test');
|
||||
expect(element.val()).to.equal('test');
|
||||
});
|
||||
it('(value): on select should set value', function() {
|
||||
it('(value): on select should set value', function () {
|
||||
var element = $('select#one').val('option_not_selected');
|
||||
expect(element.val()).to.equal('option_not_selected');
|
||||
});
|
||||
it('(value): on option should set value', function() {
|
||||
var element = $('select#one option')
|
||||
.eq(0)
|
||||
.val('option_changed');
|
||||
it('(value): on option should set value', function () {
|
||||
var element = $('select#one option').eq(0).val('option_changed');
|
||||
expect(element.val()).to.equal('option_changed');
|
||||
});
|
||||
it('(value): on radio should set value', function() {
|
||||
it('(value): on radio should set value', function () {
|
||||
var element = $('input[name="radio"]').val('off');
|
||||
expect(element.val()).to.equal('off');
|
||||
});
|
||||
it('(value): on radio with special characters should set value', function() {
|
||||
it('(value): on radio with special characters should set value', function () {
|
||||
var element = $('input[name="radio[brackets]"]').val('off');
|
||||
expect(element.val()).to.equal('off');
|
||||
});
|
||||
it('(values): on multiple select should set multiple values', function() {
|
||||
it('(values): on multiple select should set multiple values', function () {
|
||||
var element = $('select#multi').val(['1', '3', '4']);
|
||||
expect(element.val()).to.have.length(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.removeAttr', function() {
|
||||
it('(key) : should remove a single attr', function() {
|
||||
describe('.removeAttr', function () {
|
||||
it('(key) : should remove a single attr', function () {
|
||||
var $fruits = $('#fruits');
|
||||
expect($fruits.attr('id')).to.not.be(undefined);
|
||||
$fruits.removeAttr('id');
|
||||
expect($fruits.attr('id')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('should return cheerio object', function() {
|
||||
it('should return cheerio object', function () {
|
||||
var obj = $('ul').removeAttr('id');
|
||||
expect(obj).to.be.a($);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.hasClass', function() {
|
||||
describe('.hasClass', function () {
|
||||
function test(attr) {
|
||||
return cheerio('<div class="' + attr + '"></div>');
|
||||
}
|
||||
|
||||
it('(valid class) : should return true', function() {
|
||||
it('(valid class) : should return true', function () {
|
||||
var cls = $('.apple').hasClass('apple');
|
||||
expect(cls).to.be.ok();
|
||||
|
||||
@@ -521,7 +505,7 @@ describe('$(...)', function() {
|
||||
expect(test('bar foo bar').hasClass('foo')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(invalid class) : should return false', function() {
|
||||
it('(invalid class) : should return false', function () {
|
||||
var cls = $('#fruits').hasClass('fruits');
|
||||
expect(cls).to.not.be.ok();
|
||||
expect(test('foo-bar').hasClass('foo')).to.not.be.ok();
|
||||
@@ -529,7 +513,7 @@ describe('$(...)', function() {
|
||||
expect(test('foo-bar').hasClass('foo-ba')).to.not.be.ok();
|
||||
});
|
||||
|
||||
it('should check multiple classes', function() {
|
||||
it('should check multiple classes', function () {
|
||||
// Add a class
|
||||
$('.apple').addClass('red');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
@@ -537,47 +521,39 @@ describe('$(...)', function() {
|
||||
|
||||
// Remove one and test again
|
||||
$('.apple').removeClass('apple');
|
||||
expect(
|
||||
$('li')
|
||||
.eq(0)
|
||||
.hasClass('apple')
|
||||
).to.not.be.ok();
|
||||
expect($('li').eq(0).hasClass('apple')).to.not.be.ok();
|
||||
// expect($('li', $fruits).eq(0).hasClass('red')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(empty string argument) : should return false', function() {
|
||||
it('(empty string argument) : should return false', function () {
|
||||
expect(test('foo').hasClass('')).to.not.be.ok();
|
||||
expect(test('foo bar').hasClass('')).to.not.be.ok();
|
||||
expect(
|
||||
test('foo bar')
|
||||
.removeClass('foo')
|
||||
.hasClass('')
|
||||
).to.not.be.ok();
|
||||
expect(test('foo bar').removeClass('foo').hasClass('')).to.not.be.ok();
|
||||
});
|
||||
});
|
||||
|
||||
describe('.addClass', function() {
|
||||
it('(first class) : should add the class to the element', function() {
|
||||
describe('.addClass', function () {
|
||||
it('(first class) : should add the class to the element', function () {
|
||||
var $fruits = $('#fruits');
|
||||
$fruits.addClass('fruits');
|
||||
var cls = $fruits.hasClass('fruits');
|
||||
expect(cls).to.be.ok();
|
||||
});
|
||||
|
||||
it('(single class) : should add the class to the element', function() {
|
||||
it('(single class) : should add the class to the element', function () {
|
||||
$('.apple').addClass('fruit');
|
||||
var cls = $('.apple').hasClass('fruit');
|
||||
expect(cls).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class): adds classes to many selected items', function() {
|
||||
it('(class): adds classes to many selected items', function () {
|
||||
$('li').addClass('fruit');
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
expect($('.orange').hasClass('fruit')).to.be.ok();
|
||||
expect($('.pear').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class class class) : should add multiple classes to the element', function() {
|
||||
it('(class class class) : should add multiple classes to the element', function () {
|
||||
$('.apple').addClass('fruit red tasty');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
@@ -585,13 +561,13 @@ describe('$(...)', function() {
|
||||
expect($('.apple').hasClass('tasty')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should add classes returned from the function', 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) {
|
||||
$fruits.addClass(function (idx) {
|
||||
args.push(toArray(arguments));
|
||||
thisVals.push(this);
|
||||
return toAdd[idx];
|
||||
@@ -600,7 +576,7 @@ describe('$(...)', function() {
|
||||
expect(args).to.eql([
|
||||
[0, 'apple'],
|
||||
[1, 'orange'],
|
||||
[2, 'pear']
|
||||
[2, 'pear'],
|
||||
]);
|
||||
expect(thisVals).to.eql([$fruits[0], $fruits[1], $fruits[2]]);
|
||||
expect($fruits.eq(0).hasClass('apple')).to.be.ok();
|
||||
@@ -610,33 +586,33 @@ describe('$(...)', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.removeClass', function() {
|
||||
it('() : should remove all the classes', function() {
|
||||
describe('.removeClass', function () {
|
||||
it('() : should remove all the classes', function () {
|
||||
$('.pear').addClass('fruit');
|
||||
$('.pear').removeClass();
|
||||
expect($('.pear').attr('class')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('("") : should not modify class list', function() {
|
||||
it('("") : should not modify class list', function () {
|
||||
var $fruits = $('#fruits');
|
||||
$fruits.children().removeClass('');
|
||||
expect($('.apple')).to.have.length(1);
|
||||
});
|
||||
|
||||
it('(invalid class) : should not remove anything', function() {
|
||||
it('(invalid class) : should not remove anything', function () {
|
||||
$('.pear').removeClass('fruit');
|
||||
expect($('.pear').hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(no class attribute) : should not throw an exception', function() {
|
||||
it('(no class attribute) : should not throw an exception', function () {
|
||||
var $vegetables = cheerio(vegetables);
|
||||
|
||||
expect(function() {
|
||||
expect(function () {
|
||||
$('li', $vegetables).removeClass('vegetable');
|
||||
}).to.not.throwException();
|
||||
});
|
||||
|
||||
it('(single class) : should remove a single class from the element', function() {
|
||||
it('(single class) : should remove a single class from the element', function () {
|
||||
$('.pear').addClass('fruit');
|
||||
expect($('.pear').hasClass('fruit')).to.be.ok();
|
||||
$('.pear').removeClass('fruit');
|
||||
@@ -644,7 +620,7 @@ describe('$(...)', function() {
|
||||
expect($('.pear').hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(single class) : should remove a single class from multiple classes on the element', function() {
|
||||
it('(single class) : should remove a single class from multiple classes on the element', function () {
|
||||
$('.pear').addClass('fruit green tasty');
|
||||
expect($('.pear').hasClass('fruit')).to.be.ok();
|
||||
expect($('.pear').hasClass('green')).to.be.ok();
|
||||
@@ -656,7 +632,7 @@ describe('$(...)', function() {
|
||||
expect($('.pear').hasClass('tasty')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class class class) : should remove multiple classes from the element', function() {
|
||||
it('(class class class) : should remove multiple classes from the element', function () {
|
||||
$('.apple').addClass('fruit red tasty');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
@@ -670,18 +646,18 @@ describe('$(...)', function() {
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class) : should remove all occurrences of a class name', function() {
|
||||
it('(class) : should remove all occurrences of a class name', function () {
|
||||
var $div = cheerio('<div class="x x y x z"></div>');
|
||||
expect($div.removeClass('x').hasClass('x')).to.be(false);
|
||||
});
|
||||
|
||||
it('(fn) : should remove classes returned from the function', 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) {
|
||||
$fruits.removeClass(function (idx) {
|
||||
args.push(toArray(arguments));
|
||||
thisVals.push(this);
|
||||
return toAdd[idx];
|
||||
@@ -690,7 +666,7 @@ describe('$(...)', function() {
|
||||
expect(args).to.eql([
|
||||
[0, 'apple'],
|
||||
[1, 'orange'],
|
||||
[2, 'pear']
|
||||
[2, 'pear'],
|
||||
]);
|
||||
expect(thisVals).to.eql([$fruits[0], $fruits[1], $fruits[2]]);
|
||||
expect($fruits.eq(0).hasClass('apple')).to.not.be.ok();
|
||||
@@ -699,17 +675,17 @@ describe('$(...)', function() {
|
||||
expect($fruits.eq(2).hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should no op elements without attributes', function() {
|
||||
it('(fn) : should no op elements without attributes', function () {
|
||||
var $inputs = $(inputs);
|
||||
var val = $inputs.removeClass(function() {
|
||||
var val = $inputs.removeClass(function () {
|
||||
return 'tasty';
|
||||
});
|
||||
expect(val).to.have.length(15);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.toggleClass', function() {
|
||||
it('(class class) : should toggle multiple classes from the element', function() {
|
||||
describe('.toggleClass', function () {
|
||||
it('(class class) : should toggle multiple classes from the element', function () {
|
||||
$('.apple').addClass('fruit');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
@@ -721,7 +697,7 @@ describe('$(...)', function() {
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class class, true) : should add multiple classes to the element', function() {
|
||||
it('(class class, true) : should add multiple classes to the element', function () {
|
||||
$('.apple').addClass('fruit');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
@@ -733,17 +709,13 @@ describe('$(...)', function() {
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class true) : should add only one instance of class', function() {
|
||||
it('(class true) : should add only one instance of class', function () {
|
||||
$('.apple').toggleClass('tasty', true);
|
||||
$('.apple').toggleClass('tasty', true);
|
||||
expect(
|
||||
$('.apple')
|
||||
.attr('class')
|
||||
.match(/tasty/g).length
|
||||
).to.equal(1);
|
||||
expect($('.apple').attr('class').match(/tasty/g).length).to.equal(1);
|
||||
});
|
||||
|
||||
it('(class class, false) : should remove multiple classes from the element', function() {
|
||||
it('(class class, false) : should remove multiple classes from the element', function () {
|
||||
$('.apple').addClass('fruit');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
@@ -755,7 +727,7 @@ describe('$(...)', function() {
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should toggle classes returned from the function', function() {
|
||||
it('(fn) : should toggle classes returned from the function', function () {
|
||||
$ = cheerio.load(food);
|
||||
|
||||
$('.apple').addClass('fruit');
|
||||
@@ -769,12 +741,8 @@ describe('$(...)', function() {
|
||||
expect($('.sweetcorn').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.sweetcorn').hasClass('vegetable')).to.not.be.ok();
|
||||
|
||||
$('li').toggleClass(function() {
|
||||
return $(this)
|
||||
.parent()
|
||||
.is('#fruits')
|
||||
? 'fruit'
|
||||
: 'vegetable';
|
||||
$('li').toggleClass(function () {
|
||||
return $(this).parent().is('#fruits') ? 'fruit' : 'vegetable';
|
||||
});
|
||||
expect($('.apple').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.apple').hasClass('vegetable')).to.not.be.ok();
|
||||
@@ -786,9 +754,9 @@ describe('$(...)', function() {
|
||||
expect($('.sweetcorn').hasClass('vegetable')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should work with no initial class attribute', function() {
|
||||
it('(fn) : should work with no initial class attribute', function () {
|
||||
var $inputs = cheerio.load(inputs);
|
||||
$inputs('input, select').toggleClass(function() {
|
||||
$inputs('input, select').toggleClass(function () {
|
||||
return $inputs(this).get(0).tagName === 'select'
|
||||
? 'selectable'
|
||||
: 'inputable';
|
||||
@@ -797,7 +765,7 @@ describe('$(...)', function() {
|
||||
expect($inputs('.inputable')).to.have.length(9);
|
||||
});
|
||||
|
||||
it('(invalid) : should be a no-op for invalid inputs', function() {
|
||||
it('(invalid) : should be a no-op for invalid inputs', function () {
|
||||
var original = $('.apple');
|
||||
var testAgainst = original.attr('class');
|
||||
expect(original.toggleClass().attr('class')).to.be.eql(testAgainst);
|
||||
@@ -810,52 +778,52 @@ describe('$(...)', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.is', function() {
|
||||
it('() : should return false', function() {
|
||||
describe('.is', function () {
|
||||
it('() : should return false', function () {
|
||||
expect($('li.apple').is()).to.be(false);
|
||||
});
|
||||
|
||||
it('(true selector) : should return true', function() {
|
||||
it('(true selector) : should return true', function () {
|
||||
expect(cheerio('#vegetables', vegetables).is('ul')).to.be(true);
|
||||
});
|
||||
|
||||
it('(false selector) : should return false', function() {
|
||||
it('(false selector) : should return false', function () {
|
||||
expect(cheerio('#vegetables', vegetables).is('div')).to.be(false);
|
||||
});
|
||||
|
||||
it('(true selection) : should return true', function() {
|
||||
it('(true selection) : should return true', function () {
|
||||
var $vegetables = cheerio('li', vegetables);
|
||||
expect($vegetables.is($vegetables.eq(1))).to.be(true);
|
||||
});
|
||||
|
||||
it('(false selection) : should return false', function() {
|
||||
it('(false selection) : should return false', function () {
|
||||
var $vegetableList = cheerio(vegetables);
|
||||
var $vegetables = $vegetableList.find('li');
|
||||
expect($vegetables.is($vegetableList)).to.be(false);
|
||||
});
|
||||
|
||||
it('(true element) : should return true', function() {
|
||||
it('(true element) : should return true', function () {
|
||||
var $vegetables = cheerio('li', vegetables);
|
||||
expect($vegetables.is($vegetables[0])).to.be(true);
|
||||
});
|
||||
|
||||
it('(false element) : should return false', function() {
|
||||
it('(false element) : should return false', function () {
|
||||
var $vegetableList = cheerio(vegetables);
|
||||
var $vegetables = $vegetableList.find('li');
|
||||
expect($vegetables.is($vegetableList[0])).to.be(false);
|
||||
});
|
||||
|
||||
it('(true predicate) : should return true', function() {
|
||||
var result = $('li').is(function() {
|
||||
it('(true predicate) : should return true', function () {
|
||||
var result = $('li').is(function () {
|
||||
return this.tagName === 'li' && $(this).hasClass('pear');
|
||||
});
|
||||
expect(result).to.be(true);
|
||||
});
|
||||
|
||||
it('(false predicate) : should return false', function() {
|
||||
it('(false predicate) : should return false', function () {
|
||||
var result = $('li')
|
||||
.last()
|
||||
.is(function() {
|
||||
.is(function () {
|
||||
return this.tagName === 'ul';
|
||||
});
|
||||
expect(result).to.be(false);
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
var expect = require('expect.js');
|
||||
var cheerio = require('../..');
|
||||
|
||||
describe('$(...)', function() {
|
||||
describe('.css', function() {
|
||||
it('(prop): should return a css property value', function() {
|
||||
describe('$(...)', function () {
|
||||
describe('.css', function () {
|
||||
it('(prop): should return a css property value', function () {
|
||||
var el = cheerio('<li style="hai: there">');
|
||||
expect(el.css('hai')).to.equal('there');
|
||||
});
|
||||
|
||||
it('([prop1, prop2]): should return the specified property values as an object', function() {
|
||||
it('([prop1, prop2]): should return the specified property values as an object', function () {
|
||||
var el = cheerio('<li style="margin: 1px; padding: 2px; color: blue;">');
|
||||
expect(el.css(['margin', 'color'])).to.eql({
|
||||
margin: '1px',
|
||||
color: 'blue'
|
||||
color: 'blue',
|
||||
});
|
||||
});
|
||||
|
||||
it('(prop, val): should set a css property', function() {
|
||||
it('(prop, val): should set a css property', function () {
|
||||
var el = cheerio('<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() {
|
||||
it('(prop, ""): should unset a css property', function () {
|
||||
var el = cheerio('<li style="padding: 1px; margin: 0;">');
|
||||
el.css('padding', '');
|
||||
expect(el.attr('style')).to.equal('margin: 0;');
|
||||
});
|
||||
|
||||
it('(prop): should not mangle embedded urls', function() {
|
||||
it('(prop): should not mangle embedded urls', function () {
|
||||
var el = cheerio(
|
||||
'<li style="background-image:url(http://example.com/img.png);">'
|
||||
);
|
||||
@@ -38,12 +38,12 @@ describe('$(...)', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('(prop): should ignore blank properties', function() {
|
||||
it('(prop): should ignore blank properties', function () {
|
||||
var el = cheerio('<li style=":#ccc;color:#aaa;">');
|
||||
expect(el.css()).to.eql({ color: '#aaa' });
|
||||
});
|
||||
|
||||
it('(prop): should ignore blank values', function() {
|
||||
it('(prop): should ignore blank values', function () {
|
||||
var el = cheerio('<li style="color:;position:absolute;">');
|
||||
expect(el.css()).to.eql({ position: 'absolute' });
|
||||
});
|
||||
@@ -65,10 +65,10 @@ describe('$(...)', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('should iterate over the selection', function() {
|
||||
it('should iterate over the selection', function () {
|
||||
var count = 0;
|
||||
var $el = this.$el;
|
||||
this.$el.css('margin', function(idx, value) {
|
||||
this.$el.css('margin', function (idx, value) {
|
||||
expect(idx).to.equal(count);
|
||||
expect(value).to.equal(count + 'px');
|
||||
expect(this).to.equal($el[count]);
|
||||
@@ -77,9 +77,9 @@ describe('$(...)', function() {
|
||||
expect(count).to.equal(3);
|
||||
});
|
||||
|
||||
it('should set each attribute independently', function() {
|
||||
it('should set each attribute independently', function () {
|
||||
var values = ['4px', '', undefined];
|
||||
this.$el.css('margin', function(idx) {
|
||||
this.$el.css('margin', function (idx) {
|
||||
return values[idx];
|
||||
});
|
||||
expect(this.$el.eq(0).attr('style')).to.equal('margin: 4px;');
|
||||
@@ -88,15 +88,15 @@ describe('$(...)', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('(obj): should set each key and val', function() {
|
||||
it('(obj): should set each key and val', function () {
|
||||
var el = cheerio('<li style="padding: 0;"></li><li></li>');
|
||||
el.css({ 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() {
|
||||
describe('parser', function () {
|
||||
it('should allow any whitespace between declarations', function () {
|
||||
var el = cheerio('<li style="one \t:\n 0;\n two \f\r:\v 1">');
|
||||
expect(el.css(['one', 'two'])).to.eql({ one: 0, two: 1 });
|
||||
});
|
||||
|
||||
@@ -7,20 +7,22 @@ var expect = require('expect.js'),
|
||||
fixtures = require('../fixtures'),
|
||||
cheerio = require('../..');
|
||||
|
||||
describe('deprecated APIs', function() {
|
||||
describe('deprecated APIs', function () {
|
||||
/**
|
||||
* The `.parseHTML` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* In order to promote consistency with the jQuery library, users are
|
||||
* encouraged to instead use the static method of the same name as it is
|
||||
* defined on the "loaded" Cheerio factory function. For example:
|
||||
* defined on the "loaded" Cheerio factory function.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* var $ = cheerio.load('');
|
||||
* $.parseHTML('<b>markup</b>');
|
||||
*/
|
||||
describe('cheerio module', function() {
|
||||
describe('.parseHTML', function() {
|
||||
it('(html) : should preserve content', function() {
|
||||
describe('cheerio module', function () {
|
||||
describe('.parseHTML', function () {
|
||||
it('(html) : should preserve content', function () {
|
||||
var html = '<div>test div</div>';
|
||||
expect(cheerio(cheerio.parseHTML(html)[0]).html()).to.equal('test div');
|
||||
});
|
||||
@@ -30,39 +32,40 @@ describe('deprecated APIs', function() {
|
||||
* The `.merge` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* In order to promote consistency with the jQuery library, users are
|
||||
* encouraged to instead use the static method of the same name. For example:
|
||||
* encouraged to instead use the static method of the same name.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('');
|
||||
* $.merge([1, 2], [3, 4]) // [1, 2, 3, 4]
|
||||
*/
|
||||
describe('.merge', function() {
|
||||
describe('.merge', function () {
|
||||
var arr1, arr2;
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
arr1 = [1, 2, 3];
|
||||
arr2 = [4, 5, 6];
|
||||
});
|
||||
|
||||
it('should be a function', function() {
|
||||
it('should be a function', function () {
|
||||
expect(typeof cheerio.merge).to.equal('function');
|
||||
});
|
||||
|
||||
it('(arraylike, arraylike) : should return an array', function() {
|
||||
it('(arraylike, arraylike) : should return an array', function () {
|
||||
var ret = cheerio.merge(arr1, arr2);
|
||||
expect(typeof ret).to.equal('object');
|
||||
expect(ret instanceof Array).to.be.ok();
|
||||
});
|
||||
|
||||
it('(arraylike, arraylike) : should modify the first array', function() {
|
||||
it('(arraylike, arraylike) : should modify the first array', function () {
|
||||
cheerio.merge(arr1, arr2);
|
||||
expect(arr1).to.have.length(6);
|
||||
});
|
||||
|
||||
it('(arraylike, arraylike) : should not modify the second array', function() {
|
||||
it('(arraylike, arraylike) : should not modify the second array', function () {
|
||||
cheerio.merge(arr1, arr2);
|
||||
expect(arr2).to.have.length(3);
|
||||
});
|
||||
|
||||
it('(arraylike, arraylike) : should handle objects that arent arrays, but are arraylike', function() {
|
||||
it('(arraylike, arraylike) : should handle objects that arent arrays, but are arraylike', function () {
|
||||
arr1 = {};
|
||||
arr2 = {};
|
||||
arr1.length = 3;
|
||||
@@ -81,7 +84,7 @@ describe('deprecated APIs', function() {
|
||||
expect(arr2).to.have.length(3);
|
||||
});
|
||||
|
||||
it('(?, ?) : should gracefully reject invalid inputs', function() {
|
||||
it('(?, ?) : should gracefully reject invalid inputs', function () {
|
||||
var ret = cheerio.merge([4], 3);
|
||||
expect(ret).to.not.be.ok();
|
||||
ret = cheerio.merge({}, {});
|
||||
@@ -107,7 +110,7 @@ describe('deprecated APIs', function() {
|
||||
expect(ret).to.not.be.ok();
|
||||
});
|
||||
|
||||
it('(?, ?) : should no-op on invalid inputs', function() {
|
||||
it('(?, ?) : should no-op on invalid inputs', function () {
|
||||
var fakeArray1 = { length: 3 };
|
||||
fakeArray1[0] = 'a';
|
||||
fakeArray1[1] = 'b';
|
||||
@@ -129,20 +132,21 @@ describe('deprecated APIs', function() {
|
||||
* The `.contains` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* In order to promote consistency with the jQuery library, users are
|
||||
* encouraged to instead use the static method of the same name. For example:
|
||||
* encouraged to instead use the static method of the same name.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('<div><p></p></div>');
|
||||
* $.contains($('div').get(0), $('p').get(0)); // true
|
||||
* $.contains($('p').get(0), $('div').get(0)); // false
|
||||
*/
|
||||
describe('.contains', function() {
|
||||
describe('.contains', function () {
|
||||
var $;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
$ = cheerio.load(fixtures.food);
|
||||
});
|
||||
|
||||
it('(container, contained) : should correctly detect the provided element', function() {
|
||||
it('(container, contained) : should correctly detect the provided element', function () {
|
||||
var $food = $('#food');
|
||||
var $fruits = $('#fruits');
|
||||
var $apple = $('.apple');
|
||||
@@ -151,7 +155,7 @@ describe('deprecated APIs', function() {
|
||||
expect(cheerio.contains($food[0], $apple[0])).to.equal(true);
|
||||
});
|
||||
|
||||
it('(container, other) : should not detect elements that are not contained', function() {
|
||||
it('(container, other) : should not detect elements that are not contained', function () {
|
||||
var $fruits = $('#fruits');
|
||||
var $vegetables = $('#vegetables');
|
||||
var $apple = $('.apple');
|
||||
@@ -170,29 +174,30 @@ describe('deprecated APIs', function() {
|
||||
* The `.root` method exported by the Cheerio module is deprecated.
|
||||
*
|
||||
* Users seeking to access the top-level element of a parsed document should
|
||||
* instead use the `root` static method of a "loaded" Cheerio function. For
|
||||
* example:
|
||||
* instead use the `root` static method of a "loaded" Cheerio function.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('');
|
||||
* $.root();
|
||||
*/
|
||||
describe('.root', function() {
|
||||
it('returns an empty selection', function() {
|
||||
describe('.root', function () {
|
||||
it('returns an empty selection', function () {
|
||||
var $empty = cheerio.root();
|
||||
expect($empty).to.have.length(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Cheerio function', function() {
|
||||
describe('Cheerio function', function () {
|
||||
/**
|
||||
* The `.load` static method defined on the "loaded" Cheerio factory
|
||||
* function is deprecated. Users are encouraged to instead use the `load`
|
||||
* function exported by the Cheerio module. For example:
|
||||
* function exported by the Cheerio module.
|
||||
*
|
||||
* @example
|
||||
* var $ = cheerio.load('<h1>Hello, <span>world</span>.</h1>');
|
||||
*/
|
||||
it('.load', function() {
|
||||
it('.load', function () {
|
||||
var $1 = cheerio.load(fixtures.fruits);
|
||||
var $2 = $1.load('<div><p>Some <a>text</a>.</p></div>');
|
||||
|
||||
@@ -208,22 +213,22 @@ describe('deprecated APIs', function() {
|
||||
* example:
|
||||
*
|
||||
* var $ = cheerio.load('<h1>Hello, <span>world</span>.</h1>');
|
||||
* $('h1').html(); // '<h1>Hello, <span>world</span>.'
|
||||
* $('h1').html(); // '<h1>Hello, <span>world</span>.'.
|
||||
*
|
||||
* To render the markup of an entire document, invoke the `html` function
|
||||
* exported by the Cheerio module with a "root" selection, e.g.
|
||||
*
|
||||
* cheerio.html($.root()); // '<html><head></head><body><h1>Hello, <span>world</span>.</h1></body></html>'
|
||||
* Cheerio.html($.root()); // '<html><head></head><body><h1>Hello, <span>world</span>.</h1></body></html>'.
|
||||
*/
|
||||
describe('.html - deprecated API', function() {
|
||||
it('() : of empty cheerio object should return null', function() {
|
||||
describe('.html - deprecated API', function () {
|
||||
it('() : of empty cheerio object should return null', function () {
|
||||
// Note: the direct invocation of the Cheerio constructor function is
|
||||
// also deprecated.
|
||||
var $ = cheerio();
|
||||
expect($.html()).to.be(null);
|
||||
});
|
||||
|
||||
it('(selector) : should return the outerHTML of the selected element', function() {
|
||||
it('(selector) : should return the outerHTML of the selected element', function () {
|
||||
var $ = cheerio.load(fixtures.fruits);
|
||||
expect($.html('.pear')).to.equal('<li class="pear">Pear</li>');
|
||||
});
|
||||
@@ -232,12 +237,13 @@ describe('deprecated APIs', function() {
|
||||
/**
|
||||
* The `.xml` static method defined on the "loaded" Cheerio factory function
|
||||
* is deprecated. Users are encouraged to instead use the `xml` function
|
||||
* exported by the Cheerio module. For example:
|
||||
* exported by the Cheerio module.
|
||||
*
|
||||
* @example
|
||||
* cheerio.xml($.root());
|
||||
*/
|
||||
describe('.xml - deprecated API', function() {
|
||||
it('() : renders XML', function() {
|
||||
describe('.xml - deprecated API', function () {
|
||||
it('() : renders XML', function () {
|
||||
var $ = cheerio.load('<foo></foo>', { xmlMode: true });
|
||||
expect($.xml()).to.equal('<foo/>');
|
||||
});
|
||||
@@ -252,27 +258,27 @@ describe('deprecated APIs', function() {
|
||||
* example:
|
||||
*
|
||||
* var $ = cheerio.load('<h1>Hello, <span>world</span>.</h1>');
|
||||
* $('h1').text(); // 'Hello, world.'
|
||||
* $('h1').text(); // 'Hello, world.'.
|
||||
*
|
||||
* To render the text content of an entire document, invoke the `text`
|
||||
* function exported by the Cheerio module with a "root" selection, e.g.
|
||||
*
|
||||
* cheerio.text($.root()); // 'Hello, world.'
|
||||
* Cheerio.text($.root()); // 'Hello, world.'.
|
||||
*/
|
||||
describe('.text - deprecated API', function() {
|
||||
it('(cheerio object) : should return the text contents of the specified elements', function() {
|
||||
describe('.text - deprecated API', function () {
|
||||
it('(cheerio object) : should return the text contents of the specified elements', function () {
|
||||
var $ = cheerio.load('<a>This is <em>content</em>.</a>');
|
||||
expect($.text($('a'))).to.equal('This is content.');
|
||||
});
|
||||
|
||||
it('(cheerio object) : should omit comment nodes', function() {
|
||||
it('(cheerio object) : should omit comment nodes', function () {
|
||||
var $ = cheerio.load(
|
||||
'<a>This is <!-- a comment --> not a comment.</a>'
|
||||
);
|
||||
expect($.text($('a'))).to.equal('This is not a comment.');
|
||||
});
|
||||
|
||||
it('(cheerio object) : should include text contents of children recursively', function() {
|
||||
it('(cheerio object) : should include text contents of children recursively', function () {
|
||||
var $ = cheerio.load(
|
||||
'<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>'
|
||||
);
|
||||
@@ -281,7 +287,7 @@ describe('deprecated APIs', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('() : should return the rendered text content of the root', function() {
|
||||
it('() : should return the rendered text content of the root', function () {
|
||||
var $ = cheerio.load(
|
||||
'<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>'
|
||||
);
|
||||
@@ -290,19 +296,19 @@ describe('deprecated APIs', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('(cheerio object) : should omit script tags', function() {
|
||||
it('(cheerio object) : should omit script tags', function () {
|
||||
var $ = cheerio.load('<script>console.log("test")</script>');
|
||||
expect($.text()).to.equal('');
|
||||
});
|
||||
|
||||
it('(cheerio object) : should omit style tags', function() {
|
||||
it('(cheerio object) : should omit style tags', function () {
|
||||
var $ = cheerio.load(
|
||||
'<style type="text/css">.cf-hidden { display: none; } .cf-invisible { visibility: hidden; }</style>'
|
||||
);
|
||||
expect($.text()).to.equal('');
|
||||
});
|
||||
|
||||
it('(cheerio object) : should include text contents of children omiting style and script tags', function() {
|
||||
it('(cheerio object) : should include text contents of children omiting style and script tags', function () {
|
||||
var $ = cheerio.load(
|
||||
'<body>Welcome <div>Hello, testing text function,<script>console.log("hello")</script></div><style type="text/css">.cf-hidden { display: none; }</style>End of messege</body>'
|
||||
);
|
||||
|
||||
@@ -2,161 +2,161 @@ var expect = require('expect.js'),
|
||||
cheerio = require('../..'),
|
||||
forms = require('../fixtures').forms;
|
||||
|
||||
describe('$(...)', function() {
|
||||
describe('$(...)', function () {
|
||||
var $;
|
||||
|
||||
beforeEach(function() {
|
||||
beforeEach(function () {
|
||||
$ = cheerio.load(forms);
|
||||
});
|
||||
|
||||
describe('.serializeArray', function() {
|
||||
it('() : should get form controls', function() {
|
||||
describe('.serializeArray', function () {
|
||||
it('() : should get form controls', function () {
|
||||
expect($('form#simple').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
}
|
||||
value: 'Apple',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should get nested form controls', function() {
|
||||
it('() : should get nested form controls', function () {
|
||||
expect($('form#nested').serializeArray()).to.have.length(2);
|
||||
var data = $('form#nested').serializeArray();
|
||||
data.sort(function(a, b) {
|
||||
data.sort(function (a, b) {
|
||||
return a.value - b.value;
|
||||
});
|
||||
expect(data).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
value: 'Apple',
|
||||
},
|
||||
{
|
||||
name: 'vegetable',
|
||||
value: 'Carrot'
|
||||
}
|
||||
value: 'Carrot',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should not get disabled form controls', function() {
|
||||
it('() : should not get disabled form controls', function () {
|
||||
expect($('form#disabled').serializeArray()).to.eql([]);
|
||||
});
|
||||
|
||||
it('() : should not get form controls with the wrong type', function() {
|
||||
it('() : should not get form controls with the wrong type', function () {
|
||||
expect($('form#submit').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
}
|
||||
value: 'Apple',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should get selected options', function() {
|
||||
it('() : should get selected options', function () {
|
||||
expect($('form#select').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Orange'
|
||||
}
|
||||
value: 'Orange',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should not get unnamed form controls', function() {
|
||||
it('() : should not get unnamed form controls', function () {
|
||||
expect($('form#unnamed').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
}
|
||||
value: 'Apple',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should get multiple selected options', function() {
|
||||
it('() : should get multiple selected options', function () {
|
||||
expect($('form#multiple').serializeArray()).to.have.length(2);
|
||||
var data = $('form#multiple').serializeArray();
|
||||
data.sort(function(a, b) {
|
||||
data.sort(function (a, b) {
|
||||
return a.value - b.value;
|
||||
});
|
||||
expect(data).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
value: 'Apple',
|
||||
},
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Orange'
|
||||
}
|
||||
value: 'Orange',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should get individually selected elements', function() {
|
||||
it('() : should get individually selected elements', function () {
|
||||
var data = $('form#nested input').serializeArray();
|
||||
data.sort(function(a, b) {
|
||||
data.sort(function (a, b) {
|
||||
return a.value - b.value;
|
||||
});
|
||||
expect(data).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
value: 'Apple',
|
||||
},
|
||||
{
|
||||
name: 'vegetable',
|
||||
value: 'Carrot'
|
||||
}
|
||||
value: 'Carrot',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should standardize line breaks', function() {
|
||||
it('() : should standardize line breaks', function () {
|
||||
expect($('form#textarea').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruits',
|
||||
value: 'Apple\r\nOrange'
|
||||
}
|
||||
value: 'Apple\r\nOrange',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("() : shouldn't serialize the empty string", function() {
|
||||
it("() : shouldn't serialize the empty string", function () {
|
||||
expect($('<input value=pineapple>').serializeArray()).to.eql([]);
|
||||
expect($('<input name="" value=pineapple>').serializeArray()).to.eql([]);
|
||||
expect($('<input name="fruit" value=pineapple>').serializeArray()).to.eql(
|
||||
[
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'pineapple'
|
||||
}
|
||||
value: 'pineapple',
|
||||
},
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it('() : should serialize inputs without value attributes', function() {
|
||||
it('() : should serialize inputs without value attributes', function () {
|
||||
expect($('<input name="fruit">').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: ''
|
||||
}
|
||||
value: '',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.serialize', function() {
|
||||
it('() : should get form controls', function() {
|
||||
describe('.serialize', function () {
|
||||
it('() : should get form controls', function () {
|
||||
expect($('form#simple').serialize()).to.equal('fruit=Apple');
|
||||
});
|
||||
|
||||
it('() : should get nested form controls', function() {
|
||||
it('() : should get nested form controls', function () {
|
||||
expect($('form#nested').serialize()).to.equal(
|
||||
'fruit=Apple&vegetable=Carrot'
|
||||
);
|
||||
});
|
||||
|
||||
it('() : should not get disabled form controls', function() {
|
||||
it('() : should not get disabled form controls', function () {
|
||||
expect($('form#disabled').serialize()).to.equal('');
|
||||
});
|
||||
|
||||
it('() : should get multiple selected options', function() {
|
||||
it('() : should get multiple selected options', function () {
|
||||
expect($('form#multiple').serialize()).to.equal(
|
||||
'fruit=Apple&fruit=Orange'
|
||||
);
|
||||
});
|
||||
|
||||
it("() : should encode spaces as +'s", function() {
|
||||
it("() : should encode spaces as +'s", function () {
|
||||
expect($('form#spaces').serialize()).to.equal('fruit=Blood+orange');
|
||||
});
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
var expect = require('expect.js'),
|
||||
fixtures = require('../fixtures'),
|
||||
cheerio = require('../..');
|
||||
fixtures = require('../fixtures'),
|
||||
cheerio = require('../..');
|
||||
|
||||
describe('cheerio', function() {
|
||||
describe('.html', function() {
|
||||
|
||||
@@ -240,8 +240,8 @@ describe('cheerio', function () {
|
||||
expect(typeof $elem.text()).to.equal('string');
|
||||
});
|
||||
|
||||
describe('.load', function() {
|
||||
it('should generate selections as proper instances', function() {
|
||||
describe('.load', function () {
|
||||
it('should generate selections as proper instances', function () {
|
||||
var $ = cheerio.load(fruits);
|
||||
|
||||
expect($('.apple')).to.be.a($);
|
||||
@@ -256,7 +256,7 @@ describe('cheerio', function () {
|
||||
expect(lis).to.have.length(3);
|
||||
});
|
||||
|
||||
it('should allow loading a pre-parsed DOM', function() {
|
||||
it('should allow loading a pre-parsed DOM', function () {
|
||||
var dom = htmlparser2.parseDOM(food),
|
||||
$ = cheerio.load(dom);
|
||||
|
||||
@@ -291,7 +291,9 @@ describe('cheerio', function () {
|
||||
'<!doctype html><html><head><title>Some test</title></head><body><footer><p>Copyright © 2003-2014</p></footer></body></html>',
|
||||
expectedHtml = '<p>Copyright © 2003-2014</p>',
|
||||
expectedXml = '<p>Copyright © 2003-2014</p>',
|
||||
domNotEncoded = cheerio.load(str, { xml: { decodeEntities: false } }),
|
||||
domNotEncoded = cheerio.load(str, {
|
||||
xml: { decodeEntities: false },
|
||||
}),
|
||||
domEncoded = cheerio.load(str);
|
||||
|
||||
expect(domNotEncoded('footer').html()).to.be(expectedHtml);
|
||||
|
||||
@@ -4,14 +4,14 @@ exports.fruits = [
|
||||
'<li class="apple">Apple</li>',
|
||||
'<li class="orange">Orange</li>',
|
||||
'<li class="pear">Pear</li>',
|
||||
'</ul>'
|
||||
'</ul>',
|
||||
].join('');
|
||||
|
||||
exports.vegetables = [
|
||||
'<ul id="vegetables">',
|
||||
'<li class="carrot">Carrot</li>',
|
||||
'<li class="sweetcorn">Sweetcorn</li>',
|
||||
'</ul>'
|
||||
'</ul>',
|
||||
].join('');
|
||||
|
||||
exports.chocolates = [
|
||||
@@ -19,7 +19,7 @@ exports.chocolates = [
|
||||
'<li class="linth" data-highlight="Lindor" data-origin="swiss">Linth</li>',
|
||||
'<li class="frey" data-taste="sweet" data-best-collection="Mahony">Frey</li>',
|
||||
'<li class="cailler">Cailler</li>',
|
||||
'</ul>'
|
||||
'</ul>',
|
||||
].join('');
|
||||
|
||||
exports.drinks = [
|
||||
@@ -29,14 +29,14 @@ exports.drinks = [
|
||||
'<li class="milk">Milk</li>',
|
||||
'<li class="water">Water</li>',
|
||||
'<li class="cider">Cider</li>',
|
||||
'</ul>'
|
||||
'</ul>',
|
||||
].join('');
|
||||
|
||||
exports.food = [
|
||||
'<ul id="food">',
|
||||
exports.fruits,
|
||||
exports.vegetables,
|
||||
'</ul>'
|
||||
'</ul>',
|
||||
].join('');
|
||||
|
||||
exports.inputs = [
|
||||
@@ -51,12 +51,12 @@ exports.inputs = [
|
||||
'<input type="radio" value="off" name="radio[brackets]" /><input type="radio" name="radio[brackets]" value="on" checked />',
|
||||
'<input type="radio" name="radio_valueless" />',
|
||||
'<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="multi-valueless" multiple><option>1</option><option selected>2</option><option selected>3</option><option>4</option></select>'
|
||||
'<select id="multi-valueless" multiple><option>1</option><option selected>2</option><option selected>3</option><option>4</option></select>',
|
||||
].join('');
|
||||
|
||||
exports.text = [
|
||||
'<p>Apples, <b>oranges</b> and pears.</p>',
|
||||
'<p>Carrots and <!-- sweetcorn --></p>'
|
||||
'<p>Carrots and <!-- sweetcorn --></p>',
|
||||
].join('');
|
||||
|
||||
exports.forms = [
|
||||
@@ -68,5 +68,5 @@ exports.forms = [
|
||||
'<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>',
|
||||
'<form id="spaces"><input type="text" name="fruit" value="Blood orange" /></form>'
|
||||
'<form id="spaces"><input type="text" name="fruit" value="Blood orange" /></form>',
|
||||
].join('');
|
||||
|
||||
@@ -38,16 +38,16 @@ var styleEmpty = '<style></style>';
|
||||
// Directives
|
||||
var directive = '<!doctype html>';
|
||||
|
||||
describe('parse', function() {
|
||||
describe('.eval', function() {
|
||||
it('should parse basic empty tags: ' + basic, function() {
|
||||
describe('parse', function () {
|
||||
describe('.eval', function () {
|
||||
it('should parse basic empty tags: ' + basic, function () {
|
||||
var tag = parse.evaluate(basic, defaultOpts, true)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.tagName).to.equal('html');
|
||||
expect(tag.childNodes).to.have.length(2);
|
||||
});
|
||||
|
||||
it('should handle sibling tags: ' + siblings, function() {
|
||||
it('should handle sibling tags: ' + siblings, function () {
|
||||
var dom = parse.evaluate(siblings, defaultOpts, false),
|
||||
h2 = dom[0],
|
||||
p = dom[1];
|
||||
@@ -57,21 +57,21 @@ describe('parse', function() {
|
||||
expect(p.tagName).to.equal('p');
|
||||
});
|
||||
|
||||
it('should handle single tags: ' + single, function() {
|
||||
it('should handle single tags: ' + single, function () {
|
||||
var tag = parse.evaluate(single, defaultOpts, false)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.tagName).to.equal('br');
|
||||
expect(tag.childNodes).to.be.empty();
|
||||
});
|
||||
|
||||
it('should handle malformatted single tags: ' + singleWrong, function() {
|
||||
it('should handle malformatted single tags: ' + singleWrong, function () {
|
||||
var tag = parse.evaluate(singleWrong, defaultOpts, false)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.tagName).to.equal('br');
|
||||
expect(tag.childNodes).to.be.empty();
|
||||
});
|
||||
|
||||
it('should handle tags with children: ' + children, function() {
|
||||
it('should handle tags with children: ' + children, function () {
|
||||
var tag = parse.evaluate(children, defaultOpts, true)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.tagName).to.equal('html');
|
||||
@@ -80,33 +80,33 @@ describe('parse', function() {
|
||||
expect(tag.childNodes[1].childNodes).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should handle tags with children: ' + li, function() {
|
||||
it('should handle tags with children: ' + li, function () {
|
||||
var tag = parse.evaluate(li, defaultOpts, false)[0];
|
||||
expect(tag.childNodes).to.have.length(1);
|
||||
expect(tag.childNodes[0].data).to.equal('Durian');
|
||||
});
|
||||
|
||||
it('should handle tags with attributes: ' + attributes, function() {
|
||||
it('should handle tags with attributes: ' + attributes, function () {
|
||||
var attrs = parse.evaluate(attributes, defaultOpts, false)[0].attribs;
|
||||
expect(attrs).to.be.ok();
|
||||
expect(attrs.src).to.equal('hello.png');
|
||||
expect(attrs.alt).to.equal('man waving');
|
||||
});
|
||||
|
||||
it('should handle value-less attributes: ' + noValueAttribute, function() {
|
||||
it('should handle value-less attributes: ' + noValueAttribute, function () {
|
||||
var attrs = parse.evaluate(noValueAttribute, defaultOpts, false)[0]
|
||||
.attribs;
|
||||
expect(attrs).to.be.ok();
|
||||
expect(attrs.disabled).to.equal('');
|
||||
});
|
||||
|
||||
it('should handle comments: ' + comment, function() {
|
||||
it('should handle comments: ' + comment, function () {
|
||||
var elem = parse.evaluate(comment, defaultOpts, false)[0];
|
||||
expect(elem.type).to.equal('comment');
|
||||
expect(elem.data).to.equal(' sexy ');
|
||||
});
|
||||
|
||||
it('should handle conditional comments: ' + conditional, function() {
|
||||
it('should handle conditional comments: ' + conditional, function () {
|
||||
var elem = parse.evaluate(conditional, defaultOpts, false)[0];
|
||||
expect(elem.type).to.equal('comment');
|
||||
expect(elem.data).to.equal(
|
||||
@@ -114,13 +114,13 @@ describe('parse', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle text: ' + text, function() {
|
||||
it('should handle text: ' + text, function () {
|
||||
var text_ = parse.evaluate(text, defaultOpts, false)[0];
|
||||
expect(text_.type).to.equal('text');
|
||||
expect(text_.data).to.equal('lorem ipsum');
|
||||
});
|
||||
|
||||
it('should handle script tags: ' + script, function() {
|
||||
it('should handle script tags: ' + script, function () {
|
||||
var script_ = parse.evaluate(script, defaultOpts, false)[0];
|
||||
expect(script_.type).to.equal('script');
|
||||
expect(script_.tagName).to.equal('script');
|
||||
@@ -130,7 +130,7 @@ describe('parse', function() {
|
||||
expect(script_.childNodes[0].data).to.equal('alert("hi world!");');
|
||||
});
|
||||
|
||||
it('should handle style tags: ' + style, function() {
|
||||
it('should handle style tags: ' + style, function () {
|
||||
var style_ = parse.evaluate(style, defaultOpts, false)[0];
|
||||
expect(style_.type).to.equal('style');
|
||||
expect(style_.tagName).to.equal('style');
|
||||
@@ -140,7 +140,7 @@ describe('parse', function() {
|
||||
expect(style_.childNodes[0].data).to.equal(' h2 { color:blue; } ');
|
||||
});
|
||||
|
||||
it('should handle directives: ' + directive, function() {
|
||||
it('should handle directives: ' + directive, function () {
|
||||
var elem = parse.evaluate(directive, defaultOpts, true)[0];
|
||||
expect(elem.type).to.equal('directive');
|
||||
expect(elem.data).to.equal('!DOCTYPE html ""');
|
||||
@@ -148,7 +148,7 @@ describe('parse', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.parse', function() {
|
||||
describe('.parse', function () {
|
||||
// root test utility
|
||||
function rootTest(root) {
|
||||
expect(root.tagName).to.equal('root');
|
||||
@@ -162,14 +162,14 @@ describe('parse', function() {
|
||||
expect(child.parentNode).to.be(null);
|
||||
}
|
||||
|
||||
it('should add root to: ' + basic, function() {
|
||||
it('should add root to: ' + basic, function () {
|
||||
var root = parse(basic, defaultOpts, true);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].tagName).to.equal('html');
|
||||
});
|
||||
|
||||
it('should add root to: ' + siblings, function() {
|
||||
it('should add root to: ' + siblings, function () {
|
||||
var root = parse(siblings, defaultOpts, false);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(2);
|
||||
@@ -178,42 +178,42 @@ describe('parse', function() {
|
||||
expect(root.childNodes[1].parent).to.equal(null);
|
||||
});
|
||||
|
||||
it('should add root to: ' + comment, function() {
|
||||
it('should add root to: ' + comment, function () {
|
||||
var root = parse(comment, defaultOpts, false);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('comment');
|
||||
});
|
||||
|
||||
it('should add root to: ' + text, function() {
|
||||
it('should add root to: ' + text, function () {
|
||||
var root = parse(text, defaultOpts, false);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('text');
|
||||
});
|
||||
|
||||
it('should add root to: ' + scriptEmpty, function() {
|
||||
it('should add root to: ' + scriptEmpty, function () {
|
||||
var root = parse(scriptEmpty, defaultOpts, false);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('script');
|
||||
});
|
||||
|
||||
it('should add root to: ' + styleEmpty, function() {
|
||||
it('should add root to: ' + styleEmpty, function () {
|
||||
var root = parse(styleEmpty, defaultOpts, false);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('style');
|
||||
});
|
||||
|
||||
it('should add root to: ' + directive, function() {
|
||||
it('should add root to: ' + directive, function () {
|
||||
var root = parse(directive, defaultOpts, true);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(2);
|
||||
expect(root.childNodes[0].type).to.equal('directive');
|
||||
});
|
||||
|
||||
it('should expose the DOM level 1 API', function() {
|
||||
it('should expose the DOM level 1 API', function () {
|
||||
var root = parse(
|
||||
'<div><a></a><span></span><p></p></div>',
|
||||
defaultOpts,
|
||||
@@ -252,7 +252,7 @@ describe('parse', function() {
|
||||
expect(childNodes[2].lastChild).to.be(null);
|
||||
});
|
||||
|
||||
it('Should parse less than or equal sign sign', function() {
|
||||
it('Should parse less than or equal sign sign', function () {
|
||||
var root = parse('<i>A</i><=<i>B</i>', defaultOpts, false);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
@@ -263,7 +263,7 @@ describe('parse', function() {
|
||||
expect(childNodes[2].childNodes[0].data).to.be('B');
|
||||
});
|
||||
|
||||
it('Should ignore unclosed CDATA', function() {
|
||||
it('Should ignore unclosed CDATA', function () {
|
||||
var root = parse(
|
||||
'<a></a><script>foo //<![CDATA[ bar</script><b></b>',
|
||||
defaultOpts,
|
||||
@@ -277,7 +277,7 @@ describe('parse', function() {
|
||||
expect(childNodes[2].tagName).to.be('b');
|
||||
});
|
||||
|
||||
it('Should add <head> to documents', function() {
|
||||
it('Should add <head> to documents', function () {
|
||||
var root = parse('<html></html>', defaultOpts, true);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
@@ -285,7 +285,7 @@ describe('parse', function() {
|
||||
expect(childNodes[0].childNodes[0].tagName).to.be('head');
|
||||
});
|
||||
|
||||
it('Should implicitly create <tr> around <td>', function() {
|
||||
it('Should implicitly create <tr> around <td>', function () {
|
||||
var root = parse('<table><td>bar</td></tr></table>', defaultOpts, false);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
@@ -302,7 +302,7 @@ describe('parse', function() {
|
||||
).to.be('bar');
|
||||
});
|
||||
|
||||
it('Should parse custom tag <line>', function() {
|
||||
it('Should parse custom tag <line>', function () {
|
||||
var root = parse('<line>test</line>', defaultOpts, false);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
@@ -311,7 +311,7 @@ describe('parse', function() {
|
||||
expect(childNodes[0].childNodes[0].data).to.be('test');
|
||||
});
|
||||
|
||||
it('Should properly parse misnested table tags', function() {
|
||||
it('Should properly parse misnested table tags', function () {
|
||||
var root = parse(
|
||||
'<tr><td>i1</td></tr><tr><td>i2</td></td></tr><tr><td>i3</td></td></tr>',
|
||||
defaultOpts,
|
||||
@@ -321,14 +321,14 @@ describe('parse', function() {
|
||||
|
||||
expect(childNodes.length).to.be(3);
|
||||
|
||||
childNodes.forEach(function(child, i) {
|
||||
childNodes.forEach(function (child, i) {
|
||||
expect(child.tagName).to.be('tr');
|
||||
expect(child.childNodes[0].tagName).to.be('td');
|
||||
expect(child.childNodes[0].childNodes[0].data).to.be('i' + (i + 1));
|
||||
});
|
||||
});
|
||||
|
||||
it('Should correctly parse data url attributes', function() {
|
||||
it('Should correctly parse data url attributes', function () {
|
||||
var html =
|
||||
'<div style=\'font-family:"butcherman-caps"; src:url(data:font/opentype;base64,AAEA...);\'></div>';
|
||||
var expectedAttr =
|
||||
@@ -339,21 +339,21 @@ describe('parse', function() {
|
||||
expect(childNodes[0].attribs.style).to.be(expectedAttr);
|
||||
});
|
||||
|
||||
it('Should treat <xmp> tag content as text', function() {
|
||||
it('Should treat <xmp> tag content as text', function () {
|
||||
var root = parse('<xmp><h2></xmp>', defaultOpts, false);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
expect(childNodes[0].childNodes[0].data).to.be('<h2>');
|
||||
});
|
||||
|
||||
it('Should correctly parse malformed numbered entities', function() {
|
||||
it('Should correctly parse malformed numbered entities', function () {
|
||||
var root = parse('<p>z&#</p>', defaultOpts, false);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
expect(childNodes[0].childNodes[0].data).to.be('z&#');
|
||||
});
|
||||
|
||||
it('Should correctly parse mismatched headings', function() {
|
||||
it('Should correctly parse mismatched headings', function () {
|
||||
var root = parse('<h2>Test</h3><div></div>', defaultOpts, false);
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
@@ -362,7 +362,7 @@ describe('parse', function() {
|
||||
expect(childNodes[1].tagName).to.be('div');
|
||||
});
|
||||
|
||||
it('Should correctly parse tricky <pre> content', function() {
|
||||
it('Should correctly parse tricky <pre> content', function () {
|
||||
var root = parse(
|
||||
'<pre>\nA <- factor(A, levels = c("c","a","b"))\n</pre>',
|
||||
defaultOpts,
|
||||
@@ -377,7 +377,7 @@ describe('parse', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('should pass the options for including the location info to parse5', function() {
|
||||
it('should pass the options for including the location info to parse5', function () {
|
||||
var root = parse(
|
||||
'<p>Hello</p>',
|
||||
assign({}, defaultOpts, { sourceCodeLocationInfo: true }),
|
||||
|
||||
24
test/xml.js
24
test/xml.js
@@ -1,23 +1,23 @@
|
||||
var expect = require('expect.js'),
|
||||
cheerio = require('..'),
|
||||
_ = {
|
||||
extend: require('lodash/assignIn')
|
||||
extend: require('lodash/assignIn'),
|
||||
};
|
||||
|
||||
var xml = function(str, options) {
|
||||
var xml = function (str, options) {
|
||||
options = _.extend({ xml: true }, options);
|
||||
var $ = cheerio.load(str, options);
|
||||
return $.xml();
|
||||
};
|
||||
|
||||
var dom = function(str, options) {
|
||||
var dom = function (str, options) {
|
||||
var $ = cheerio.load('', options);
|
||||
return $(str).html();
|
||||
};
|
||||
|
||||
describe('render', function() {
|
||||
describe('(xml)', function() {
|
||||
it('should render <media:thumbnail /> tags correctly', function() {
|
||||
describe('render', function () {
|
||||
describe('(xml)', function () {
|
||||
it('should render <media:thumbnail /> tags correctly', function () {
|
||||
var str =
|
||||
'<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time="12:05:01.123" />';
|
||||
expect(xml(str)).to.equal(
|
||||
@@ -25,33 +25,33 @@ describe('render', function() {
|
||||
);
|
||||
});
|
||||
|
||||
it('should render <link /> tags (RSS) correctly', function() {
|
||||
it('should render <link /> tags (RSS) correctly', function () {
|
||||
var str = '<link>http://www.github.com/</link>';
|
||||
expect(xml(str)).to.equal('<link>http://www.github.com/</link>');
|
||||
});
|
||||
|
||||
it('should escape entities', function() {
|
||||
it('should escape entities', function () {
|
||||
var str = '<tag attr="foo & bar"/>';
|
||||
expect(xml(str)).to.equal(str);
|
||||
});
|
||||
});
|
||||
|
||||
describe('(dom)', function() {
|
||||
it('should keep camelCase for new nodes', function() {
|
||||
describe('(dom)', function () {
|
||||
it('should keep camelCase for new nodes', function () {
|
||||
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
|
||||
expect(dom(str, { xml: false })).to.equal(
|
||||
'<someelem someattribute="something">hello</someelem>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should keep camelCase for new nodes', function() {
|
||||
it('should keep camelCase for new nodes', function () {
|
||||
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
|
||||
expect(dom(str, { xml: true })).to.equal(
|
||||
'<someElem someAttribute="something">hello</someElem>'
|
||||
);
|
||||
});
|
||||
|
||||
it('should maintain the parsing options of distinct contexts independently', function() {
|
||||
it('should maintain the parsing options of distinct contexts independently', function () {
|
||||
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
|
||||
var $ = cheerio.load('', { xml: false });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user