@y14e/disclosure 2.0.11 → 2.0.12

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.12";
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.12/+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.12";
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,
@@ -882,7 +920,7 @@ function waitAnimationFinish(animation) {
882
920
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
883
921
  * Using the <details> and <summary> element.
884
922
  *
885
- * @version 2.0.11
923
+ * @version 2.0.12
886
924
  * @author Yusuke Kamiyamane
887
925
  * @license MIT
888
926
  * @copyright Copyright (c) Yusuke Kamiyamane
@@ -894,7 +932,7 @@ function waitAnimationFinish(animation) {
894
932
  (**
895
933
  * Attribute Utils
896
934
  *
897
- * @version 2.0.5
935
+ * @version 2.0.6
898
936
  * @author Yusuke Kamiyamane
899
937
  * @license MIT
900
938
  * @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,
@@ -880,7 +918,7 @@ function waitAnimationFinish(animation) {
880
918
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
881
919
  * Using the <details> and <summary> element.
882
920
  *
883
- * @version 2.0.11
921
+ * @version 2.0.12
884
922
  * @author Yusuke Kamiyamane
885
923
  * @license MIT
886
924
  * @copyright Copyright (c) Yusuke Kamiyamane
@@ -892,7 +930,7 @@ function waitAnimationFinish(animation) {
892
930
  (**
893
931
  * Attribute Utils
894
932
  *
895
- * @version 2.0.5
933
+ * @version 2.0.6
896
934
  * @author Yusuke Kamiyamane
897
935
  * @license MIT
898
936
  * @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,
@@ -326,7 +326,7 @@ function waitAnimationFinish(animation) {
326
326
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
327
327
  * Using the <details> and <summary> element.
328
328
  *
329
- * @version 2.0.11
329
+ * @version 2.0.12
330
330
  * @author Yusuke Kamiyamane
331
331
  * @license MIT
332
332
  * @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.12
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.12
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,
@@ -304,7 +304,7 @@ function waitAnimationFinish(animation) {
304
304
  * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
305
305
  * Using the <details> and <summary> element.
306
306
  *
307
- * @version 2.0.11
307
+ * @version 2.0.12
308
308
  * @author Yusuke Kamiyamane
309
309
  * @license MIT
310
310
  * @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.12",
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
  }