@medyll/idae-be 0.79.0 → 0.80.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/be.d.ts CHANGED
@@ -11,7 +11,7 @@ import { WalkHandler } from './modules/walk.js';
11
11
  import { TextHandler, type TextHandlerHandle } from './modules/text.js';
12
12
  import { TimersHandler } from './modules/timers.js';
13
13
  export declare class Be {
14
- node: HTMLElement | HTMLElement[] | string;
14
+ inputNode: HTMLElement | HTMLElement[] | string;
15
15
  isWhat: IsWhat;
16
16
  BeTimer: NodeJS.Timeout | null;
17
17
  BeInterval: NodeJS.Timeout | null;
@@ -88,6 +88,12 @@ export declare class Be {
88
88
  clearTimeout: TimersHandler['clearTimeout'];
89
89
  clearInterval: TimersHandler['clearInterval'];
90
90
  private constructor();
91
+ /**
92
+ * Normalizes the input to ensure `node` is always an HTMLElement or an array of HTMLElements.
93
+ * @param input - The input to normalize (string, HTMLElement, or array of HTMLElements).
94
+ * @returns A valid HTMLElement or an array of HTMLElements.
95
+ */
96
+ private static getNode;
91
97
  static elem(node: HTMLElement | HTMLElement[] | string): Be;
92
98
  static createBe(tagOrHtml: CreateFragment, options?: {
93
99
  is?: string;
@@ -114,6 +120,7 @@ export declare class Be {
114
120
  data?: T;
115
121
  headers?: Record<string, string>;
116
122
  }): Promise<any>;
123
+ get node(): HTMLElement | HTMLElement[];
117
124
  eachNode(callback: (el: HTMLElement) => void, firstChild?: boolean): void;
118
125
  /** DOM
119
126
  * Handles various DOM operations on the element(s).
package/dist/be.js CHANGED
@@ -11,7 +11,7 @@ import { WalkHandler } from './modules/walk.js';
11
11
  import { TextHandler } from './modules/text.js';
12
12
  import { TimersHandler } from './modules/timers.js';
13
13
  export class Be {
14
- node;
14
+ inputNode;
15
15
  isWhat;
16
16
  //
17
17
  BeTimer = null;
@@ -103,8 +103,13 @@ export class Be {
103
103
  if (input instanceof Be) {
104
104
  return input;
105
105
  }
106
- this.node = input;
107
- this.isWhat = typeof input === 'string' ? 'qy' : Array.isArray(input) ? 'array' : 'element';
106
+ this.inputNode = Be.getNode(input);
107
+ this.isWhat =
108
+ typeof this.inputNode === 'string'
109
+ ? 'qy'
110
+ : Array.isArray(this.inputNode)
111
+ ? 'array'
112
+ : 'element';
108
113
  // styles
109
114
  this.styleHandler = new StylesHandler(this);
110
115
  this.styles = this.handle(this.styleHandler);
@@ -150,6 +155,27 @@ export class Be {
150
155
  this.timers = this.handle(this.timerHandler);
151
156
  this.attach(TimersHandler);
152
157
  }
158
+ /**
159
+ * Normalizes the input to ensure `node` is always an HTMLElement or an array of HTMLElements.
160
+ * @param input - The input to normalize (string, HTMLElement, or array of HTMLElements).
161
+ * @returns A valid HTMLElement or an array of HTMLElements.
162
+ */
163
+ static getNode(input) {
164
+ if (typeof input === 'string') {
165
+ // Si `input` est une chaîne, sélectionnez les éléments correspondants
166
+ const elements = Array.from(document.querySelectorAll(input));
167
+ return elements.length === 1 ? elements[0] : elements;
168
+ }
169
+ else if (input instanceof HTMLElement) {
170
+ // Si `input` est un seul élément DOM, retournez-le
171
+ return input;
172
+ }
173
+ else if (Array.isArray(input)) {
174
+ // Si `input` est un tableau, filtrez pour ne garder que les éléments DOM valides
175
+ return input.filter((n) => n instanceof HTMLElement);
176
+ }
177
+ throw new Error('Invalid input: must be a string, HTMLElement, or an array of HTMLElements.');
178
+ }
153
179
  static elem(node) {
154
180
  return new Be(node);
155
181
  }
@@ -239,20 +265,30 @@ export class Be {
239
265
  headers: options.headers || {}
240
266
  }).then((response) => response.json());
241
267
  }
268
+ get node() {
269
+ switch (this.isWhat) {
270
+ case 'element':
271
+ return this.inputNode;
272
+ case 'array':
273
+ return Array.from(this.inputNode);
274
+ case 'qy':
275
+ return Array.from(document.querySelectorAll(this.inputNode));
276
+ }
277
+ }
242
278
  eachNode(callback, firstChild) {
243
279
  switch (this.isWhat) {
244
280
  case 'element':
245
- BeUtils.applyCallback(this.node, callback);
281
+ BeUtils.applyCallback(this.inputNode, callback);
246
282
  break;
247
283
  case 'array':
248
- this.node.forEach((lo) => {
284
+ this.inputNode.forEach((lo) => {
249
285
  BeUtils.applyCallback(lo, callback);
250
286
  if (firstChild)
251
287
  return;
252
288
  });
253
289
  break;
254
290
  case 'qy':
255
- document.querySelectorAll(this.node).forEach((el) => {
291
+ document.querySelectorAll(this.inputNode).forEach((el) => {
256
292
  callback(el);
257
293
  if (firstChild)
258
294
  return;
@@ -266,7 +302,7 @@ export class Be {
266
302
  * @returns The Be instance for method chaining.
267
303
  */
268
304
  get html() {
269
- return this.isWhat === 'element' ? this.node.innerHTML : null;
305
+ return this.isWhat === 'element' ? this.inputNode.innerHTML : null;
270
306
  }
271
307
  attach(Handler, suffix = '') {
272
308
  const fromMethods = Handler.methods || [];
@@ -21,11 +21,11 @@ export class AttrHandler {
21
21
  return this.beElement;
22
22
  }
23
23
  get(name) {
24
- if (typeof this.beElement.node === 'string')
25
- return document.querySelector(this.beElement.node || '')?.getAttribute(name || '') || null;
24
+ if (typeof this.beElement.inputNode === 'string')
25
+ return (document.querySelector(this.beElement.inputNode || '')?.getAttribute(name || '') || null);
26
26
  if (this.beElement.isWhat !== 'element')
27
27
  return null;
28
- const el = this.beElement.node;
28
+ const el = this.beElement.inputNode;
29
29
  return name ? el.getAttribute(name) : null;
30
30
  }
31
31
  set(nameOrObject, value) {
@@ -57,7 +57,7 @@ export class AttrHandler {
57
57
  valueOf() {
58
58
  if (this.beElement.isWhat !== 'element')
59
59
  return null;
60
- const el = this.beElement.node;
60
+ const el = this.beElement.inputNode;
61
61
  const attrs = {};
62
62
  for (let i = 0; i < el.attributes.length; i++) {
63
63
  const attr = el.attributes[i];
@@ -27,7 +27,7 @@ export class DataHandler {
27
27
  // if space in string
28
28
  if (this.beElement.isWhat !== 'element')
29
29
  return null;
30
- return this.beElement.node.dataset[key] || null;
30
+ return this.beElement.inputNode.dataset[key] || null;
31
31
  }
32
32
  set(keyOrObject, value) {
33
33
  this.beElement.eachNode((el) => {
@@ -59,11 +59,11 @@ export class DataHandler {
59
59
  // if spaces in string
60
60
  if (this.beElement.isWhat !== 'element')
61
61
  return null;
62
- return this.beElement.node.dataset[key] || null;
62
+ return this.beElement.inputNode.dataset[key] || null;
63
63
  }
64
64
  valueOf() {
65
65
  if (this.beElement.isWhat !== 'element')
66
66
  return null;
67
- return this.beElement.node.dataset;
67
+ return this.beElement.inputNode.dataset;
68
68
  }
69
69
  }
@@ -229,6 +229,6 @@ export class DomHandler {
229
229
  valueOf() {
230
230
  if (this.beElement.isWhat !== 'element')
231
231
  return null;
232
- return this.beElement.node.innerHTML;
232
+ return this.beElement.inputNode.innerHTML;
233
233
  }
234
234
  }
@@ -44,7 +44,7 @@ export class PositionHandler {
44
44
  if (!sourceEl)
45
45
  return this.beElement;
46
46
  const sourceRect = sourceEl.getBoundingClientRect();
47
- const targetRect = this.beElement.node.getBoundingClientRect();
47
+ const targetRect = this.beElement.inputNode.getBoundingClientRect();
48
48
  const { offsetX = 0, offsetY = 0, useTransform = false } = options;
49
49
  this.beElement.eachNode((el) => {
50
50
  if (useTransform) {
@@ -82,7 +82,7 @@ export class PositionHandler {
82
82
  return this.beElement;
83
83
  const { alignment = 'center', offset = 0, useTransform = false } = options;
84
84
  const targetRect = targetEl.getBoundingClientRect();
85
- const selfRect = this.beElement.node.getBoundingClientRect();
85
+ const selfRect = this.beElement.inputNode.getBoundingClientRect();
86
86
  let x = 0, y = 0;
87
87
  switch (alignment) {
88
88
  case 'center':
@@ -138,7 +138,7 @@ export class PositionHandler {
138
138
  const targetEl = typeof targetElement === 'string' ? document.querySelector(targetElement) : targetElement;
139
139
  if (!targetEl)
140
140
  return this.beElement;
141
- const sourceRect = this.beElement.node.getBoundingClientRect();
141
+ const sourceRect = this.beElement.inputNode.getBoundingClientRect();
142
142
  const targetRect = targetEl.getBoundingClientRect();
143
143
  const { sourceAnchor, targetAnchor, offset = { x: 0, y: 0 } } = options;
144
144
  let sourceX, sourceY, targetX, targetY;
@@ -169,6 +169,6 @@ export class PositionHandler {
169
169
  valueOf() {
170
170
  if (this.beElement.isWhat !== 'element')
171
171
  return null;
172
- return this.beElement.node.getBoundingClientRect();
172
+ return this.beElement.inputNode.getBoundingClientRect();
173
173
  }
174
174
  }
@@ -33,7 +33,7 @@ export class PropsHandler {
33
33
  get(name, callback) {
34
34
  if (this.beElement.isWhat !== 'element')
35
35
  return null;
36
- return this.beElement.node[name];
36
+ return this.beElement.inputNode[name];
37
37
  }
38
38
  set(nameOrObject, value, callback) {
39
39
  this.beElement.eachNode((el) => {
@@ -55,7 +55,7 @@ export class PropsHandler {
55
55
  valueOf() {
56
56
  if (this.beElement.isWhat !== 'element')
57
57
  return null;
58
- const el = this.beElement.node;
58
+ const el = this.beElement.inputNode;
59
59
  const props = {};
60
60
  for (let prop in el) {
61
61
  if (el.hasOwnProperty(prop)) {
@@ -20,7 +20,7 @@ export class TextHandler {
20
20
  get text() {
21
21
  if (this.beElement.isWhat !== 'element')
22
22
  return null;
23
- return this.beElement.node.textContent;
23
+ return this.beElement.inputNode.textContent;
24
24
  }
25
25
  handle(actions) {
26
26
  this.beElement.eachNode((el) => {
@@ -86,6 +86,6 @@ export class TextHandler {
86
86
  valueOf() {
87
87
  if (this.beElement.isWhat !== 'element')
88
88
  return null;
89
- return this.beElement.node.innerText;
89
+ return this.beElement.inputNode.innerText;
90
90
  }
91
91
  }
@@ -114,6 +114,7 @@ export class WalkHandler {
114
114
  this.beElement.eachNode((el) => {
115
115
  if (el.parentNode) {
116
116
  const siblings = Array.from(el.parentNode.children).filter((child) => child !== el);
117
+ console.log('siblings found:', siblings); // Debug
117
118
  ret.push(...siblings.filter((sibling) => !qy || sibling.matches(qy)));
118
119
  }
119
120
  });
@@ -186,15 +187,19 @@ export class WalkHandler {
186
187
  find(qy, callback) {
187
188
  const ret = [];
188
189
  this.beElement.eachNode((el) => {
189
- ret.push(el.querySelector(qy));
190
+ const found = el.querySelector(qy);
191
+ console.log('find result:', found); // Debug
192
+ if (found)
193
+ ret.push(found);
190
194
  });
195
+ const resultBe = Be.elem(ret); // Encapsule les résultats dans une instance de Be
191
196
  callback?.({
192
197
  root: this.beElement,
193
- be: Be.elem(ret),
198
+ be: resultBe,
194
199
  fragment: 'result',
195
- requested: Be.elem(ret)
200
+ requested: resultBe
196
201
  });
197
- return this.beElement;
202
+ return resultBe;
198
203
  }
199
204
  /**
200
205
  * Finds all descendants that match the selector.
@@ -225,18 +230,22 @@ export class WalkHandler {
225
230
  try {
226
231
  const ret = [];
227
232
  this.beElement.eachNode((el) => {
228
- const results = this.selectWhile(el, method, qy);
229
- ret.push(...results);
233
+ const result = this.selectWhile(el, method, qy);
234
+ console.log(`methodize result for ${method}:`, typeof result, result); // Debug
235
+ if (result)
236
+ ret.push(...(Array.isArray(result) ? result : [result]));
230
237
  });
238
+ const resultBe = Be.elem(ret);
231
239
  callback?.({
232
240
  root: this.beElement,
233
- be: Be.elem(ret),
241
+ be: resultBe,
234
242
  fragment: 'result',
235
- requested: Be.elem(ret)
243
+ requested: resultBe
236
244
  });
245
+ return resultBe;
237
246
  }
238
247
  catch (e) {
239
- console.log(e);
248
+ console.error(`Error in methodize for ${method}:`, e); // Debug
240
249
  }
241
250
  return this.beElement;
242
251
  };
@@ -250,31 +259,47 @@ export class WalkHandler {
250
259
  */
251
260
  selectWhile(element, direction, selector) {
252
261
  const dict = {
253
- up: 'parentNode',
254
- parent: 'parentNode',
262
+ up: 'parentElement',
255
263
  next: 'nextElementSibling',
256
264
  previous: 'previousElementSibling',
257
- siblings: 'nextElementSibling',
265
+ siblings: 'parentElement',
258
266
  children: 'children',
259
267
  firstChild: 'firstElementChild',
260
268
  lastChild: 'lastElementChild',
261
269
  closest: 'closest'
262
270
  };
263
- const property = dict[direction] ?? direction;
264
- if (property === 'children') {
271
+ const property = dict[direction];
272
+ // Handle recursive traversal for `up`
273
+ if (direction === 'up') {
274
+ let current = element;
275
+ while (current) {
276
+ current = current[property];
277
+ if (!selector || (current && current.matches(selector))) {
278
+ return current;
279
+ }
280
+ }
281
+ return null;
282
+ }
283
+ // Handle `siblings`
284
+ if (direction === 'siblings') {
285
+ const parent = element.parentElement;
286
+ if (!parent)
287
+ return [];
288
+ const siblings = Array.from(parent.children).filter((child) => child !== element);
289
+ return selector ? siblings.filter((sibling) => sibling.matches(selector)) : siblings;
290
+ }
291
+ // Handle `children`
292
+ if (direction === 'children') {
265
293
  const children = Array.from(element.children);
266
294
  return selector ? children.filter((child) => child.matches(selector)) : children;
267
295
  }
268
- if (property === 'closest') {
269
- return element.closest(selector ?? '*');
270
- }
271
- let sibling = element[property];
272
- while (sibling) {
273
- if (!selector || sibling.matches(selector)) {
274
- return sibling;
275
- }
276
- sibling = sibling[property];
296
+ // Handle `closest`
297
+ if (direction === 'closest') {
298
+ const closest = element.closest(selector ?? '*');
299
+ return closest;
277
300
  }
278
- return null;
301
+ // Handle single-step traversal (e.g., `next`, `previous`, `firstChild`, `lastChild`)
302
+ const target = element[property];
303
+ return target && (!selector || target.matches(selector)) ? target : null;
279
304
  }
280
305
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@medyll/idae-be",
3
3
  "scope": "@medyll",
4
- "version": "0.79.0",
4
+ "version": "0.80.0",
5
5
  "description": "A powerful DOM manipulation library with a callback-based approach for precise element targeting. Provides consistent chaining, comprehensive DOM traversal, event handling, style management, and more. Written in TypeScript for modern browsers.",
6
6
  "scripts": {
7
7
  "dev": "vite dev",