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

Commit febb4c1

Browse files
fix(jqLite): children() should only return elements
The jQuery implementation of children only returns child nodes of the given element that are elements themselves. The previous jqLite implementation was returning all nodes except those that are text nodes. Use jQLite.contents() to get all the child nodes. The jQuery implementation of contents returns [] if the object has no child nodes. The previous jqLite implementation was returning undefined, causing a stack overflow in test/testabilityPatch.js when it tried to `cleanup()` a window object. The testabilityPatch was incorrectly using children() rather than contents() inside cleanup() to iterate down through all the child nodes of the element to clean up.
1 parent 76a6047 commit febb4c1

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/jqLite.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,14 @@ forEach({
647647
children: function(element) {
648648
var children = [];
649649
forEach(element.childNodes, function(element){
650-
if (element.nodeName != '#text')
650+
if (element.nodeType === 1)
651651
children.push(element);
652652
});
653653
return children;
654654
},
655655

656656
contents: function(element) {
657-
return element.childNodes;
657+
return element.childNodes || [];
658658
},
659659

660660
append: function(element, node) {

test/jqLiteSpec.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,8 @@ describe('jqLite', function() {
924924

925925

926926
describe('children', function() {
927-
it('should select non-text children', function() {
928-
var root = jqLite('<div>').html('before-<div></div>after-<span></span>');
927+
it('should only select element nodes', function() {
928+
var root = jqLite('<div><!-- some comment -->before-<div></div>after-<span></span>');
929929
var div = root.find('div');
930930
var span = root.find('span');
931931
expect(root.children()).toJqEqual([div, span]);
@@ -934,11 +934,12 @@ describe('jqLite', function() {
934934

935935

936936
describe('contents', function() {
937-
it('should select all children nodes', function() {
938-
var root = jqLite('<div>').html('before-<div></div>after-<span></span>');
937+
it('should select all types child nodes', function() {
938+
var root = jqLite('<div><!-- some comment -->before-<div></div>after-<span></span></div>');
939939
var contents = root.contents();
940-
expect(contents.length).toEqual(4);
941-
expect(jqLite(contents[0]).text()).toEqual('before-');
940+
expect(contents.length).toEqual(5);
941+
expect(contents[0].data).toEqual(' some comment ');
942+
expect(contents[1].data).toEqual('before-');
942943
});
943944
});
944945

test/testabilityPatch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function dealoc(obj) {
7878

7979
function cleanup(element) {
8080
element.unbind().removeData();
81-
for ( var i = 0, children = element.children() || []; i < children.length; i++) {
81+
for ( var i = 0, children = element.contents() || []; i < children.length; i++) {
8282
cleanup(jqLite(children[i]));
8383
}
8484
}

0 commit comments

Comments
 (0)