@oscarpalmer/toretto 0.19.0 → 0.20.0

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.
package/dist/find.cjs CHANGED
@@ -48,13 +48,15 @@ function findElement(selector, context) {
48
48
  return findElementOrElements(selector, context, true);
49
49
  }
50
50
  function findElementOrElements(selector, context, single) {
51
- const callback = single ? document.querySelector : document.querySelectorAll;
52
- const contexts = context == null ? [document] : findElementOrElements(context, void 0, false);
51
+ const callback = single ? "querySelector" : "querySelectorAll";
52
+ const contexts = context == null ? [document] : findElementOrElements(context, void 0, false).filter(
53
+ isContext
54
+ );
53
55
  const result = [];
54
56
  if (typeof selector === "string") {
55
57
  const { length: length2 } = contexts;
56
58
  for (let index = 0; index < length2; index += 1) {
57
- const value = callback.call(contexts[index], selector);
59
+ const value = contexts[index][callback](selector);
58
60
  if (single) {
59
61
  if (value == null) {
60
62
  continue;
@@ -128,6 +130,9 @@ function getElementUnderPointer(skipIgnore) {
128
130
  }
129
131
  return returned.at(-1) ?? null;
130
132
  }
133
+ function isContext(value) {
134
+ return typeof (value == null ? void 0 : value.querySelector) === "function" && typeof (value == null ? void 0 : value.querySelectorAll) === "function";
135
+ }
131
136
  function traverse(from, to) {
132
137
  let index = [...to.children].indexOf(from);
133
138
  if (index > -1) {
package/dist/find.js CHANGED
@@ -46,13 +46,15 @@ function findElement(selector, context) {
46
46
  return findElementOrElements(selector, context, true);
47
47
  }
48
48
  function findElementOrElements(selector, context, single) {
49
- const callback = single ? document.querySelector : document.querySelectorAll;
50
- const contexts = context == null ? [document] : findElementOrElements(context, void 0, false);
49
+ const callback = single ? "querySelector" : "querySelectorAll";
50
+ const contexts = context == null ? [document] : findElementOrElements(context, void 0, false).filter(
51
+ isContext
52
+ );
51
53
  const result = [];
52
54
  if (typeof selector === "string") {
53
55
  const { length: length2 } = contexts;
54
56
  for (let index = 0; index < length2; index += 1) {
55
- const value = callback.call(contexts[index], selector);
57
+ const value = contexts[index][callback](selector);
56
58
  if (single) {
57
59
  if (value == null) {
58
60
  continue;
@@ -126,6 +128,9 @@ function getElementUnderPointer(skipIgnore) {
126
128
  }
127
129
  return returned.at(-1) ?? null;
128
130
  }
131
+ function isContext(value) {
132
+ return typeof (value == null ? void 0 : value.querySelector) === "function" && typeof (value == null ? void 0 : value.querySelectorAll) === "function";
133
+ }
129
134
  function traverse(from, to) {
130
135
  let index = [...to.children].indexOf(from);
131
136
  if (index > -1) {
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "@vitest/coverage-istanbul": "^3.1",
14
14
  "dts-bundle-generator": "^9.5",
15
15
  "glob": "^11",
16
- "happy-dom": "^17.4",
16
+ "jsdom": "^26.1",
17
17
  "typescript": "^5.8",
18
18
  "vite": "^6.3",
19
19
  "vitest": "^3.1"
@@ -148,5 +148,5 @@
148
148
  },
149
149
  "type": "module",
150
150
  "types": "types/index.d.cts",
151
- "version": "0.19.0"
151
+ "version": "0.20.0"
152
152
  }
package/src/find.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type {PlainObject} from '@oscarpalmer/atoms/models';
1
2
  import type {Selector} from './models';
2
3
 
3
4
  /**
@@ -79,23 +80,25 @@ export function findAncestor(
79
80
  * - `context` is optional and defaults to `document`
80
81
  */
81
82
  export function findElement(
82
- selector: string,
83
- context?: Selector,
84
- ): Element | null {
85
- return findElementOrElements(selector, context, true) as never;
86
- }
83
+ selector: string,
84
+ context?: Selector | null,
85
+ ): Element | null {
86
+ return findElementOrElements(selector, context, true) as never;
87
+ }
87
88
 
88
89
  function findElementOrElements(
89
90
  selector: Selector,
90
- context: Selector | undefined,
91
+ context: Selector | null | undefined,
91
92
  single: boolean,
92
93
  ): Element | Element[] | null {
93
- const callback = single ? document.querySelector : document.querySelectorAll;
94
+ const callback = single ? 'querySelector' : 'querySelectorAll';
94
95
 
95
96
  const contexts =
96
97
  context == null
97
98
  ? [document]
98
- : (findElementOrElements(context, undefined, false) as Element[]);
99
+ : (findElementOrElements(context, undefined, false) as Element[]).filter(
100
+ isContext,
101
+ );
99
102
 
100
103
  const result: Element[] = [];
101
104
 
@@ -103,10 +106,9 @@ function findElementOrElements(
103
106
  const {length} = contexts;
104
107
 
105
108
  for (let index = 0; index < length; index += 1) {
106
- const value = callback.call(contexts[index], selector) as
107
- | Element
108
- | Element[]
109
- | null;
109
+ const value = (
110
+ contexts[index][callback] as (selector: string) => Node | null
111
+ )(selector) as Element | Element[] | null;
110
112
 
111
113
  if (single) {
112
114
  if (value == null) {
@@ -166,11 +168,11 @@ function findElementOrElements(
166
168
  * - `context` is optional and defaults to `document`
167
169
  */
168
170
  export function findElements(
169
- selector: Selector,
170
- context?: Selector,
171
- ): Element[] {
172
- return findElementOrElements(selector, context, false) as never;
173
- }
171
+ selector: Selector,
172
+ context?: Selector | null,
173
+ ): Element[] {
174
+ return findElementOrElements(selector, context, false) as never;
175
+ }
174
176
 
175
177
  /**
176
178
  * - Finds the closest elements to the origin element that matches the selector
@@ -261,6 +263,13 @@ export function getElementUnderPointer(skipIgnore?: boolean): Element | null {
261
263
  return returned.at(-1) ?? null;
262
264
  }
263
265
 
266
+ function isContext(value: unknown): boolean {
267
+ return (
268
+ typeof (value as PlainObject)?.querySelector === 'function' &&
269
+ typeof (value as PlainObject)?.querySelectorAll === 'function'
270
+ );
271
+ }
272
+
264
273
  function traverse(from: Element, to: Element): number | undefined {
265
274
  let index = [...to.children].indexOf(from);
266
275
 
package/types/find.d.cts CHANGED
@@ -12,13 +12,13 @@ export declare function findAncestor(origin: Element, selector: string | ((eleme
12
12
  * - Find the first element that matches the selector
13
13
  * - `context` is optional and defaults to `document`
14
14
  */
15
- export declare function findElement(selector: string, context?: Selector): Element | null;
15
+ export declare function findElement(selector: string, context?: Selector | null): Element | null;
16
16
  /**
17
17
  * - Find elements that match the selector
18
18
  * - If `selector` is a node or a list of nodes, they are filtered and returned
19
19
  * - `context` is optional and defaults to `document`
20
20
  */
21
- export declare function findElements(selector: Selector, context?: Selector): Element[];
21
+ export declare function findElements(selector: Selector, context?: Selector | null): Element[];
22
22
  /**
23
23
  * - Finds the closest elements to the origin element that matches the selector
24
24
  * - Traverses up, down, and sideways in the _DOM_-tree
package/types/find.d.ts CHANGED
@@ -10,13 +10,13 @@ export declare function findAncestor(origin: Element, selector: string | ((eleme
10
10
  * - Find the first element that matches the selector
11
11
  * - `context` is optional and defaults to `document`
12
12
  */
13
- export declare function findElement(selector: string, context?: Selector): Element | null;
13
+ export declare function findElement(selector: string, context?: Selector | null): Element | null;
14
14
  /**
15
15
  * - Find elements that match the selector
16
16
  * - If `selector` is a node or a list of nodes, they are filtered and returned
17
17
  * - `context` is optional and defaults to `document`
18
18
  */
19
- export declare function findElements(selector: Selector, context?: Selector): Element[];
19
+ export declare function findElements(selector: Selector, context?: Selector | null): Element[];
20
20
  /**
21
21
  * - Finds the closest elements to the origin element that matches the selector
22
22
  * - Traverses up, down, and sideways in the _DOM_-tree
package/types/index.d.cts CHANGED
@@ -176,13 +176,13 @@ export declare function findAncestor(origin: Element, selector: string | ((eleme
176
176
  * - Find the first element that matches the selector
177
177
  * - `context` is optional and defaults to `document`
178
178
  */
179
- export declare function findElement(selector: string, context?: Selector): Element | null;
179
+ export declare function findElement(selector: string, context?: Selector | null): Element | null;
180
180
  /**
181
181
  * - Find elements that match the selector
182
182
  * - If `selector` is a node or a list of nodes, they are filtered and returned
183
183
  * - `context` is optional and defaults to `document`
184
184
  */
185
- export declare function findElements(selector: Selector, context?: Selector): Element[];
185
+ export declare function findElements(selector: Selector, context?: Selector | null): Element[];
186
186
  /**
187
187
  * - Finds the closest elements to the origin element that matches the selector
188
188
  * - Traverses up, down, and sideways in the _DOM_-tree