shadow-dom-selector 6.2.1 → 6.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,37 +3,40 @@ interface AsyncParams {
3
3
  delay?: number;
4
4
  shouldReject?: boolean;
5
5
  }
6
+ type Root = Document | Element | ShadowRoot;
7
+ type Return<T> = T | null;
8
+ type PromiseReturn<T> = Promise<Return<T>>;
6
9
 
7
- declare function querySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): E | null;
8
- declare function querySelector<E extends Element = Element>(selectors: string): E | null;
9
- declare function deepQuerySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): E | null;
10
- declare function deepQuerySelector<E extends Element = Element>(selectors: string): E | null;
11
- declare function querySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): NodeListOf<E>;
10
+ declare function querySelector<E extends Element = Element>(root: Root, selectors: string): Return<E>;
11
+ declare function querySelector<E extends Element = Element>(selectors: string): Return<E>;
12
+ declare function deepQuerySelector<E extends Element = Element>(root: Root, selectors: string): Return<E>;
13
+ declare function deepQuerySelector<E extends Element = Element>(selectors: string): Return<E>;
14
+ declare function querySelectorAll<E extends Element = Element>(root: Root, selectors: string): NodeListOf<E>;
12
15
  declare function querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
13
- declare function deepQuerySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): NodeListOf<E>;
16
+ declare function deepQuerySelectorAll<E extends Element = Element>(root: Root, selectors: string): NodeListOf<E>;
14
17
  declare function deepQuerySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
15
- declare function shadowRootQuerySelector(root: Document | Element | ShadowRoot, selectors: string): ShadowRoot | null;
16
- declare function shadowRootQuerySelector(selectors: string): ShadowRoot | null;
17
- declare function asyncQuerySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
18
- declare function asyncQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
19
- declare function asyncDeepQuerySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
20
- declare function asyncDeepQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
21
- declare function asyncQuerySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
18
+ declare function shadowRootQuerySelector(root: Root, selectors: string): Return<ShadowRoot>;
19
+ declare function shadowRootQuerySelector(selectors: string): Return<ShadowRoot>;
20
+ declare function asyncQuerySelector<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
21
+ declare function asyncQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
22
+ declare function asyncDeepQuerySelector<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
23
+ declare function asyncDeepQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
24
+ declare function asyncQuerySelectorAll<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
22
25
  declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
23
- declare function asyncDeepQuerySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
26
+ declare function asyncDeepQuerySelectorAll<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
24
27
  declare function asyncDeepQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
25
- declare function asyncShadowRootQuerySelector(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
26
- declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
27
- declare class AsyncSelector<T extends Document | Element | ShadowRoot> {
28
+ declare function asyncShadowRootQuerySelector(root: Root, selectors: string, asyncParams?: AsyncParams): PromiseReturn<ShadowRoot>;
29
+ declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): PromiseReturn<ShadowRoot>;
30
+ declare class AsyncSelector<T extends Root> {
28
31
  constructor(asyncParams?: AsyncParams);
29
32
  constructor(root?: T | Promise<T | NodeListOf<Element>>, asyncParams?: AsyncParams);
30
33
  private _element;
31
34
  private _asyncParams;
32
- get element(): Promise<T | null>;
35
+ get element(): PromiseReturn<T>;
33
36
  get $(): AsyncSelector<ShadowRoot>;
34
37
  get all(): Promise<NodeListOf<Element>>;
35
38
  get asyncParams(): AsyncParams;
36
- eq(index: number): Promise<Element | null>;
39
+ eq(index: number): PromiseReturn<Element>;
37
40
  query(selector: string): AsyncSelector<Element>;
38
41
  deepQuery(selector: string): AsyncSelector<Element>;
39
42
  }
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{getPromisableResult as e}from"get-promisable-result";function t(e,t,n,r){return new(n||(n=Promise))(function(o,s){function l(e){try{u(r.next(e))}catch(e){s(e)}}function i(e){try{u(r.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(l,i)}u((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n="$",r=":host",o="invalid selector",s=10,l=10,i=!1,u="Wrong parameters have been provided.",a=()=>`You can not select a shadowRoot (${n}) of the document.`,c=()=>`You can not select a shadowRoot (${n}) of a shadowRoot.`,h=e=>"string"==typeof e,d=e=>!!e&&(e instanceof Document||e instanceof Element||e instanceof ShadowRoot),f=e=>{const t=["retries","delay","shouldReject"];return n=e,"[object Object]"===Object.prototype.toString.call(n)&&(0===Object.keys(e).length||Object.keys(e).every(e=>t.includes(e)));var n},y=(e,t)=>{if(h(e))return[document,e];if(d(e)&&h(t))return[e,t];throw new TypeError(u)},m=(e,t,n)=>{var r,o,a,c,y,m;if(h(e))return f(t)?[document,e,null!==(r=t.retries)&&void 0!==r?r:s,null!==(o=t.delay)&&void 0!==o?o:l,null!==(a=t.shouldReject)&&void 0!==a?a:i]:[document,e,s,l,i];if(d(e)&&h(t))return f(n)?[e,t,null!==(c=n.retries)&&void 0!==c?c:s,null!==(y=n.delay)&&void 0!==y?y:l,null!==(m=n.shouldReject)&&void 0!==m?m:i]:[e,t,s,l,i];throw new TypeError(u)};function w(e,t){return function(e){return e.split(",").map(e=>e.trim())}(e).map(e=>{const r=function(e){return e.split(n).map(e=>e.trim())}(e);return t(r)})}function S(e,t){return`${e} cannot be used with a selector ending in a shadowRoot (${n}).${t?` If you want to select a shadowRoot, use ${t} instead.`:""}`}function v(e){return e instanceof Promise?e:Promise.resolve(e)}function R(e,t){var n,o;let s=null;const l=e.length;for(let i=0;i<l;i++){if(0===i)if(e[i].length)s=t.querySelector(e[i]);else{if(t instanceof Document)throw new SyntaxError(a());if(t instanceof ShadowRoot)throw new SyntaxError(c());s=(null===(n=t.shadowRoot)||void 0===n?void 0:n.querySelector(e[++i]))||null}else s=(null===(o=s.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${e[i]}`))||null;if(null===s)return null}return s}function g(e,t){var n;const o=[...e],s=o.pop();if(!o.length)return t.querySelectorAll(s);const l=R(o,t);return(null===(n=null==l?void 0:l.shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${r} ${s}`))||null}function _(e,t){if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(a());if(t instanceof ShadowRoot)throw new SyntaxError(c());return t.shadowRoot}const n=R(e,t);return(null==n?void 0:n.shadowRoot)||null}function P(e,t,n="querySelector",r="shadowRootQuerySelector"){const o=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n,r));return e}),s=o.length;for(let t=0;t<s;t++){const n=R(o[t],e);if(n)return n}return null}function p(e,t,n="querySelectorAll"){const r=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n));return e}),s=r.length;for(let t=0;t<s;t++){const n=g(r[t],e);if(null==n?void 0:n.length)return n}return document.querySelectorAll(o)}function j(e,t,r="shadowRootQuerySelector",o="querySelector"){const s=w(t,e=>{if(e.pop().length)throw new SyntaxError(function(e,t){return`${e} must be used with a selector ending in a shadowRoot (${n}). If you don't want to select a shadowRoot, use ${t} instead.`}(r,o));return e}),l=s.length;for(let t=0;t<l;t++){const n=_(s[t],e);if(n)return n}return null}const E=(e,t)=>{const n=e.querySelectorAll(t);if(n.length)return n;if(e instanceof Element&&e.shadowRoot){const n=E(e.shadowRoot,t);if(n.length)return n}const r=Array.from(e.querySelectorAll("*"));for(const e of r){const n=E(e,t);if(n.length)return n}return document.querySelectorAll(o)},x=(t,n,r,o,s)=>e(()=>E(t,n),e=>!!e.length,{retries:r,delay:o,shouldReject:s});function b(e,t){return P(...y(e,t))}function q(e,t){return E(...y(e,t))[0]||null}function $(e,t){return p(...y(e,t))}function N(e,t){return E(...y(e,t))}function L(e,t){return j(...y(e,t))}function A(n,r,o){return t(this,void 0,void 0,function*(){return yield function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e(()=>P(n,r,"asyncQuerySelector","asyncShadowRootQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}function Q(e,n,r){return t(this,void 0,void 0,function*(){return(yield x(...m(e,n,r)))[0]||null})}function T(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e(()=>p(n,r,"asyncQuerySelectorAll"),e=>!!e.length,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}function O(e,t,n){return x(...m(e,t,n))}function D(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e(()=>j(n,r,"asyncShadowRootQuerySelector","asyncQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}class k{constructor(e,t){e instanceof Node||e instanceof Promise?(this._element=e,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},t||{})):(this._element=document,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},e||{}))}get element(){return v(this._element).then(e=>{if(e instanceof NodeList){if(e[0])return e[0];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a NodeList with elements.')}if(e)return e;if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a non-null element.')})}get $(){const e=v(this._element).then(e=>{if(e instanceof Document||e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "$" method can only be called in an element with a ShadowRoot.')}return e instanceof NodeList?D(e[0],n,this._asyncParams):D(e,n,this._asyncParams)});return new k(e,this._asyncParams)}get all(){return v(this._element).then(e=>{if(e instanceof NodeList)return e;if(!this._asyncParams.shouldReject)return document.querySelectorAll(o);throw new SyntaxError('The "all" method can only be called in a NodeList element.')})}get asyncParams(){return this._asyncParams}eq(e){return t(this,void 0,void 0,function*(){return v(this._element).then(t=>{if(t instanceof NodeList){if(t[e])return t[e];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError(`Could not get any element at index ${e}.`)}if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "eq" method can only be called in a NodeList element.')})})}query(e){const t=v(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "query" method can only be called from a defined element.')}return t instanceof NodeList?T(t[0],e,this._asyncParams):T(t,e,this._asyncParams)});return new k(t,this._asyncParams)}deepQuery(e){const t=v(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "deepQuery" method can only be called from a defined element.')}return t instanceof NodeList?Promise.race(Array.from(t).map(t=>x(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject))):x(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject)});return new k(t,this._asyncParams)}}export{k as AsyncSelector,Q as asyncDeepQuerySelector,O as asyncDeepQuerySelectorAll,A as asyncQuerySelector,T as asyncQuerySelectorAll,D as asyncShadowRootQuerySelector,q as deepQuerySelector,N as deepQuerySelectorAll,b as querySelector,$ as querySelectorAll,L as shadowRootQuerySelector};
1
+ import{getPromisableResult as e}from"get-promisable-result";function t(e,t,n,r){return new(n||(n=Promise))(function(o,s){function l(e){try{a(r.next(e))}catch(e){s(e)}}function i(e){try{a(r.throw(e))}catch(e){s(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(l,i)}a((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n="$",r=":host",o="invalid selector",s=10,l=10,i=!1,a="Wrong parameters have been provided.",u=()=>`You can not select a shadowRoot (${n}) of the document.`,c=()=>`You can not select a shadowRoot (${n}) of a shadowRoot.`,h=e=>"string"==typeof e,d=e=>!!e&&(e instanceof Document||e instanceof Element||e instanceof ShadowRoot),f=e=>{return t=e,"[object Object]"===Object.prototype.toString.call(t)&&(0===Object.keys(e).length||["retries","delay","shouldReject"].some(t=>t in e));var t},y=(e,t)=>{if(h(e))return[document,e];if(d(e)&&h(t))return[e,t];throw new TypeError(a)},m=(e,t,n)=>{var r,o,u,c,y,m;if(h(e))return f(t)?[document,e,null!==(r=t.retries)&&void 0!==r?r:s,null!==(o=t.delay)&&void 0!==o?o:l,null!==(u=t.shouldReject)&&void 0!==u?u:i]:[document,e,s,l,i];if(d(e)&&h(t))return f(n)?[e,t,null!==(c=n.retries)&&void 0!==c?c:s,null!==(y=n.delay)&&void 0!==y?y:l,null!==(m=n.shouldReject)&&void 0!==m?m:i]:[e,t,s,l,i];throw new TypeError(a)};function w(e,t){return function(e){return e.split(",").map(e=>e.trim())}(e).map(e=>{const r=function(e){return e.split(n).map(e=>e.trim())}(e);return t(r)})}function S(e,t){return`${e} cannot be used with a selector ending in a shadowRoot (${n}).${t?` If you want to select a shadowRoot, use ${t} instead.`:""}`}function v(e){return e instanceof Promise?e:Promise.resolve(e)}function R(e,t){var n,o;let s=null;const l=e.length;for(let i=0;i<l;i++){if(0===i)if(e[i].length)s=t.querySelector(e[i]);else{if(t instanceof Document)throw new SyntaxError(u());if(t instanceof ShadowRoot)throw new SyntaxError(c());s=(null===(n=t.shadowRoot)||void 0===n?void 0:n.querySelector(e[++i]))||null}else s=(null===(o=s.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${e[i]}`))||null;if(null===s)return null}return s}function g(e,t){var n;const o=[...e],s=o.pop();if(!o.length)return t.querySelectorAll(s);const l=R(o,t);return(null===(n=null==l?void 0:l.shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${r} ${s}`))||null}function _(e,t){if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(u());if(t instanceof ShadowRoot)throw new SyntaxError(c());return t.shadowRoot}const n=R(e,t);return(null==n?void 0:n.shadowRoot)||null}function P(e,t,n="querySelector",r="shadowRootQuerySelector"){const o=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n,r));return e}),s=o.length;for(let t=0;t<s;t++){const n=R(o[t],e);if(n)return n}return null}function p(e,t,n="querySelectorAll"){const r=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n));return e}),s=r.length;for(let t=0;t<s;t++){const n=g(r[t],e);if(null==n?void 0:n.length)return n}return document.querySelectorAll(o)}function j(e,t,r="shadowRootQuerySelector",o="querySelector"){const s=w(t,e=>{if(e.pop().length)throw new SyntaxError(function(e,t){return`${e} must be used with a selector ending in a shadowRoot (${n}). If you don't want to select a shadowRoot, use ${t} instead.`}(r,o));return e}),l=s.length;for(let t=0;t<l;t++){const n=_(s[t],e);if(n)return n}return null}const E=(e,t)=>{const n=e.querySelectorAll(t);if(n.length)return n;if(e instanceof Element&&e.shadowRoot){const n=E(e.shadowRoot,t);if(n.length)return n}const r=Array.from(e.querySelectorAll("*"));for(const e of r){const n=E(e,t);if(n.length)return n}return document.querySelectorAll(o)},x=(t,n,r,o,s)=>e(()=>E(t,n),e=>!!e.length,{retries:r,delay:o,shouldReject:s});function b(e,t){return P(...y(e,t))}function q(e,t){return E(...y(e,t))[0]||null}function $(e,t){return p(...y(e,t))}function N(e,t){return E(...y(e,t))}function L(e,t){return j(...y(e,t))}function A(n,r,o){return t(this,void 0,void 0,function*(){return yield function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e(()=>P(n,r,"asyncQuerySelector","asyncShadowRootQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}function T(e,n,r){return t(this,void 0,void 0,function*(){return(yield x(...m(e,n,r)))[0]||null})}function Q(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e(()=>p(n,r,"asyncQuerySelectorAll"),e=>!!e.length,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}function O(e,t,n){return x(...m(e,t,n))}function D(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e(()=>j(n,r,"asyncShadowRootQuerySelector","asyncQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}class I{constructor(e,t){e instanceof Node||e instanceof Promise?(this._element=e,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},t||{})):(this._element=document,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},e||{}))}get element(){return v(this._element).then(e=>{if(e instanceof NodeList){if(e[0])return e[0];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a NodeList with elements.')}if(e)return e;if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a non-null element.')})}get $(){const e=v(this._element).then(e=>{if(e instanceof Document||e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "$" method can only be called in an element with a ShadowRoot.')}return e instanceof NodeList?D(e[0],n,this._asyncParams):D(e,n,this._asyncParams)});return new I(e,this._asyncParams)}get all(){return v(this._element).then(e=>{if(e instanceof NodeList){if(0===e.length&&this._asyncParams.shouldReject)throw new SyntaxError('The "all" method can only be called in a valid element.');return e}if(!this._asyncParams.shouldReject)return document.querySelectorAll(o);throw new SyntaxError('The "all" method can only be called in a NodeList element.')})}get asyncParams(){return this._asyncParams}eq(e){return t(this,void 0,void 0,function*(){return v(this._element).then(t=>{if(t instanceof NodeList){if(t[e])return t[e];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError(`Could not get any element at index ${e}.`)}if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "eq" method can only be called in a NodeList element.')})})}query(e){const t=v(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "query" method can only be called from a defined element.')}return t instanceof NodeList?Q(t[0],e,this._asyncParams):Q(t,e,this._asyncParams)});return new I(t,this._asyncParams)}deepQuery(e){const t=v(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "deepQuery" method can only be called from a defined element.')}return t instanceof NodeList?Promise.race(Array.from(t).map(t=>x(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject))):x(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject)});return new I(t,this._asyncParams)}}export{I as AsyncSelector,T as asyncDeepQuerySelector,O as asyncDeepQuerySelectorAll,A as asyncQuerySelector,Q as asyncQuerySelectorAll,D as asyncShadowRootQuerySelector,q as deepQuerySelector,N as deepQuerySelectorAll,b as querySelector,$ as querySelectorAll,L as shadowRootQuerySelector};
package/dist/index.d.ts CHANGED
@@ -3,37 +3,40 @@ interface AsyncParams {
3
3
  delay?: number;
4
4
  shouldReject?: boolean;
5
5
  }
6
+ type Root = Document | Element | ShadowRoot;
7
+ type Return<T> = T | null;
8
+ type PromiseReturn<T> = Promise<Return<T>>;
6
9
 
7
- declare function querySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): E | null;
8
- declare function querySelector<E extends Element = Element>(selectors: string): E | null;
9
- declare function deepQuerySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): E | null;
10
- declare function deepQuerySelector<E extends Element = Element>(selectors: string): E | null;
11
- declare function querySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): NodeListOf<E>;
10
+ declare function querySelector<E extends Element = Element>(root: Root, selectors: string): Return<E>;
11
+ declare function querySelector<E extends Element = Element>(selectors: string): Return<E>;
12
+ declare function deepQuerySelector<E extends Element = Element>(root: Root, selectors: string): Return<E>;
13
+ declare function deepQuerySelector<E extends Element = Element>(selectors: string): Return<E>;
14
+ declare function querySelectorAll<E extends Element = Element>(root: Root, selectors: string): NodeListOf<E>;
12
15
  declare function querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
13
- declare function deepQuerySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string): NodeListOf<E>;
16
+ declare function deepQuerySelectorAll<E extends Element = Element>(root: Root, selectors: string): NodeListOf<E>;
14
17
  declare function deepQuerySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;
15
- declare function shadowRootQuerySelector(root: Document | Element | ShadowRoot, selectors: string): ShadowRoot | null;
16
- declare function shadowRootQuerySelector(selectors: string): ShadowRoot | null;
17
- declare function asyncQuerySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
18
- declare function asyncQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
19
- declare function asyncDeepQuerySelector<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
20
- declare function asyncDeepQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<E | null>;
21
- declare function asyncQuerySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
18
+ declare function shadowRootQuerySelector(root: Root, selectors: string): Return<ShadowRoot>;
19
+ declare function shadowRootQuerySelector(selectors: string): Return<ShadowRoot>;
20
+ declare function asyncQuerySelector<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
21
+ declare function asyncQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
22
+ declare function asyncDeepQuerySelector<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
23
+ declare function asyncDeepQuerySelector<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): PromiseReturn<E>;
24
+ declare function asyncQuerySelectorAll<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
22
25
  declare function asyncQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
23
- declare function asyncDeepQuerySelectorAll<E extends Element = Element>(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
26
+ declare function asyncDeepQuerySelectorAll<E extends Element = Element>(root: Root, selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
24
27
  declare function asyncDeepQuerySelectorAll<E extends Element = Element>(selectors: string, asyncParams?: AsyncParams): Promise<NodeListOf<E>>;
25
- declare function asyncShadowRootQuerySelector(root: Document | Element | ShadowRoot, selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
26
- declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): Promise<ShadowRoot | null>;
27
- declare class AsyncSelector<T extends Document | Element | ShadowRoot> {
28
+ declare function asyncShadowRootQuerySelector(root: Root, selectors: string, asyncParams?: AsyncParams): PromiseReturn<ShadowRoot>;
29
+ declare function asyncShadowRootQuerySelector(selectors: string, asyncParams?: AsyncParams): PromiseReturn<ShadowRoot>;
30
+ declare class AsyncSelector<T extends Root> {
28
31
  constructor(asyncParams?: AsyncParams);
29
32
  constructor(root?: T | Promise<T | NodeListOf<Element>>, asyncParams?: AsyncParams);
30
33
  private _element;
31
34
  private _asyncParams;
32
- get element(): Promise<T | null>;
35
+ get element(): PromiseReturn<T>;
33
36
  get $(): AsyncSelector<ShadowRoot>;
34
37
  get all(): Promise<NodeListOf<Element>>;
35
38
  get asyncParams(): AsyncParams;
36
- eq(index: number): Promise<Element | null>;
39
+ eq(index: number): PromiseReturn<Element>;
37
40
  query(selector: string): AsyncSelector<Element>;
38
41
  deepQuery(selector: string): AsyncSelector<Element>;
39
42
  }
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("get-promisable-result");function t(e,t,n,r){return new(n||(n=Promise))(function(o,s){function l(e){try{u(r.next(e))}catch(e){s(e)}}function i(e){try{u(r.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(l,i)}u((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n="$",r=":host",o="invalid selector",s=10,l=10,i=!1,u="Wrong parameters have been provided.",a=()=>`You can not select a shadowRoot (${n}) of the document.`,c=()=>`You can not select a shadowRoot (${n}) of a shadowRoot.`,h=e=>"string"==typeof e,d=e=>!!e&&(e instanceof Document||e instanceof Element||e instanceof ShadowRoot),f=e=>{const t=["retries","delay","shouldReject"];return n=e,"[object Object]"===Object.prototype.toString.call(n)&&(0===Object.keys(e).length||Object.keys(e).every(e=>t.includes(e)));var n},y=(e,t)=>{if(h(e))return[document,e];if(d(e)&&h(t))return[e,t];throw new TypeError(u)},m=(e,t,n)=>{var r,o,a,c,y,m;if(h(e))return f(t)?[document,e,null!==(r=t.retries)&&void 0!==r?r:s,null!==(o=t.delay)&&void 0!==o?o:l,null!==(a=t.shouldReject)&&void 0!==a?a:i]:[document,e,s,l,i];if(d(e)&&h(t))return f(n)?[e,t,null!==(c=n.retries)&&void 0!==c?c:s,null!==(y=n.delay)&&void 0!==y?y:l,null!==(m=n.shouldReject)&&void 0!==m?m:i]:[e,t,s,l,i];throw new TypeError(u)};function w(e,t){return function(e){return e.split(",").map(e=>e.trim())}(e).map(e=>{const r=function(e){return e.split(n).map(e=>e.trim())}(e);return t(r)})}function S(e,t){return`${e} cannot be used with a selector ending in a shadowRoot (${n}).${t?` If you want to select a shadowRoot, use ${t} instead.`:""}`}function R(e){return e instanceof Promise?e:Promise.resolve(e)}function v(e,t){var n,o;let s=null;const l=e.length;for(let i=0;i<l;i++){if(0===i)if(e[i].length)s=t.querySelector(e[i]);else{if(t instanceof Document)throw new SyntaxError(a());if(t instanceof ShadowRoot)throw new SyntaxError(c());s=(null===(n=t.shadowRoot)||void 0===n?void 0:n.querySelector(e[++i]))||null}else s=(null===(o=s.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${e[i]}`))||null;if(null===s)return null}return s}function p(e,t){var n;const o=[...e],s=o.pop();if(!o.length)return t.querySelectorAll(s);const l=v(o,t);return(null===(n=null==l?void 0:l.shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${r} ${s}`))||null}function g(e,t){if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(a());if(t instanceof ShadowRoot)throw new SyntaxError(c());return t.shadowRoot}const n=v(e,t);return(null==n?void 0:n.shadowRoot)||null}function P(e,t,n="querySelector",r="shadowRootQuerySelector"){const o=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n,r));return e}),s=o.length;for(let t=0;t<s;t++){const n=v(o[t],e);if(n)return n}return null}function _(e,t,n="querySelectorAll"){const r=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n));return e}),s=r.length;for(let t=0;t<s;t++){const n=p(r[t],e);if(null==n?void 0:n.length)return n}return document.querySelectorAll(o)}function x(e,t,r="shadowRootQuerySelector",o="querySelector"){const s=w(t,e=>{if(e.pop().length)throw new SyntaxError(function(e,t){return`${e} must be used with a selector ending in a shadowRoot (${n}). If you don't want to select a shadowRoot, use ${t} instead.`}(r,o));return e}),l=s.length;for(let t=0;t<l;t++){const n=g(s[t],e);if(n)return n}return null}const j=(e,t)=>{const n=e.querySelectorAll(t);if(n.length)return n;if(e instanceof Element&&e.shadowRoot){const n=j(e.shadowRoot,t);if(n.length)return n}const r=Array.from(e.querySelectorAll("*"));for(const e of r){const n=j(e,t);if(n.length)return n}return document.querySelectorAll(o)},b=(t,n,r,o,s)=>e.getPromisableResult(()=>j(t,n),e=>!!e.length,{retries:r,delay:o,shouldReject:s});function E(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e.getPromisableResult(()=>_(n,r,"asyncQuerySelectorAll"),e=>!!e.length,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}function q(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e.getPromisableResult(()=>x(n,r,"asyncShadowRootQuerySelector","asyncQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}class Q{constructor(e,t){e instanceof Node||e instanceof Promise?(this._element=e,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},t||{})):(this._element=document,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},e||{}))}get element(){return R(this._element).then(e=>{if(e instanceof NodeList){if(e[0])return e[0];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a NodeList with elements.')}if(e)return e;if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a non-null element.')})}get $(){const e=R(this._element).then(e=>{if(e instanceof Document||e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "$" method can only be called in an element with a ShadowRoot.')}return e instanceof NodeList?q(e[0],n,this._asyncParams):q(e,n,this._asyncParams)});return new Q(e,this._asyncParams)}get all(){return R(this._element).then(e=>{if(e instanceof NodeList)return e;if(!this._asyncParams.shouldReject)return document.querySelectorAll(o);throw new SyntaxError('The "all" method can only be called in a NodeList element.')})}get asyncParams(){return this._asyncParams}eq(e){return t(this,void 0,void 0,function*(){return R(this._element).then(t=>{if(t instanceof NodeList){if(t[e])return t[e];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError(`Could not get any element at index ${e}.`)}if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "eq" method can only be called in a NodeList element.')})})}query(e){const t=R(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "query" method can only be called from a defined element.')}return t instanceof NodeList?E(t[0],e,this._asyncParams):E(t,e,this._asyncParams)});return new Q(t,this._asyncParams)}deepQuery(e){const t=R(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "deepQuery" method can only be called from a defined element.')}return t instanceof NodeList?Promise.race(Array.from(t).map(t=>b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject))):b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject)});return new Q(t,this._asyncParams)}}exports.AsyncSelector=Q,exports.asyncDeepQuerySelector=function(e,n,r){return t(this,void 0,void 0,function*(){return(yield b(...m(e,n,r)))[0]||null})},exports.asyncDeepQuerySelectorAll=function(e,t,n){return b(...m(e,t,n))},exports.asyncQuerySelector=function(n,r,o){return t(this,void 0,void 0,function*(){return yield function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e.getPromisableResult(()=>P(n,r,"asyncQuerySelector","asyncShadowRootQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})},exports.asyncQuerySelectorAll=E,exports.asyncShadowRootQuerySelector=q,exports.deepQuerySelector=function(e,t){return j(...y(e,t))[0]||null},exports.deepQuerySelectorAll=function(e,t){return j(...y(e,t))},exports.querySelector=function(e,t){return P(...y(e,t))},exports.querySelectorAll=function(e,t){return _(...y(e,t))},exports.shadowRootQuerySelector=function(e,t){return x(...y(e,t))};
1
+ "use strict";var e=require("get-promisable-result");function t(e,t,n,r){return new(n||(n=Promise))(function(o,s){function l(e){try{a(r.next(e))}catch(e){s(e)}}function i(e){try{a(r.throw(e))}catch(e){s(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(l,i)}a((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n="$",r=":host",o="invalid selector",s=10,l=10,i=!1,a="Wrong parameters have been provided.",u=()=>`You can not select a shadowRoot (${n}) of the document.`,c=()=>`You can not select a shadowRoot (${n}) of a shadowRoot.`,h=e=>"string"==typeof e,d=e=>!!e&&(e instanceof Document||e instanceof Element||e instanceof ShadowRoot),f=e=>{return t=e,"[object Object]"===Object.prototype.toString.call(t)&&(0===Object.keys(e).length||["retries","delay","shouldReject"].some(t=>t in e));var t},y=(e,t)=>{if(h(e))return[document,e];if(d(e)&&h(t))return[e,t];throw new TypeError(a)},m=(e,t,n)=>{var r,o,u,c,y,m;if(h(e))return f(t)?[document,e,null!==(r=t.retries)&&void 0!==r?r:s,null!==(o=t.delay)&&void 0!==o?o:l,null!==(u=t.shouldReject)&&void 0!==u?u:i]:[document,e,s,l,i];if(d(e)&&h(t))return f(n)?[e,t,null!==(c=n.retries)&&void 0!==c?c:s,null!==(y=n.delay)&&void 0!==y?y:l,null!==(m=n.shouldReject)&&void 0!==m?m:i]:[e,t,s,l,i];throw new TypeError(a)};function w(e,t){return function(e){return e.split(",").map(e=>e.trim())}(e).map(e=>{const r=function(e){return e.split(n).map(e=>e.trim())}(e);return t(r)})}function S(e,t){return`${e} cannot be used with a selector ending in a shadowRoot (${n}).${t?` If you want to select a shadowRoot, use ${t} instead.`:""}`}function R(e){return e instanceof Promise?e:Promise.resolve(e)}function v(e,t){var n,o;let s=null;const l=e.length;for(let i=0;i<l;i++){if(0===i)if(e[i].length)s=t.querySelector(e[i]);else{if(t instanceof Document)throw new SyntaxError(u());if(t instanceof ShadowRoot)throw new SyntaxError(c());s=(null===(n=t.shadowRoot)||void 0===n?void 0:n.querySelector(e[++i]))||null}else s=(null===(o=s.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${e[i]}`))||null;if(null===s)return null}return s}function p(e,t){var n;const o=[...e],s=o.pop();if(!o.length)return t.querySelectorAll(s);const l=v(o,t);return(null===(n=null==l?void 0:l.shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${r} ${s}`))||null}function g(e,t){if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(u());if(t instanceof ShadowRoot)throw new SyntaxError(c());return t.shadowRoot}const n=v(e,t);return(null==n?void 0:n.shadowRoot)||null}function P(e,t,n="querySelector",r="shadowRootQuerySelector"){const o=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n,r));return e}),s=o.length;for(let t=0;t<s;t++){const n=v(o[t],e);if(n)return n}return null}function _(e,t,n="querySelectorAll"){const r=w(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(S(n));return e}),s=r.length;for(let t=0;t<s;t++){const n=p(r[t],e);if(null==n?void 0:n.length)return n}return document.querySelectorAll(o)}function x(e,t,r="shadowRootQuerySelector",o="querySelector"){const s=w(t,e=>{if(e.pop().length)throw new SyntaxError(function(e,t){return`${e} must be used with a selector ending in a shadowRoot (${n}). If you don't want to select a shadowRoot, use ${t} instead.`}(r,o));return e}),l=s.length;for(let t=0;t<l;t++){const n=g(s[t],e);if(n)return n}return null}const j=(e,t)=>{const n=e.querySelectorAll(t);if(n.length)return n;if(e instanceof Element&&e.shadowRoot){const n=j(e.shadowRoot,t);if(n.length)return n}const r=Array.from(e.querySelectorAll("*"));for(const e of r){const n=j(e,t);if(n.length)return n}return document.querySelectorAll(o)},b=(t,n,r,o,s)=>e.getPromisableResult(()=>j(t,n),e=>!!e.length,{retries:r,delay:o,shouldReject:s});function E(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e.getPromisableResult(()=>_(n,r,"asyncQuerySelectorAll"),e=>!!e.length,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}function q(n,r,o){return t(this,void 0,void 0,function*(){return function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e.getPromisableResult(()=>x(n,r,"asyncShadowRootQuerySelector","asyncQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})}class Q{constructor(e,t){e instanceof Node||e instanceof Promise?(this._element=e,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},t||{})):(this._element=document,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},e||{}))}get element(){return R(this._element).then(e=>{if(e instanceof NodeList){if(e[0])return e[0];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a NodeList with elements.')}if(e)return e;if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a non-null element.')})}get $(){const e=R(this._element).then(e=>{if(e instanceof Document||e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "$" method can only be called in an element with a ShadowRoot.')}return e instanceof NodeList?q(e[0],n,this._asyncParams):q(e,n,this._asyncParams)});return new Q(e,this._asyncParams)}get all(){return R(this._element).then(e=>{if(e instanceof NodeList){if(0===e.length&&this._asyncParams.shouldReject)throw new SyntaxError('The "all" method can only be called in a valid element.');return e}if(!this._asyncParams.shouldReject)return document.querySelectorAll(o);throw new SyntaxError('The "all" method can only be called in a NodeList element.')})}get asyncParams(){return this._asyncParams}eq(e){return t(this,void 0,void 0,function*(){return R(this._element).then(t=>{if(t instanceof NodeList){if(t[e])return t[e];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError(`Could not get any element at index ${e}.`)}if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "eq" method can only be called in a NodeList element.')})})}query(e){const t=R(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "query" method can only be called from a defined element.')}return t instanceof NodeList?E(t[0],e,this._asyncParams):E(t,e,this._asyncParams)});return new Q(t,this._asyncParams)}deepQuery(e){const t=R(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "deepQuery" method can only be called from a defined element.')}return t instanceof NodeList?Promise.race(Array.from(t).map(t=>b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject))):b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject)});return new Q(t,this._asyncParams)}}exports.AsyncSelector=Q,exports.asyncDeepQuerySelector=function(e,n,r){return t(this,void 0,void 0,function*(){return(yield b(...m(e,n,r)))[0]||null})},exports.asyncDeepQuerySelectorAll=function(e,t,n){return b(...m(e,t,n))},exports.asyncQuerySelector=function(n,r,o){return t(this,void 0,void 0,function*(){return yield function(n,r,o,s,l){return t(this,void 0,void 0,function*(){return e.getPromisableResult(()=>P(n,r,"asyncQuerySelector","asyncShadowRootQuerySelector"),e=>!!e,{retries:o,delay:s,shouldReject:l})})}(...m(n,r,o))})},exports.asyncQuerySelectorAll=E,exports.asyncShadowRootQuerySelector=q,exports.deepQuerySelector=function(e,t){return j(...y(e,t))[0]||null},exports.deepQuerySelectorAll=function(e,t){return j(...y(e,t))},exports.querySelector=function(e,t){return P(...y(e,t))},exports.querySelectorAll=function(e,t){return _(...y(e,t))},exports.shadowRootQuerySelector=function(e,t){return x(...y(e,t))};
@@ -1 +1 @@
1
- var ShadowDomSelector=function(e){"use strict";function t(e,t,n,r){return new(n||(n=Promise))(function(o,s){function l(e){try{u(r.next(e))}catch(e){s(e)}}function i(e){try{u(r.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(l,i)}u((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n="$",r=":host",o="invalid selector",s=10,l=10,i=!1,u="Wrong parameters have been provided.",a=10,c=10,h=(e,t,n={})=>{var r;const{retries:o=a,delay:s=c,shouldReject:l=!0}=n,i=null!==(r=n.rejectMessage)&&void 0!==r?r:"Could not get the result after {{ retries }} retries";return new Promise((n,r)=>{let u=0;const a=()=>{const c=e();t(c)?n(c):(u++,u<o?setTimeout(a,s):l?r(new Error(i.replace(/\{\{\s*retries\s*\}\}/g,`${o}`))):n(c))};a()})},d=()=>`You can not select a shadowRoot (${n}) of the document.`,f=()=>`You can not select a shadowRoot (${n}) of a shadowRoot.`,y=e=>"string"==typeof e,m=e=>!!e&&(e instanceof Document||e instanceof Element||e instanceof ShadowRoot),w=e=>{const t=["retries","delay","shouldReject"];return n=e,"[object Object]"===Object.prototype.toString.call(n)&&(0===Object.keys(e).length||Object.keys(e).every(e=>t.includes(e)));var n},S=(e,t)=>{if(y(e))return[document,e];if(m(e)&&y(t))return[e,t];throw new TypeError(u)},v=(e,t,n)=>{var r,o,a,c,h,d;if(y(e))return w(t)?[document,e,null!==(r=t.retries)&&void 0!==r?r:s,null!==(o=t.delay)&&void 0!==o?o:l,null!==(a=t.shouldReject)&&void 0!==a?a:i]:[document,e,s,l,i];if(m(e)&&y(t))return w(n)?[e,t,null!==(c=n.retries)&&void 0!==c?c:s,null!==(h=n.delay)&&void 0!==h?h:l,null!==(d=n.shouldReject)&&void 0!==d?d:i]:[e,t,s,l,i];throw new TypeError(u)};function R(e,t){return function(e){return e.split(",").map(e=>e.trim())}(e).map(e=>{const r=function(e){return e.split(n).map(e=>e.trim())}(e);return t(r)})}function g(e,t){return`${e} cannot be used with a selector ending in a shadowRoot (${n}).${t?` If you want to select a shadowRoot, use ${t} instead.`:""}`}function _(e){return e instanceof Promise?e:Promise.resolve(e)}function p(e,t){var n,o;let s=null;const l=e.length;for(let i=0;i<l;i++){if(0===i)if(e[i].length)s=t.querySelector(e[i]);else{if(t instanceof Document)throw new SyntaxError(d());if(t instanceof ShadowRoot)throw new SyntaxError(f());s=(null===(n=t.shadowRoot)||void 0===n?void 0:n.querySelector(e[++i]))||null}else s=(null===(o=s.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${e[i]}`))||null;if(null===s)return null}return s}function P(e,t){var n;const o=[...e],s=o.pop();if(!o.length)return t.querySelectorAll(s);const l=p(o,t);return(null===(n=null==l?void 0:l.shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${r} ${s}`))||null}function j(e,t){if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(d());if(t instanceof ShadowRoot)throw new SyntaxError(f());return t.shadowRoot}const n=p(e,t);return(null==n?void 0:n.shadowRoot)||null}function E(e,t,n="querySelector",r="shadowRootQuerySelector"){const o=R(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(g(n,r));return e}),s=o.length;for(let t=0;t<s;t++){const n=p(o[t],e);if(n)return n}return null}function q(e,t,n="querySelectorAll"){const r=R(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(g(n));return e}),s=r.length;for(let t=0;t<s;t++){const n=P(r[t],e);if(null==n?void 0:n.length)return n}return document.querySelectorAll(o)}function x(e,t,r="shadowRootQuerySelector",o="querySelector"){const s=R(t,e=>{if(e.pop().length)throw new SyntaxError(function(e,t){return`${e} must be used with a selector ending in a shadowRoot (${n}). If you don't want to select a shadowRoot, use ${t} instead.`}(r,o));return e}),l=s.length;for(let t=0;t<l;t++){const n=j(s[t],e);if(n)return n}return null}const $=(e,t)=>{const n=e.querySelectorAll(t);if(n.length)return n;if(e instanceof Element&&e.shadowRoot){const n=$(e.shadowRoot,t);if(n.length)return n}const r=Array.from(e.querySelectorAll("*"));for(const e of r){const n=$(e,t);if(n.length)return n}return document.querySelectorAll(o)},b=(e,t,n,r,o)=>h(()=>$(e,t),e=>!!e.length,{retries:n,delay:r,shouldReject:o});function Q(e,n,r){return t(this,void 0,void 0,function*(){return function(e,n,r,o,s){return t(this,void 0,void 0,function*(){return h(()=>q(e,n,"asyncQuerySelectorAll"),e=>!!e.length,{retries:r,delay:o,shouldReject:s})})}(...v(e,n,r))})}function A(e,n,r){return t(this,void 0,void 0,function*(){return function(e,n,r,o,s){return t(this,void 0,void 0,function*(){return h(()=>x(e,n,"asyncShadowRootQuerySelector","asyncQuerySelector"),e=>!!e,{retries:r,delay:o,shouldReject:s})})}(...v(e,n,r))})}class N{constructor(e,t){e instanceof Node||e instanceof Promise?(this._element=e,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},t||{})):(this._element=document,this._asyncParams=Object.assign({retries:s,delay:l,shouldReject:i},e||{}))}get element(){return _(this._element).then(e=>{if(e instanceof NodeList){if(e[0])return e[0];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a NodeList with elements.')}if(e)return e;if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a non-null element.')})}get $(){const e=_(this._element).then(e=>{if(e instanceof Document||e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "$" method can only be called in an element with a ShadowRoot.')}return e instanceof NodeList?A(e[0],n,this._asyncParams):A(e,n,this._asyncParams)});return new N(e,this._asyncParams)}get all(){return _(this._element).then(e=>{if(e instanceof NodeList)return e;if(!this._asyncParams.shouldReject)return document.querySelectorAll(o);throw new SyntaxError('The "all" method can only be called in a NodeList element.')})}get asyncParams(){return this._asyncParams}eq(e){return t(this,void 0,void 0,function*(){return _(this._element).then(t=>{if(t instanceof NodeList){if(t[e])return t[e];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError(`Could not get any element at index ${e}.`)}if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "eq" method can only be called in a NodeList element.')})})}query(e){const t=_(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "query" method can only be called from a defined element.')}return t instanceof NodeList?Q(t[0],e,this._asyncParams):Q(t,e,this._asyncParams)});return new N(t,this._asyncParams)}deepQuery(e){const t=_(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "deepQuery" method can only be called from a defined element.')}return t instanceof NodeList?Promise.race(Array.from(t).map(t=>b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject))):b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject)});return new N(t,this._asyncParams)}}return e.AsyncSelector=N,e.asyncDeepQuerySelector=function(e,n,r){return t(this,void 0,void 0,function*(){return(yield b(...v(e,n,r)))[0]||null})},e.asyncDeepQuerySelectorAll=function(e,t,n){return b(...v(e,t,n))},e.asyncQuerySelector=function(e,n,r){return t(this,void 0,void 0,function*(){return yield function(e,n,r,o,s){return t(this,void 0,void 0,function*(){return h(()=>E(e,n,"asyncQuerySelector","asyncShadowRootQuerySelector"),e=>!!e,{retries:r,delay:o,shouldReject:s})})}(...v(e,n,r))})},e.asyncQuerySelectorAll=Q,e.asyncShadowRootQuerySelector=A,e.deepQuerySelector=function(e,t){return $(...S(e,t))[0]||null},e.deepQuerySelectorAll=function(e,t){return $(...S(e,t))},e.querySelector=function(e,t){return E(...S(e,t))},e.querySelectorAll=function(e,t){return q(...S(e,t))},e.shadowRootQuerySelector=function(e,t){return x(...S(e,t))},e}({});
1
+ var ShadowDomSelector=function(e){"use strict";function t(e,t,n,r){return new(n||(n=Promise))(function(o,l){function s(e){try{a(r.next(e))}catch(e){l(e)}}function i(e){try{a(r.throw(e))}catch(e){l(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(s,i)}a((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const n="$",r=":host",o="invalid selector",l=10,s=10,i=!1,a="Wrong parameters have been provided.",u=10,c=10,h=(e,t,n={})=>{var r;const{retries:o=u,delay:l=c,shouldReject:s=!0}=n,i=null!==(r=n.rejectMessage)&&void 0!==r?r:"Could not get the result after {{ retries }} retries";return new Promise((n,r)=>{let a=0;const u=()=>{const c=e();t(c)?n(c):(a++,a<o?setTimeout(u,l):s?r(new Error(i.replace(/\{\{\s*retries\s*\}\}/g,`${o}`))):n(c))};u()})},d=()=>`You can not select a shadowRoot (${n}) of the document.`,f=()=>`You can not select a shadowRoot (${n}) of a shadowRoot.`,y=e=>"string"==typeof e,m=e=>!!e&&(e instanceof Document||e instanceof Element||e instanceof ShadowRoot),w=e=>{return t=e,"[object Object]"===Object.prototype.toString.call(t)&&(0===Object.keys(e).length||["retries","delay","shouldReject"].some(t=>t in e));var t},S=(e,t)=>{if(y(e))return[document,e];if(m(e)&&y(t))return[e,t];throw new TypeError(a)},v=(e,t,n)=>{var r,o,u,c,h,d;if(y(e))return w(t)?[document,e,null!==(r=t.retries)&&void 0!==r?r:l,null!==(o=t.delay)&&void 0!==o?o:s,null!==(u=t.shouldReject)&&void 0!==u?u:i]:[document,e,l,s,i];if(m(e)&&y(t))return w(n)?[e,t,null!==(c=n.retries)&&void 0!==c?c:l,null!==(h=n.delay)&&void 0!==h?h:s,null!==(d=n.shouldReject)&&void 0!==d?d:i]:[e,t,l,s,i];throw new TypeError(a)};function R(e,t){return function(e){return e.split(",").map(e=>e.trim())}(e).map(e=>{const r=function(e){return e.split(n).map(e=>e.trim())}(e);return t(r)})}function g(e,t){return`${e} cannot be used with a selector ending in a shadowRoot (${n}).${t?` If you want to select a shadowRoot, use ${t} instead.`:""}`}function _(e){return e instanceof Promise?e:Promise.resolve(e)}function P(e,t){var n,o;let l=null;const s=e.length;for(let i=0;i<s;i++){if(0===i)if(e[i].length)l=t.querySelector(e[i]);else{if(t instanceof Document)throw new SyntaxError(d());if(t instanceof ShadowRoot)throw new SyntaxError(f());l=(null===(n=t.shadowRoot)||void 0===n?void 0:n.querySelector(e[++i]))||null}else l=(null===(o=l.shadowRoot)||void 0===o?void 0:o.querySelector(`${r} ${e[i]}`))||null;if(null===l)return null}return l}function p(e,t){var n;const o=[...e],l=o.pop();if(!o.length)return t.querySelectorAll(l);const s=P(o,t);return(null===(n=null==s?void 0:s.shadowRoot)||void 0===n?void 0:n.querySelectorAll(`${r} ${l}`))||null}function j(e,t){if(1===e.length&&!e[0].length){if(t instanceof Document)throw new SyntaxError(d());if(t instanceof ShadowRoot)throw new SyntaxError(f());return t.shadowRoot}const n=P(e,t);return(null==n?void 0:n.shadowRoot)||null}function E(e,t,n="querySelector",r="shadowRootQuerySelector"){const o=R(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(g(n,r));return e}),l=o.length;for(let t=0;t<l;t++){const n=P(o[t],e);if(n)return n}return null}function q(e,t,n="querySelectorAll"){const r=R(t,e=>{if(!e[e.length-1].length)throw new SyntaxError(g(n));return e}),l=r.length;for(let t=0;t<l;t++){const n=p(r[t],e);if(null==n?void 0:n.length)return n}return document.querySelectorAll(o)}function x(e,t,r="shadowRootQuerySelector",o="querySelector"){const l=R(t,e=>{if(e.pop().length)throw new SyntaxError(function(e,t){return`${e} must be used with a selector ending in a shadowRoot (${n}). If you don't want to select a shadowRoot, use ${t} instead.`}(r,o));return e}),s=l.length;for(let t=0;t<s;t++){const n=j(l[t],e);if(n)return n}return null}const $=(e,t)=>{const n=e.querySelectorAll(t);if(n.length)return n;if(e instanceof Element&&e.shadowRoot){const n=$(e.shadowRoot,t);if(n.length)return n}const r=Array.from(e.querySelectorAll("*"));for(const e of r){const n=$(e,t);if(n.length)return n}return document.querySelectorAll(o)},b=(e,t,n,r,o)=>h(()=>$(e,t),e=>!!e.length,{retries:n,delay:r,shouldReject:o});function Q(e,n,r){return t(this,void 0,void 0,function*(){return function(e,n,r,o,l){return t(this,void 0,void 0,function*(){return h(()=>q(e,n,"asyncQuerySelectorAll"),e=>!!e.length,{retries:r,delay:o,shouldReject:l})})}(...v(e,n,r))})}function A(e,n,r){return t(this,void 0,void 0,function*(){return function(e,n,r,o,l){return t(this,void 0,void 0,function*(){return h(()=>x(e,n,"asyncShadowRootQuerySelector","asyncQuerySelector"),e=>!!e,{retries:r,delay:o,shouldReject:l})})}(...v(e,n,r))})}class N{constructor(e,t){e instanceof Node||e instanceof Promise?(this._element=e,this._asyncParams=Object.assign({retries:l,delay:s,shouldReject:i},t||{})):(this._element=document,this._asyncParams=Object.assign({retries:l,delay:s,shouldReject:i},e||{}))}get element(){return _(this._element).then(e=>{if(e instanceof NodeList){if(e[0])return e[0];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a NodeList with elements.')}if(e)return e;if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "element" method can only be called from a non-null element.')})}get $(){const e=_(this._element).then(e=>{if(e instanceof Document||e instanceof ShadowRoot||null===e||e instanceof NodeList&&0===e.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "$" method can only be called in an element with a ShadowRoot.')}return e instanceof NodeList?A(e[0],n,this._asyncParams):A(e,n,this._asyncParams)});return new N(e,this._asyncParams)}get all(){return _(this._element).then(e=>{if(e instanceof NodeList){if(0===e.length&&this._asyncParams.shouldReject)throw new SyntaxError('The "all" method can only be called in a valid element.');return e}if(!this._asyncParams.shouldReject)return document.querySelectorAll(o);throw new SyntaxError('The "all" method can only be called in a NodeList element.')})}get asyncParams(){return this._asyncParams}eq(e){return t(this,void 0,void 0,function*(){return _(this._element).then(t=>{if(t instanceof NodeList){if(t[e])return t[e];if(!this._asyncParams.shouldReject)return null;throw new SyntaxError(`Could not get any element at index ${e}.`)}if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "eq" method can only be called in a NodeList element.')})})}query(e){const t=_(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "query" method can only be called from a defined element.')}return t instanceof NodeList?Q(t[0],e,this._asyncParams):Q(t,e,this._asyncParams)});return new N(t,this._asyncParams)}deepQuery(e){const t=_(this._element).then(t=>{if(null===t||t instanceof NodeList&&0===t.length){if(!this._asyncParams.shouldReject)return null;throw new SyntaxError('The "deepQuery" method can only be called from a defined element.')}return t instanceof NodeList?Promise.race(Array.from(t).map(t=>b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject))):b(t,e,this._asyncParams.retries,this._asyncParams.delay,this._asyncParams.shouldReject)});return new N(t,this._asyncParams)}}return e.AsyncSelector=N,e.asyncDeepQuerySelector=function(e,n,r){return t(this,void 0,void 0,function*(){return(yield b(...v(e,n,r)))[0]||null})},e.asyncDeepQuerySelectorAll=function(e,t,n){return b(...v(e,t,n))},e.asyncQuerySelector=function(e,n,r){return t(this,void 0,void 0,function*(){return yield function(e,n,r,o,l){return t(this,void 0,void 0,function*(){return h(()=>E(e,n,"asyncQuerySelector","asyncShadowRootQuerySelector"),e=>!!e,{retries:r,delay:o,shouldReject:l})})}(...v(e,n,r))})},e.asyncQuerySelectorAll=Q,e.asyncShadowRootQuerySelector=A,e.deepQuerySelector=function(e,t){return $(...S(e,t))[0]||null},e.deepQuerySelectorAll=function(e,t){return $(...S(e,t))},e.querySelector=function(e,t){return E(...S(e,t))},e.querySelectorAll=function(e,t){return q(...S(e,t))},e.shadowRootQuerySelector=function(e,t){return x(...S(e,t))},e}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shadow-dom-selector",
3
- "version": "6.2.1",
3
+ "version": "6.2.3",
4
4
  "description": "A very small JavaScript utility to query DOM elements through the Shadow Dom subtrees in a sync or an async way",
5
5
  "keywords": [
6
6
  "query-selector",
@@ -59,37 +59,29 @@
59
59
  },
60
60
  "devDependencies": {
61
61
  "@eslint/js": "^10.0.1",
62
- "@playwright/test": "^1.59.1",
62
+ "@playwright/test": "^1.61.0",
63
63
  "@rollup/plugin-node-resolve": "^16.0.3",
64
64
  "@rollup/plugin-terser": "^1.0.0",
65
65
  "@rollup/plugin-typescript": "^12.3.0",
66
66
  "@types/eslint": "^9.6.1",
67
- "@types/node": "^25.6.0",
68
- "eslint": "^10.3.0",
67
+ "@types/node": "^26.0.0",
68
+ "eslint": "^10.5.0",
69
69
  "globals": "^17.6.0",
70
70
  "nyc": "^18.0.0",
71
71
  "playwright-test-coverage": "^1.2.12",
72
- "rollup": "4.60.2",
72
+ "rollup": "4.62.2",
73
73
  "rollup-plugin-dts": "^6.4.1",
74
74
  "rollup-plugin-istanbul": "^5.0.0",
75
75
  "rollup-plugin-serve": "^3.0.0",
76
76
  "rollup-plugin-tsconfig-paths": "^1.5.2",
77
77
  "tslib": "^2.8.1",
78
78
  "typescript": "^6.0.3",
79
- "typescript-eslint": "^8.59.1"
79
+ "typescript-eslint": "^8.61.1"
80
80
  },
81
81
  "dependencies": {
82
82
  "get-promisable-result": "^2.0.0"
83
83
  },
84
84
  "peerDependencies": {
85
85
  "get-promisable-result": "^2.0.0"
86
- },
87
- "pnpm": {
88
- "overrides": {
89
- "brace-expansion@>=4.0.0 <5.0.5": ">=5.0.5",
90
- "picomatch@<2.3.2": ">=2.3.2",
91
- "picomatch@>=4.0.0 <4.0.4": ">=4.0.4",
92
- "serialize-javascript@<7.0.5": ">=7.0.5"
93
- }
94
86
  }
95
87
  }