* Convert a potential selector into an actual list of HTML elements. * Supports CSS selectors, element references, and special ID handling. * * @param els selector string, HTMLElement, or array of elements * @param root optional root element to search within (defaults to document, useful
(els: GridStackElement, root: HTMLElement | Document = document)
| 110 | * const fromShadow = Utils.getElements('.item', shadowRoot); |
| 111 | */ |
| 112 | static getElements(els: GridStackElement, root: HTMLElement | Document = document): HTMLElement[] { |
| 113 | if (typeof els === 'string') { |
| 114 | const doc = ('getElementById' in root) ? root as Document : undefined; |
| 115 | |
| 116 | // Note: very common for people use to id='1,2,3' which is only legal as HTML5 id, but not CSS selectors |
| 117 | // so if we start with a number, assume it's an id and just return that one item... |
| 118 | // see https://github.com/gridstack/gridstack.js/issues/2234#issuecomment-1523796562 |
| 119 | if (doc && !isNaN(+els[0])) { // start with digit |
| 120 | const el = doc.getElementById(els); |
| 121 | return el ? [el] : []; |
| 122 | } |
| 123 | |
| 124 | let list = root.querySelectorAll(els); |
| 125 | if (!list.length && els[0] !== '.' && els[0] !== '#') { |
| 126 | // see if mean to be a class |
| 127 | list = root.querySelectorAll('.' + els); |
| 128 | |
| 129 | // else if mean to be an id |
| 130 | if (!list.length) list = root.querySelectorAll('#' + els); |
| 131 | |
| 132 | // else see if gs-id attribute |
| 133 | if (!list.length) { |
| 134 | const el = root.querySelector<HTMLElement>(`[gs-id="${els}"]`); |
| 135 | return el ? [el] : []; |
| 136 | } |
| 137 | } |
| 138 | return Array.from(list) as HTMLElement[]; |
| 139 | } |
| 140 | return [els]; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Convert a potential selector into a single HTML element. |
no outgoing calls
no test coverage detected