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

Commit c099539

Browse files
KeyamoonIgorMinar
authored andcommitted
fix(jqLite): make next() ignore non-element nodes
next() is supposed to return the next sibling *element* so it should ignore text nodes. To achieve this, nextElementSibling() should be used instead of nextSibling().
1 parent de6cc28 commit c099539

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/jqLite.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,16 @@ forEach({
717717
},
718718

719719
next: function(element) {
720-
return element.nextSibling;
720+
if (element.nextElementSibling) {
721+
return element.nextElementSibling;
722+
}
723+
724+
// IE8 doesn't have nextElementSibling
725+
var elm = element.nextSibling;
726+
while (elm != null && elm.nodeType !== 1) {
727+
elm = elm.nextSibling;
728+
}
729+
return elm;
721730
},
722731

723732
find: function(element, selector) {

test/jqLiteSpec.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
describe('jqLite', function() {
32
var scope, a, b, c;
43

@@ -1068,6 +1067,14 @@ describe('jqLite', function() {
10681067
var i = element.find('i');
10691068
expect(b.next()).toJqEqual([i]);
10701069
});
1070+
1071+
1072+
it('should ignore non-element siblings', function() {
1073+
var element = jqLite('<div><b>b</b>TextNode<!-- comment node --><i>i</i></div>');
1074+
var b = element.find('b');
1075+
var i = element.find('i');
1076+
expect(b.next()).toJqEqual([i]);
1077+
});
10711078
});
10721079

10731080

0 commit comments

Comments
 (0)