@@ -4,8 +4,14 @@ import events from "./events";
44const DATA_PREFIX = "__patternslib__data_prefix__" ;
55const DATA_STYLE_DISPLAY = "__patternslib__style__display" ;
66
7+ /**
8+ * Return an array of DOM nodes.
9+ *
10+ * @param {Node|NodeList|jQuery } nodes - The DOM node to start the search from.
11+ *
12+ * @returns {Array } - An array of DOM nodes.
13+ */
714const toNodeArray = ( nodes ) => {
8- // Return an array of DOM nodes
915 if ( nodes . jquery || nodes instanceof NodeList ) {
1016 // jQuery or document.querySelectorAll
1117 nodes = [ ...nodes ] ;
@@ -15,10 +21,15 @@ const toNodeArray = (nodes) => {
1521 return nodes ;
1622} ;
1723
24+ /**
25+ * Like querySelectorAll but including the element where it starts from.
26+ * Returns an Array, not a NodeList
27+ *
28+ * @param {Node } el - The DOM node to start the search from.
29+ *
30+ * @returns {Array } - The DOM nodes found.
31+ */
1832const querySelectorAllAndMe = ( el , selector ) => {
19- // Like querySelectorAll but including the element where it starts from.
20- // Returns an Array, not a NodeList
21-
2233 if ( ! el ) {
2334 return [ ] ;
2435 }
@@ -30,16 +41,23 @@ const querySelectorAllAndMe = (el, selector) => {
3041 return all ;
3142} ;
3243
44+ /**
45+ * Wrap a element with a wrapper element.
46+ *
47+ * @param {Node } el - The DOM node to wrap.
48+ */
3349const wrap = ( el , wrapper ) => {
34- // Wrap a element with a wrapper element.
3550 // See: https://stackoverflow.com/a/13169465/1337474
36-
3751 el . parentNode . insertBefore ( wrapper , el ) ;
3852 wrapper . appendChild ( el ) ;
3953} ;
4054
55+ /**
56+ * Hides the element with ``display: none`` and stores the current display value.
57+ *
58+ * @param {Node } el - The DOM node to hide.
59+ */
4160const hide = ( el ) => {
42- // Hides the element with ``display: none``
4361 if ( el . style . display === "none" ) {
4462 // Nothing to do.
4563 return ;
@@ -51,38 +69,65 @@ const hide = (el) => {
5169 el . setAttribute ( "hidden" , "" ) ;
5270} ;
5371
72+ /**
73+ * Shows element by removing ``display: none`` and restoring the display value
74+ * to whatever it was before.
75+ *
76+ * @param {Node } el - The DOM node to show.
77+ */
5478const show = ( el ) => {
55- // Shows element by removing ``display: none`` and restoring the display
56- // value to whatever it was before.
5779 const val = el [ DATA_STYLE_DISPLAY ] || null ;
5880 el . style . display = val ;
5981 delete el [ DATA_STYLE_DISPLAY ] ;
6082 el . removeAttribute ( "hidden" , "" ) ;
6183} ;
6284
85+ /**
86+ * Return all direct parents of ``el`` matching ``selector``.
87+ * This matches against all parents but not the element itself.
88+ * The order of elements is from the search starting point up to higher
89+ * DOM levels.
90+ *
91+ * @param {Node } el - The DOM node to start the search from.
92+ * @param {String } selector - CSS selector to match against.
93+ * @returns {Array } - List of matching DOM nodes.
94+ */
6395const find_parents = ( el , selector ) => {
64- // Return all direct parents of ``el`` matching ``selector``.
65- // This matches against all parents but not the element itself.
66- // The order of elements is from the search starting point up to higher
67- // DOM levels.
6896 const ret = [ ] ;
69- let parent = el ?. parentNode ?. closest ?. ( selector ) ;
97+ let parent = el ;
7098 while ( parent ) {
71- ret . push ( parent ) ;
7299 parent = parent . parentNode ?. closest ?. ( selector ) ;
100+ if ( parent ) ret . push ( parent ) ;
73101 }
74102 return ret ;
75103} ;
76104
105+ /**
106+ * Find an element in the whole DOM tree if the selector is an ID selector,
107+ * otherwise use the given element as the starting point.
108+ *
109+ * @param {Node } el - The DOM node to start the search from.
110+ * @param {String } selector - The CSS selector to search for.
111+ *
112+ * @returns {NodeList } - The DOM nodes found.
113+ *
114+ */
77115const find_scoped = ( el , selector ) => {
78116 // If the selector starts with an object id do a global search,
79117 // otherwise do a local search.
80118 return ( selector . indexOf ( "#" ) === 0 ? document : el ) . querySelectorAll ( selector ) ;
81119} ;
82120
121+ /**
122+ * Return all HTMLElement parents of el, starting from the direct parent of el.
123+ * The document itself is excluded because it's not a real DOM node.
124+ *
125+ * @param {Node } el - The DOM node to start the search from.
126+ *
127+ * @returns {Array } - The DOM nodes found.
128+ */
83129const get_parents = ( el ) => {
84130 // Return all HTMLElement parents of el, starting from the direct parent of el.
85- // The document itself is excluded because it's not a real DOM node.
86131 const parents = [ ] ;
87132 let parent = el ?. parentNode ;
88133 while ( parent ) {
@@ -211,7 +256,7 @@ const find_scroll_container = (el, direction, fallback = document.body) => {
211256 * @param name {String} - The name of the variable. Note - this is stored on
212257 * the DOM node prefixed with the DATA_PREFIX.
213258 * @param default_value {Any} - Optional default value.
214- * @return {Any } - The value which is stored on the DOM node.
259+ * @returns {Any } - The value which is stored on the DOM node.
215260 */
216261const get_data = ( el , name , default_value ) => {
217262 return el [ `${ DATA_PREFIX } ${ name } ` ] || default_value ;
0 commit comments