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

Commit 76a6047

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 b6b7c5a commit 76a6047

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
@@ -718,7 +718,16 @@ forEach({
718718
},
719719

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

724733
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

@@ -1073,6 +1072,14 @@ describe('jqLite', function() {
10731072
var i = element.find('i');
10741073
expect(b.next()).toJqEqual([i]);
10751074
});
1075+
1076+
1077+
it('should ignore non-element siblings', function() {
1078+
var element = jqLite('<div><b>b</b>TextNode<!-- comment node --><i>i</i></div>');
1079+
var b = element.find('b');
1080+
var i = element.find('i');
1081+
expect(b.next()).toJqEqual([i]);
1082+
});
10761083
});
10771084

10781085

0 commit comments

Comments
 (0)