');
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 = $('
fruits
');
// 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('
Another Pear!');
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
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 = '
',
expected = '
',
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 = '
',
// since parsing done without xmlMode flag, all tags converted to lowercase
expectedXml = '
',
expectedNoXml = '
',
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 = '
Some test',
expectedHtml = '
Copyright © 2003-2014
',
expectedXml = '
Copyright © 2003-2014
',
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('
');
expect($c).to.be.a(Function);
});
describe('prototype extensions', function() {
it('should honor extensions defined on `prototype` property', function() {
var $c = $.load('
');
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('
');
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('
');
var $b = $.load('
');
$a.prototype.foo = function() {};
expect($b('div').foo).to.be(undefined);
});
});
});
});