diff options
| author | Igor Minar | 2011-03-31 01:17:34 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-03-31 01:17:34 -0700 |
| commit | 56c00800c78d3d896fa6cb97ab97b974805152c4 (patch) | |
| tree | 9eea63f150ad3b67527c6ae3805883912c4ba1ac | |
| parent | 15ec78f5eff3f8fa74714fe10986be094915c800 (diff) | |
| download | angular.js-56c00800c78d3d896fa6cb97ab97b974805152c4.tar.bz2 | |
fix jqLite#parent to be compatible with jQuery
our original implementation doesn't work with
document fragments on IE
- tests were added to cover missing cases
| -rw-r--r-- | src/jqLite.js | 4 | ||||
| -rw-r--r-- | test/jqLiteSpec.js | 19 |
2 files changed, 21 insertions, 2 deletions
diff --git a/src/jqLite.js b/src/jqLite.js index 78fc1655..d16ce19e 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -371,8 +371,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) { diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 346017f1..2fc670ce 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -323,11 +323,30 @@ describe('jqLite', function(){ }); }); 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(){ |
