Skip to content

Commit 0c8c715

Browse files
committed
tech(core dom): toNodeArray - filter for instances of Node only.
1 parent 0549076 commit 0c8c715

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/core/dom.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ const document_ready = (fn) => {
3737
/**
3838
* Return an array of DOM nodes.
3939
*
40-
* @param {Node|NodeList|jQuery} nodes - The DOM node to start the search from.
40+
* @param {Node|NodeList|jQuery} nodes - The object which whould be returned as array.
4141
*
4242
* @returns {Array} - An array of DOM nodes.
4343
*/
4444
const toNodeArray = (nodes) => {
45-
if (nodes.jquery || nodes instanceof NodeList) {
46-
// jQuery or document.querySelectorAll
45+
if (nodes?.jquery || nodes instanceof NodeList) {
4746
nodes = [...nodes];
4847
} else if (nodes instanceof Array === false) {
4948
nodes = [nodes];
5049
}
50+
// Filter for DOM nodes only.
51+
nodes = nodes.filter(node => node instanceof Node)
5152
return nodes;
5253
};
5354

src/core/dom.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ describe("core.dom tests", () => {
101101
expect(testee.length).toBe(2);
102102

103103
const ret = dom.toNodeArray(testee);
104+
104105
expect(ret.jquery).toBeFalsy();
105106
expect(ret.length).toBe(2);
107+
106108
expect(ret[0]).toBe(el1);
109+
expect(ret[0].jquery).toBeFalsy();
110+
expect(ret[0] instanceof Node).toBe(true);
111+
107112
expect(ret[1]).toBe(el2);
113+
expect(ret[1].jquery).toBeFalsy();
114+
expect(ret[1] instanceof Node).toBe(true);
108115

109116
done();
110117
});
@@ -139,6 +146,25 @@ describe("core.dom tests", () => {
139146

140147
done();
141148
});
149+
150+
it("returns an empty array, if nothing was passed", (done) => {
151+
const ret = dom.toNodeArray();
152+
expect(ret.length).toBe(0);
153+
expect(ret instanceof Array).toBe(true);
154+
155+
done();
156+
});
157+
158+
it("returns only DOM Nodes", (done) => {
159+
const el = document.body;
160+
const txt = document.createTextNode("okay");
161+
const ret = dom.toNodeArray([1, false, txt, "okay", el], false);
162+
expect(ret.length).toBe(2);
163+
expect(ret[0]).toBe(txt);
164+
expect(ret[1]).toBe(el);
165+
166+
done();
167+
});
142168
});
143169

144170
describe("querySelectorAllAndMe tests", () => {

0 commit comments

Comments
 (0)