Skip to content

Commit 132e42b

Browse files
committed
feat(core dom): Add method is_button.
dom.is_button tests, if an element is a button like element. Button like elements are the following: button, button[type=button], button[type=submit], input[type=image], input[type=button], input[type=reset], input[type=submit]
1 parent 8efe802 commit 132e42b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/core/dom.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ const is_input = (el) => {
141141
return re_input.test(el.nodeName);
142142
};
143143

144+
/**
145+
* Test, if a element is a button-like input type.
146+
*
147+
* @param {Node} el - The DOM node to test.
148+
* @returns {Boolean} - True if the element is a input-type element.
149+
*/
150+
const is_button = (el) => {
151+
return el.matches(`
152+
button,
153+
input[type=image],
154+
input[type=button],
155+
input[type=reset],
156+
input[type=submit]
157+
`);
158+
};
159+
160+
161+
144162
/**
145163
* Return all direct parents of ``el`` matching ``selector``.
146164
* This matches against all parents but not the element itself.
@@ -613,6 +631,7 @@ const dom = {
613631
acquire_attribute: acquire_attribute,
614632
is_visible: is_visible,
615633
is_input: is_input,
634+
is_button: is_button,
616635
create_from_string: create_from_string,
617636
get_css_value: get_css_value,
618637
find_scroll_container: find_scroll_container,

src/core/dom.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,47 @@ describe("core.dom tests", () => {
547547
});
548548
});
549549

550+
describe("is_button", () => {
551+
it("checks, if an element is a button-like element or not.", (done) => {
552+
553+
const button = document.createElement("button");
554+
const button_button = document.createElement("button");
555+
button_button.setAttribute("type", "button");
556+
const button_submit = document.createElement("button");
557+
button_submit.setAttribute("type", "submit");
558+
559+
const input_button = document.createElement("input");
560+
input_button.setAttribute("type", "button");
561+
const input_submit = document.createElement("input");
562+
input_submit.setAttribute("type", "submit");
563+
const input_reset = document.createElement("input");
564+
input_reset.setAttribute("type", "reset");
565+
const input_image = document.createElement("input");
566+
input_image.setAttribute("type", "image");
567+
568+
expect(dom.is_button(button)).toBe(true);
569+
expect(dom.is_button(button_button)).toBe(true);
570+
expect(dom.is_button(button_submit)).toBe(true);
571+
expect(dom.is_button(input_button)).toBe(true);
572+
expect(dom.is_button(input_image)).toBe(true);
573+
expect(dom.is_button(input_reset)).toBe(true);
574+
expect(dom.is_button(input_submit)).toBe(true);
575+
576+
const input_text = document.createElement("input");
577+
input_text.setAttribute("type", "text");
578+
579+
expect(dom.is_button(input_text)).toBe(false);
580+
expect(dom.is_button(document.createElement("input"))).toBe(false);
581+
expect(dom.is_button(document.createElement("select"))).toBe(false);
582+
expect(dom.is_button(document.createElement("textarea"))).toBe(false);
583+
expect(dom.is_button(document.createElement("form"))).toBe(false);
584+
expect(dom.is_button(document.createElement("div"))).toBe(false);
585+
586+
done();
587+
});
588+
});
589+
590+
550591
describe("create_from_string", () => {
551592
it("Creates a DOM element from a string", (done) => {
552593
const res = dom.create_from_string(`

0 commit comments

Comments
 (0)