@y14e/attribute-utils 2.0.4

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yusuke Kamiyamane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Attribute Utils
2
+
3
+ For internal use.
package/dist/index.cjs ADDED
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ var DEFAULT_PARSER = (value) => value.split(/\s+/);
5
+ var DEFAULT_SERIALIZER = (tokens) => tokens.join(" ");
6
+ function addAttributeToken(element, name, token, options = {}) {
7
+ const value = element.getAttribute(name)?.trim();
8
+ const {
9
+ caseInsensitive = false,
10
+ parse = DEFAULT_PARSER,
11
+ serialize = DEFAULT_SERIALIZER
12
+ } = options;
13
+ const tokens = value ? parse(value).filter(Boolean) : [];
14
+ if (caseInsensitive) {
15
+ const lower = token.toLowerCase();
16
+ if (tokens.every((token2) => token2.toLowerCase() !== lower)) {
17
+ tokens.push(token);
18
+ element.setAttribute(name, serialize(tokens));
19
+ }
20
+ } else {
21
+ const set = new Set(tokens);
22
+ set.add(token);
23
+ element.setAttribute(name, serialize([...set]));
24
+ }
25
+ }
26
+ function removeAttributeToken(element, name, token, options = {}) {
27
+ const value = element.getAttribute(name)?.trim();
28
+ if (!value) {
29
+ return;
30
+ }
31
+ const {
32
+ caseInsensitive = false,
33
+ parse = DEFAULT_PARSER,
34
+ serialize = DEFAULT_SERIALIZER
35
+ } = options;
36
+ const tokens = parse(value).filter(Boolean);
37
+ if (!tokens.length) {
38
+ return;
39
+ }
40
+ if (caseInsensitive) {
41
+ const lower = token.toLowerCase();
42
+ const filtered = tokens.filter((token2) => token2.toLowerCase() !== lower);
43
+ if (filtered.length !== tokens.length) {
44
+ filtered.length ? element.setAttribute(name, serialize(filtered)) : element.removeAttribute(name);
45
+ }
46
+ } else {
47
+ const set = new Set(tokens);
48
+ set.delete(token);
49
+ if (set.size !== tokens.length) {
50
+ set.size ? element.setAttribute(name, serialize([...set])) : element.removeAttribute(name);
51
+ }
52
+ }
53
+ }
54
+ var snapshots = /* @__PURE__ */ new WeakMap();
55
+ function restoreAttributes(element_or_elements) {
56
+ for (const element of Array.isArray(element_or_elements) ? element_or_elements : [element_or_elements]) {
57
+ const snapshot = snapshots.get(element);
58
+ if (!snapshot) {
59
+ continue;
60
+ }
61
+ for (const [name, value] of snapshot.entries()) {
62
+ value === null ? element.removeAttribute(name) : element.setAttribute(name, value);
63
+ }
64
+ snapshots.delete(element);
65
+ }
66
+ }
67
+ function saveAttributes(element_or_elements, name_or_names) {
68
+ const names = Array.isArray(name_or_names) ? name_or_names : [name_or_names];
69
+ (Array.isArray(element_or_elements) ? element_or_elements : [element_or_elements]).forEach((element) => {
70
+ let snapshot = snapshots.get(element);
71
+ if (!snapshot) {
72
+ snapshot = /* @__PURE__ */ new Map();
73
+ snapshots.set(element, snapshot);
74
+ }
75
+ names.forEach((name) => {
76
+ snapshot.set(name, element.getAttribute(name));
77
+ });
78
+ });
79
+ }
80
+ /**
81
+ * Attribute Utils
82
+ *
83
+ * @version 2.0.4
84
+ * @author Yusuke Kamiyamane
85
+ * @license MIT
86
+ * @copyright Copyright (c) Yusuke Kamiyamane
87
+ * @see {@link https://github.com/y14e/attribute-utils}
88
+ */
89
+
90
+ exports.addAttributeToken = addAttributeToken;
91
+ exports.removeAttributeToken = removeAttributeToken;
92
+ exports.restoreAttributes = restoreAttributes;
93
+ exports.saveAttributes = saveAttributes;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Attribute Utils
3
+ *
4
+ * @version 2.0.4
5
+ * @author Yusuke Kamiyamane
6
+ * @license MIT
7
+ * @copyright Copyright (c) Yusuke Kamiyamane
8
+ * @see {@link https://github.com/y14e/attribute-utils}
9
+ */
10
+ interface AttributeUtilsOptions {
11
+ caseInsensitive: boolean;
12
+ parse: (value: string) => string[];
13
+ serialize: (tokens: string[]) => string;
14
+ }
15
+ declare function addAttributeToken(element: Element, name: string, token: string, options?: Partial<AttributeUtilsOptions>): void;
16
+ declare function removeAttributeToken(element: Element, name: string, token: string, options?: Partial<AttributeUtilsOptions>): void;
17
+ declare function restoreAttributes(element_or_elements: Element | Element[]): void;
18
+ declare function saveAttributes(element_or_elements: Element | Element[], name_or_names: string | string[]): void;
19
+
20
+ export { type AttributeUtilsOptions, addAttributeToken, removeAttributeToken, restoreAttributes, saveAttributes };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Attribute Utils
3
+ *
4
+ * @version 2.0.4
5
+ * @author Yusuke Kamiyamane
6
+ * @license MIT
7
+ * @copyright Copyright (c) Yusuke Kamiyamane
8
+ * @see {@link https://github.com/y14e/attribute-utils}
9
+ */
10
+ interface AttributeUtilsOptions {
11
+ caseInsensitive: boolean;
12
+ parse: (value: string) => string[];
13
+ serialize: (tokens: string[]) => string;
14
+ }
15
+ declare function addAttributeToken(element: Element, name: string, token: string, options?: Partial<AttributeUtilsOptions>): void;
16
+ declare function removeAttributeToken(element: Element, name: string, token: string, options?: Partial<AttributeUtilsOptions>): void;
17
+ declare function restoreAttributes(element_or_elements: Element | Element[]): void;
18
+ declare function saveAttributes(element_or_elements: Element | Element[], name_or_names: string | string[]): void;
19
+
20
+ export { type AttributeUtilsOptions, addAttributeToken, removeAttributeToken, restoreAttributes, saveAttributes };
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ // src/index.ts
2
+ var DEFAULT_PARSER = (value) => value.split(/\s+/);
3
+ var DEFAULT_SERIALIZER = (tokens) => tokens.join(" ");
4
+ function addAttributeToken(element, name, token, options = {}) {
5
+ const value = element.getAttribute(name)?.trim();
6
+ const {
7
+ caseInsensitive = false,
8
+ parse = DEFAULT_PARSER,
9
+ serialize = DEFAULT_SERIALIZER
10
+ } = options;
11
+ const tokens = value ? parse(value).filter(Boolean) : [];
12
+ if (caseInsensitive) {
13
+ const lower = token.toLowerCase();
14
+ if (tokens.every((token2) => token2.toLowerCase() !== lower)) {
15
+ tokens.push(token);
16
+ element.setAttribute(name, serialize(tokens));
17
+ }
18
+ } else {
19
+ const set = new Set(tokens);
20
+ set.add(token);
21
+ element.setAttribute(name, serialize([...set]));
22
+ }
23
+ }
24
+ function removeAttributeToken(element, name, token, options = {}) {
25
+ const value = element.getAttribute(name)?.trim();
26
+ if (!value) {
27
+ return;
28
+ }
29
+ const {
30
+ caseInsensitive = false,
31
+ parse = DEFAULT_PARSER,
32
+ serialize = DEFAULT_SERIALIZER
33
+ } = options;
34
+ const tokens = parse(value).filter(Boolean);
35
+ if (!tokens.length) {
36
+ return;
37
+ }
38
+ if (caseInsensitive) {
39
+ const lower = token.toLowerCase();
40
+ const filtered = tokens.filter((token2) => token2.toLowerCase() !== lower);
41
+ if (filtered.length !== tokens.length) {
42
+ filtered.length ? element.setAttribute(name, serialize(filtered)) : element.removeAttribute(name);
43
+ }
44
+ } else {
45
+ const set = new Set(tokens);
46
+ set.delete(token);
47
+ if (set.size !== tokens.length) {
48
+ set.size ? element.setAttribute(name, serialize([...set])) : element.removeAttribute(name);
49
+ }
50
+ }
51
+ }
52
+ var snapshots = /* @__PURE__ */ new WeakMap();
53
+ function restoreAttributes(element_or_elements) {
54
+ for (const element of Array.isArray(element_or_elements) ? element_or_elements : [element_or_elements]) {
55
+ const snapshot = snapshots.get(element);
56
+ if (!snapshot) {
57
+ continue;
58
+ }
59
+ for (const [name, value] of snapshot.entries()) {
60
+ value === null ? element.removeAttribute(name) : element.setAttribute(name, value);
61
+ }
62
+ snapshots.delete(element);
63
+ }
64
+ }
65
+ function saveAttributes(element_or_elements, name_or_names) {
66
+ const names = Array.isArray(name_or_names) ? name_or_names : [name_or_names];
67
+ (Array.isArray(element_or_elements) ? element_or_elements : [element_or_elements]).forEach((element) => {
68
+ let snapshot = snapshots.get(element);
69
+ if (!snapshot) {
70
+ snapshot = /* @__PURE__ */ new Map();
71
+ snapshots.set(element, snapshot);
72
+ }
73
+ names.forEach((name) => {
74
+ snapshot.set(name, element.getAttribute(name));
75
+ });
76
+ });
77
+ }
78
+ /**
79
+ * Attribute Utils
80
+ *
81
+ * @version 2.0.4
82
+ * @author Yusuke Kamiyamane
83
+ * @license MIT
84
+ * @copyright Copyright (c) Yusuke Kamiyamane
85
+ * @see {@link https://github.com/y14e/attribute-utils}
86
+ */
87
+
88
+ export { addAttributeToken, removeAttributeToken, restoreAttributes, saveAttributes };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@y14e/attribute-utils",
3
+ "version": "2.0.4",
4
+ "description": "",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE",
19
+ "README.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "prepublishOnly": "npm run build",
24
+ "lint": "tsc --noEmit"
25
+ },
26
+ "keywords": [],
27
+ "author": "Yusuke Kamiyamane",
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/y14e/attribute-utils.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/y14e/attribute-utils/issues"
35
+ },
36
+ "homepage": "https://github.com/y14e/attribute-utils#readme",
37
+ "devDependencies": {
38
+ "bun-types": "latest",
39
+ "tsup": "^8.0.0",
40
+ "typescript": "^5.6.0"
41
+ },
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "allowScripts": {
46
+ "esbuild": true
47
+ }
48
+ }