Skip to content

Commit e096ed0

Browse files
committed
feat(core dom): Introduce to_element_array.
This method can be passed a single object, a NodeList or an array and will return an array, filtered for DOM elements.
1 parent 0c8c715 commit e096ed0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/core/dom.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ const toNodeArray = (nodes) => {
5252
return nodes;
5353
};
5454

55+
/**
56+
* Return an array of DOM elements.
57+
*
58+
* @param {Node|NodeList|jQuery} nodes - The object which whould be returned as array.
59+
*
60+
* @returns {Array} - An array of DOM elements.
61+
*/
62+
const to_element_array = (nodes) => {
63+
nodes = toNodeArray(nodes);
64+
// Filter for DOM elements only.
65+
nodes = nodes.filter(node => node instanceof Node)
66+
return nodes;
67+
};
68+
5569
/**
5670
* Like querySelectorAll but including the element where it starts from.
5771
* Returns an Array, not a NodeList
@@ -620,6 +634,7 @@ const find_inputs = (el) => {
620634

621635
const dom = {
622636
document_ready: document_ready,
637+
to_element_array: to_element_array,
623638
toNodeArray: toNodeArray,
624639
querySelectorAllAndMe: querySelectorAllAndMe,
625640
wrap: wrap,

src/core/dom.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,85 @@ describe("core.dom tests", () => {
167167
});
168168
});
169169

170+
describe("to_element_array tests", () => {
171+
it("returns an array of DOM elements, if a jQuery object was passed.", (done) => {
172+
const html = document.createElement("div");
173+
html.innerHTML = `
174+
<span id="id1" />
175+
<span id="id2" />
176+
`;
177+
const el1 = html.querySelector("#id1");
178+
const el2 = html.querySelector("#id2");
179+
const testee = $("span", html);
180+
expect(testee.length).toBe(2);
181+
182+
const ret = dom.to_element_array(testee);
183+
184+
expect(ret.jquery).toBeFalsy();
185+
expect(ret.length).toBe(2);
186+
187+
expect(ret[0]).toBe(el1);
188+
expect(ret[0].jquery).toBeFalsy();
189+
expect(ret[0] instanceof Element).toBe(true);
190+
191+
expect(ret[1]).toBe(el2);
192+
expect(ret[1].jquery).toBeFalsy();
193+
expect(ret[1] instanceof Element).toBe(true);
194+
195+
done();
196+
});
197+
198+
it("returns an array of elements, if a NodeList was passed.", (done) => {
199+
const html = document.createElement("div");
200+
html.innerHTML = `
201+
<span id="id1" />
202+
<span id="id2" />
203+
`;
204+
const el1 = html.querySelector("#id1");
205+
const el2 = html.querySelector("#id2");
206+
const testee = html.querySelectorAll("span");
207+
expect(testee.length).toBe(2);
208+
209+
const ret = dom.to_element_array(testee);
210+
expect(ret instanceof NodeList).toBeFalsy();
211+
expect(ret.length).toBe(2);
212+
expect(ret[0]).toBe(el1);
213+
expect(ret[1]).toBe(el2);
214+
215+
done();
216+
});
217+
218+
it("returns an array with a single element, if a single element was passed.", (done) => {
219+
const html = document.createElement("div");
220+
221+
const ret = dom.to_element_array(html);
222+
expect(ret instanceof Array).toBeTruthy();
223+
expect(ret.length).toBe(1);
224+
expect(ret[0]).toBe(html);
225+
226+
done();
227+
});
228+
229+
it("returns an empty array, if nothing was passed", (done) => {
230+
const ret = dom.to_element_array();
231+
expect(ret.length).toBe(0);
232+
expect(ret instanceof Array).toBe(true);
233+
234+
done();
235+
});
236+
237+
it("returns only DOM Elements, no Nodes or others", (done) => {
238+
const el = document.body;
239+
const txt = document.createTextNode("okay");
240+
const ret = dom.to_element_array([1, false, txt, "okay", el]);
241+
expect(ret.length).toBe(1);
242+
expect(ret[0]).toBe(el);
243+
244+
done();
245+
});
246+
});
247+
248+
170249
describe("querySelectorAllAndMe tests", () => {
171250
it("return also starting node if query matches.", (done) => {
172251
const el = document.createElement("div");

0 commit comments

Comments
 (0)