autopair 1.1.0 → 1.2.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/LICENSE.md CHANGED
@@ -2,7 +2,7 @@ Adjusted MIT License
2
2
 
3
3
  Copyright (c) 2025 Dennis Hackethal
4
4
 
5
- Permission is hereby granted, free of charge, to any person* obtaining a copy
5
+ Permission is hereby granted, free of charge, to any person[^1] obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
7
7
  in the Software without restriction, including without limitation the rights
8
8
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -20,7 +20,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
22
 
23
- * Exception: no one in the public sector may use this software. That means
23
+ [^1]: Exception: no one in the public sector may use this software. That means
24
24
  no politicians, public-school teachers, police, intelligence services, etc.
25
25
  Private recipients of public moneys may not use this software either unless
26
26
  they oppose the welfare state and consider such moneys restitution. No one
package/README.md CHANGED
@@ -57,7 +57,7 @@ npm install autopair
57
57
  '*': '*' // For markdown italics
58
58
  });
59
59
 
60
- // Teardown, ie remove autopair.js functionality
60
+ // Teardown, ie remove autopair.js functionality from an element
61
61
  let teardown = autopair(textarea);
62
62
  teardown();
63
63
  </script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autopair",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Automatically closes parentheses and similar characters.",
5
5
  "main": "index.js",
6
6
  "author": "Dennis Hackethal <engineering@dennishackethal.com>",
package/src/autopair.js CHANGED
@@ -55,11 +55,30 @@ export default function autopair(textarea, pairs = {
55
55
  return;
56
56
  }
57
57
 
58
- // Only autopair if next char is whitespace, punctuation, or a closing of the same type
59
58
  const nextChar = value[end] || '';
59
+ const prevChar = value[start - 1] || '';
60
+
60
61
  const insidePair = closing === nextChar;
61
62
  const safeNext = !isWordChar.test(nextChar) || punctuation.test(nextChar);
62
- if (!insidePair && !safeNext) return;
63
+ const isSymmetric = evt.key === closing;
64
+
65
+ // Autoclose only when allowed
66
+ if (
67
+ !insidePair &&
68
+ (
69
+ !safeNext ||
70
+ (
71
+ isSymmetric &&
72
+ start === end &&
73
+ (
74
+ isWordChar.test(prevChar) || // end of word
75
+ prevChar === evt.key // repeated quote
76
+ )
77
+ )
78
+ )
79
+ ) {
80
+ return;
81
+ }
63
82
 
64
83
  evt.preventDefault();
65
84
  textarea.selectionStart = textarea.selectionEnd = start;