/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

/**
 * Get all ancestors of an element until the first match of a given selector.
 *
 * @param el - The DOM element.
 * @param selector - A CSS selector to stop at. If the element matches this selector, it will not be included in the result.
 * @returns {HTMLElement[]} An array of ancestor elements, until the first match of the selector.
 */
export const getParentsUntil = ( el, selector ) => {
	const parents = [];
	let _el = el.parentNode;

	while ( _el && typeof _el.matches === 'function' ) {
		parents.unshift( _el );
		if ( _el.matches( selector ) ) {
			return parents;
		} else {
			_el = _el.parentNode;
		}
	}
	return [];
};
