Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

jqLlite fixes #319

Merged
merged 4 commits into from
Mar 31, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ forEach({

append: function(element, node) {
forEach(new JQLite(node), function(child){
element.appendChild(child);
if (element.nodeType === 1)
element.appendChild(child);
});
},

Expand Down Expand Up @@ -371,8 +372,8 @@ forEach({
},

parent: function(element) {
// in IE it returns undefined, but we need differentiate it from functions which have no return
return element.parentNode || null;
var parent = element.parentNode;
return parent && parent.nodeType !== 11 ? parent : null;
},

next: function(element) {
Expand Down
104 changes: 101 additions & 3 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
describe('jqLite', function(){
var scope;
var a, b, c;
var scope, a, b, c;

beforeEach(function(){
a = jqLite('<div>A</div>')[0];
b = jqLite('<div>B</div>')[0];
c = jqLite('<div>C</div>')[0];
});


beforeEach(function(){
scope = angular.scope();
this.addMatchers({
Expand All @@ -28,27 +29,53 @@ describe('jqLite', function(){
});
});


afterEach(function(){
dealoc(a);
dealoc(b);
dealoc(c);
});


describe('construction', function(){
it('should allow construction with text node', function(){
var text = a.firstChild;
var selected = jqLite(text);
expect(selected.length).toEqual(1);
expect(selected[0]).toEqual(text);
});


it('should allow construction with html', function(){
var nodes = jqLite('<div>1</div><span>2</span>');
expect(nodes.length).toEqual(2);
expect(nodes[0].innerHTML).toEqual('1');
expect(nodes[1].innerHTML).toEqual('2');
});


it('should allow creation of comment tags', function() {
var nodes = jqLite('<!-- foo -->');
expect(nodes.length).toBe(1);
expect(nodes[0].nodeType).toBe(8);
});


it('should allow creation of script tags', function() {
var nodes = jqLite('<script></script>');
expect(nodes.length).toBe(1);
expect(nodes[0].tagName.toUpperCase()).toBe('SCRIPT');
});


it('should wrap document fragment', function() {
var fragment = jqLite(document.createDocumentFragment());
expect(fragment.length).toBe(1);
expect(fragment[0].nodeType).toBe(11);
});
});


describe('scope', function() {
it('should retrieve scope attached to the current element', function() {
var element = jqLite('<i>foo</i>');
Expand All @@ -57,6 +84,7 @@ describe('jqLite', function(){
dealoc(element);
});


it('should walk up the dom to find scope', function() {
var element = jqLite('<ul><li><p><b>deep deep</b><p></li></ul>');
var deepChild = jqLite(element[0].getElementsByTagName('b')[0]);
Expand All @@ -65,6 +93,7 @@ describe('jqLite', function(){
dealoc(element);
});


it('should return undefined when no scope was found', function() {
var element = jqLite('<ul><li><p><b>deep deep</b><p></li></ul>');
var deepChild = jqLite(element[0].getElementsByTagName('b')[0]);
Expand All @@ -73,6 +102,7 @@ describe('jqLite', function(){
});
});


describe('data', function(){
it('should set and get ande remove data', function(){
var selected = jqLite([a, b, c]);
Expand All @@ -96,6 +126,7 @@ describe('jqLite', function(){
});
});


describe('attr', function(){
it('shoul read wirite and remove attr', function(){
var selector = jqLite([a, b]);
Expand All @@ -117,13 +148,18 @@ describe('jqLite', function(){
expect(jqLite(b).attr('prop')).toBeFalsy();
});
});


describe('class', function(){

describe('hasClass', function(){
it('should check class', function(){
var selector = jqLite([a, b]);
expect(selector.hasClass('abc')).toEqual(false);
});
});


describe('addClass', function(){
it('should allow adding of class', function(){
var selector = jqLite([a, b]);
Expand All @@ -132,6 +168,8 @@ describe('jqLite', function(){
expect(jqLite(b).hasClass('abc')).toEqual(true);
});
});


describe('toggleClass', function(){
it('should allow toggling of class', function(){
var selector = jqLite([a, b]);
Expand All @@ -153,6 +191,8 @@ describe('jqLite', function(){

});
});


describe('removeClass', function(){
it('should allow removal of class', function(){
var selector = jqLite([a, b]);
Expand All @@ -163,6 +203,8 @@ describe('jqLite', function(){
});
});
});


describe('css', function(){
it('should set and read css', function(){
var selector = jqLite([a, b]);
Expand All @@ -184,12 +226,15 @@ describe('jqLite', function(){
expect(jqLite(b).css('prop')).toBeFalsy();
});
});


describe('text', function(){
it('should return null on empty', function(){
expect(jqLite().length).toEqual(0);
expect(jqLite().text()).toEqual('');
});


it('should read/write value', function(){
var element = jqLite('<div>abc</div>');
expect(element.length).toEqual(1);
Expand All @@ -199,6 +244,8 @@ describe('jqLite', function(){
expect(element.text()).toEqual('xyz');
});
});


describe('val', function(){
it('should read, write value', function(){
var input = jqLite('<input type="text"/>');
Expand All @@ -207,12 +254,15 @@ describe('jqLite', function(){
expect(input.val()).toEqual('abc');
});
});


describe('html', function(){
it('should return null on empty', function(){
expect(jqLite().length).toEqual(0);
expect(jqLite().html()).toEqual(null);
});


it('should read/write value', function(){
var element = jqLite('<div>abc</div>');
expect(element.length).toEqual(1);
Expand All @@ -223,6 +273,7 @@ describe('jqLite', function(){
});
});


describe('bind', function(){
it('should bind to window on hashchange', function(){
if (jqLite.fn) return; // don't run in jQuery
Expand Down Expand Up @@ -253,6 +304,7 @@ describe('jqLite', function(){
dealoc(jWindow);
});


it('should bind to all elements and return functions', function(){
var selected = jqLite([a, b]);
var log = '';
Expand All @@ -266,20 +318,25 @@ describe('jqLite', function(){
});
});


describe('replaceWith', function(){
it('should replaceWith', function(){
var root = jqLite('<div>').html('before-<div></div>after');
var div = root.find('div');
expect(div.replaceWith('<span>span-</span><b>bold-</b>')).toEqual(div);
expect(root.text()).toEqual('before-span-bold-after');
});


it('should replaceWith text', function(){
var root = jqLite('<div>').html('before-<div></div>after');
var div = root.find('div');
expect(div.replaceWith('text-')).toEqual(div);
expect(root.text()).toEqual('before-text-after');
});
});


describe('children', function(){
it('should select non-text children', function(){
var root = jqLite('<div>').html('before-<div></div>after-<span></span>');
Expand All @@ -288,6 +345,8 @@ describe('jqLite', function(){
expect(root.children()).toJqEqual([div, span]);
});
});


describe('append', function(){
it('should append', function(){
var root = jqLite('<div>');
Expand All @@ -299,7 +358,14 @@ describe('jqLite', function(){
expect(root.append('text')).toEqual(root);
expect(root.html()).toEqual('text');
});
it('should not append anything if parent node is not of type element', function() {
var root = jqLite(document.createDocumentFragment());
expect(root.append('<p>foo</p>')).toBe(root);
expect(root.children().length).toBe(0);
});
});


describe('remove', function(){
it('should remove', function(){
var root = jqLite('<div><span>abc</span></div>');
Expand All @@ -308,27 +374,58 @@ describe('jqLite', function(){
expect(root.html()).toEqual('');
});
});


describe('after', function(){
it('should after', function(){
var root = jqLite('<div><span></span></div>');
var span = root.find('span');
expect(span.after('<i></i><b></b>')).toEqual(span);
expect(root.html().toLowerCase()).toEqual('<span></span><i></i><b></b>');
});


it('should allow taking text', function(){
var root = jqLite('<div><span></span></div>');
var span = root.find('span');
span.after('abc');
expect(root.html().toLowerCase()).toEqual('<span></span>abc');
});
});


describe('parent', function(){
it('should return parent or an empty set when no parent', function(){
var parent = jqLite('<div><p>abc</p></div>'),
child = parent.find('p');

expect(parent.parent()).toBeTruthy();
expect(parent.parent().length).toEqual(0);

expect(child.parent().length).toBe(1);
expect(child.parent()[0]).toBe(parent[0]);
});


it('should return empty set when no parent', function(){
var element = jqLite('<div>abc</div>');
expect(element.parent()).toBeTruthy();
expect(element.parent().length).toEqual(0);
});


it('should return empty jqLite object when parent is a document fragment', function() {
//this is quite unfortunate but jQuery 1.5.1 behaves this way
var fragment = document.createDocumentFragment(),
child = jqLite('<p>foo</p>');

fragment.appendChild(child[0]);
expect(child[0].parentNode).toBe(fragment);
expect(child.parent().length).toBe(0);
});
});


describe('next', function(){
it('should return next sibling', function(){
var element = jqLite('<div><b>b</b><i>i</i></div>');
Expand All @@ -337,6 +434,8 @@ describe('jqLite', function(){
expect(b.next()).toJqEqual([i]);
});
});


describe('find', function(){
it('should find child by name', function(){
var root = jqLite('<div><div>text</div></div>');
Expand All @@ -345,5 +444,4 @@ describe('jqLite', function(){
expect(innerDiv.html()).toEqual('text');
});
});

});