@y14e/disclosure 2.0.11 → 2.0.13

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/README.md CHANGED
@@ -13,11 +13,11 @@ npm i @y14e/disclosure
13
13
  import { Disclosure } from "@y14e/disclosure";
14
14
 
15
15
  // CDNs
16
- import { Disclosure } from "https://esm.sh/@y14e/disclosure@2.0.11";
16
+ import { Disclosure } from "https://esm.sh/@y14e/disclosure@2.0.13";
17
17
  // or
18
- import { Disclosure } from "https://cdn.jsdelivr.net/npm/@y14e/disclosure@2.0.11/+esm";
18
+ import { Disclosure } from "https://cdn.jsdelivr.net/npm/@y14e/disclosure@2.0.13/+esm";
19
19
  // or
20
- import { Disclosure } from "https://esm.unpkg.com/@y14e/disclosure@2.0.11";
20
+ import { Disclosure } from "https://esm.unpkg.com/@y14e/disclosure@2.0.13";
21
21
  ```
22
22
 
23
23
  ## Usage
@@ -4,12 +4,11 @@
4
4
  var DEFAULT_PARSER = (value) => value.split(/\s+/);
5
5
  var DEFAULT_SERIALIZER = (tokens) => tokens.join(" ");
6
6
  function addAttributeToken(element, name, token, options = {}) {
7
+ if (!isValid(element, name, token)) {
8
+ return;
9
+ }
7
10
  const value = element.getAttribute(name)?.trim();
8
- const {
9
- caseInsensitive = false,
10
- parse = DEFAULT_PARSER,
11
- serialize = DEFAULT_SERIALIZER
12
- } = options;
11
+ const { caseInsensitive, parse, serialize } = resolveOptions(options);
13
12
  const tokens = value ? parse(value).filter(Boolean) : [];
14
13
  if (caseInsensitive) {
15
14
  const lower = token.toLowerCase();
@@ -49,6 +48,45 @@ function saveAttributes(element_or_elements, name_or_names) {
49
48
  });
50
49
  });
51
50
  }
51
+ function isValid(element, name, token) {
52
+ if (!(element instanceof Element)) {
53
+ console.warn("Invalid element");
54
+ return false;
55
+ }
56
+ if (!element.hasAttribute(name)) {
57
+ console.warn("Invalid attribute name");
58
+ return false;
59
+ }
60
+ if (typeof token !== "string" || !token.trim()) {
61
+ console.warn("Invalid token");
62
+ return false;
63
+ }
64
+ return true;
65
+ }
66
+ function resolveOptions(options) {
67
+ let {
68
+ caseInsensitive = false,
69
+ parse = DEFAULT_PARSER,
70
+ serialize = DEFAULT_SERIALIZER
71
+ } = options;
72
+ if (typeof caseInsensitive !== "boolean") {
73
+ console.warn("Invalid caseInsensitive option. Fallback: false.");
74
+ caseInsensitive = false;
75
+ }
76
+ if (typeof parse !== "function") {
77
+ console.warn(
78
+ "Invalid parser. Fallback: default parser (splits by whitespace)."
79
+ );
80
+ parse = DEFAULT_PARSER;
81
+ }
82
+ if (typeof serialize !== "function") {
83
+ console.warn(
84
+ "Invalid serializer. Fallback: default serializer (joins by whitespace)."
85
+ );
86
+ serialize = DEFAULT_SERIALIZER;
87
+ }
88
+ return { caseInsensitive, parse, serialize };
89
+ }
52
90
 
53
91
  // node_modules/power-focusable/dist/index.js
54
92
  var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
@@ -609,8 +647,8 @@ var Disclosure = class _Disclosure {
609
647
  return;
610
648
  }
611
649
  this.#rootElement = root;
612
- this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
613
- this.#settings = this.#mergeOptions(this.#defaults, options);
650
+ this.#defaults = this.#resolveOptions(this.#defaults, _Disclosure.defaults);
651
+ this.#settings = this.#resolveOptions(this.#defaults, options);
614
652
  matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
615
653
  const NOT_NESTED = ":not(:scope summary + * *)";
616
654
  this.#detailsElements = [
@@ -833,7 +871,7 @@ var Disclosure = class _Disclosure {
833
871
  #isFocusable(element) {
834
872
  return element.tabIndex >= 0;
835
873
  }
836
- #mergeOptions(target, source) {
874
+ #resolveOptions(target, source) {
837
875
  const merged = {
838
876
  ...target,
839
877
  ...source,
@@ -841,7 +879,8 @@ var Disclosure = class _Disclosure {
841
879
  };
842
880
  const mergedAnimation = merged.animation;
843
881
  const duration = mergedAnimation.duration;
844
- const defaultAnimation = this.#defaults.animation;
882
+ const defaults = this.#defaults;
883
+ const defaultAnimation = defaults.animation;
845
884
  if (typeof duration !== "number" || Number.isNaN(duration)) {
846
885
  const duration2 = defaultAnimation.duration;
847
886
  console.warn(`Invalid animation duration. Fallback: ${duration2} (ms).`);
@@ -857,7 +896,7 @@ var Disclosure = class _Disclosure {
857
896
  mergedAnimation.easing = easing;
858
897
  }
859
898
  if (typeof merged.collapsible !== "boolean") {
860
- const collapsible = this.#defaults.collapsible;
899
+ const collapsible = defaults.collapsible;
861
900
  console.warn(`Invalid collapsible option. Fallback: ${collapsible}.`);
862
901
  merged.collapsible = collapsible;
863
902
  }
@@ -882,7 +921,7 @@ function waitAnimationFinish(animation) {
882
921
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
883
922
  * Using the <details> and <summary> element.
884
923
  *
885
- * @version 2.0.11
924
+ * @version 2.0.13
886
925
  * @author Yusuke Kamiyamane
887
926
  * @license MIT
888
927
  * @copyright Copyright (c) Yusuke Kamiyamane
@@ -894,7 +933,7 @@ function waitAnimationFinish(animation) {
894
933
  (**
895
934
  * Attribute Utils
896
935
  *
897
- * @version 2.0.5
936
+ * @version 2.0.6
898
937
  * @author Yusuke Kamiyamane
899
938
  * @license MIT
900
939
  * @copyright Copyright (c) Yusuke Kamiyamane
@@ -2,12 +2,11 @@
2
2
  var DEFAULT_PARSER = (value) => value.split(/\s+/);
3
3
  var DEFAULT_SERIALIZER = (tokens) => tokens.join(" ");
4
4
  function addAttributeToken(element, name, token, options = {}) {
5
+ if (!isValid(element, name, token)) {
6
+ return;
7
+ }
5
8
  const value = element.getAttribute(name)?.trim();
6
- const {
7
- caseInsensitive = false,
8
- parse = DEFAULT_PARSER,
9
- serialize = DEFAULT_SERIALIZER
10
- } = options;
9
+ const { caseInsensitive, parse, serialize } = resolveOptions(options);
11
10
  const tokens = value ? parse(value).filter(Boolean) : [];
12
11
  if (caseInsensitive) {
13
12
  const lower = token.toLowerCase();
@@ -47,6 +46,45 @@ function saveAttributes(element_or_elements, name_or_names) {
47
46
  });
48
47
  });
49
48
  }
49
+ function isValid(element, name, token) {
50
+ if (!(element instanceof Element)) {
51
+ console.warn("Invalid element");
52
+ return false;
53
+ }
54
+ if (!element.hasAttribute(name)) {
55
+ console.warn("Invalid attribute name");
56
+ return false;
57
+ }
58
+ if (typeof token !== "string" || !token.trim()) {
59
+ console.warn("Invalid token");
60
+ return false;
61
+ }
62
+ return true;
63
+ }
64
+ function resolveOptions(options) {
65
+ let {
66
+ caseInsensitive = false,
67
+ parse = DEFAULT_PARSER,
68
+ serialize = DEFAULT_SERIALIZER
69
+ } = options;
70
+ if (typeof caseInsensitive !== "boolean") {
71
+ console.warn("Invalid caseInsensitive option. Fallback: false.");
72
+ caseInsensitive = false;
73
+ }
74
+ if (typeof parse !== "function") {
75
+ console.warn(
76
+ "Invalid parser. Fallback: default parser (splits by whitespace)."
77
+ );
78
+ parse = DEFAULT_PARSER;
79
+ }
80
+ if (typeof serialize !== "function") {
81
+ console.warn(
82
+ "Invalid serializer. Fallback: default serializer (joins by whitespace)."
83
+ );
84
+ serialize = DEFAULT_SERIALIZER;
85
+ }
86
+ return { caseInsensitive, parse, serialize };
87
+ }
50
88
 
51
89
  // node_modules/power-focusable/dist/index.js
52
90
  var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
@@ -607,8 +645,8 @@ var Disclosure = class _Disclosure {
607
645
  return;
608
646
  }
609
647
  this.#rootElement = root;
610
- this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
611
- this.#settings = this.#mergeOptions(this.#defaults, options);
648
+ this.#defaults = this.#resolveOptions(this.#defaults, _Disclosure.defaults);
649
+ this.#settings = this.#resolveOptions(this.#defaults, options);
612
650
  matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
613
651
  const NOT_NESTED = ":not(:scope summary + * *)";
614
652
  this.#detailsElements = [
@@ -831,7 +869,7 @@ var Disclosure = class _Disclosure {
831
869
  #isFocusable(element) {
832
870
  return element.tabIndex >= 0;
833
871
  }
834
- #mergeOptions(target, source) {
872
+ #resolveOptions(target, source) {
835
873
  const merged = {
836
874
  ...target,
837
875
  ...source,
@@ -839,7 +877,8 @@ var Disclosure = class _Disclosure {
839
877
  };
840
878
  const mergedAnimation = merged.animation;
841
879
  const duration = mergedAnimation.duration;
842
- const defaultAnimation = this.#defaults.animation;
880
+ const defaults = this.#defaults;
881
+ const defaultAnimation = defaults.animation;
843
882
  if (typeof duration !== "number" || Number.isNaN(duration)) {
844
883
  const duration2 = defaultAnimation.duration;
845
884
  console.warn(`Invalid animation duration. Fallback: ${duration2} (ms).`);
@@ -855,7 +894,7 @@ var Disclosure = class _Disclosure {
855
894
  mergedAnimation.easing = easing;
856
895
  }
857
896
  if (typeof merged.collapsible !== "boolean") {
858
- const collapsible = this.#defaults.collapsible;
897
+ const collapsible = defaults.collapsible;
859
898
  console.warn(`Invalid collapsible option. Fallback: ${collapsible}.`);
860
899
  merged.collapsible = collapsible;
861
900
  }
@@ -880,7 +919,7 @@ function waitAnimationFinish(animation) {
880
919
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
881
920
  * Using the <details> and <summary> element.
882
921
  *
883
- * @version 2.0.11
922
+ * @version 2.0.13
884
923
  * @author Yusuke Kamiyamane
885
924
  * @license MIT
886
925
  * @copyright Copyright (c) Yusuke Kamiyamane
@@ -892,7 +931,7 @@ function waitAnimationFinish(animation) {
892
931
  (**
893
932
  * Attribute Utils
894
933
  *
895
- * @version 2.0.5
934
+ * @version 2.0.6
896
935
  * @author Yusuke Kamiyamane
897
936
  * @license MIT
898
937
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.cjs CHANGED
@@ -53,8 +53,8 @@ var Disclosure = class _Disclosure {
53
53
  return;
54
54
  }
55
55
  this.#rootElement = root;
56
- this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
57
- this.#settings = this.#mergeOptions(this.#defaults, options);
56
+ this.#defaults = this.#resolveOptions(this.#defaults, _Disclosure.defaults);
57
+ this.#settings = this.#resolveOptions(this.#defaults, options);
58
58
  matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
59
59
  const NOT_NESTED = ":not(:scope summary + * *)";
60
60
  this.#detailsElements = [
@@ -277,7 +277,7 @@ var Disclosure = class _Disclosure {
277
277
  #isFocusable(element) {
278
278
  return element.tabIndex >= 0;
279
279
  }
280
- #mergeOptions(target, source) {
280
+ #resolveOptions(target, source) {
281
281
  const merged = {
282
282
  ...target,
283
283
  ...source,
@@ -285,7 +285,8 @@ var Disclosure = class _Disclosure {
285
285
  };
286
286
  const mergedAnimation = merged.animation;
287
287
  const duration = mergedAnimation.duration;
288
- const defaultAnimation = this.#defaults.animation;
288
+ const defaults = this.#defaults;
289
+ const defaultAnimation = defaults.animation;
289
290
  if (typeof duration !== "number" || Number.isNaN(duration)) {
290
291
  const duration2 = defaultAnimation.duration;
291
292
  console.warn(`Invalid animation duration. Fallback: ${duration2} (ms).`);
@@ -301,7 +302,7 @@ var Disclosure = class _Disclosure {
301
302
  mergedAnimation.easing = easing;
302
303
  }
303
304
  if (typeof merged.collapsible !== "boolean") {
304
- const collapsible = this.#defaults.collapsible;
305
+ const collapsible = defaults.collapsible;
305
306
  console.warn(`Invalid collapsible option. Fallback: ${collapsible}.`);
306
307
  merged.collapsible = collapsible;
307
308
  }
@@ -326,7 +327,7 @@ function waitAnimationFinish(animation) {
326
327
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
327
328
  * Using the <details> and <summary> element.
328
329
  *
329
- * @version 2.0.11
330
+ * @version 2.0.13
330
331
  * @author Yusuke Kamiyamane
331
332
  * @license MIT
332
333
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.d.cts CHANGED
@@ -3,7 +3,7 @@
3
3
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
4
  * Using the <details> and <summary> element.
5
5
  *
6
- * @version 2.0.11
6
+ * @version 2.0.13
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
4
  * Using the <details> and <summary> element.
5
5
  *
6
- * @version 2.0.11
6
+ * @version 2.0.13
7
7
  * @author Yusuke Kamiyamane
8
8
  * @license MIT
9
9
  * @copyright Copyright (c) Yusuke Kamiyamane
package/dist/index.js CHANGED
@@ -31,8 +31,8 @@ var Disclosure = class _Disclosure {
31
31
  return;
32
32
  }
33
33
  this.#rootElement = root;
34
- this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
35
- this.#settings = this.#mergeOptions(this.#defaults, options);
34
+ this.#defaults = this.#resolveOptions(this.#defaults, _Disclosure.defaults);
35
+ this.#settings = this.#resolveOptions(this.#defaults, options);
36
36
  matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
37
37
  const NOT_NESTED = ":not(:scope summary + * *)";
38
38
  this.#detailsElements = [
@@ -255,7 +255,7 @@ var Disclosure = class _Disclosure {
255
255
  #isFocusable(element) {
256
256
  return element.tabIndex >= 0;
257
257
  }
258
- #mergeOptions(target, source) {
258
+ #resolveOptions(target, source) {
259
259
  const merged = {
260
260
  ...target,
261
261
  ...source,
@@ -263,7 +263,8 @@ var Disclosure = class _Disclosure {
263
263
  };
264
264
  const mergedAnimation = merged.animation;
265
265
  const duration = mergedAnimation.duration;
266
- const defaultAnimation = this.#defaults.animation;
266
+ const defaults = this.#defaults;
267
+ const defaultAnimation = defaults.animation;
267
268
  if (typeof duration !== "number" || Number.isNaN(duration)) {
268
269
  const duration2 = defaultAnimation.duration;
269
270
  console.warn(`Invalid animation duration. Fallback: ${duration2} (ms).`);
@@ -279,7 +280,7 @@ var Disclosure = class _Disclosure {
279
280
  mergedAnimation.easing = easing;
280
281
  }
281
282
  if (typeof merged.collapsible !== "boolean") {
282
- const collapsible = this.#defaults.collapsible;
283
+ const collapsible = defaults.collapsible;
283
284
  console.warn(`Invalid collapsible option. Fallback: ${collapsible}.`);
284
285
  merged.collapsible = collapsible;
285
286
  }
@@ -304,7 +305,7 @@ function waitAnimationFinish(animation) {
304
305
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
305
306
  * Using the <details> and <summary> element.
306
307
  *
307
- * @version 2.0.11
308
+ * @version 2.0.13
308
309
  * @author Yusuke Kamiyamane
309
310
  * @license MIT
310
311
  * @copyright Copyright (c) Yusuke Kamiyamane
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@y14e/disclosure",
3
- "version": "2.0.11",
3
+ "version": "2.0.13",
4
4
  "description": "WAI-ARIA compliant disclosure pattern implementation in TypeScript",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -54,7 +54,7 @@
54
54
  "esbuild": true
55
55
  },
56
56
  "dependencies": {
57
- "@y14e/attribute-utils": "^2.0.5",
57
+ "@y14e/attribute-utils": "^2.0.6",
58
58
  "@y14e/roving-tabindex": "^3.1.25"
59
59
  }
60
60
  }