all da files
This commit is contained in:
216
node_modules/cheerio/test/api.attributes.js
generated
vendored
Normal file
216
node_modules/cheerio/test/api.attributes.js
generated
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
var expect = require('expect.js');
|
||||
|
||||
var $ = require('../');
|
||||
var fruits = require('./fixtures').fruits;
|
||||
var vegetables = require('./fixtures').vegetables;
|
||||
|
||||
describe('$(...)', function() {
|
||||
|
||||
describe('.attr', function() {
|
||||
|
||||
it('() : should get all the attributes', function() {
|
||||
var attrs = $('ul', fruits).attr();
|
||||
expect(attrs.id).to.equal('fruits');
|
||||
});
|
||||
|
||||
it('(invalid key) : invalid attr should get undefined', function() {
|
||||
var attr = $('.apple', fruits).attr('lol');
|
||||
expect(attr).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(valid key) : valid attr should get value', function() {
|
||||
var cls = $('.apple', fruits).attr('class');
|
||||
expect(cls).to.equal('apple');
|
||||
});
|
||||
|
||||
it('(key, value) : should set attr', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $pear = $('.pear', $fruits).attr('id', 'pear');
|
||||
expect($('#pear', $fruits)).to.have.length(1);
|
||||
expect($pear.cheerio).to.not.be(undefined);
|
||||
});
|
||||
|
||||
it('(map) : object map should set multiple attributes', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.apple', $fruits).attr({
|
||||
id: 'apple',
|
||||
style: 'color:red;',
|
||||
'data-url': 'http://apple.com'
|
||||
});
|
||||
var attrs = $('.apple', $fruits).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, value) : should correctly encode then decode unsafe values', function() {
|
||||
var $apple = $('.apple', fruits);
|
||||
$apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
|
||||
expect($apple[0].attribs.href).to.equal('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', fruits);
|
||||
$apple.attr('data-test', 1);
|
||||
expect($apple[0].attribs['data-test']).to.equal('1');
|
||||
expect($apple.attr('data-test')).to.equal('1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.removeAttr', function() {
|
||||
|
||||
it('(key) : should remove a single attr', function() {
|
||||
var $fruits = $(fruits);
|
||||
expect($('ul', $fruits).attr('id')).to.not.be(undefined);
|
||||
$('ul', $fruits).removeAttr('id');
|
||||
expect($('ul', $fruits).attr('id')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('should return cheerio object', function() {
|
||||
var obj = $('ul', fruits).removeAttr('id').cheerio;
|
||||
expect(obj).to.be.ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.hasClass', function() {
|
||||
|
||||
it('(valid class) : should return true', function() {
|
||||
var $fruits = $(fruits);
|
||||
var cls = $('.apple', $fruits).hasClass('apple');
|
||||
expect(cls).to.be.ok();
|
||||
});
|
||||
|
||||
it('(invalid class) : should return false', function() {
|
||||
var cls = $('#fruits', fruits).hasClass('fruits');
|
||||
expect(cls).to.not.be.ok();
|
||||
});
|
||||
|
||||
it('should check multiple classes', function() {
|
||||
var $fruits = $(fruits);
|
||||
|
||||
// Add a class
|
||||
$('.apple', $fruits).addClass('red');
|
||||
expect($('.apple', $fruits).hasClass('apple')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('red')).to.be.ok();
|
||||
|
||||
// Remove one and test again
|
||||
$('.apple', $fruits).removeClass('apple');
|
||||
expect($('li', $fruits).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', $fruits).addClass('fruits');
|
||||
var cls = $('#fruits', $fruits).hasClass('fruits');
|
||||
expect(cls).to.be.ok();
|
||||
});
|
||||
|
||||
it('(single class) : should add the class to the element', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.apple', $fruits).addClass('fruit');
|
||||
var cls = $('.apple', $fruits).hasClass('fruit');
|
||||
expect(cls).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class): adds classes to many selected items', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('li', $fruits).addClass('fruit');
|
||||
expect($('.apple', $fruits).hasClass('fruit')).to.be.ok();
|
||||
expect($('.orange', $fruits).hasClass('fruit')).to.be.ok();
|
||||
expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class class class) : should add multiple classes to the element', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.apple', $fruits).addClass('fruit red tasty');
|
||||
expect($('.apple', $fruits).hasClass('apple')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('fruit')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('red')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('tasty')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should add classes returned from the function');
|
||||
|
||||
});
|
||||
|
||||
describe('.removeClass', function() {
|
||||
|
||||
it('() : should remove all the classes', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.pear', $fruits).addClass('fruit');
|
||||
$('.pear', $fruits).removeClass();
|
||||
expect($('.pear', $fruits).attr('class')).to.be(undefined);
|
||||
});
|
||||
|
||||
it('(invalid class) : should not remove anything', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.pear', $fruits).removeClass('fruit');
|
||||
expect($('.pear', $fruits).hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(no class attribute) : should not throw an exception', function() {
|
||||
var $vegetables = $(vegetables);
|
||||
var thrown = null;
|
||||
expect(function() {
|
||||
$('li', $vegetables).removeClass('vegetable');
|
||||
})
|
||||
.to.not.throwException();
|
||||
});
|
||||
|
||||
it('(single class) : should remove a single class from the element', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.pear', $fruits).addClass('fruit');
|
||||
expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
|
||||
$('.pear', $fruits).removeClass('fruit');
|
||||
expect($('.pear', $fruits).hasClass('fruit')).to.not.be.ok();
|
||||
expect($('.pear', $fruits).hasClass('pear')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(single class) : should remove a single class from multiple classes on the element', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.pear', $fruits).addClass('fruit green tasty');
|
||||
expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
|
||||
expect($('.pear', $fruits).hasClass('green')).to.be.ok();
|
||||
expect($('.pear', $fruits).hasClass('tasty')).to.be.ok();
|
||||
|
||||
$('.pear', $fruits).removeClass('green');
|
||||
expect($('.pear', $fruits).hasClass('fruit')).to.be.ok();
|
||||
expect($('.pear', $fruits).hasClass('green')).to.not.be.ok();
|
||||
expect($('.pear', $fruits).hasClass('tasty')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class class class) : should remove multiple classes from the element', function() {
|
||||
var $fruits = $(fruits);
|
||||
|
||||
$('.apple', $fruits).addClass('fruit red tasty');
|
||||
expect($('.apple', $fruits).hasClass('apple')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('fruit')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('red')).to.be.ok();
|
||||
expect($('.apple', $fruits).hasClass('tasty')).to.be.ok();
|
||||
|
||||
$('.apple', $fruits).removeClass('apple red tasty');
|
||||
expect($('.fruit', $fruits).hasClass('apple')).to.not.be.ok();
|
||||
expect($('.fruit', $fruits).hasClass('red')).to.not.be.ok();
|
||||
expect($('.fruit', $fruits).hasClass('tasty')).to.not.be.ok();
|
||||
expect($('.fruit', $fruits).hasClass('fruit')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(class) : should remove all occurrences of a class name', function() {
|
||||
var $div = $('<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');
|
||||
|
||||
});
|
||||
|
||||
});
|
312
node_modules/cheerio/test/api.manipulation.js
generated
vendored
Normal file
312
node_modules/cheerio/test/api.manipulation.js
generated
vendored
Normal file
@@ -0,0 +1,312 @@
|
||||
var expect = require('expect.js'),
|
||||
$ = require('../'),
|
||||
fruits = require('./fixtures').fruits;
|
||||
|
||||
describe('$(...)', function() {
|
||||
|
||||
describe('.append', function() {
|
||||
|
||||
it('() : should do nothing', function() {
|
||||
expect($('#fruits', fruits).append()[0].name).to.equal('ul');
|
||||
});
|
||||
|
||||
it('(html) : should add element as last child', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('#fruits', $fruits).append('<li class="plum">Plum</li>');
|
||||
expect($('#fruits', $fruits).children(3).hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...)) : should add element as last child', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
$('#fruits', $fruits).append($plum);
|
||||
expect($('#fruits', $fruits).children(3).hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...), html) : should add multiple elements as last children', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
var grape = '<li class="grape">Grape</li>';
|
||||
$('#fruits', $fruits).append($plum, grape);
|
||||
expect($('#fruits', $fruits).children(3).hasClass('plum')).to.be.ok();
|
||||
expect($('#fruits', $fruits).children(4).hasClass('grape')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should add returned element as last child');
|
||||
|
||||
it('should maintain correct object state (Issue: #10)', function() {
|
||||
var $obj = $('<div></div>')
|
||||
.append('<div><div></div></div>')
|
||||
.children()
|
||||
.children()
|
||||
.parent();
|
||||
expect($obj).to.be.ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.prepend', function() {
|
||||
|
||||
it('() : should do nothing', function() {
|
||||
expect($('#fruits', fruits).prepend()[0].name).to.equal('ul');
|
||||
});
|
||||
|
||||
it('(html) : should add element as first child', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('#fruits', $fruits).prepend('<li class="plum">Plum</li>');
|
||||
expect($('#fruits', $fruits).children(0).hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...)) : should add element as first child', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
$('#fruits', $fruits).prepend($plum);
|
||||
expect($('#fruits', $fruits).children(0).hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(html, $(...), html) : should add multiple elements as first children', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
var grape = '<li class="grape">Grape</li>';
|
||||
$('#fruits', $fruits).prepend($plum, grape);
|
||||
expect($('#fruits', $fruits).children(0).hasClass('plum')).to.be.ok();
|
||||
expect($('#fruits', $fruits).children(1).hasClass('grape')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should add returned element as first child');
|
||||
|
||||
});
|
||||
|
||||
describe('.after', function() {
|
||||
|
||||
it('() : should do nothing', function() {
|
||||
expect($('#fruits', fruits).after()[0].name).to.equal('ul');
|
||||
});
|
||||
|
||||
it('(html) : should add element as next sibling', function() {
|
||||
var $fruits = $(fruits);
|
||||
var grape = '<li class="grape">Grape</li>';
|
||||
$('.apple', $fruits).after(grape);
|
||||
expect($('.apple', $fruits).next().hasClass('grape')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...)) : should add element as next sibling', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
$('.apple', $fruits).after($plum);
|
||||
expect($('.apple', $fruits).next().hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...), html) : should add multiple elements as next siblings', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
var grape = '<li class="grape">Grape</li>';
|
||||
$('.apple', $fruits).after($plum, grape);
|
||||
expect($('.apple', $fruits).next().hasClass('plum')).to.be.ok();
|
||||
expect($('.plum', $fruits).next().hasClass('grape')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should add returned element as next sibling');
|
||||
|
||||
});
|
||||
|
||||
describe('.before', function() {
|
||||
|
||||
it('() : should do nothing', function() {
|
||||
expect($('#fruits', fruits).before()[0].name).to.equal('ul');
|
||||
});
|
||||
|
||||
it('(html) : should add element as previous sibling', function() {
|
||||
var $fruits = $(fruits);
|
||||
var grape = '<li class="grape">Grape</li>';
|
||||
$('.apple', $fruits).before(grape);
|
||||
expect($('.apple', $fruits).prev().hasClass('grape')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...)) : should add element as previous sibling', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
$('.apple', $fruits).before($plum);
|
||||
expect($('.apple', $fruits).prev().hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('($(...), html) : should add multiple elements as previous siblings', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
var grape = '<li class="grape">Grape</li>';
|
||||
$('.apple', $fruits).before($plum, grape);
|
||||
expect($('.apple', $fruits).prev().hasClass('grape')).to.be.ok();
|
||||
expect($('.grape', $fruits).prev().hasClass('plum')).to.be.ok();
|
||||
});
|
||||
|
||||
it('(fn) : should add returned element as previous sibling');
|
||||
|
||||
});
|
||||
|
||||
describe('.remove', function() {
|
||||
|
||||
it('() : should remove selected elements', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.apple', $fruits).remove();
|
||||
expect($fruits.find('.apple')).to.have.length(0);
|
||||
});
|
||||
|
||||
it('(selector) : should remove matching selected elements', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('li', $fruits).remove('.apple');
|
||||
expect($fruits.find('.apple')).to.have.length(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.replaceWith', function() {
|
||||
|
||||
it('(elem) : should replace one <li> tag with another', function() {
|
||||
var $fruits = $(fruits);
|
||||
var $plum = $('<li class="plum">Plum</li>');
|
||||
$('.pear', $fruits).replaceWith($plum);
|
||||
expect($('.orange', $fruits).next().hasClass('plum')).to.be.ok();
|
||||
expect($('.orange', $fruits).next().html()).to.equal('Plum');
|
||||
});
|
||||
|
||||
it('(elem) : should replace the selected element with given element', function() {
|
||||
var $src = $('<ul></ul>');
|
||||
var $elem = $('<h2>hi <span>there</span></h2>');
|
||||
var $replaced = $src.replaceWith($elem);
|
||||
expect($replaced[0].parent.type).to.equal('root');
|
||||
expect($.html($replaced[0].parent)).to.equal('<h2>hi <span>there</span></h2>');
|
||||
expect($.html($replaced)).to.equal('<ul></ul>');
|
||||
});
|
||||
|
||||
it('(elem) : should replace the single selected element with given element', function() {
|
||||
var $src = $('<br/>');
|
||||
var $elem = $('<h2>hi <span>there</span></h2>');
|
||||
var $replaced = $src.replaceWith($elem);
|
||||
expect($replaced[0].parent.type).to.equal('root');
|
||||
expect($.html($replaced[0].parent)).to.equal('<h2>hi <span>there</span></h2>');
|
||||
expect($.html($replaced)).to.equal('<br>');
|
||||
});
|
||||
|
||||
it('(str) : should accept strings', function() {
|
||||
var $src = $('<br/>');
|
||||
var $replaced = $src.replaceWith('<h2>hi <span>there</span></h2>');
|
||||
expect($replaced[0].parent.type).to.equal('root');
|
||||
expect($.html($replaced[0].parent)).to.equal('<h2>hi <span>there</span></h2>');
|
||||
expect($.html($replaced)).to.equal('<br>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.empty', function() {
|
||||
|
||||
it('() : should remove all children from selected elements', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('#fruits', $fruits).empty();
|
||||
expect($('#fruits', $fruits).children()).to.have.length(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.html', function() {
|
||||
|
||||
it('() : should get the innerHTML for an element', function() {
|
||||
var $fruits = $(fruits);
|
||||
expect($('#fruits', $fruits).html()).to.equal([
|
||||
'<li class="apple">Apple</li>',
|
||||
'<li class="orange">Orange</li>',
|
||||
'<li class="pear">Pear</li>'
|
||||
].join(''));
|
||||
});
|
||||
|
||||
it('() : should get innerHTML even if its just text', function() {
|
||||
var item = '<li class="pear">Pear</li>';
|
||||
expect($('.pear', item).html()).to.equal('Pear');
|
||||
});
|
||||
|
||||
it('() : should return empty string if nothing inside', function() {
|
||||
var item = '<li></li>';
|
||||
expect($('li', item).html()).to.equal('');
|
||||
});
|
||||
|
||||
it('(html) : should set the html for its children', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('#fruits', $fruits).html('<li class="durian">Durian</li>');
|
||||
var html = $('#fruits', $fruits).html();
|
||||
expect(html).to.equal('<li class="durian">Durian</li>');
|
||||
});
|
||||
|
||||
it('(elem) : should set the html for its children with element', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('#fruits', $fruits).html($('<li class="durian">Durian</li>'));
|
||||
var html = $('#fruits', $fruits).html();
|
||||
expect(html).to.equal('<li class="durian">Durian</li>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.toString', function() {
|
||||
it('() : should get the outerHTML for an element', function() {
|
||||
var $fruits = $(fruits);
|
||||
expect($fruits.toString()).to.equal(fruits);
|
||||
});
|
||||
|
||||
it('() : should return an html string for a set of elements', function() {
|
||||
var $fruits = $(fruits);
|
||||
expect($fruits.find('li').toString()).to.equal('<li class="apple">Apple</li><li class="orange">Orange</li><li class="pear">Pear</li>');
|
||||
});
|
||||
|
||||
it('() : should be called implicitly', function() {
|
||||
var string = [$("<foo>"), $("<bar>"), $("<baz>")].join("");
|
||||
expect(string).to.equal('<foo></foo><bar></bar><baz></baz>');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.text', function() {
|
||||
|
||||
it('() : gets the text for a single element', function() {
|
||||
expect($('.apple', fruits).text()).to.equal('Apple');
|
||||
});
|
||||
|
||||
it('() : combines all text from children text nodes', function() {
|
||||
expect($('#fruits', fruits).text()).to.equal('AppleOrangePear');
|
||||
});
|
||||
|
||||
it('(text) : sets the text for the child node', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.apple', $fruits).text('Granny Smith Apple');
|
||||
expect($('.apple', $fruits)[0].children[0].data).to.equal('Granny Smith Apple');
|
||||
});
|
||||
|
||||
it('should allow functions as arguments', function() {
|
||||
var $fruits = $(fruits);
|
||||
$('.apple', $fruits).text(function(idx, content) {
|
||||
expect(idx).to.equal(0);
|
||||
expect(content).to.equal('Apple');
|
||||
return 'whatever mate';
|
||||
});
|
||||
expect($('.apple', $fruits)[0].children[0].data).to.equal('whatever mate');
|
||||
});
|
||||
|
||||
it('should decode special chars', function() {
|
||||
var text = $('<p>M&M</p>').text();
|
||||
expect(text).to.equal('M&M');
|
||||
});
|
||||
|
||||
it('should work with special chars added as strings', function() {
|
||||
var text = $('<p>M&M</p>').text();
|
||||
expect(text).to.equal('M&M');
|
||||
});
|
||||
|
||||
it('(str) should encode then decode unsafe characters', function() {
|
||||
var $apple = $('.apple', fruits);
|
||||
|
||||
$apple.text('blah <script>alert("XSS!")</script> blah');
|
||||
expect($apple[0].children[0].data).to.equal('blah <script>alert("XSS!")</script> blah');
|
||||
expect($apple.text()).to.equal('blah <script>alert("XSS!")</script> blah');
|
||||
|
||||
$apple.text('blah <script>alert("XSS!")</script> blah');
|
||||
expect($apple.html()).to.not.contain('<script>alert("XSS!")</script>');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
254
node_modules/cheerio/test/api.traversing.js
generated
vendored
Normal file
254
node_modules/cheerio/test/api.traversing.js
generated
vendored
Normal file
@@ -0,0 +1,254 @@
|
||||
var expect = require('expect.js'),
|
||||
$ = require('../'),
|
||||
food = require('./fixtures').food,
|
||||
fruits = require('./fixtures').fruits;
|
||||
|
||||
describe('$(...)', function() {
|
||||
|
||||
describe('.find', function() {
|
||||
|
||||
it('() : should return this', function() {
|
||||
expect($('ul', fruits).find()[0].name).to.equal('ul');
|
||||
});
|
||||
|
||||
it('(single) : should find one descendant', function() {
|
||||
expect($('#fruits', fruits).find('.apple')[0].attribs['class']).to.equal('apple');
|
||||
});
|
||||
|
||||
it('(many) : should find all matching descendant', function() {
|
||||
expect($('#fruits', fruits).find('li')).to.have.length(3);
|
||||
});
|
||||
|
||||
it('(many) : should merge all selected elems with matching descendants');
|
||||
|
||||
it('(invalid single) : should return empty if cant find', function() {
|
||||
expect($('ul', fruits).find('blah')).to.have.length(0);
|
||||
});
|
||||
|
||||
it('(invalid single) : should query descendants only', function() {
|
||||
expect($('#fruits', fruits).find('ul')).to.have.length(0);
|
||||
});
|
||||
|
||||
it('should return empty if search already empty result', function() {
|
||||
expect($('#fruits').find('li')).to.have.length(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.children', function() {
|
||||
|
||||
it('() : should get all children', function() {
|
||||
expect($('ul', fruits).children()).to.have.length(3);
|
||||
});
|
||||
|
||||
it('() : should return children of all matched elements', function() {
|
||||
expect($('ul ul', food).children()).to.have.length(5);
|
||||
});
|
||||
|
||||
it('(selector) : should return children matching selector', function() {
|
||||
var cls = $('ul', fruits).children('.orange')[0].attribs['class'];
|
||||
expect(cls).to.equal('orange');
|
||||
});
|
||||
|
||||
it('(invalid selector) : should return empty', function() {
|
||||
expect($('ul', fruits).children('.lulz')).to.have.length(0);
|
||||
});
|
||||
|
||||
it('should only match immediate children, not ancestors');
|
||||
|
||||
});
|
||||
|
||||
describe('.next', function() {
|
||||
|
||||
it('() : should return next element', function() {
|
||||
var cls = $('.orange', fruits).next()[0].attribs['class'];
|
||||
expect(cls).to.equal('pear');
|
||||
});
|
||||
|
||||
it('(no next) : should return null (?)');
|
||||
|
||||
});
|
||||
|
||||
describe('.prev', function() {
|
||||
|
||||
it('() : should return previous element', function() {
|
||||
var cls = $('.orange', fruits).prev()[0].attribs['class'];
|
||||
expect(cls).to.equal('apple');
|
||||
});
|
||||
|
||||
it('(no prev) : should return null (?)');
|
||||
|
||||
});
|
||||
|
||||
describe('.siblings', function() {
|
||||
|
||||
it('() : should get all the siblings', function() {
|
||||
expect($('.orange', fruits).siblings()).to.have.length(2);
|
||||
});
|
||||
|
||||
it('(selector) : should get all siblings that match the selector', function() {
|
||||
expect($('.orange', fruits).siblings('li')).to.have.length(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.each', function() {
|
||||
|
||||
it('( (i, elem) -> ) : should loop selected returning fn with (i, elem)', function() {
|
||||
var items = [],
|
||||
classes = ['apple', 'orange', 'pear'];
|
||||
$('li', fruits).each(function(idx, elem) {
|
||||
items[idx] = elem;
|
||||
expect(this[0].attribs['class']).to.equal(classes[idx]);
|
||||
});
|
||||
expect(items[0].attribs['class']).to.equal('apple');
|
||||
expect(items[1].attribs['class']).to.equal('orange');
|
||||
expect(items[2].attribs['class']).to.equal('pear');
|
||||
});
|
||||
|
||||
it('( (i, elem) -> ) : should break iteration when the iterator function returns false', function() {
|
||||
|
||||
var iterationCount = 0;
|
||||
$('li', fruits).each(function(idx, elem) {
|
||||
iterationCount++;
|
||||
return idx < 1;
|
||||
});
|
||||
|
||||
expect(iterationCount).to.equal(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.map', function() {
|
||||
it('(fn) : should return an array of mapped items', function() {
|
||||
var classes = $('li', fruits).map(function(i, el) {
|
||||
expect(this[0]).to.be(el);
|
||||
expect(el.name).to.be('li');
|
||||
expect(i).to.be.a('number');
|
||||
return el.attribs['class'];
|
||||
}).join(', ');
|
||||
|
||||
expect(classes).to.be('apple, orange, pear');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.filter', function() {
|
||||
it('(selector) : should reduce the set of matched elements to those that match the selector', function() {
|
||||
var pear = $('li', fruits).filter('.pear').text();
|
||||
expect(pear).to.be('Pear');
|
||||
});
|
||||
|
||||
it('(selector) : should not consider nested elements', function() {
|
||||
var lis = $(fruits).filter('li');
|
||||
expect(lis).to.have.length(0);
|
||||
});
|
||||
|
||||
it('(fn) : should reduce the set of matched elements to those that pass the function\'s test', function() {
|
||||
var orange = $('li', fruits).filter(function(i, el) {
|
||||
expect(this[0]).to.be(el);
|
||||
expect(el.name).to.be('li');
|
||||
expect(i).to.be.a('number');
|
||||
return this.attr('class') === 'orange';
|
||||
}).text();
|
||||
|
||||
expect(orange).to.be('Orange');
|
||||
});
|
||||
});
|
||||
|
||||
describe('.first', function() {
|
||||
|
||||
it('() : should return the first item', function() {
|
||||
var $src = $('<span>foo</span><span>bar</span><span>baz</span>');
|
||||
var $elem = $src.first();
|
||||
expect($elem.length).to.equal(1);
|
||||
expect($elem[0].children[0].data).to.equal('foo');
|
||||
});
|
||||
|
||||
it('() : should return an empty object for an empty object', function() {
|
||||
var $src = $();
|
||||
var $first = $src.first();
|
||||
expect($first.length).to.equal(0);
|
||||
expect($first[0]).to.be(undefined);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.last', function() {
|
||||
|
||||
it('() : should return the last element', function() {
|
||||
var $src = $('<span>foo</span><span>bar</span><span>baz</span>');
|
||||
var $elem = $src.last();
|
||||
expect($elem.length).to.equal(1);
|
||||
expect($elem[0].children[0].data).to.equal('baz');
|
||||
});
|
||||
|
||||
it('() : should return an empty object for an empty object', function() {
|
||||
var $src = $();
|
||||
var $last = $src.last();
|
||||
expect($last.length).to.equal(0);
|
||||
expect($last[0]).to.be(undefined);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.first & .last', function() {
|
||||
|
||||
it('() : should return equivalent collections if only one element', function() {
|
||||
var $src = $('<span>bar</span>');
|
||||
var $first = $src.first();
|
||||
var $last = $src.last();
|
||||
expect($first.length).to.equal(1);
|
||||
expect($first[0].children[0].data).to.equal('bar');
|
||||
expect($last.length).to.equal(1);
|
||||
expect($last[0].children[0].data).to.equal('bar');
|
||||
expect($first[0]).to.equal($last[0]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.eq', function() {
|
||||
|
||||
function getText(el) {
|
||||
if(!el.length) return '';
|
||||
return el[0].children[0].data;
|
||||
}
|
||||
|
||||
it('(i) : should return the element at the specified index', function() {
|
||||
expect(getText($('li', fruits).eq(0))).to.equal('Apple');
|
||||
expect(getText($('li', fruits).eq(1))).to.equal('Orange');
|
||||
expect(getText($('li', fruits).eq(2))).to.equal('Pear');
|
||||
expect(getText($('li', fruits).eq(3))).to.equal('');
|
||||
expect(getText($('li', fruits).eq(-1))).to.equal('Pear');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.slice', function() {
|
||||
|
||||
function getText(el) {
|
||||
if(!el.length) return '';
|
||||
return el[0].children[0].data;
|
||||
}
|
||||
|
||||
it('(start) : should return all elements after the given index', function() {
|
||||
var sliced = $('li', fruits).slice(1);
|
||||
expect(sliced).to.have.length(2);
|
||||
expect(getText(sliced.eq(0))).to.equal('Orange');
|
||||
expect(getText(sliced.eq(1))).to.equal('Pear');
|
||||
});
|
||||
|
||||
it('(start, end) : should return all elements matching the given range', function() {
|
||||
var sliced = $('li', fruits).slice(1, 2);
|
||||
expect(sliced).to.have.length(1);
|
||||
expect(getText(sliced.eq(0))).to.equal('Orange');
|
||||
});
|
||||
|
||||
it('(-start) : should return element matching the offset from the end', function() {
|
||||
var sliced = $('li', fruits).slice(-1);
|
||||
expect(sliced).to.have.length(1);
|
||||
expect(getText(sliced.eq(0))).to.equal('Pear');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
94
node_modules/cheerio/test/api.utils.js
generated
vendored
Normal file
94
node_modules/cheerio/test/api.utils.js
generated
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
var expect = require('expect.js'),
|
||||
fixtures = require('./fixtures'),
|
||||
$ = require('../');
|
||||
|
||||
|
||||
describe('$', function() {
|
||||
|
||||
describe('.html', function() {
|
||||
|
||||
it('() : should return innerHTML; $.html(obj) should return outerHTML', function() {
|
||||
var $div = $('div', '<div><span>foo</span><span>bar</span></div>');
|
||||
var span = $div.children()[1];
|
||||
expect($(span).html()).to.equal('bar');
|
||||
expect($.html(span)).to.equal('<span>bar</span>');
|
||||
});
|
||||
|
||||
it('(<obj>) : should accept an object, an array, or a cheerio object', function() {
|
||||
var $span = $('<span>foo</span>');
|
||||
expect($.html($span[0])).to.equal('<span>foo</span>');
|
||||
expect($.html($span)).to.equal('<span>foo</span>');
|
||||
});
|
||||
|
||||
it('(<value>) : should be able to set to an empty string', function() {
|
||||
var $elem = $('<span>foo</span>').html('');
|
||||
expect($.html($elem)).to.equal('<span></span>');
|
||||
});
|
||||
|
||||
it('() : of empty cheerio object should return null', function() {
|
||||
expect($().html()).to.be(null);
|
||||
});
|
||||
|
||||
it('(selector) : should return the outerHTML of the selected element', function() {
|
||||
var _$ = $.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 = $.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 = $.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 ignore whitepace option', function() {
|
||||
var $html = $.load('<body><a href="http://yahoo.com">Yahoo</a> <a href="http://google.com">Google</a></body>', { ignoreWhitespace : true });
|
||||
expect($html.html()).to.be('<body><a href="http://yahoo.com">Yahoo</a><a href="http://google.com">Google</a></body>');
|
||||
});
|
||||
|
||||
// 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');
|
||||
// });
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('.clone', function() {
|
||||
|
||||
it('() : should return a copy', function() {
|
||||
var $src = $('<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().length).to.equal(1);
|
||||
expect($elem.parent()[0].type).to.equal('root');
|
||||
expect($elem.text()).to.equal($src.text());
|
||||
$src.text('rofl');
|
||||
expect($elem.text()).to.not.equal($src.text());
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.root', function() {
|
||||
|
||||
it('() : should return a cheerio-wrapped root object', function() {
|
||||
var $html = $.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>');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
190
node_modules/cheerio/test/cheerio.js
generated
vendored
Normal file
190
node_modules/cheerio/test/cheerio.js
generated
vendored
Normal file
@@ -0,0 +1,190 @@
|
||||
var expect = require('expect.js'),
|
||||
_ = require('underscore'),
|
||||
$ = require('../'),
|
||||
fruits = require('./fixtures').fruits;
|
||||
|
||||
// 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('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].name).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].children).to.be.empty();
|
||||
});
|
||||
|
||||
var testAppleSelect = function($apple) {
|
||||
expect($apple).to.have.length(1);
|
||||
$apple = $apple[0];
|
||||
expect($apple.parent.name).to.equal('ul');
|
||||
expect($apple.prev).to.be(null);
|
||||
expect($apple.next.attribs['class']).to.equal('orange');
|
||||
expect($apple.children).to.have.length(1);
|
||||
expect($apple.children[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 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].name).to.equal('ul');
|
||||
});
|
||||
|
||||
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 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].children[0].data).to.equal('Save');
|
||||
});
|
||||
|
||||
it('should be able to select multiple elements: $(".apple, #fruits")', function() {
|
||||
var $elems = $('.apple, #fruits', fruits);
|
||||
expect($elems).to.have.length(2);
|
||||
|
||||
var $apple = _($elems).filter(function(elem) {
|
||||
return elem.attribs['class'] === 'apple';
|
||||
});
|
||||
var $fruits = _($elems).filter(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 $fruitsWithMorePear = $('.pear', fruits).append('<li class="pear">Another Pear!</li>');
|
||||
expect($('#fruits .pear', $fruitsWithMorePear)).to.have.length(2);
|
||||
var $elem = $('#fruits > .pear', $fruitsWithMorePear);
|
||||
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); // []
|
||||
});
|
||||
|
||||
});
|
21
node_modules/cheerio/test/fixtures.js
generated
vendored
Normal file
21
node_modules/cheerio/test/fixtures.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
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>Carrot</li>',
|
||||
'<li>Sweetcorn</li>',
|
||||
'</ul>'
|
||||
].join('');
|
||||
|
||||
exports.food = [
|
||||
'<ul id="food">',
|
||||
exports.fruits,
|
||||
exports.vegetables,
|
||||
'</ul>'
|
||||
].join('');
|
2
node_modules/cheerio/test/mocha.opts
generated
vendored
Normal file
2
node_modules/cheerio/test/mocha.opts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
--reporter list
|
||||
--growl
|
336
node_modules/cheerio/test/parse.js
generated
vendored
Normal file
336
node_modules/cheerio/test/parse.js
generated
vendored
Normal file
@@ -0,0 +1,336 @@
|
||||
var expect = require('expect.js'),
|
||||
parse = require('../lib/parse');
|
||||
|
||||
|
||||
// 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)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.name).to.equal('html');
|
||||
expect(tag.children).to.be.empty();
|
||||
});
|
||||
|
||||
it('should handle sibling tags: ' + siblings, function() {
|
||||
var dom = parse.evaluate(siblings),
|
||||
h2 = dom[0],
|
||||
p = dom[1];
|
||||
|
||||
expect(dom).to.have.length(2);
|
||||
expect(h2.name).to.equal('h2');
|
||||
expect(p.name).to.equal('p');
|
||||
});
|
||||
|
||||
it('should handle single tags: ' + single, function() {
|
||||
var tag = parse.evaluate(single)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.name).to.equal('br');
|
||||
expect(tag.children).to.be.empty();
|
||||
});
|
||||
|
||||
it('should handle malformatted single tags: ' + singleWrong, function() {
|
||||
var tag = parse.evaluate(singleWrong)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.name).to.equal('br');
|
||||
expect(tag.children).to.be.empty();
|
||||
});
|
||||
|
||||
it('should handle tags with children: ' + children, function() {
|
||||
var tag = parse.evaluate(children)[0];
|
||||
expect(tag.type).to.equal('tag');
|
||||
expect(tag.name).to.equal('html');
|
||||
expect(tag.children).to.be.ok();
|
||||
expect(tag.children).to.have.length(1);
|
||||
});
|
||||
|
||||
it('should handle tags with children: ' + li, function() {
|
||||
var tag = parse.evaluate(li)[0];
|
||||
expect(tag.children).to.have.length(1);
|
||||
expect(tag.children[0].data).to.equal('Durian');
|
||||
});
|
||||
|
||||
it('should handle tags with attributes: ' + attributes, function() {
|
||||
var attrs = parse.evaluate(attributes)[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)[0].attribs;
|
||||
expect(attrs).to.be.ok();
|
||||
expect(attrs.disabled).to.equal('');
|
||||
});
|
||||
|
||||
it('should handle comments: ' + comment, function() {
|
||||
var elem = parse.evaluate(comment)[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)[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)[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)[0];
|
||||
expect(script_.type).to.equal('script');
|
||||
expect(script_.name).to.equal('script');
|
||||
expect(script_.attribs.type).to.equal('text/javascript');
|
||||
expect(script_.children).to.have.length(1);
|
||||
expect(script_.children[0].type).to.equal('text');
|
||||
expect(script_.children[0].data).to.equal('alert("hi world!");');
|
||||
});
|
||||
|
||||
it('should handle style tags: ' + style, function() {
|
||||
var style_ = parse.evaluate(style)[0];
|
||||
expect(style_.type).to.equal('style');
|
||||
expect(style_.name).to.equal('style');
|
||||
expect(style_.attribs.type).to.equal('text/css');
|
||||
expect(style_.children).to.have.length(1);
|
||||
expect(style_.children[0].type).to.equal('text');
|
||||
expect(style_.children[0].data).to.equal(' h2 { color:blue; } ');
|
||||
});
|
||||
|
||||
it('should handle directives: ' + directive, function() {
|
||||
var elem = parse.evaluate(directive)[0];
|
||||
expect(elem.type).to.equal('directive');
|
||||
expect(elem.data).to.equal('!doctype html');
|
||||
expect(elem.name).to.equal('!doctype');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.connect', function() {
|
||||
|
||||
var create = function(html) {
|
||||
var dom = parse.evaluate(html);
|
||||
return parse.connect(dom);
|
||||
};
|
||||
|
||||
it('should fill in empty attributes: ' + basic, function() {
|
||||
var tag = create(basic)[0];
|
||||
|
||||
// Should exist but be null
|
||||
expect(tag.parent).to.be(null);
|
||||
expect(tag.next).to.be(null);
|
||||
expect(tag.prev).to.be(null);
|
||||
|
||||
// Should exist but be empty
|
||||
expect(tag.children).to.be.empty();
|
||||
expect(tag.attribs).to.be.ok();
|
||||
});
|
||||
|
||||
it('should should fill in empty attributes for scripts: ' + scriptEmpty, function() {
|
||||
var script = create(scriptEmpty)[0];
|
||||
|
||||
// Should exist but be null
|
||||
expect(script.parent).to.be(null);
|
||||
expect(script.next).to.be(null);
|
||||
expect(script.prev).to.be(null);
|
||||
|
||||
// Should exist but be empty
|
||||
expect(script.children).to.be.empty();
|
||||
expect(script.attribs).to.be.ok();
|
||||
});
|
||||
|
||||
it('should should fill in empty attributes for styles: ' + styleEmpty, function() {
|
||||
var style = create(styleEmpty)[0];
|
||||
|
||||
// Should exist but be null
|
||||
expect(style.parent).to.be(null);
|
||||
expect(style.next).to.be(null);
|
||||
expect(style.prev).to.be(null);
|
||||
|
||||
// Should exist but be empty
|
||||
expect(style.children).to.be.empty();
|
||||
expect(style.attribs).to.be.ok();
|
||||
});
|
||||
|
||||
it('should have next and prev siblings: ' + siblings, function() {
|
||||
var dom = create(siblings),
|
||||
h2 = dom[0],
|
||||
p = dom[1];
|
||||
|
||||
// No parents
|
||||
expect(h2.parent).to.be(null);
|
||||
expect(p.parent).to.be(null);
|
||||
|
||||
// Neighbors
|
||||
expect(h2.next.name).to.equal('p');
|
||||
expect(p.prev.name).to.equal('h2');
|
||||
|
||||
// Should exist but be empty
|
||||
expect(h2.children).to.be.empty();
|
||||
expect(h2.attribs).to.be.ok();
|
||||
expect(p.children).to.be.empty();
|
||||
expect(p.attribs).to.be.ok();
|
||||
});
|
||||
|
||||
it('should connect child with parent: ' + children, function() {
|
||||
var html = create(children)[0],
|
||||
br = html.children[0];
|
||||
|
||||
// html has 1 child and it's <br>
|
||||
expect(html.children).to.have.length(1);
|
||||
expect(html.children[0].name).to.equal('br');
|
||||
|
||||
// br's parent is html
|
||||
expect(br.parent.name).to.equal('html');
|
||||
});
|
||||
|
||||
it('should fill in some empty attributes for comments: ' + comment, function() {
|
||||
var elem = create(comment)[0];
|
||||
|
||||
// Should exist but be null
|
||||
expect(elem.parent).to.be(null);
|
||||
expect(elem.next).to.be(null);
|
||||
expect(elem.prev).to.be(null);
|
||||
|
||||
// Should not exist at all
|
||||
expect(elem.children).to.not.be.ok();
|
||||
expect(elem.attribs).to.not.be.ok();
|
||||
});
|
||||
|
||||
it('should fill in some empty attributes for text: ' + text, function() {
|
||||
var text = create(text)[0];
|
||||
|
||||
// Should exist but be null
|
||||
expect(text.parent).to.be(null);
|
||||
expect(text.next).to.be(null);
|
||||
expect(text.prev).to.be(null);
|
||||
|
||||
// Should not exist at all
|
||||
expect(text.children).to.not.be.ok();
|
||||
expect(text.attribs).to.not.be.ok();
|
||||
});
|
||||
|
||||
it('should fill in some empty attributes for directives: ' + directive, function() {
|
||||
var elem = create(directive)[0];
|
||||
|
||||
// Should exist but be null
|
||||
expect(elem.parent).to.be(null);
|
||||
expect(elem.next).to.be(null);
|
||||
expect(elem.prev).to.be(null);
|
||||
|
||||
// Should not exist at all
|
||||
expect(elem.children).to.not.be.ok();
|
||||
expect(elem.attribs).to.not.be.ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('.parse', function() {
|
||||
|
||||
// root test utility
|
||||
function rootTest(root) {
|
||||
expect(root.name).to.equal('root');
|
||||
|
||||
// Should exist but be null
|
||||
expect(root.next).to.be(null);
|
||||
expect(root.prev).to.be(null);
|
||||
expect(root.parent).to.be(null);
|
||||
|
||||
var child = root.children[0];
|
||||
expect(child.parent).to.equal(root);
|
||||
}
|
||||
|
||||
it('should add root to: ' + basic, function() {
|
||||
var root = parse(basic);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(1);
|
||||
expect(root.children[0].name).to.equal('html');
|
||||
});
|
||||
|
||||
it('should add root to: ' + siblings, function() {
|
||||
var root = parse(siblings);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(2);
|
||||
expect(root.children[0].name).to.equal('h2');
|
||||
expect(root.children[1].name).to.equal('p');
|
||||
expect(root.children[1].parent.name).to.equal('root');
|
||||
});
|
||||
|
||||
it('should add root to: ' + comment, function() {
|
||||
var root = parse(comment);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(1);
|
||||
expect(root.children[0].type).to.equal('comment');
|
||||
});
|
||||
|
||||
it('should add root to: ' + text, function() {
|
||||
var root = parse(text);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(1);
|
||||
expect(root.children[0].type).to.equal('text');
|
||||
});
|
||||
|
||||
it('should add root to: ' + scriptEmpty, function() {
|
||||
var root = parse(scriptEmpty);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(1);
|
||||
expect(root.children[0].type).to.equal('script');
|
||||
});
|
||||
|
||||
it('should add root to: ' + styleEmpty, function() {
|
||||
var root = parse(styleEmpty);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(1);
|
||||
expect(root.children[0].type).to.equal('style');
|
||||
});
|
||||
|
||||
it('should add root to: ' + directive, function() {
|
||||
var root = parse(directive);
|
||||
rootTest(root);
|
||||
expect(root.children).to.have.length(1);
|
||||
expect(root.children[0].type).to.equal('directive');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
53
node_modules/cheerio/test/render.js
generated
vendored
Normal file
53
node_modules/cheerio/test/render.js
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
var expect = require('expect.js'),
|
||||
parse = require('../lib/parse'),
|
||||
render = require('../lib/render');
|
||||
|
||||
var html = function(str, options) {
|
||||
options = options || {};
|
||||
var dom = parse(str, options);
|
||||
return render(dom);
|
||||
};
|
||||
|
||||
describe('render', function() {
|
||||
|
||||
describe('(html)', function() {
|
||||
|
||||
it('should render <br /> tags correctly', function(done) {
|
||||
var str = '<br />';
|
||||
expect(html(str)).to.equal('<br>');
|
||||
done();
|
||||
});
|
||||
|
||||
it('should shorten the "checked" attribute when it contains the value "checked"', function(done) {
|
||||
var str = '<input checked/>';
|
||||
expect(html(str)).to.equal('<input checked>');
|
||||
done();
|
||||
});
|
||||
|
||||
it('should not shorten the "name" attribute when it contains the value "name"', function(done) {
|
||||
var str = '<input name="name"/>';
|
||||
expect(html(str)).to.equal('<input name="name">');
|
||||
done();
|
||||
});
|
||||
|
||||
it('should render comments correctly', function(done) {
|
||||
var str = '<!-- comment -->';
|
||||
expect(html(str)).to.equal('<!-- comment -->');
|
||||
done();
|
||||
});
|
||||
|
||||
it('should render whitespace by default', function(done) {
|
||||
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah</a>';
|
||||
expect(html(str)).to.equal(str);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should ignore whitespace if specified', function(done) {
|
||||
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>';
|
||||
expect(html(str, {ignoreWhitespace: true})).to.equal('<a href="./haha.html">hi</a><a href="./blah.html">blah </a>');
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
33
node_modules/cheerio/test/utilities.js
generated
vendored
Normal file
33
node_modules/cheerio/test/utilities.js
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
var expect = require('expect.js'),
|
||||
$ = require('../'),
|
||||
food = require('./fixtures').food;
|
||||
|
||||
describe('utility methods', function() {
|
||||
|
||||
describe('.contains', function() {
|
||||
|
||||
it('(container, contained) : should correctly detect the provided element', function() {
|
||||
var $food = $(food);
|
||||
var $fruits = $food.find('#fruits');
|
||||
var $apple = $fruits.find('.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 $food = $(food);
|
||||
var $fruits = $food.find('#fruits');
|
||||
var $vegetables = $food.find('#vegetables');
|
||||
var $apple = $fruits.find('.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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
Reference in New Issue
Block a user