tiny-essentials 1.21.3 → 1.21.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.
@@ -2806,6 +2806,27 @@ class TinyHtml {
2806
2806
  return TinyHtml.blur(this);
2807
2807
  }
2808
2808
 
2809
+ /**
2810
+ * Select the text content of an input or textarea element.
2811
+ *
2812
+ * @param {TinyHtml|HTMLInputElement|HTMLTextAreaElement} el - The element or a selector string.
2813
+ * @returns {TinyHtml|HTMLInputElement|HTMLTextAreaElement}
2814
+ */
2815
+ static select(el) {
2816
+ const elem = TinyHtml._preHtmlElem(el, 'select');
2817
+ if (elem instanceof HTMLInputElement || elem instanceof HTMLTextAreaElement) elem.select();
2818
+ else throw new Error('Element must be an <input> or <textarea> to use select().');
2819
+ return el;
2820
+ }
2821
+
2822
+ /**
2823
+ * Select the text content of an input or textarea element.
2824
+ * @returns {TinyHtml|HTMLInputElement|HTMLTextAreaElement}
2825
+ */
2826
+ select() {
2827
+ return TinyHtml.select(this);
2828
+ }
2829
+
2809
2830
  /**
2810
2831
  * Interprets a value as a boolean `true` if it matches a common truthy representation.
2811
2832
  *
@@ -3852,6 +3873,182 @@ class TinyHtml {
3852
3873
  return TinyHtml.id(this);
3853
3874
  }
3854
3875
 
3876
+ /**
3877
+ * Returns the BigInt content of the element.
3878
+ * @param {TinyElement} el - Target element.
3879
+ * @returns {bigint|null} The BigInt content or null if none.
3880
+ */
3881
+ static toBigInt(el) {
3882
+ const elem = TinyHtml._preElem(el, 'toBigInt');
3883
+ const txt = elem.textContent?.trim();
3884
+ let value = null;
3885
+ try {
3886
+ value = BigInt(txt ?? '');
3887
+ } catch {
3888
+ value = null;
3889
+ }
3890
+ return value;
3891
+ }
3892
+
3893
+ /**
3894
+ * Returns the BigInt content of the element.
3895
+ * @returns {bigint|null}
3896
+ */
3897
+ toBigInt() {
3898
+ return TinyHtml.toBigInt(this);
3899
+ }
3900
+
3901
+ /**
3902
+ * Set BigInt content of elements.
3903
+ * @param {TinyElement|TinyElement[]} el
3904
+ * @param {bigint} value
3905
+ * @returns {TinyElement|TinyElement[]}
3906
+ */
3907
+ static setBigInt(el, value) {
3908
+ if (typeof value !== 'bigint') throw new Error('Value is not a valid BigInt.');
3909
+ TinyHtml._preElems(el, 'setBigInt').forEach((el) => (el.textContent = value.toString()));
3910
+ return el;
3911
+ }
3912
+
3913
+ /**
3914
+ * Set BigInt content of the element.
3915
+ * @param {bigint} value
3916
+ * @returns {TinyElement|TinyElement[]}
3917
+ */
3918
+ setBigInt(value) {
3919
+ return TinyHtml.setBigInt(this, value);
3920
+ }
3921
+
3922
+ /**
3923
+ * Returns the Date content of the element.
3924
+ * @param {TinyElement} el - Target element.
3925
+ * @returns {Date|null} The Date content or null if invalid.
3926
+ */
3927
+ static toDate(el) {
3928
+ const elem = TinyHtml._preElem(el, 'toDate');
3929
+ const txt = elem.textContent?.trim();
3930
+ if (!txt) return null;
3931
+ const d = new Date(txt);
3932
+ return Number.isNaN(d.getTime()) ? null : d;
3933
+ }
3934
+
3935
+ /**
3936
+ * Returns the Date content of the element.
3937
+ * @returns {Date|null}
3938
+ */
3939
+ toDate() {
3940
+ return TinyHtml.toDate(this);
3941
+ }
3942
+
3943
+ /**
3944
+ * Set Date content of elements.
3945
+ * @param {TinyElement|TinyElement[]} el
3946
+ * @param {Date} value
3947
+ * @returns {TinyElement|TinyElement[]}
3948
+ */
3949
+ static setDate(el, value) {
3950
+ if (!(value instanceof Date) || Number.isNaN(value.getTime()))
3951
+ throw new Error('Value is not a valid Date.');
3952
+ TinyHtml._preElems(el, 'setDate').forEach((el) => (el.textContent = value.toISOString()));
3953
+ return el;
3954
+ }
3955
+
3956
+ /**
3957
+ * Set Date content of the element.
3958
+ * @param {Date} value
3959
+ * @returns {TinyElement|TinyElement[]}
3960
+ */
3961
+ setDate(value) {
3962
+ return TinyHtml.setDate(this, value);
3963
+ }
3964
+
3965
+ /**
3966
+ * Returns the JSON content of the element.
3967
+ * @param {TinyElement} el - Target element.
3968
+ * @returns {any|null} The parsed JSON content or null if invalid.
3969
+ */
3970
+ static toJson(el) {
3971
+ const elem = TinyHtml._preElem(el, 'toJson');
3972
+ const txt = elem.textContent?.trim();
3973
+ if (!txt) return null;
3974
+ try {
3975
+ return JSON.parse(txt);
3976
+ } catch {
3977
+ return null;
3978
+ }
3979
+ }
3980
+
3981
+ /**
3982
+ * Returns the JSON content of the element.
3983
+ * @returns {any|null}
3984
+ */
3985
+ toJson() {
3986
+ return TinyHtml.toJson(this);
3987
+ }
3988
+
3989
+ /**
3990
+ * Set JSON content of elements.
3991
+ * @param {TinyElement|TinyElement[]} el
3992
+ * @param {any} value
3993
+ * @param {number|string} [space] - Indentation level or string for formatting.
3994
+ * @returns {TinyElement|TinyElement[]}
3995
+ */
3996
+ static setJson(el, value, space) {
3997
+ TinyHtml._preElems(el, 'setJson').forEach(
3998
+ (el) => (el.textContent = JSON.stringify(value, null, space)),
3999
+ );
4000
+ return el;
4001
+ }
4002
+
4003
+ /**
4004
+ * Set JSON content of the element.
4005
+ * @param {any} value
4006
+ * @param {number|string} [space] - Indentation level or string for formatting.
4007
+ * @returns {TinyElement|TinyElement[]}
4008
+ */
4009
+ setJson(value, space) {
4010
+ return TinyHtml.setJson(this, value, space);
4011
+ }
4012
+
4013
+ /**
4014
+ * Returns the number content of the element.
4015
+ * @param {TinyElement} el - Target element.
4016
+ * @returns {number|null} The text content or null if none.
4017
+ */
4018
+ static toNumber(el) {
4019
+ const elem = TinyHtml._preElem(el, 'toNumber');
4020
+ return typeof elem.textContent === 'string' ? parseFloat(elem.textContent.trim()) : null;
4021
+ }
4022
+
4023
+ /**
4024
+ * Returns the number content of the element.
4025
+ * @returns {number|null} The text content or null if none.
4026
+ */
4027
+ toNumber() {
4028
+ return TinyHtml.toNumber(this);
4029
+ }
4030
+
4031
+ /**
4032
+ * Set number content of elements.
4033
+ * @param {TinyElement|TinyElement[]} el
4034
+ * @param {number} value
4035
+ * @returns {TinyElement|TinyElement[]}
4036
+ */
4037
+ static setNumber(el, value) {
4038
+ if (typeof value !== 'number') throw new Error('Value is not a valid number.');
4039
+ TinyHtml._preElems(el, 'setNumber').forEach((el) => (el.textContent = value.toString()));
4040
+ return el;
4041
+ }
4042
+
4043
+ /**
4044
+ * Set number content of the element.
4045
+ * @param {number} value
4046
+ * @returns {TinyElement|TinyElement[]}
4047
+ */
4048
+ setNumber(value) {
4049
+ return TinyHtml.setNumber(this, value);
4050
+ }
4051
+
3855
4052
  /**
3856
4053
  * Returns the text content of the element.
3857
4054
  * @param {TinyElement} el - Target element.
@@ -4501,141 +4698,169 @@ class TinyHtml {
4501
4698
  * Registers an event listener on the specified element.
4502
4699
  *
4503
4700
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
4504
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4701
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4505
4702
  * @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
4506
4703
  * @param {EventRegistryOptions} [options] - Optional event listener options.
4507
4704
  * @returns {TinyEventTarget|TinyEventTarget[]}
4508
4705
  */
4509
- static on(el, event, handler, options) {
4510
- if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4511
- TinyHtml._preEventTargetElems(el, 'on').forEach((elem) => {
4512
- elem.addEventListener(event, handler, options);
4513
-
4514
- if (!__eventRegistry.has(elem)) __eventRegistry.set(elem, {});
4515
- const events = __eventRegistry.get(elem);
4516
- if (!events) return;
4517
- if (!Array.isArray(events[event])) events[event] = [];
4518
- events[event].push({ handler, options });
4519
- });
4706
+ static on(el, events, handler, options) {
4707
+ if (
4708
+ typeof events !== 'string' &&
4709
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string'))
4710
+ )
4711
+ throw new TypeError('The events must be a string or array of strings.');
4712
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4713
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4714
+ TinyHtml._preEventTargetElems(el, 'on').forEach((elem) => {
4715
+ elem.addEventListener(event, handler, options);
4716
+
4717
+ if (!__eventRegistry.has(elem)) __eventRegistry.set(elem, {});
4718
+ const events = __eventRegistry.get(elem);
4719
+ if (!events) return;
4720
+ if (!Array.isArray(events[event])) events[event] = [];
4721
+ events[event].push({ handler, options });
4722
+ });
4723
+ }
4520
4724
  return el;
4521
4725
  }
4522
4726
 
4523
4727
  /**
4524
4728
  * Registers an event listener on the specified element.
4525
4729
  *
4526
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4730
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4527
4731
  * @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
4528
4732
  * @param {EventRegistryOptions} [options] - Optional event listener options.
4529
4733
  * @returns {TinyEventTarget|TinyEventTarget[]}
4530
4734
  */
4531
- on(event, handler, options) {
4532
- return TinyHtml.on(this, event, handler, options);
4735
+ on(events, handler, options) {
4736
+ return TinyHtml.on(this, events, handler, options);
4533
4737
  }
4534
4738
 
4535
4739
  /**
4536
4740
  * Registers an event listener that runs only once, then is removed.
4537
4741
  *
4538
4742
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
4539
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4743
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4540
4744
  * @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
4541
4745
  * @param {EventRegistryOptions} [options={}] - Optional event listener options.
4542
4746
  * @returns {TinyEventTarget|TinyEventTarget[]}
4543
4747
  */
4544
- static once(el, event, handler, options = {}) {
4545
- if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4546
- TinyHtml._preEventTargetElems(el, 'once').forEach((elem) => {
4547
- /** @type {EventListenerOrEventListenerObject} */
4548
- const wrapped = (e) => {
4549
- TinyHtml.off(elem, event, wrapped);
4550
- if (typeof handler === 'function') handler(e);
4551
- };
4552
-
4553
- TinyHtml.on(
4554
- elem,
4555
- event,
4556
- wrapped,
4557
- typeof options === 'boolean' ? options : { ...options, once: true },
4558
- );
4559
- });
4748
+ static once(el, events, handler, options = {}) {
4749
+ if (
4750
+ typeof events !== 'string' &&
4751
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string'))
4752
+ )
4753
+ throw new TypeError('The events must be a string or array of strings.');
4754
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4755
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4756
+ TinyHtml._preEventTargetElems(el, 'once').forEach((elem) => {
4757
+ /** @type {EventListenerOrEventListenerObject} */
4758
+ const wrapped = (e) => {
4759
+ TinyHtml.off(elem, event, wrapped);
4760
+ if (typeof handler === 'function') handler(e);
4761
+ };
4762
+
4763
+ TinyHtml.on(
4764
+ elem,
4765
+ event,
4766
+ wrapped,
4767
+ typeof options === 'boolean' ? options : { ...options, once: true },
4768
+ );
4769
+ });
4770
+ }
4560
4771
  return el;
4561
4772
  }
4562
4773
 
4563
4774
  /**
4564
4775
  * Registers an event listener that runs only once, then is removed.
4565
4776
  *
4566
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4777
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4567
4778
  * @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
4568
4779
  * @param {EventRegistryOptions} [options={}] - Optional event listener options.
4569
4780
  * @returns {TinyEventTarget|TinyEventTarget[]}
4570
4781
  */
4571
- once(event, handler, options = {}) {
4572
- return TinyHtml.once(this, event, handler, options);
4782
+ once(events, handler, options = {}) {
4783
+ return TinyHtml.once(this, events, handler, options);
4573
4784
  }
4574
4785
 
4575
4786
  /**
4576
4787
  * Removes a specific event listener from an element.
4577
4788
  *
4578
4789
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
4579
- * @param {string} event - The event type.
4790
+ * @param {string|string[]} events - The event type.
4580
4791
  * @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
4581
4792
  * @param {boolean|EventListenerOptions} [options] - Optional listener options.
4582
4793
  * @returns {TinyEventTarget|TinyEventTarget[]}
4583
4794
  */
4584
- static off(el, event, handler, options) {
4585
- if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4586
- TinyHtml._preEventTargetElems(el, 'off').forEach((elem) => {
4587
- elem.removeEventListener(event, handler, options);
4588
-
4589
- const events = __eventRegistry.get(elem);
4590
- if (events && events[event]) {
4591
- events[event] = events[event].filter((entry) => entry.handler !== handler);
4592
- if (events[event].length === 0) delete events[event];
4593
- }
4594
- });
4795
+ static off(el, events, handler, options) {
4796
+ if (
4797
+ typeof events !== 'string' &&
4798
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string'))
4799
+ )
4800
+ throw new TypeError('The events must be a string or array of strings.');
4801
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4802
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4803
+ TinyHtml._preEventTargetElems(el, 'off').forEach((elem) => {
4804
+ elem.removeEventListener(event, handler, options);
4805
+
4806
+ const events = __eventRegistry.get(elem);
4807
+ if (events && events[event]) {
4808
+ events[event] = events[event].filter((entry) => entry.handler !== handler);
4809
+ if (events[event].length === 0) delete events[event];
4810
+ }
4811
+ });
4812
+ }
4595
4813
  return el;
4596
4814
  }
4597
4815
 
4598
4816
  /**
4599
4817
  * Removes a specific event listener from an element.
4600
4818
  *
4601
- * @param {string} event - The event type.
4819
+ * @param {string|string[]} events - The event type.
4602
4820
  * @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
4603
4821
  * @param {boolean|EventListenerOptions} [options] - Optional listener options.
4604
4822
  * @returns {TinyEventTarget|TinyEventTarget[]}
4605
4823
  */
4606
- off(event, handler, options) {
4607
- return TinyHtml.off(this, event, handler, options);
4824
+ off(events, handler, options) {
4825
+ return TinyHtml.off(this, events, handler, options);
4608
4826
  }
4609
4827
 
4610
4828
  /**
4611
4829
  * Removes all event listeners of a specific type from the element.
4612
4830
  *
4613
4831
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
4614
- * @param {string} event - The event type to remove (e.g. 'click').
4832
+ * @param {string|string[]} events - The event type to remove (e.g. 'click').
4615
4833
  * @returns {TinyEventTarget|TinyEventTarget[]}
4616
4834
  */
4617
- static offAll(el, event) {
4618
- if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4619
- TinyHtml._preEventTargetElems(el, 'offAll').forEach((elem) => {
4620
- const events = __eventRegistry.get(elem);
4621
- if (events && events[event]) {
4622
- for (const entry of events[event]) {
4623
- elem.removeEventListener(event, entry.handler, entry.options);
4835
+ static offAll(el, events) {
4836
+ if (
4837
+ typeof events !== 'string' &&
4838
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string'))
4839
+ )
4840
+ throw new TypeError('The events must be a string or array of strings.');
4841
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4842
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4843
+ TinyHtml._preEventTargetElems(el, 'offAll').forEach((elem) => {
4844
+ const events = __eventRegistry.get(elem);
4845
+ if (events && events[event]) {
4846
+ for (const entry of events[event]) {
4847
+ elem.removeEventListener(event, entry.handler, entry.options);
4848
+ }
4849
+ delete events[event];
4624
4850
  }
4625
- delete events[event];
4626
- }
4627
- });
4851
+ });
4852
+ }
4628
4853
  return el;
4629
4854
  }
4630
4855
 
4631
4856
  /**
4632
4857
  * Removes all event listeners of a specific type from the element.
4633
4858
  *
4634
- * @param {string} event - The event type to remove (e.g. 'click').
4859
+ * @param {string|string[]} events - The event type to remove (e.g. 'click').
4635
4860
  * @returns {TinyEventTarget|TinyEventTarget[]}
4636
4861
  */
4637
- offAll(event) {
4638
- return TinyHtml.offAll(this, event);
4862
+ offAll(events) {
4863
+ return TinyHtml.offAll(this, events);
4639
4864
  }
4640
4865
 
4641
4866
  /**
@@ -4681,36 +4906,43 @@ class TinyHtml {
4681
4906
  * Triggers all handlers associated with a specific event on the given element.
4682
4907
  *
4683
4908
  * @param {TinyEventTarget|TinyEventTarget[]} el - Target element where the event should be triggered.
4684
- * @param {string} event - Name of the event to trigger.
4909
+ * @param {string|string[]} events - Name of the event to trigger.
4685
4910
  * @param {Event|CustomEvent|CustomEventInit} [payload] - Optional event object or data to pass.
4686
4911
  * @returns {TinyEventTarget|TinyEventTarget[]}
4687
4912
  */
4688
- static trigger(el, event, payload = {}) {
4689
- if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4690
- TinyHtml._preEventTargetElems(el, 'trigger').forEach((elem) => {
4691
- const evt =
4692
- payload instanceof Event || payload instanceof CustomEvent
4693
- ? payload
4694
- : new CustomEvent(event, {
4695
- bubbles: true,
4696
- cancelable: true,
4697
- detail: payload,
4698
- });
4699
-
4700
- elem.dispatchEvent(evt);
4701
- });
4913
+ static trigger(el, events, payload = {}) {
4914
+ if (
4915
+ typeof events !== 'string' &&
4916
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string'))
4917
+ )
4918
+ throw new TypeError('The events must be a string or array of strings.');
4919
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4920
+ if (typeof event !== 'string') throw new TypeError('The event name must be a string.');
4921
+ TinyHtml._preEventTargetElems(el, 'trigger').forEach((elem) => {
4922
+ const evt =
4923
+ payload instanceof Event || payload instanceof CustomEvent
4924
+ ? payload
4925
+ : new CustomEvent(event, {
4926
+ bubbles: true,
4927
+ cancelable: true,
4928
+ detail: payload,
4929
+ });
4930
+
4931
+ elem.dispatchEvent(evt);
4932
+ });
4933
+ }
4702
4934
  return el;
4703
4935
  }
4704
4936
 
4705
4937
  /**
4706
4938
  * Triggers all handlers associated with a specific event on the given element.
4707
4939
  *
4708
- * @param {string} event - Name of the event to trigger.
4940
+ * @param {string|string[]} events - Name of the event to trigger.
4709
4941
  * @param {Event|CustomEvent|CustomEventInit} [payload] - Optional event object or data to pass.
4710
4942
  * @returns {TinyEventTarget|TinyEventTarget[]}
4711
4943
  */
4712
- trigger(event, payload = {}) {
4713
- return TinyHtml.trigger(this, event, payload);
4944
+ trigger(events, payload = {}) {
4945
+ return TinyHtml.trigger(this, events, payload);
4714
4946
  }
4715
4947
 
4716
4948
  ///////////////////////////////////////////////////////////////