Skip to content

Commit db271ce

Browse files
committed
feat(core dom): Add find_inputs to find all inputs in a node tree, including the node itself.
1 parent e73a98e commit db271ce

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/core/dom.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const logger = logging.getLogger("core dom");
66
const DATA_PREFIX = "__patternslib__data_prefix__";
77
const DATA_STYLE_DISPLAY = "__patternslib__style__display";
88

9+
const INPUT_SELECTOR = "input, select, textarea, button";
10+
911
/**
1012
* Return an array of DOM nodes.
1113
*
@@ -571,17 +573,25 @@ const find_form = (el) => {
571573
const form =
572574
el.closest(".pat-subform") || // Special Patternslib subform concept has precedence.
573575
el.form ||
574-
el.querySelector("input, select, textarea, button")?.form ||
576+
el.querySelector(INPUT_SELECTOR)?.form ||
575577
el.closest("form");
576578
return form;
577579
};
578580

581+
/**
582+
* Find any input type.
583+
*/
584+
const find_inputs = (el) => {
585+
return querySelectorAllAndMe(el, INPUT_SELECTOR);
586+
};
587+
579588
const dom = {
580589
toNodeArray: toNodeArray,
581590
querySelectorAllAndMe: querySelectorAllAndMe,
582591
wrap: wrap,
583592
hide: hide,
584593
show: show,
594+
find_inputs: find_inputs,
585595
find_parents: find_parents,
586596
find_scoped: find_scoped,
587597
get_parents: get_parents,

src/core/dom.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,3 +1017,44 @@ describe("find_form", function () {
10171017
expect(dom.find_form(el)).toBe(subform);
10181018
});
10191019
});
1020+
1021+
describe("find_inputs", () => {
1022+
it("finds an input within a node structure.", (done) => {
1023+
const wrapper = document.createElement("div");
1024+
wrapper.innerHTML = `
1025+
<p>hello</p>
1026+
<fieldset>
1027+
<div>
1028+
<input type="text" />
1029+
</div>
1030+
<select>
1031+
<option>1</option>
1032+
<option>2</option>
1033+
</select>
1034+
<textarea></textarea>
1035+
</fieldset>
1036+
<button>Click me!</button>
1037+
`;
1038+
const inputs = dom.find_inputs(wrapper);
1039+
const input_types = inputs.map((node) => node.nodeName);
1040+
1041+
expect(inputs.length).toBe(4);
1042+
expect(input_types.includes("INPUT")).toBeTruthy();
1043+
expect(input_types.includes("SELECT")).toBeTruthy();
1044+
expect(input_types.includes("TEXTAREA")).toBeTruthy();
1045+
expect(input_types.includes("BUTTON")).toBeTruthy();
1046+
1047+
done();
1048+
});
1049+
1050+
it("finds the input on the node itself.", (done) => {
1051+
const wrapper = document.createElement("input");
1052+
const inputs = dom.find_inputs(wrapper);
1053+
const input_types = inputs.map((node) => node.nodeName);
1054+
1055+
expect(inputs.length).toBe(1);
1056+
expect(input_types.includes("INPUT")).toBeTruthy();
1057+
1058+
done();
1059+
});
1060+
});

0 commit comments

Comments
 (0)