Skip to content

Commit ffe0485

Browse files
committed
feat(core dom): Add is_input method.
Add "is_input" to test if a element is of input type. This is basically the same as $(":input") from Sizzle/jQuery.
1 parent 6246aed commit ffe0485

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/core/dom.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ const is_visible = (el) => {
9292
return el.offsetWidth > 0 && el.offsetHeight > 0;
9393
};
9494

95+
/**
96+
* Test, if a element is a input-type element.
97+
*
98+
* This is taken from Sizzle/jQuery at:
99+
* https://github.com/jquery/sizzle/blob/f2a2412e5e8a5d9edf168ae3b6633ac8e6bd9f2e/src/sizzle.js#L139
100+
* https://github.com/jquery/sizzle/blob/f2a2412e5e8a5d9edf168ae3b6633ac8e6bd9f2e/src/sizzle.js#L1773
101+
*
102+
* @param {Node} el - The DOM node to test.
103+
* @returns {Boolean} - True if the element is a input-type element.
104+
*/
105+
const is_input = (el) => {
106+
const re_input = /^(?:input|select|textarea|button)$/i;
107+
return re_input.test(el.nodeName);
108+
};
109+
95110
/**
96111
* Return all direct parents of ``el`` matching ``selector``.
97112
* This matches against all parents but not the element itself.
@@ -322,6 +337,7 @@ const dom = {
322337
get_parents: get_parents,
323338
acquire_attribute: acquire_attribute,
324339
is_visible: is_visible,
340+
is_input: is_input,
325341
create_from_string: create_from_string,
326342
get_css_value: get_css_value,
327343
find_scroll_container: find_scroll_container,

src/core/dom.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,20 @@ describe("core.dom tests", () => {
444444
});
445445
});
446446

447+
describe("is_input", () => {
448+
it("checks, if an element is of type input or not.", (done) => {
449+
expect(dom.is_input(document.createElement("input"))).toBe(true);
450+
expect(dom.is_input(document.createElement("select"))).toBe(true);
451+
expect(dom.is_input(document.createElement("textarea"))).toBe(true);
452+
expect(dom.is_input(document.createElement("button"))).toBe(true);
453+
454+
expect(dom.is_input(document.createElement("form"))).toBe(false);
455+
expect(dom.is_input(document.createElement("div"))).toBe(false);
456+
457+
done();
458+
});
459+
});
460+
447461
describe("create_from_string", () => {
448462
it("Creates a DOM element from a string", (done) => {
449463
const res = dom.create_from_string(`

0 commit comments

Comments
 (0)