File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ const logger = logging.getLogger("core dom");
6
6
const DATA_PREFIX = "__patternslib__data_prefix__" ;
7
7
const DATA_STYLE_DISPLAY = "__patternslib__style__display" ;
8
8
9
+ const INPUT_SELECTOR = "input, select, textarea, button" ;
10
+
9
11
/**
10
12
* Return an array of DOM nodes.
11
13
*
@@ -571,17 +573,25 @@ const find_form = (el) => {
571
573
const form =
572
574
el . closest ( ".pat-subform" ) || // Special Patternslib subform concept has precedence.
573
575
el . form ||
574
- el . querySelector ( "input, select, textarea, button" ) ?. form ||
576
+ el . querySelector ( INPUT_SELECTOR ) ?. form ||
575
577
el . closest ( "form" ) ;
576
578
return form ;
577
579
} ;
578
580
581
+ /**
582
+ * Find any input type.
583
+ */
584
+ const find_inputs = ( el ) => {
585
+ return querySelectorAllAndMe ( el , INPUT_SELECTOR ) ;
586
+ } ;
587
+
579
588
const dom = {
580
589
toNodeArray : toNodeArray ,
581
590
querySelectorAllAndMe : querySelectorAllAndMe ,
582
591
wrap : wrap ,
583
592
hide : hide ,
584
593
show : show ,
594
+ find_inputs : find_inputs ,
585
595
find_parents : find_parents ,
586
596
find_scoped : find_scoped ,
587
597
get_parents : get_parents ,
Original file line number Diff line number Diff line change @@ -1017,3 +1017,44 @@ describe("find_form", function () {
1017
1017
expect ( dom . find_form ( el ) ) . toBe ( subform ) ;
1018
1018
} ) ;
1019
1019
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments