first commit
This commit is contained in:
9
build/node_modules/cheerio/test/.jshintrc
generated
vendored
Normal file
9
build/node_modules/cheerio/test/.jshintrc
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../.jshintrc",
|
||||
"globals": {
|
||||
"expect": true,
|
||||
"it": true,
|
||||
"describe": true,
|
||||
"beforeEach": true
|
||||
}
|
||||
}
|
||||
677
build/node_modules/cheerio/test/api/attributes.js
generated
vendored
Normal file
677
build/node_modules/cheerio/test/api/attributes.js
generated
vendored
Normal file
@@ -0,0 +1,677 @@
|
||||
var expect = require('expect.js');
|
||||
|
||||
var cheerio = require('../..');
|
||||
var fruits = require('../fixtures').fruits;
|
||||
var vegetables = require('../fixtures').vegetables;
|
||||
var food = require('../fixtures').food;
|
||||
var chocolates = require('../fixtures').chocolates;
|
||||
var inputs = require('../fixtures').inputs;
|
||||
var toArray = Function.call.bind(Array.prototype.slice);
|
||||
|
||||
describe('$(...)', function() {
|
||||
var $;
|
||||
|
||||
beforeEach(function() {
|
||||
$ = cheerio.load(fruits);
|
||||
});
|
||||
|
||||
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() {
|
||||
var attr = $('.apple').attr('lol');
|
||||
expect(attr).to.be(undefined);
|
||||
});
|
||||
|
||||
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() {
|
||||
var attr = $('<input name=email autofocus>').attr('autofocus');
|
||||
expect(attr).to.equal('autofocus');
|
||||
});
|
||||
|
||||
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() {
|
||||
var $el = cheerio('<div></div> <div></div>').attr('class', 'pear');
|
||||
|
||||
expect($el[0].attribs['class']).to.equal('pear');
|
||||
expect($el[1].attribs).to.equal(undefined);
|
||||
expect($el[2].attribs['class']).to.equal('pear');
|
||||
});
|
||||
|
||||
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() {
|
||||
$('.apple').attr({
|
||||
id: 'apple',
|
||||
style: 'color:red;',
|
||||
'data-url': 'http://apple.com'
|
||||
});
|
||||
var attrs = $('.apple').attr();
|
||||
expect(attrs.id).to.equal('apple');
|
||||
expect(attrs.style).to.equal('color:red;');
|
||||
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() {
|
||||
var $fruits = $('#fruits');
|
||||
$fruits.attr('id', function(index, value) {
|
||||
expect(index).to.equal(0);
|
||||
expect(value).to.equal('fruits');
|
||||
return 'ninja';
|
||||
});
|
||||
var attrs = $fruits.attr();
|
||||
expect(attrs.id).to.equal('ninja');
|
||||
});
|
||||
|
||||
it('(key, value) : should correctly encode then decode unsafe values', function() {
|
||||
var $apple = $('.apple');
|
||||
$apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
|
||||
expect($apple.attr('href')).to.equal('http://github.com/"><script>alert("XSS!")</script><br');
|
||||
|
||||
$apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
|
||||
expect($apple.html()).to.not.contain('<script>alert("XSS!")</script>');
|
||||
});
|
||||
|
||||
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() {
|
||||
var $apple = $('.apple');
|
||||
$apple.attr('autofocus', 'autofocus');
|
||||
expect($apple.attr('autofocus')).to.equal('autofocus');
|
||||
$apple.removeAttr('autofocus');
|
||||
expect($apple.attr('autofocus')).to.be(undefined);
|
||||
});
|
||||
|
||||
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');
|
||||
$apple.removeAttr('data-autofocus');
|
||||
expect($apple.attr('data-autofocus')).to.be(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.data', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
$ = cheerio.load(chocolates);
|
||||
});
|
||||
|
||||
it('() : should get all data attributes initially declared in the markup', function() {
|
||||
var data = $('.linth').data();
|
||||
expect(data).to.eql({
|
||||
highlight: 'Lindor',
|
||||
origin: 'swiss'
|
||||
});
|
||||
});
|
||||
|
||||
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
|
||||
});
|
||||
});
|
||||
|
||||
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'
|
||||
});
|
||||
});
|
||||
|
||||
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() {
|
||||
var data = $('.frey').data('lol');
|
||||
expect(data).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(valid key) : valid data attribute should get value', function() {
|
||||
var highlight = $('.linth').data('highlight');
|
||||
var origin = $('.linth').data('origin');
|
||||
|
||||
expect(highlight).to.equal('Lindor');
|
||||
expect(origin).to.equal('swiss');
|
||||
});
|
||||
|
||||
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">');
|
||||
|
||||
expect($el.data('ThreeWordAttribute')).to.be('a');
|
||||
expect($el.data('fooBar_baz-')).to.be('b');
|
||||
});
|
||||
|
||||
it('(key) : should retrieve object values', function() {
|
||||
var data = {};
|
||||
var $el = cheerio('<div>');
|
||||
|
||||
$el.data('test', data);
|
||||
|
||||
expect($el.data('test')).to.be(data);
|
||||
});
|
||||
|
||||
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() {
|
||||
var $el = cheerio('<div>');
|
||||
$el.data('json', '[1, 2, 3]');
|
||||
|
||||
expect($el.data('json')).to.be('[1, 2, 3]');
|
||||
});
|
||||
|
||||
// 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() {
|
||||
var $el = cheerio('<div data-test="a">');
|
||||
|
||||
expect($el.data('test')).to.be('a');
|
||||
|
||||
$el.attr('data-test', 'b');
|
||||
|
||||
expect($el.data('test')).to.be('a');
|
||||
});
|
||||
|
||||
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() {
|
||||
var data = $('.frey').data();
|
||||
expect(data).to.eql({
|
||||
taste: 'sweet',
|
||||
bestCollection: 'Mahony'
|
||||
});
|
||||
});
|
||||
|
||||
it('(key, value) : should set data attribute', function() {
|
||||
// Adding as object.
|
||||
var a = $('.frey').data({
|
||||
balls: 'giandor'
|
||||
});
|
||||
// Adding as string.
|
||||
var b = $('.linth').data('snack', 'chocoletti');
|
||||
|
||||
expect(a.data('balls')).to.eql('giandor');
|
||||
expect(b.data('snack')).to.eql('chocoletti');
|
||||
});
|
||||
|
||||
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/'
|
||||
})['0'].data;
|
||||
|
||||
expect(data.id).to.equal('Cailler');
|
||||
expect(data.flop).to.equal('Pippilotti Rist');
|
||||
expect(data.top).to.equal('Frigor');
|
||||
expect(data.url).to.equal('http://www.cailler.ch/');
|
||||
});
|
||||
|
||||
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() {
|
||||
var $el = cheerio('<div data-number="23">');
|
||||
expect($el.data('number')).to.be(23);
|
||||
});
|
||||
|
||||
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() {
|
||||
var $el = cheerio('<div data-null="null">');
|
||||
expect($el.data('null')).to.be(null);
|
||||
});
|
||||
|
||||
it('object', function() {
|
||||
var $el = cheerio('<div data-obj=\'{ "a": 45 }\'>');
|
||||
expect($el.data('obj')).to.eql({ a: 45 });
|
||||
});
|
||||
|
||||
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() {
|
||||
$ = cheerio.load(inputs);
|
||||
});
|
||||
|
||||
it('.val(): on select should get value', function() {
|
||||
var val = $('select#one').val();
|
||||
expect(val).to.equal('option_selected');
|
||||
});
|
||||
it('.val(): on option should get value', function() {
|
||||
var val = $('select#one option').eq(0).val();
|
||||
expect(val).to.equal('option_not_selected');
|
||||
});
|
||||
it('.val(): on text input should get value', function() {
|
||||
var val = $('input[type="text"]').val();
|
||||
expect(val).to.equal('input_text');
|
||||
});
|
||||
it('.val(): on checked checkbox should get value', function() {
|
||||
var val = $('input[name="checkbox_on"]').val();
|
||||
expect(val).to.equal('on');
|
||||
});
|
||||
it('.val(): on unchecked checkbox should get value', function() {
|
||||
var val = $('input[name="checkbox_off"]').val();
|
||||
expect(val).to.equal('off');
|
||||
});
|
||||
it('.val(): on radio should get value', function() {
|
||||
var val = $('input[type="radio"]').val();
|
||||
expect(val).to.equal('off');
|
||||
});
|
||||
it('.val(): on multiple select should get an array of values', function() {
|
||||
var val = $('select#multi').val();
|
||||
expect(val).to.have.length(2);
|
||||
});
|
||||
it('.val(value): on input text should set value', function() {
|
||||
var element = $('input[type="text"]').val('test');
|
||||
expect(element.val()).to.equal('test');
|
||||
});
|
||||
it('.val(value): on select should set value', function() {
|
||||
var element = $('select#one').val('option_not_selected');
|
||||
expect(element.val()).to.equal('option_not_selected');
|
||||
});
|
||||
it('.val(value): on option should set value', function() {
|
||||
var element = $('select#one option').eq(0).val('option_changed');
|
||||
expect(element.val()).to.equal('option_changed');
|
||||
});
|
||||
it('.val(value): on radio should set value', function() {
|
||||
var element = $('input[name="radio"]').val('off');
|
||||
expect(element.val()).to.equal('off');
|
||||
});
|
||||
it('.val(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('.val(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() {
|
||||
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() {
|
||||
var obj = $('ul').removeAttr('id');
|
||||
expect(obj).to.be.a($);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.hasClass', function() {
|
||||
function test(attr) {
|
||||
return cheerio('<div class="' + attr + '"></div>');
|
||||
}
|
||||
|
||||
it('(valid class) : should return true', function() {
|
||||
var cls = $('.apple').hasClass('apple');
|
||||
expect(cls).to.be.ok();
|
||||
|
||||
expect(test('foo').hasClass('foo')).to.be.ok();
|
||||
expect(test('foo bar').hasClass('foo')).to.be.ok();
|
||||
expect(test('bar foo').hasClass('foo')).to.be.ok();
|
||||
expect(test('bar foo bar').hasClass('foo')).to.be.ok();
|
||||
});
|
||||
|
||||
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();
|
||||
expect(test('foo-bar').hasClass('foo')).to.not.be.ok();
|
||||
expect(test('foo-bar').hasClass('foo-ba')).to.not.be.ok();
|
||||
});
|
||||
|
||||
it('should check multiple classes', function() {
|
||||
// Add a class
|
||||
$('.apple').addClass('red');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('red')).to.be.ok();
|
||||
|
||||
// Remove one and test again
|
||||
$('.apple').removeClass('apple');
|
||||
expect($('li').eq(0).hasClass('apple')).to.not.be.ok();
|
||||
// expect($('li', $fruits).eq(0).hasClass('red')).to.be.ok();
|
||||
});
|
||||
});
|
||||
|
||||
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() {
|
||||
$('.apple').addClass('fruit');
|
||||
var cls = $('.apple').hasClass('fruit');
|
||||
expect(cls).to.be.ok();
|
||||
});
|
||||
|
||||
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() {
|
||||
$('.apple').addClass('fruit red tasty');
|
||||
expect($('.apple').hasClass('apple')).to.be.ok();
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
expect($('.apple').hasClass('red')).to.be.ok();
|
||||
expect($('.apple').hasClass('tasty')).to.be.ok();
|
||||
});
|
||||
|
||||
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) {
|
||||
args.push(toArray(arguments));
|
||||
thisVals.push(this);
|
||||
return toAdd[idx];
|
||||
});
|
||||
|
||||
expect(args).to.eql([
|
||||
[0, 'apple'],
|
||||
[1, 'orange'],
|
||||
[2, 'pear']
|
||||
]);
|
||||
expect(thisVals).to.eql([
|
||||
$fruits[0],
|
||||
$fruits[1],
|
||||
$fruits[2]
|
||||
]);
|
||||
expect($fruits.eq(0).hasClass('apple')).to.be.ok();
|
||||
expect($fruits.eq(0).hasClass('red')).to.be.ok();
|
||||
expect($fruits.eq(1).hasClass('orange')).to.be.ok();
|
||||
expect($fruits.eq(2).hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
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() {
|
||||
var $fruits = $('#fruits');
|
||||
$fruits.children().removeClass('');
|
||||
expect($('.apple')).to.have.length(1);
|
||||
});
|
||||
|
||||
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() {
|
||||
var $vegetables = cheerio(vegetables);
|
||||
|
||||
expect(function() {
|
||||
$('li', $vegetables).removeClass('vegetable');
|
||||
})
|
||||
.to.not.throwException();
|
||||
});
|
||||
|
||||
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');
|
||||
expect($('.pear').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.pear').hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
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();
|
||||
expect($('.pear').hasClass('tasty')).to.be.ok();
|
||||
|
||||
$('.pear').removeClass('green');
|
||||
expect($('.pear').hasClass('fruit')).to.be.ok();
|
||||
expect($('.pear').hasClass('green')).to.not.be.ok();
|
||||
expect($('.pear').hasClass('tasty')).to.be.ok();
|
||||
});
|
||||
|
||||
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();
|
||||
expect($('.apple').hasClass('red')).to.be.ok();
|
||||
expect($('.apple').hasClass('tasty')).to.be.ok();
|
||||
|
||||
$('.apple').removeClass('apple red tasty');
|
||||
expect($('.fruit').hasClass('apple')).to.not.be.ok();
|
||||
expect($('.fruit').hasClass('red')).to.not.be.ok();
|
||||
expect($('.fruit').hasClass('tasty')).to.not.be.ok();
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
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() {
|
||||
var $fruits = $('#fruits').children();
|
||||
var args = [];
|
||||
var thisVals = [];
|
||||
var toAdd = ['apple red', '', undefined];
|
||||
|
||||
$fruits.removeClass(function(idx) {
|
||||
args.push(toArray(arguments));
|
||||
thisVals.push(this);
|
||||
return toAdd[idx];
|
||||
});
|
||||
|
||||
expect(args).to.eql([
|
||||
[0, 'apple'],
|
||||
[1, 'orange'],
|
||||
[2, 'pear']
|
||||
]);
|
||||
expect(thisVals).to.eql([
|
||||
$fruits[0],
|
||||
$fruits[1],
|
||||
$fruits[2]
|
||||
]);
|
||||
expect($fruits.eq(0).hasClass('apple')).to.not.be.ok();
|
||||
expect($fruits.eq(0).hasClass('red')).to.not.be.ok();
|
||||
expect($fruits.eq(1).hasClass('orange')).to.be.ok();
|
||||
expect($fruits.eq(2).hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
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();
|
||||
expect($('.apple').hasClass('red')).to.not.be.ok();
|
||||
|
||||
$('.apple').toggleClass('apple red');
|
||||
expect($('.fruit').hasClass('apple')).to.not.be.ok();
|
||||
expect($('.fruit').hasClass('red')).to.be.ok();
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
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();
|
||||
expect($('.apple').hasClass('red')).to.not.be.ok();
|
||||
|
||||
$('.apple').toggleClass('apple red', true);
|
||||
expect($('.fruit').hasClass('apple')).to.be.ok();
|
||||
expect($('.fruit').hasClass('red')).to.be.ok();
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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();
|
||||
expect($('.apple').hasClass('red')).to.not.be.ok();
|
||||
|
||||
$('.apple').toggleClass('apple red', false);
|
||||
expect($('.fruit').hasClass('apple')).to.not.be.ok();
|
||||
expect($('.fruit').hasClass('red')).to.not.be.ok();
|
||||
expect($('.fruit').hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should toggle classes returned from the function', function() {
|
||||
$ = cheerio.load(food);
|
||||
|
||||
$('.apple').addClass('fruit');
|
||||
$('.carrot').addClass('vegetable');
|
||||
expect($('.apple').hasClass('fruit')).to.be.ok();
|
||||
expect($('.apple').hasClass('vegetable')).to.not.be.ok();
|
||||
expect($('.orange').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.orange').hasClass('vegetable')).to.not.be.ok();
|
||||
expect($('.carrot').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.carrot').hasClass('vegetable')).to.be.ok();
|
||||
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';
|
||||
});
|
||||
expect($('.apple').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.apple').hasClass('vegetable')).to.not.be.ok();
|
||||
expect($('.orange').hasClass('fruit')).to.be.ok();
|
||||
expect($('.orange').hasClass('vegetable')).to.not.be.ok();
|
||||
expect($('.carrot').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.carrot').hasClass('vegetable')).to.not.be.ok();
|
||||
expect($('.sweetcorn').hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.sweetcorn').hasClass('vegetable')).to.be.ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.is', function () {
|
||||
it('() : should return false', function() {
|
||||
expect($('li.apple').is()).to.be(false);
|
||||
});
|
||||
|
||||
it('(true selector) : should return true', function() {
|
||||
expect(cheerio('#vegetables', vegetables).is('ul')).to.be(true);
|
||||
});
|
||||
|
||||
it('(false selector) : should return false', function() {
|
||||
expect(cheerio('#vegetables', vegetables).is('div')).to.be(false);
|
||||
});
|
||||
|
||||
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() {
|
||||
var $vegetableList = cheerio(vegetables);
|
||||
var $vegetables = $vegetableList.find('li');
|
||||
expect($vegetables.is($vegetableList)).to.be(false);
|
||||
});
|
||||
|
||||
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() {
|
||||
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() {
|
||||
return this.tagName === 'li' && $(this).hasClass('pear');
|
||||
});
|
||||
expect(result).to.be(true);
|
||||
});
|
||||
|
||||
it('(false predicate) : should return false', function () {
|
||||
var result = $('li').last().is(function() {
|
||||
return this.tagName === 'ul';
|
||||
});
|
||||
expect(result).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
88
build/node_modules/cheerio/test/api/css.js
generated
vendored
Normal file
88
build/node_modules/cheerio/test/api/css.js
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
var expect = require('expect.js');
|
||||
var cheerio = require('../..');
|
||||
|
||||
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() {
|
||||
var el = cheerio('<li style="margin: 1px; padding: 2px; color: blue;">');
|
||||
expect(el.css(['margin', 'color'])).to.eql({ margin: '1px', color: 'blue' });
|
||||
});
|
||||
|
||||
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() {
|
||||
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() {
|
||||
var el = cheerio('<li style="background-image:url(http://example.com/img.png);">');
|
||||
expect(el.css('background-image')).to.equal('url(http://example.com/img.png)');
|
||||
});
|
||||
|
||||
it('(prop): should ignore blank properties', function() {
|
||||
var el = cheerio('<li style=":#ccc;color:#aaa;">');
|
||||
expect(el.css()).to.eql({color:'#aaa'});
|
||||
});
|
||||
|
||||
it('(prop): should ignore blank values', function() {
|
||||
var el = cheerio('<li style="color:;position:absolute;">');
|
||||
expect(el.css()).to.eql({position:'absolute'});
|
||||
});
|
||||
|
||||
describe('(prop, function):', function() {
|
||||
beforeEach(function() {
|
||||
this.$el = cheerio('<div style="margin: 0px;"></div><div style="margin: 1px;"></div><div style="margin: 2px;">');
|
||||
});
|
||||
|
||||
it('should iterate over the selection', function() {
|
||||
var count = 0;
|
||||
var $el = this.$el;
|
||||
this.$el.css('margin', function(idx, value) {
|
||||
expect(idx).to.equal(count);
|
||||
expect(value).to.equal(count + 'px');
|
||||
expect(this).to.equal($el[count]);
|
||||
count++;
|
||||
});
|
||||
expect(count).to.equal(3);
|
||||
});
|
||||
|
||||
it('should set each attribute independently', function() {
|
||||
var values = ['4px', '', undefined];
|
||||
this.$el.css('margin', function(idx) {
|
||||
return values[idx];
|
||||
});
|
||||
expect(this.$el.eq(0).attr('style')).to.equal('margin: 4px;');
|
||||
expect(this.$el.eq(1).attr('style')).to.equal('');
|
||||
expect(this.$el.eq(2).attr('style')).to.equal('margin: 2px;');
|
||||
});
|
||||
});
|
||||
|
||||
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() {
|
||||
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 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
120
build/node_modules/cheerio/test/api/forms.js
generated
vendored
Normal file
120
build/node_modules/cheerio/test/api/forms.js
generated
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
var expect = require('expect.js'),
|
||||
cheerio = require('../..'),
|
||||
forms = require('../fixtures').forms;
|
||||
|
||||
describe('$(...)', function() {
|
||||
|
||||
var $;
|
||||
|
||||
beforeEach(function() {
|
||||
$ = cheerio.load(forms);
|
||||
});
|
||||
|
||||
describe('.serializeArray', function() {
|
||||
|
||||
it('() : should get form controls', function() {
|
||||
expect($('form#simple').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
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) {
|
||||
return a.value - b.value;
|
||||
});
|
||||
expect(data).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
},
|
||||
{
|
||||
name: 'vegetable',
|
||||
value: 'Carrot'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
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() {
|
||||
expect($('form#submit').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should get selected options', function() {
|
||||
expect($('form#select').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Orange'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should not get unnamed form controls', function() {
|
||||
expect($('form#unnamed').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
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) {
|
||||
return a.value - b.value;
|
||||
});
|
||||
expect(data).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
},
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Orange'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it('() : should get individually selected elements', function() {
|
||||
var data = $('form#nested input').serializeArray();
|
||||
data.sort(function (a, b) {
|
||||
return a.value - b.value;
|
||||
});
|
||||
expect(data).to.eql([
|
||||
{
|
||||
name: 'fruit',
|
||||
value: 'Apple'
|
||||
},
|
||||
{
|
||||
name: 'vegetable',
|
||||
value: 'Carrot'
|
||||
}
|
||||
]);
|
||||
|
||||
});
|
||||
|
||||
it('() : should standardize line breaks', function() {
|
||||
expect($('form#textarea').serializeArray()).to.eql([
|
||||
{
|
||||
name: 'fruits',
|
||||
value: 'Apple\r\nOrange'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
1265
build/node_modules/cheerio/test/api/manipulation.js
generated
vendored
Normal file
1265
build/node_modules/cheerio/test/api/manipulation.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1408
build/node_modules/cheerio/test/api/traversing.js
generated
vendored
Normal file
1408
build/node_modules/cheerio/test/api/traversing.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
211
build/node_modules/cheerio/test/api/utils.js
generated
vendored
Normal file
211
build/node_modules/cheerio/test/api/utils.js
generated
vendored
Normal file
@@ -0,0 +1,211 @@
|
||||
var expect = require('expect.js'),
|
||||
fixtures = require('../fixtures'),
|
||||
cheerio = require('../..');
|
||||
|
||||
describe('cheerio', function() {
|
||||
|
||||
describe('.html', function() {
|
||||
|
||||
it('() : should return innerHTML; $.html(obj) should return outerHTML', function() {
|
||||
var $div = cheerio('div', '<div><span>foo</span><span>bar</span></div>');
|
||||
var span = $div.children()[1];
|
||||
expect(cheerio(span).html()).to.equal('bar');
|
||||
expect(cheerio.html(span)).to.equal('<span>bar</span>');
|
||||
});
|
||||
|
||||
it('(<obj>) : should accept an object, an array, or a cheerio object', function() {
|
||||
var $span = cheerio('<span>foo</span>');
|
||||
expect(cheerio.html($span[0])).to.equal('<span>foo</span>');
|
||||
expect(cheerio.html($span)).to.equal('<span>foo</span>');
|
||||
});
|
||||
|
||||
it('(<value>) : should be able to set to an empty string', function() {
|
||||
var $elem = cheerio('<span>foo</span>').html('');
|
||||
expect(cheerio.html($elem)).to.equal('<span></span>');
|
||||
});
|
||||
|
||||
it('() : of empty cheerio object should return null', function() {
|
||||
expect(cheerio().html()).to.be(null);
|
||||
});
|
||||
|
||||
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>');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
describe('.load', function() {
|
||||
|
||||
it('(html) : should retain original root after creating a new node', function() {
|
||||
var $html = cheerio.load('<body><ul id="fruits"></ul></body>');
|
||||
expect($html('body')).to.have.length(1);
|
||||
$html('<script>');
|
||||
expect($html('body')).to.have.length(1);
|
||||
});
|
||||
|
||||
it('(html) : should handle lowercase tag options', function() {
|
||||
var $html = cheerio.load('<BODY><ul id="fruits"></ul></BODY>', { lowerCaseTags : true });
|
||||
expect($html.html()).to.be('<body><ul id="fruits"></ul></body>');
|
||||
});
|
||||
|
||||
it('(html) : should handle the `normalizeWhitepace` option', function() {
|
||||
var $html = cheerio.load('<body><b>foo</b> <b>bar</b></body>', { normalizeWhitespace : true });
|
||||
expect($html.html()).to.be('<body><b>foo</b> <b>bar</b></body>');
|
||||
});
|
||||
|
||||
// TODO:
|
||||
// it('(html) : should handle xml tag option', function() {
|
||||
// var $html = $.load('<body><script>oh hai</script></body>', { xmlMode : true });
|
||||
// console.log($html('script')[0].type);
|
||||
// expect($html('script')[0].type).to.be('tag');
|
||||
// });
|
||||
|
||||
it('(buffer) : should accept a buffer', function() {
|
||||
var $html = cheerio.load(new Buffer('<div>foo</div>'));
|
||||
expect($html.html()).to.be('<div>foo</div>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('.clone', function() {
|
||||
|
||||
it('() : should return a copy', function() {
|
||||
var $src = cheerio('<div><span>foo</span><span>bar</span><span>baz</span></div>').children();
|
||||
var $elem = $src.clone();
|
||||
expect($elem.length).to.equal(3);
|
||||
expect($elem.parent()).to.have.length(0);
|
||||
expect($elem.text()).to.equal($src.text());
|
||||
$src.text('rofl');
|
||||
expect($elem.text()).to.not.equal($src.text());
|
||||
});
|
||||
|
||||
it('() : should preserve parsing options', function() {
|
||||
var $ = cheerio.load('<div>π</div>', { decodeEntities: false });
|
||||
var $div = $('div');
|
||||
|
||||
expect($div.text()).to.equal($div.clone().text());
|
||||
});
|
||||
});
|
||||
|
||||
describe('.parseHTML', function() {
|
||||
|
||||
it('() : returns null', function() {
|
||||
expect(cheerio.parseHTML()).to.equal(null);
|
||||
});
|
||||
|
||||
it('(null) : returns null', function() {
|
||||
expect(cheerio.parseHTML(null)).to.equal(null);
|
||||
});
|
||||
|
||||
it('("") : returns null', function() {
|
||||
expect(cheerio.parseHTML('')).to.equal(null);
|
||||
});
|
||||
|
||||
it('(largeHtmlString) : parses large HTML strings', function() {
|
||||
var html = new Array(10).join('<div></div>');
|
||||
var nodes = cheerio.parseHTML(html);
|
||||
|
||||
expect(nodes.length).to.be.greaterThan(4);
|
||||
expect(nodes).to.be.an('array');
|
||||
});
|
||||
|
||||
it('("<script>") : ignores scripts by default', function() {
|
||||
var html = '<script>undefined()</script>';
|
||||
expect(cheerio.parseHTML(html)).to.have.length(0);
|
||||
});
|
||||
|
||||
it('("<script>", true) : preserves scripts when requested', function() {
|
||||
var html = '<script>undefined()</script>';
|
||||
expect(cheerio.parseHTML(html, true)[0].tagName).to.match(/script/i);
|
||||
});
|
||||
|
||||
it('("scriptAndNonScript) : preserves non-script nodes', function() {
|
||||
var html = '<script>undefined()</script><div></div>';
|
||||
expect(cheerio.parseHTML(html)[0].tagName).to.match(/div/i);
|
||||
});
|
||||
|
||||
it('(scriptAndNonScript, true) : Preserves script position', function() {
|
||||
var html = '<script>undefined()</script><div></div>';
|
||||
expect(cheerio.parseHTML(html, true)[0].tagName).to.match(/script/i);
|
||||
});
|
||||
|
||||
it('(text) : returns a text node', function() {
|
||||
expect(cheerio.parseHTML('text')[0].type).to.be('text');
|
||||
});
|
||||
|
||||
it('(\\ttext) : preserves leading whitespace', function() {
|
||||
expect(cheerio.parseHTML('\t<div></div>')[0].data).to.equal('\t');
|
||||
});
|
||||
|
||||
it('( text) : Leading spaces are treated as text nodes', function() {
|
||||
expect(cheerio.parseHTML(' <div/> ')[0].type).to.be('text');
|
||||
});
|
||||
|
||||
it('(html) : should preserve content', function() {
|
||||
var html = '<div>test div</div>';
|
||||
expect(cheerio(cheerio.parseHTML(html)[0]).html()).to.equal('test div');
|
||||
});
|
||||
|
||||
it('(malformedHtml) : should not break', function() {
|
||||
expect(cheerio.parseHTML('<span><span>')).to.have.length(1);
|
||||
});
|
||||
|
||||
it('(garbageInput) : should not cause an error', function() {
|
||||
expect(cheerio.parseHTML('<#if><tr><p>This is a test.</p></tr><#/if>') || true).to.be.ok();
|
||||
});
|
||||
|
||||
it('(text) : should return an array that is not effected by DOM manipulation methods', function() {
|
||||
var $ = cheerio.load('<div>');
|
||||
var elems = $.parseHTML('<b></b><i></i>');
|
||||
|
||||
$('div').append(elems);
|
||||
|
||||
expect(elems).to.have.length(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.contains', function() {
|
||||
|
||||
var $;
|
||||
|
||||
beforeEach(function() {
|
||||
$ = cheerio.load(fixtures.food);
|
||||
});
|
||||
|
||||
it('(container, contained) : should correctly detect the provided element', function() {
|
||||
var $food = $('#food');
|
||||
var $fruits = $('#fruits');
|
||||
var $apple = $('.apple');
|
||||
|
||||
expect($.contains($food[0], $fruits[0])).to.equal(true);
|
||||
expect($.contains($food[0], $apple[0])).to.equal(true);
|
||||
});
|
||||
|
||||
it('(container, other) : should not detect elements that are not contained', function() {
|
||||
var $fruits = $('#fruits');
|
||||
var $vegetables = $('#vegetables');
|
||||
var $apple = $('.apple');
|
||||
|
||||
expect($.contains($vegetables[0], $apple[0])).to.equal(false);
|
||||
expect($.contains($fruits[0], $vegetables[0])).to.equal(false);
|
||||
expect($.contains($vegetables[0], $fruits[0])).to.equal(false);
|
||||
expect($.contains($fruits[0], $fruits[0])).to.equal(false);
|
||||
expect($.contains($vegetables[0], $vegetables[0])).to.equal(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.root', function() {
|
||||
|
||||
it('() : should return a cheerio-wrapped root object', function() {
|
||||
var $html = cheerio.load('<div><span>foo</span><span>bar</span></div>');
|
||||
$html.root().append('<div id="test"></div>');
|
||||
expect($html.html()).to.equal('<div><span>foo</span><span>bar</span></div><div id="test"></div>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
340
build/node_modules/cheerio/test/cheerio.js
generated
vendored
Normal file
340
build/node_modules/cheerio/test/cheerio.js
generated
vendored
Normal file
@@ -0,0 +1,340 @@
|
||||
var expect = require('expect.js'),
|
||||
_ = require('lodash'),
|
||||
htmlparser2 = require('htmlparser2'),
|
||||
$ = require('../'),
|
||||
fixtures = require('./fixtures'),
|
||||
fruits = fixtures.fruits,
|
||||
food = fixtures.food;
|
||||
|
||||
// HTML
|
||||
var script = '<script src="script.js" type="text/javascript"></script>',
|
||||
multiclass = '<p><a class="btn primary" href="#">Save</a></p>';
|
||||
|
||||
describe('cheerio', function() {
|
||||
|
||||
it('should get the version', function() {
|
||||
expect(/\d+\.\d+\.\d+/.test($.version)).to.be.ok();
|
||||
});
|
||||
|
||||
it('$(null) should return be empty', function() {
|
||||
expect($(null)).to.be.empty();
|
||||
});
|
||||
|
||||
it('$(undefined) should be empty', function() {
|
||||
expect($(undefined)).to.be.empty();
|
||||
});
|
||||
|
||||
it('$(null) should be empty', function() {
|
||||
expect($('')).to.be.empty();
|
||||
});
|
||||
|
||||
it('$(selector) with no context or root should be empty', function() {
|
||||
expect($('.h2')).to.be.empty();
|
||||
expect($('#fruits')).to.be.empty();
|
||||
});
|
||||
|
||||
it('$(node) : should override previously-loaded nodes', function() {
|
||||
var C = $.load('<div><span></span></div>');
|
||||
var spanNode = C('span')[0];
|
||||
var $span = C(spanNode);
|
||||
expect($span[0]).to.equal(spanNode);
|
||||
});
|
||||
|
||||
it('should be able to create html without a root or context', function() {
|
||||
var $h2 = $('<h2>');
|
||||
expect($h2).to.not.be.empty();
|
||||
expect($h2).to.have.length(1);
|
||||
expect($h2[0].tagName).to.equal('h2');
|
||||
});
|
||||
|
||||
it('should be able to create complicated html', function() {
|
||||
var $script = $(script);
|
||||
expect($script).to.not.be.empty();
|
||||
expect($script).to.have.length(1);
|
||||
expect($script[0].attribs.src).to.equal('script.js');
|
||||
expect($script[0].attribs.type).to.equal('text/javascript');
|
||||
expect($script[0].childNodes).to.be.empty();
|
||||
});
|
||||
|
||||
var testAppleSelect = function($apple) {
|
||||
expect($apple).to.have.length(1);
|
||||
$apple = $apple[0];
|
||||
expect($apple.parentNode.tagName).to.equal('ul');
|
||||
expect($apple.prev).to.be(null);
|
||||
expect($apple.next.attribs['class']).to.equal('orange');
|
||||
expect($apple.childNodes).to.have.length(1);
|
||||
expect($apple.childNodes[0].data).to.equal('Apple');
|
||||
};
|
||||
|
||||
it('should be able to select .apple with only a context', function() {
|
||||
var $apple = $('.apple', fruits);
|
||||
testAppleSelect($apple);
|
||||
});
|
||||
|
||||
it('should be able to select .apple with a node as context', function() {
|
||||
var $apple = $('.apple', $(fruits)[0]);
|
||||
testAppleSelect($apple);
|
||||
});
|
||||
|
||||
it('should be able to select .apple with only a root', function() {
|
||||
var $apple = $('.apple', null, fruits);
|
||||
testAppleSelect($apple);
|
||||
});
|
||||
|
||||
it('should be able to select an id', function() {
|
||||
var $fruits = $('#fruits', null, fruits);
|
||||
expect($fruits).to.have.length(1);
|
||||
expect($fruits[0].attribs.id).to.equal('fruits');
|
||||
});
|
||||
|
||||
it('should be able to select a tag', function() {
|
||||
var $ul = $('ul', fruits);
|
||||
expect($ul).to.have.length(1);
|
||||
expect($ul[0].tagName).to.equal('ul');
|
||||
});
|
||||
|
||||
it('should accept a node reference as a context', function() {
|
||||
var $elems = $('<div><span></span></div>');
|
||||
expect($('span', $elems[0])).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should accept an array of node references as a context', function() {
|
||||
var $elems = $('<div><span></span></div>');
|
||||
expect($('span', $elems.toArray())).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should select only elements inside given context (Issue #193)', function() {
|
||||
var q = $.load(food),
|
||||
fruits = q('#fruits'),
|
||||
fruitElements = q('li', fruits);
|
||||
|
||||
expect(fruitElements).to.have.length(3);
|
||||
});
|
||||
|
||||
it('should be able to select multiple tags', function() {
|
||||
var $fruits = $('li', null, fruits);
|
||||
expect($fruits).to.have.length(3);
|
||||
var classes = ['apple', 'orange', 'pear'];
|
||||
$fruits.each(function(idx, $fruit) {
|
||||
expect($fruit.attribs['class']).to.equal(classes[idx]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to do: $("#fruits .apple")', function() {
|
||||
var $apple = $('#fruits .apple', fruits);
|
||||
testAppleSelect($apple);
|
||||
});
|
||||
|
||||
it('should be able to do: $("li.apple")', function() {
|
||||
var $apple = $('li.apple', fruits);
|
||||
testAppleSelect($apple);
|
||||
});
|
||||
|
||||
it('should be able to select by attributes', function() {
|
||||
var $apple = $('li[class=apple]', fruits);
|
||||
testAppleSelect($apple);
|
||||
});
|
||||
|
||||
it('should be able to select multiple classes: $(".btn.primary")', function() {
|
||||
var $a = $('.btn.primary', multiclass);
|
||||
expect($a).to.have.length(1);
|
||||
expect($a[0].childNodes[0].data).to.equal('Save');
|
||||
});
|
||||
|
||||
it('should not create a top-level node', function() {
|
||||
var $elem = $('* div', '<div>');
|
||||
expect($elem).to.have.length(0);
|
||||
});
|
||||
|
||||
it('should be able to select multiple elements: $(".apple, #fruits")', function() {
|
||||
var $elems = $('.apple, #fruits', fruits);
|
||||
expect($elems).to.have.length(2);
|
||||
|
||||
var $apple = _.filter($elems, function(elem) {
|
||||
return elem.attribs['class'] === 'apple';
|
||||
});
|
||||
var $fruits = _.filter($elems, function(elem) {
|
||||
return elem.attribs.id === 'fruits';
|
||||
});
|
||||
testAppleSelect($apple);
|
||||
expect($fruits[0].attribs.id).to.equal('fruits');
|
||||
});
|
||||
|
||||
it('should select first element $(:first)');
|
||||
// var $elem = $(':first', fruits);
|
||||
// var $h2 = $('<h2>fruits</h2>');
|
||||
// console.log($elem.before('hi'));
|
||||
// console.log($elem.before($h2));
|
||||
|
||||
it('should be able to select immediate children: $("#fruits > .pear")', function() {
|
||||
var $food = $(food);
|
||||
$('.pear', $food).append('<li class="pear">Another Pear!</li>');
|
||||
expect($('#fruits .pear', $food)).to.have.length(2);
|
||||
var $elem = $('#fruits > .pear', $food);
|
||||
expect($elem).to.have.length(1);
|
||||
expect($elem.attr('class')).to.equal('pear');
|
||||
});
|
||||
|
||||
it('should be able to select immediate children: $(".apple + .pear")', function() {
|
||||
var $elem = $('.apple + li', fruits);
|
||||
expect($elem).to.have.length(1);
|
||||
$elem = $('.apple + .pear', fruits);
|
||||
expect($elem).to.have.length(0);
|
||||
$elem = $('.apple + .orange', fruits);
|
||||
expect($elem).to.have.length(1);
|
||||
expect($elem.attr('class')).to.equal('orange');
|
||||
});
|
||||
|
||||
it('should be able to select immediate children: $(".apple ~ .pear")', function() {
|
||||
var $elem = $('.apple ~ li', fruits);
|
||||
expect($elem).to.have.length(2);
|
||||
$elem = $('.apple ~ .pear', fruits);
|
||||
expect($elem.attr('class')).to.equal('pear');
|
||||
});
|
||||
|
||||
it('should handle wildcards on attributes: $("li[class*=r]")', function() {
|
||||
var $elem = $('li[class*=r]', fruits);
|
||||
expect($elem).to.have.length(2);
|
||||
expect($elem.eq(0).attr('class')).to.equal('orange');
|
||||
expect($elem.eq(1).attr('class')).to.equal('pear');
|
||||
});
|
||||
|
||||
it('should handle beginning of attr selectors: $("li[class^=o]")', function() {
|
||||
var $elem = $('li[class^=o]', fruits);
|
||||
expect($elem).to.have.length(1);
|
||||
expect($elem.eq(0).attr('class')).to.equal('orange');
|
||||
});
|
||||
|
||||
it('should handle beginning of attr selectors: $("li[class$=e]")', function() {
|
||||
var $elem = $('li[class$=e]', fruits);
|
||||
expect($elem).to.have.length(2);
|
||||
expect($elem.eq(0).attr('class')).to.equal('apple');
|
||||
expect($elem.eq(1).attr('class')).to.equal('orange');
|
||||
});
|
||||
|
||||
it('should gracefully degrade on complex, unmatched queries', function() {
|
||||
var $elem = $('Eastern States Cup #8-fin <br>Downhill ');
|
||||
expect($elem).to.have.length(0); // []
|
||||
});
|
||||
|
||||
it('(extended Array) should not interfere with prototype methods (issue #119)', function() {
|
||||
var extended = [];
|
||||
extended.find = extended.children = extended.each = function() {};
|
||||
var $empty = $(extended);
|
||||
|
||||
expect($empty.find).to.be($.prototype.find);
|
||||
expect($empty.children).to.be($.prototype.children);
|
||||
expect($empty.each).to.be($.prototype.each);
|
||||
});
|
||||
|
||||
describe('.load', function() {
|
||||
|
||||
it('should generate selections as proper instances', function() {
|
||||
var q = $.load(fruits);
|
||||
|
||||
expect(q('.apple')).to.be.a(q);
|
||||
});
|
||||
|
||||
it('should be able to filter down using the context', function() {
|
||||
var q = $.load(fruits),
|
||||
apple = q('.apple', 'ul'),
|
||||
lis = q('li', 'ul');
|
||||
|
||||
expect(apple).to.have.length(1);
|
||||
expect(lis).to.have.length(3);
|
||||
});
|
||||
|
||||
it('should allow loading a pre-parsed DOM', function() {
|
||||
var dom = htmlparser2.parseDOM(food),
|
||||
q = $.load(dom);
|
||||
|
||||
expect(q('ul')).to.have.length(3);
|
||||
});
|
||||
|
||||
it('should render xml in html() when options.xmlMode = true', function() {
|
||||
var str = '<MixedCaseTag UPPERCASEATTRIBUTE=""></MixedCaseTag>',
|
||||
expected = '<MixedCaseTag UPPERCASEATTRIBUTE=""/>',
|
||||
dom = $.load(str, {xmlMode: true});
|
||||
|
||||
expect(dom('MixedCaseTag').get(0).tagName).to.equal('MixedCaseTag');
|
||||
expect(dom.html()).to.be(expected);
|
||||
});
|
||||
|
||||
it('should render xml in html() when options.xmlMode = true passed to html()', function() {
|
||||
var str = '<MixedCaseTag UPPERCASEATTRIBUTE=""></MixedCaseTag>',
|
||||
// since parsing done without xmlMode flag, all tags converted to lowercase
|
||||
expectedXml = '<mixedcasetag uppercaseattribute=""/>',
|
||||
expectedNoXml = '<mixedcasetag uppercaseattribute=""></mixedcasetag>',
|
||||
dom = $.load(str);
|
||||
|
||||
expect(dom('MixedCaseTag').get(0).tagName).to.equal('mixedcasetag');
|
||||
expect(dom.html()).to.be(expectedNoXml);
|
||||
expect(dom.html({xmlMode: true})).to.be(expectedXml);
|
||||
});
|
||||
|
||||
it('should respect options on the element level', function() {
|
||||
var str = '<!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 = $.load(str, {decodeEntities: false}),
|
||||
domEncoded = $.load(str);
|
||||
|
||||
expect(domNotEncoded('footer').html()).to.be(expectedHtml);
|
||||
// TODO: Make it more html friendly, maybe with custom encode tables
|
||||
expect(domEncoded('footer').html()).to.be(expectedXml);
|
||||
});
|
||||
|
||||
it('should return a fully-qualified Function', function() {
|
||||
var $c = $.load('<div>');
|
||||
|
||||
expect($c).to.be.a(Function);
|
||||
});
|
||||
|
||||
describe('prototype extensions', function() {
|
||||
it('should honor extensions defined on `prototype` property', function() {
|
||||
var $c = $.load('<div>');
|
||||
var $div;
|
||||
$c.prototype.myPlugin = function() {
|
||||
return {
|
||||
context: this,
|
||||
args: arguments
|
||||
};
|
||||
};
|
||||
|
||||
$div = $c('div');
|
||||
|
||||
expect($div.myPlugin).to.be.a('function');
|
||||
expect($div.myPlugin().context).to.be($div);
|
||||
expect(Array.prototype.slice.call($div.myPlugin(1, 2, 3).args))
|
||||
.to.eql([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should honor extensions defined on `fn` property', function() {
|
||||
var $c = $.load('<div>');
|
||||
var $div;
|
||||
$c.fn.myPlugin = function() {
|
||||
return {
|
||||
context: this,
|
||||
args: arguments
|
||||
};
|
||||
};
|
||||
|
||||
$div = $c('div');
|
||||
|
||||
expect($div.myPlugin).to.be.a('function');
|
||||
expect($div.myPlugin().context).to.be($div);
|
||||
expect(Array.prototype.slice.call($div.myPlugin(1, 2, 3).args))
|
||||
.to.eql([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('should isolate extensions between loaded functions', function() {
|
||||
var $a = $.load('<div>');
|
||||
var $b = $.load('<div>');
|
||||
|
||||
$a.prototype.foo = function() {};
|
||||
|
||||
expect($b('div').foo).to.be(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
65
build/node_modules/cheerio/test/fixtures.js
generated
vendored
Normal file
65
build/node_modules/cheerio/test/fixtures.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
/* jshint indent: false */
|
||||
exports.fruits = [
|
||||
'<ul id="fruits">',
|
||||
'<li class="apple">Apple</li>',
|
||||
'<li class="orange">Orange</li>',
|
||||
'<li class="pear">Pear</li>',
|
||||
'</ul>'
|
||||
].join('');
|
||||
|
||||
exports.vegetables = [
|
||||
'<ul id="vegetables">',
|
||||
'<li class="carrot">Carrot</li>',
|
||||
'<li class="sweetcorn">Sweetcorn</li>',
|
||||
'</ul>'
|
||||
].join('');
|
||||
|
||||
exports.chocolates = [
|
||||
'<ul id="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>'
|
||||
].join('');
|
||||
|
||||
exports.drinks = [
|
||||
'<ul id="drinks">',
|
||||
'<li class="beer">Beer</li>',
|
||||
'<li class="juice">Juice</li>',
|
||||
'<li class="milk">Milk</li>',
|
||||
'<li class="water">Water</li>',
|
||||
'<li class="cider">Cider</li>',
|
||||
'</ul>'
|
||||
].join('');
|
||||
|
||||
exports.food = [
|
||||
'<ul id="food">',
|
||||
exports.fruits,
|
||||
exports.vegetables,
|
||||
'</ul>'
|
||||
].join('');
|
||||
|
||||
exports.inputs = [
|
||||
'<select id="one"><option value="option_not_selected">Option not selected</option><option value="option_selected" selected>Option selected</option></select>',
|
||||
'<input type="text" value="input_text" />',
|
||||
'<input type="checkbox" name="checkbox_off" value="off" /><input type="checkbox" name="checkbox_on" value="on" checked />',
|
||||
'<input type="radio" value="off" name="radio" /><input type="radio" name="radio" value="on" checked />',
|
||||
'<input type="radio" value="off" name="radio[brackets]" /><input type="radio" name="radio[brackets]" value="on" checked />',
|
||||
'<select id="multi" multiple><option value="1">1</option><option value="2" selected>2</option><option value="3" selected>3</option><option value="4">4</option></select>'
|
||||
].join('');
|
||||
|
||||
exports.text = [
|
||||
'<p>Apples, <b>oranges</b> and pears.</p>',
|
||||
'<p>Carrots and <!-- sweetcorn --></p>'
|
||||
].join('');
|
||||
|
||||
exports.forms = [
|
||||
'<form id="simple"><input type="text" name="fruit" value="Apple" /></form>',
|
||||
'<form id="nested"><div><input type="text" name="fruit" value="Apple" /></div><input type="text" name="vegetable" value="Carrot" /></form>',
|
||||
'<form id="disabled"><input type="text" name="fruit" value="Apple" disabled /></form>',
|
||||
'<form id="submit"><input type="text" name="fruit" value="Apple" /><input type="submit" name="submit" value="Submit" /></form>',
|
||||
'<form id="select"><select name="fruit"><option value="Apple">Apple</option><option value="Orange" selected>Orange</option></select></form>',
|
||||
'<form id="unnamed"><input type="text" name="fruit" value="Apple" /><input type="text" value="Carrot" /></form>',
|
||||
'<form id="multiple"><select name="fruit" multiple><option value="Apple" selected>Apple</option><option value="Orange" selected>Orange</option><option value="Carrot">Carrot</option></select></form>',
|
||||
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>'
|
||||
].join('');
|
||||
2
build/node_modules/cheerio/test/mocha.opts
generated
vendored
Normal file
2
build/node_modules/cheerio/test/mocha.opts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
--reporter list
|
||||
--growl
|
||||
252
build/node_modules/cheerio/test/parse.js
generated
vendored
Normal file
252
build/node_modules/cheerio/test/parse.js
generated
vendored
Normal file
@@ -0,0 +1,252 @@
|
||||
var expect = require('expect.js'),
|
||||
parse = require('../lib/parse'),
|
||||
defaultOpts = require('..').prototype.options;
|
||||
|
||||
|
||||
// Tags
|
||||
var basic = '<html></html>';
|
||||
var siblings = '<h2></h2><p></p>';
|
||||
|
||||
// Single Tags
|
||||
var single = '<br/>';
|
||||
var singleWrong = '<br>';
|
||||
|
||||
// Children
|
||||
var children = '<html><br/></html>';
|
||||
var li = '<li class="durian">Durian</li>';
|
||||
|
||||
// Attributes
|
||||
var attributes = '<img src="hello.png" alt="man waving">';
|
||||
var noValueAttribute = '<textarea disabled></textarea>';
|
||||
|
||||
// Comments
|
||||
var comment = '<!-- sexy -->';
|
||||
var conditional = '<!--[if IE 8]><html class="no-js ie8" lang="en"><![endif]-->';
|
||||
|
||||
// Text
|
||||
var text = 'lorem ipsum';
|
||||
|
||||
// Script
|
||||
var script = '<script type="text/javascript">alert("hi world!");</script>';
|
||||
var scriptEmpty = '<script></script>';
|
||||
|
||||
// Style
|
||||
var style = '<style type="text/css"> h2 { color:blue; } </style>';
|
||||
var styleEmpty = '<style></style>';
|
||||
|
||||
// Directives
|
||||
var directive = '<!doctype html>';
|
||||
|
||||
|
||||
describe('parse', function() {
|
||||
|
||||
describe('.eval', function() {
|
||||
|
||||
it('should parse basic empty tags: ' + basic, function() {
|
||||
var tag = parse.evaluate(basic, defaultOpts)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.tagName).to.equal('html');
|
||||
expect(tag.childNodes).to.be.empty();
|
||||
});
|
||||
|
||||
it('should handle sibling tags: ' + siblings, function() {
|
||||
var dom = parse.evaluate(siblings, defaultOpts),
|
||||
h2 = dom[0],
|
||||
p = dom[1];
|
||||
|
||||
expect(dom).to.have.length(2);
|
||||
expect(h2.tagName).to.equal('h2');
|
||||
expect(p.tagName).to.equal('p');
|
||||
});
|
||||
|
||||
it('should handle single tags: ' + single, function() {
|
||||
var tag = parse.evaluate(single, defaultOpts)[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() {
|
||||
var tag = parse.evaluate(singleWrong, defaultOpts)[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() {
|
||||
var tag = parse.evaluate(children, defaultOpts)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.tagName).to.equal('html');
|
||||
expect(tag.childNodes).to.be.ok();
|
||||
expect(tag.childNodes).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should handle tags with children: ' + li, function() {
|
||||
var tag = parse.evaluate(li, defaultOpts)[0];
|
||||
expect(tag.childNodes).to.have.length(1);
|
||||
expect(tag.childNodes[0].data).to.equal('Durian');
|
||||
});
|
||||
|
||||
it('should handle tags with attributes: ' + attributes, function() {
|
||||
var attrs = parse.evaluate(attributes, defaultOpts)[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() {
|
||||
var attrs = parse.evaluate(noValueAttribute, defaultOpts)[0].attribs;
|
||||
expect(attrs).to.be.ok();
|
||||
expect(attrs.disabled).to.equal('');
|
||||
});
|
||||
|
||||
it('should handle comments: ' + comment, function() {
|
||||
var elem = parse.evaluate(comment, defaultOpts)[0];
|
||||
expect(elem.type).to.equal('comment');
|
||||
expect(elem.data).to.equal(' sexy ');
|
||||
});
|
||||
|
||||
it('should handle conditional comments: ' + conditional, function() {
|
||||
var elem = parse.evaluate(conditional, defaultOpts)[0];
|
||||
expect(elem.type).to.equal('comment');
|
||||
expect(elem.data).to.equal(conditional.replace('<!--', '').replace('-->', ''));
|
||||
});
|
||||
|
||||
it('should handle text: ' + text, function() {
|
||||
var text_ = parse.evaluate(text, defaultOpts)[0];
|
||||
expect(text_.type).to.equal('text');
|
||||
expect(text_.data).to.equal('lorem ipsum');
|
||||
});
|
||||
|
||||
it('should handle script tags: ' + script, function() {
|
||||
var script_ = parse.evaluate(script, defaultOpts)[0];
|
||||
expect(script_.type).to.equal('script');
|
||||
expect(script_.tagName).to.equal('script');
|
||||
expect(script_.attribs.type).to.equal('text/javascript');
|
||||
expect(script_.childNodes).to.have.length(1);
|
||||
expect(script_.childNodes[0].type).to.equal('text');
|
||||
expect(script_.childNodes[0].data).to.equal('alert("hi world!");');
|
||||
});
|
||||
|
||||
it('should handle style tags: ' + style, function() {
|
||||
var style_ = parse.evaluate(style, defaultOpts)[0];
|
||||
expect(style_.type).to.equal('style');
|
||||
expect(style_.tagName).to.equal('style');
|
||||
expect(style_.attribs.type).to.equal('text/css');
|
||||
expect(style_.childNodes).to.have.length(1);
|
||||
expect(style_.childNodes[0].type).to.equal('text');
|
||||
expect(style_.childNodes[0].data).to.equal(' h2 { color:blue; } ');
|
||||
});
|
||||
|
||||
it('should handle directives: ' + directive, function() {
|
||||
var elem = parse.evaluate(directive, defaultOpts)[0];
|
||||
expect(elem.type).to.equal('directive');
|
||||
expect(elem.data).to.equal('!doctype html');
|
||||
expect(elem.tagName).to.equal('!doctype');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.parse', function() {
|
||||
|
||||
// root test utility
|
||||
function rootTest(root) {
|
||||
expect(root.tagName).to.equal('root');
|
||||
|
||||
// Should exist but be null
|
||||
expect(root.nextSibling).to.be(null);
|
||||
expect(root.previousSibling).to.be(null);
|
||||
expect(root.parentNode).to.be(null);
|
||||
|
||||
var child = root.childNodes[0];
|
||||
expect(child.parentNode).to.be(null);
|
||||
}
|
||||
|
||||
it('should add root to: ' + basic, function() {
|
||||
var root = parse(basic, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].tagName).to.equal('html');
|
||||
});
|
||||
|
||||
it('should add root to: ' + siblings, function() {
|
||||
var root = parse(siblings, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(2);
|
||||
expect(root.childNodes[0].tagName).to.equal('h2');
|
||||
expect(root.childNodes[1].tagName).to.equal('p');
|
||||
expect(root.childNodes[1].parent).to.equal(null);
|
||||
});
|
||||
|
||||
it('should add root to: ' + comment, function() {
|
||||
var root = parse(comment, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('comment');
|
||||
});
|
||||
|
||||
it('should add root to: ' + text, function() {
|
||||
var root = parse(text, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('text');
|
||||
});
|
||||
|
||||
it('should add root to: ' + scriptEmpty, function() {
|
||||
var root = parse(scriptEmpty, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('script');
|
||||
});
|
||||
|
||||
it('should add root to: ' + styleEmpty, function() {
|
||||
var root = parse(styleEmpty, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('style');
|
||||
});
|
||||
|
||||
it('should add root to: ' + directive, function() {
|
||||
var root = parse(directive, defaultOpts);
|
||||
rootTest(root);
|
||||
expect(root.childNodes).to.have.length(1);
|
||||
expect(root.childNodes[0].type).to.equal('directive');
|
||||
});
|
||||
|
||||
it('should expose the DOM level 1 API', function() {
|
||||
var root = parse('<div><a></a><span></span><p></p></div>', defaultOpts).childNodes[0];
|
||||
var childNodes = root.childNodes;
|
||||
|
||||
expect(childNodes).to.have.length(3);
|
||||
|
||||
expect(root.tagName).to.be('div');
|
||||
expect(root.firstChild).to.be(childNodes[0]);
|
||||
expect(root.lastChild).to.be(childNodes[2]);
|
||||
|
||||
expect(childNodes[0].tagName).to.be('a');
|
||||
expect(childNodes[0].previousSibling).to.be(null);
|
||||
expect(childNodes[0].nextSibling).to.be(childNodes[1]);
|
||||
expect(childNodes[0].parentNode).to.be(root);
|
||||
expect(childNodes[0].childNodes).to.have.length(0);
|
||||
expect(childNodes[0].firstChild).to.be(null);
|
||||
expect(childNodes[0].lastChild).to.be(null);
|
||||
|
||||
expect(childNodes[1].tagName).to.be('span');
|
||||
expect(childNodes[1].previousSibling).to.be(childNodes[0]);
|
||||
expect(childNodes[1].nextSibling).to.be(childNodes[2]);
|
||||
expect(childNodes[1].parentNode).to.be(root);
|
||||
expect(childNodes[1].childNodes).to.have.length(0);
|
||||
expect(childNodes[1].firstChild).to.be(null);
|
||||
expect(childNodes[1].lastChild).to.be(null);
|
||||
|
||||
expect(childNodes[2].tagName).to.be('p');
|
||||
expect(childNodes[2].previousSibling).to.be(childNodes[1]);
|
||||
expect(childNodes[2].nextSibling).to.be(null);
|
||||
expect(childNodes[2].parentNode).to.be(root);
|
||||
expect(childNodes[2].childNodes).to.have.length(0);
|
||||
expect(childNodes[2].firstChild).to.be(null);
|
||||
expect(childNodes[2].lastChild).to.be(null);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
58
build/node_modules/cheerio/test/xml.js
generated
vendored
Normal file
58
build/node_modules/cheerio/test/xml.js
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
var expect = require('expect.js'),
|
||||
_ = require('lodash'),
|
||||
cheerio = require('..');
|
||||
|
||||
var xml = function(str, options) {
|
||||
options = _.extend({ xmlMode: true }, options);
|
||||
var dom = cheerio.load(str, options);
|
||||
return dom.xml();
|
||||
};
|
||||
|
||||
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() {
|
||||
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('<media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time="12:05:01.123"/>');
|
||||
});
|
||||
|
||||
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(){
|
||||
var str = '<tag attr="foo & bar"/>';
|
||||
expect(xml(str)).to.equal(str);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('(dom)', function () {
|
||||
|
||||
it('should keep camelCase for new nodes', function() {
|
||||
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
|
||||
expect(dom(str, {xmlMode: false})).to.equal('<someelem someattribute="something">hello</someelem>');
|
||||
});
|
||||
|
||||
it('should keep camelCase for new nodes', function() {
|
||||
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
|
||||
expect(dom(str, {xmlMode: true})).to.equal('<someElem someAttribute="something">hello</someElem>');
|
||||
});
|
||||
|
||||
it('should maintain the parsing options of distinct contexts independently', function() {
|
||||
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
|
||||
var $x = cheerio.load('', { xmlMode: false });
|
||||
|
||||
expect($x(str).html()).to.equal('<someelem someattribute="something">hello</someelem>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user