tiny-essentials 1.22.4 → 1.22.5

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.
@@ -6518,6 +6518,291 @@ class TinyHtml {
6518
6518
  return propName;
6519
6519
  }
6520
6520
 
6521
+ ////////////////////////////////////////////////////////////////////
6522
+
6523
+ /**
6524
+ * Returns the BigInt value of an attribute.
6525
+ * @param {TinyElement} el
6526
+ * @param {string} name
6527
+ * @returns {bigint|null}
6528
+ */
6529
+ static attrBigInt(el, name) {
6530
+ const elem = TinyHtml._preElem(el, 'attrBigInt');
6531
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
6532
+ if (val == null) return null;
6533
+ try {
6534
+ return BigInt(val);
6535
+ } catch {
6536
+ return null;
6537
+ }
6538
+ }
6539
+
6540
+ /**
6541
+ * Returns the BigInt value of an attribute.
6542
+ * @param {string} name
6543
+ * @returns {bigint|null}
6544
+ */
6545
+ attrBigInt(name) {
6546
+ return TinyHtml.attrBigInt(this, name);
6547
+ }
6548
+
6549
+ /**
6550
+ * Set a BigInt attribute.
6551
+ * @template {TinyElement|TinyElement[]} T
6552
+ * @param {T} el
6553
+ * @param {string} name
6554
+ * @param {bigint} value
6555
+ * @returns {T}
6556
+ */
6557
+ static setAttrBigInt(el, name, value) {
6558
+ if (typeof value !== 'bigint') throw new Error('Value is not a valid BigInt.');
6559
+ return TinyHtml.setAttr(el, name, value.toString());
6560
+ }
6561
+
6562
+ /**
6563
+ * Set a BigInt attribute.
6564
+ * @param {string} name
6565
+ * @param {bigint} value
6566
+ * @returns {this}
6567
+ */
6568
+ setAttrBigInt(name, value) {
6569
+ return TinyHtml.setAttrBigInt(this, name, value);
6570
+ }
6571
+
6572
+ /**
6573
+ * Returns the Date value of an attribute.
6574
+ * @param {TinyElement} el
6575
+ * @param {string} name
6576
+ * @returns {Date|null}
6577
+ */
6578
+ static attrDate(el, name) {
6579
+ const elem = TinyHtml._preElem(el, 'attrDate');
6580
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
6581
+ if (!val) return null;
6582
+ const d = new Date(val);
6583
+ return Number.isNaN(d.getTime()) ? null : d;
6584
+ }
6585
+
6586
+ /**
6587
+ * Returns the Date value of an attribute.
6588
+ * @param {string} name
6589
+ * @returns {Date|null}
6590
+ */
6591
+ attrDate(name) {
6592
+ return TinyHtml.attrDate(this, name);
6593
+ }
6594
+
6595
+ /**
6596
+ * Set a Date attribute.
6597
+ * @template {TinyElement|TinyElement[]} T
6598
+ * @param {T} el
6599
+ * @param {string} name
6600
+ * @param {Date} value
6601
+ * @returns {T}
6602
+ */
6603
+ static setAttrDate(el, name, value) {
6604
+ if (!(value instanceof Date) || Number.isNaN(value.getTime()))
6605
+ throw new Error('Value is not a valid Date.');
6606
+ return TinyHtml.setAttr(el, name, value.toISOString());
6607
+ }
6608
+
6609
+ /**
6610
+ * Set a Date attribute.
6611
+ * @param {string} name
6612
+ * @param {Date} value
6613
+ * @returns {this}
6614
+ */
6615
+ setAttrDate(name, value) {
6616
+ return TinyHtml.setAttrDate(this, name, value);
6617
+ }
6618
+
6619
+ /**
6620
+ * Returns the JSON value of an attribute.
6621
+ * @param {TinyElement} el
6622
+ * @param {string} name
6623
+ * @returns {any|null}
6624
+ */
6625
+ static attrJson(el, name) {
6626
+ const elem = TinyHtml._preElem(el, 'attrJson');
6627
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
6628
+ if (!val) return null;
6629
+ try {
6630
+ return JSON.parse(val);
6631
+ } catch {
6632
+ return null;
6633
+ }
6634
+ }
6635
+
6636
+ /**
6637
+ * Returns the JSON value of an attribute.
6638
+ * @param {string} name
6639
+ * @returns {any|null}
6640
+ */
6641
+ attrJson(name) {
6642
+ return TinyHtml.attrJson(this, name);
6643
+ }
6644
+
6645
+ /**
6646
+ * Set a JSON attribute.
6647
+ * @template {TinyElement|TinyElement[]} T
6648
+ * @param {T} el
6649
+ * @param {string} name
6650
+ * @param {any} value
6651
+ * @param {(this: any, key: string, value: any) => any} [replacer]
6652
+ * @param {number|string} [space]
6653
+ * @returns {T}
6654
+ */
6655
+ static setAttrJson(el, name, value, replacer, space) {
6656
+ const data = JSON.stringify(value, replacer, space);
6657
+ return TinyHtml.setAttr(el, name, data);
6658
+ }
6659
+
6660
+ /**
6661
+ * Set a JSON attribute.
6662
+ * @param {string} name
6663
+ * @param {any} value
6664
+ * @param {(this: any, key: string, value: any) => any} [replacer]
6665
+ * @param {number|string} [space]
6666
+ * @returns {this}
6667
+ */
6668
+ setAttrJson(name, value, replacer, space) {
6669
+ return TinyHtml.setAttrJson(this, name, value, replacer, space);
6670
+ }
6671
+
6672
+ /**
6673
+ * Returns the number value of an attribute.
6674
+ * @param {TinyElement} el
6675
+ * @param {string} name
6676
+ * @returns {number|null}
6677
+ */
6678
+ static attrNumber(el, name) {
6679
+ const elem = TinyHtml._preElem(el, 'attrNumber');
6680
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
6681
+ return val != null ? parseFloat(val) : null;
6682
+ }
6683
+
6684
+ /**
6685
+ * Returns the number value of an attribute.
6686
+ * @param {string} name
6687
+ * @returns {number|null}
6688
+ */
6689
+ attrNumber(name) {
6690
+ return TinyHtml.attrNumber(this, name);
6691
+ }
6692
+
6693
+ /**
6694
+ * Set a number attribute.
6695
+ * @template {TinyElement|TinyElement[]} T
6696
+ * @param {T} el
6697
+ * @param {string} name
6698
+ * @param {number} value
6699
+ * @returns {T}
6700
+ */
6701
+ static setAttrNumber(el, name, value) {
6702
+ if (typeof value !== 'number') throw new Error('Value is not a valid number.');
6703
+ return TinyHtml.setAttr(el, name, value.toString());
6704
+ }
6705
+
6706
+ /**
6707
+ * Set a number attribute.
6708
+ * @param {string} name
6709
+ * @param {number} value
6710
+ * @returns {this}
6711
+ */
6712
+ setAttrNumber(name, value) {
6713
+ return TinyHtml.setAttrNumber(this, name, value);
6714
+ }
6715
+
6716
+ /**
6717
+ * Returns the boolean value of an attribute.
6718
+ * @param {TinyElement} el
6719
+ * @param {string} name
6720
+ * @returns {boolean|null}
6721
+ */
6722
+ static attrBoolean(el, name) {
6723
+ const elem = TinyHtml._preElem(el, 'attrBoolean');
6724
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
6725
+ if (val === null) return null;
6726
+ if (val !== 'true' && val !== 'false') return null;
6727
+ return /^true$/i.test(val);
6728
+ }
6729
+
6730
+ /**
6731
+ * Returns the boolean value of an attribute.
6732
+ * @param {string} name
6733
+ * @returns {boolean|null}
6734
+ */
6735
+ attrBoolean(name) {
6736
+ return TinyHtml.attrBoolean(this, name);
6737
+ }
6738
+
6739
+ /**
6740
+ * Set a boolean attribute.
6741
+ * @template {TinyElement|TinyElement[]} T
6742
+ * @param {T} el
6743
+ * @param {string} name
6744
+ * @param {boolean} value
6745
+ * @returns {T}
6746
+ */
6747
+ static setAttrBoolean(el, name, value) {
6748
+ if (typeof value !== 'boolean') throw new Error('Value is not a valid boolean.');
6749
+ return TinyHtml.setAttr(el, name, value.toString());
6750
+ }
6751
+
6752
+ /**
6753
+ * Set a boolean attribute.
6754
+ * @param {string} name
6755
+ * @param {boolean} value
6756
+ * @returns {this}
6757
+ */
6758
+ setAttrBoolean(name, value) {
6759
+ return TinyHtml.setAttrBoolean(this, name, value);
6760
+ }
6761
+
6762
+ /**
6763
+ * Returns the string value of an attribute.
6764
+ * @param {TinyElement} el
6765
+ * @param {string} name
6766
+ * @returns {string|null}
6767
+ */
6768
+ static attrString(el, name) {
6769
+ const elem = TinyHtml._preElem(el, 'attrString');
6770
+ const value = elem.getAttribute(TinyHtml.getAttrName(name));
6771
+ return typeof value === 'string' ? value : null;
6772
+ }
6773
+
6774
+ /**
6775
+ * Returns the string value of an attribute.
6776
+ * @param {string} name
6777
+ * @returns {string|null}
6778
+ */
6779
+ attrString(name) {
6780
+ return TinyHtml.attrString(this, name);
6781
+ }
6782
+
6783
+ /**
6784
+ * Set a string attribute.
6785
+ * @template {TinyElement|TinyElement[]} T
6786
+ * @param {T} el
6787
+ * @param {string} name
6788
+ * @param {string} value
6789
+ * @returns {T}
6790
+ */
6791
+ static setAttrString(el, name, value) {
6792
+ if (typeof value !== 'string') throw new Error('Value is not a valid string.');
6793
+ return TinyHtml.setAttr(el, name, value);
6794
+ }
6795
+
6796
+ /**
6797
+ * Set a string attribute.
6798
+ * @param {string} name
6799
+ * @param {string} value
6800
+ * @returns {this}
6801
+ */
6802
+ setAttrString(name, value) {
6803
+ return TinyHtml.setAttrString(this, name, value);
6804
+ }
6805
+
6521
6806
  /**
6522
6807
  * Get an attribute on an element.
6523
6808
  * @param {TinyElement} el
@@ -6543,8 +6828,8 @@ class TinyHtml {
6543
6828
  * Set one or multiple attributes on an element.
6544
6829
  * @template {TinyElement|TinyElement[]} T
6545
6830
  * @param {T} el - Target element(s).
6546
- * @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
6547
- * @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
6831
+ * @param {string|Record<string, any>} name - Attribute name or an object of attributes.
6832
+ * @param {any} [value=null] - Attribute value (ignored if "name" is an object).
6548
6833
  * @returns {T}
6549
6834
  */
6550
6835
  static setAttr(el, name, value = null) {
@@ -6552,25 +6837,20 @@ class TinyHtml {
6552
6837
 
6553
6838
  // Multiple attributes at once
6554
6839
  if (typeof name === 'object' && name !== null) {
6555
- Object.entries(name).forEach(([attr, val]) => {
6556
- if (val !== null && typeof val !== 'string' && typeof val !== 'number')
6557
- throw new TypeError(`The value for "${attr}" must be a string/number or null.`);
6840
+ Object.entries(name).forEach(([attr, val]) =>
6558
6841
  elems.forEach((elem) => {
6559
6842
  if (val === null) elem.removeAttribute(TinyHtml.getAttrName(attr));
6560
- else elem.setAttribute(TinyHtml.getAttrName(attr), val);
6561
- });
6562
- });
6843
+ else elem.setAttribute(TinyHtml.getAttrName(attr), String(val));
6844
+ }),
6845
+ );
6563
6846
  return el;
6564
6847
  }
6565
6848
 
6566
6849
  // Single attribute
6567
6850
  if (typeof name !== 'string') throw new TypeError('The "name" must be a string.');
6568
- if (value !== null && typeof value !== 'string')
6569
- throw new TypeError('The "value" must be a string.');
6570
-
6571
6851
  elems.forEach((elem) => {
6572
6852
  if (value === null) elem.removeAttribute(TinyHtml.getAttrName(name));
6573
- else elem.setAttribute(TinyHtml.getAttrName(name), value);
6853
+ else elem.setAttribute(TinyHtml.getAttrName(name), String(value));
6574
6854
  });
6575
6855
 
6576
6856
  return el;
@@ -6578,8 +6858,8 @@ class TinyHtml {
6578
6858
 
6579
6859
  /**
6580
6860
  * Set one or multiple attributes on an element.
6581
- * @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
6582
- * @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
6861
+ * @param {string|Record<string, any>} name - Attribute name or an object of attributes.
6862
+ * @param {any} [value=null] - Attribute value (ignored if "name" is an object).
6583
6863
  * @returns {this}
6584
6864
  */
6585
6865
  setAttr(name, value) {
@@ -2346,6 +2346,104 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
2346
2346
  * @returns {string} - The corresponding attribute name.
2347
2347
  */
2348
2348
  static getAttrName(name: string): string;
2349
+ /**
2350
+ * Returns the BigInt value of an attribute.
2351
+ * @param {TinyElement} el
2352
+ * @param {string} name
2353
+ * @returns {bigint|null}
2354
+ */
2355
+ static attrBigInt(el: TinyElement, name: string): bigint | null;
2356
+ /**
2357
+ * Set a BigInt attribute.
2358
+ * @template {TinyElement|TinyElement[]} T
2359
+ * @param {T} el
2360
+ * @param {string} name
2361
+ * @param {bigint} value
2362
+ * @returns {T}
2363
+ */
2364
+ static setAttrBigInt<T extends TinyElement | TinyElement[]>(el: T, name: string, value: bigint): T;
2365
+ /**
2366
+ * Returns the Date value of an attribute.
2367
+ * @param {TinyElement} el
2368
+ * @param {string} name
2369
+ * @returns {Date|null}
2370
+ */
2371
+ static attrDate(el: TinyElement, name: string): Date | null;
2372
+ /**
2373
+ * Set a Date attribute.
2374
+ * @template {TinyElement|TinyElement[]} T
2375
+ * @param {T} el
2376
+ * @param {string} name
2377
+ * @param {Date} value
2378
+ * @returns {T}
2379
+ */
2380
+ static setAttrDate<T extends TinyElement | TinyElement[]>(el: T, name: string, value: Date): T;
2381
+ /**
2382
+ * Returns the JSON value of an attribute.
2383
+ * @param {TinyElement} el
2384
+ * @param {string} name
2385
+ * @returns {any|null}
2386
+ */
2387
+ static attrJson(el: TinyElement, name: string): any | null;
2388
+ /**
2389
+ * Set a JSON attribute.
2390
+ * @template {TinyElement|TinyElement[]} T
2391
+ * @param {T} el
2392
+ * @param {string} name
2393
+ * @param {any} value
2394
+ * @param {(this: any, key: string, value: any) => any} [replacer]
2395
+ * @param {number|string} [space]
2396
+ * @returns {T}
2397
+ */
2398
+ static setAttrJson<T extends TinyElement | TinyElement[]>(el: T, name: string, value: any, replacer?: (this: any, key: string, value: any) => any, space?: number | string): T;
2399
+ /**
2400
+ * Returns the number value of an attribute.
2401
+ * @param {TinyElement} el
2402
+ * @param {string} name
2403
+ * @returns {number|null}
2404
+ */
2405
+ static attrNumber(el: TinyElement, name: string): number | null;
2406
+ /**
2407
+ * Set a number attribute.
2408
+ * @template {TinyElement|TinyElement[]} T
2409
+ * @param {T} el
2410
+ * @param {string} name
2411
+ * @param {number} value
2412
+ * @returns {T}
2413
+ */
2414
+ static setAttrNumber<T extends TinyElement | TinyElement[]>(el: T, name: string, value: number): T;
2415
+ /**
2416
+ * Returns the boolean value of an attribute.
2417
+ * @param {TinyElement} el
2418
+ * @param {string} name
2419
+ * @returns {boolean|null}
2420
+ */
2421
+ static attrBoolean(el: TinyElement, name: string): boolean | null;
2422
+ /**
2423
+ * Set a boolean attribute.
2424
+ * @template {TinyElement|TinyElement[]} T
2425
+ * @param {T} el
2426
+ * @param {string} name
2427
+ * @param {boolean} value
2428
+ * @returns {T}
2429
+ */
2430
+ static setAttrBoolean<T extends TinyElement | TinyElement[]>(el: T, name: string, value: boolean): T;
2431
+ /**
2432
+ * Returns the string value of an attribute.
2433
+ * @param {TinyElement} el
2434
+ * @param {string} name
2435
+ * @returns {string|null}
2436
+ */
2437
+ static attrString(el: TinyElement, name: string): string | null;
2438
+ /**
2439
+ * Set a string attribute.
2440
+ * @template {TinyElement|TinyElement[]} T
2441
+ * @param {T} el
2442
+ * @param {string} name
2443
+ * @param {string} value
2444
+ * @returns {T}
2445
+ */
2446
+ static setAttrString<T extends TinyElement | TinyElement[]>(el: T, name: string, value: string): T;
2349
2447
  /**
2350
2448
  * Get an attribute on an element.
2351
2449
  * @param {TinyElement} el
@@ -2357,11 +2455,11 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
2357
2455
  * Set one or multiple attributes on an element.
2358
2456
  * @template {TinyElement|TinyElement[]} T
2359
2457
  * @param {T} el - Target element(s).
2360
- * @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
2361
- * @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
2458
+ * @param {string|Record<string, any>} name - Attribute name or an object of attributes.
2459
+ * @param {any} [value=null] - Attribute value (ignored if "name" is an object).
2362
2460
  * @returns {T}
2363
2461
  */
2364
- static setAttr<T extends TinyElement | TinyElement[]>(el: T, name: string | Record<string, string | null>, value?: string | number | null): T;
2462
+ static setAttr<T extends TinyElement | TinyElement[]>(el: T, name: string | Record<string, any>, value?: any): T;
2365
2463
  /**
2366
2464
  * Remove attribute(s) from an element.
2367
2465
  * @template {TinyElement|TinyElement[]} T
@@ -3528,6 +3626,86 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
3528
3626
  * @returns {this}
3529
3627
  */
3530
3628
  trigger(events: string | string[], payload?: Event | CustomEvent | CustomEventInit): this;
3629
+ /**
3630
+ * Returns the BigInt value of an attribute.
3631
+ * @param {string} name
3632
+ * @returns {bigint|null}
3633
+ */
3634
+ attrBigInt(name: string): bigint | null;
3635
+ /**
3636
+ * Set a BigInt attribute.
3637
+ * @param {string} name
3638
+ * @param {bigint} value
3639
+ * @returns {this}
3640
+ */
3641
+ setAttrBigInt(name: string, value: bigint): this;
3642
+ /**
3643
+ * Returns the Date value of an attribute.
3644
+ * @param {string} name
3645
+ * @returns {Date|null}
3646
+ */
3647
+ attrDate(name: string): Date | null;
3648
+ /**
3649
+ * Set a Date attribute.
3650
+ * @param {string} name
3651
+ * @param {Date} value
3652
+ * @returns {this}
3653
+ */
3654
+ setAttrDate(name: string, value: Date): this;
3655
+ /**
3656
+ * Returns the JSON value of an attribute.
3657
+ * @param {string} name
3658
+ * @returns {any|null}
3659
+ */
3660
+ attrJson(name: string): any | null;
3661
+ /**
3662
+ * Set a JSON attribute.
3663
+ * @param {string} name
3664
+ * @param {any} value
3665
+ * @param {(this: any, key: string, value: any) => any} [replacer]
3666
+ * @param {number|string} [space]
3667
+ * @returns {this}
3668
+ */
3669
+ setAttrJson(name: string, value: any, replacer?: (this: any, key: string, value: any) => any, space?: number | string): this;
3670
+ /**
3671
+ * Returns the number value of an attribute.
3672
+ * @param {string} name
3673
+ * @returns {number|null}
3674
+ */
3675
+ attrNumber(name: string): number | null;
3676
+ /**
3677
+ * Set a number attribute.
3678
+ * @param {string} name
3679
+ * @param {number} value
3680
+ * @returns {this}
3681
+ */
3682
+ setAttrNumber(name: string, value: number): this;
3683
+ /**
3684
+ * Returns the boolean value of an attribute.
3685
+ * @param {string} name
3686
+ * @returns {boolean|null}
3687
+ */
3688
+ attrBoolean(name: string): boolean | null;
3689
+ /**
3690
+ * Set a boolean attribute.
3691
+ * @param {string} name
3692
+ * @param {boolean} value
3693
+ * @returns {this}
3694
+ */
3695
+ setAttrBoolean(name: string, value: boolean): this;
3696
+ /**
3697
+ * Returns the string value of an attribute.
3698
+ * @param {string} name
3699
+ * @returns {string|null}
3700
+ */
3701
+ attrString(name: string): string | null;
3702
+ /**
3703
+ * Set a string attribute.
3704
+ * @param {string} name
3705
+ * @param {string} value
3706
+ * @returns {this}
3707
+ */
3708
+ setAttrString(name: string, value: string): this;
3531
3709
  /**
3532
3710
  * Get an attribute on an element.
3533
3711
  * @param {string} name
@@ -3536,11 +3714,11 @@ declare class TinyHtml<TinyHtmlT extends ConstructorElValues | ConstructorElValu
3536
3714
  attr(name: string): string | null;
3537
3715
  /**
3538
3716
  * Set one or multiple attributes on an element.
3539
- * @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
3540
- * @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
3717
+ * @param {string|Record<string, any>} name - Attribute name or an object of attributes.
3718
+ * @param {any} [value=null] - Attribute value (ignored if "name" is an object).
3541
3719
  * @returns {this}
3542
3720
  */
3543
- setAttr(name: string | Record<string, string | null>, value?: string | number | null): this;
3721
+ setAttr(name: string | Record<string, any>, value?: any): this;
3544
3722
  /**
3545
3723
  * Remove attribute(s) from an element.
3546
3724
  * @param {string} name Space-separated list of attributes.