@idomatic/parser-html 1.0.43 → 1.1.1

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/consts/index.js CHANGED
@@ -1,2 +1 @@
1
1
  export const TEMPLATE_REGEX = /<template(?:\s[^>]*)?>([\s\S]*?)<\/template>/i;
2
- export const TAG_REGEX = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
package/idGen.js ADDED
@@ -0,0 +1,58 @@
1
+ import { randomUUID } from "crypto";
2
+
3
+ /**
4
+ * Id generation with a configurable strategy, for HTML/Vue templates.
5
+ *
6
+ * - "semantic" (default): derive a human-readable slug from the tag and its
7
+ * descriptive attributes, then guarantee uniqueness by suffixing `-2`, `-3`,
8
+ * ... only on collision.
9
+ * - "random": use `${prefix}${randomUUID()}`.
10
+ *
11
+ * Either way the factory is seeded with ids already present in the file, so
12
+ * re-runs are idempotent: a new element never duplicates an existing id.
13
+ */
14
+
15
+ const MAX_SLUG_LENGTH = 40;
16
+
17
+ export function slugify(str, maxLen = MAX_SLUG_LENGTH) {
18
+ if (!str) return "";
19
+ return String(str)
20
+ .trim()
21
+ .toLowerCase()
22
+ .replace(/[^a-z0-9]+/g, "-")
23
+ .replace(/-{2,}/g, "-")
24
+ .replace(/^-+|-+$/g, "")
25
+ .slice(0, maxLen)
26
+ .replace(/-+$/g, "");
27
+ }
28
+
29
+ /**
30
+ * @param {object} config - iDomatic config (uses `prefix` and `idStrategy`).
31
+ * @param {Iterable<string>} existing - ids already present in the file.
32
+ * @returns {(parts: string[]) => string} - returns a unique id per call.
33
+ */
34
+ export function createIdFactory(config, existing = []) {
35
+ const prefix = config.prefix || "";
36
+ const random = config.idStrategy === "random";
37
+ const taken = new Set(existing);
38
+
39
+ return function makeId(parts) {
40
+ if (random) {
41
+ let candidate = `${prefix}${randomUUID()}`;
42
+ while (taken.has(candidate)) candidate = `${prefix}${randomUUID()}`;
43
+ taken.add(candidate);
44
+ return candidate;
45
+ }
46
+
47
+ const slug =
48
+ slugify(parts.map((p) => slugify(p)).filter(Boolean).join("-")) || "el";
49
+
50
+ let candidate = `${prefix}${slug}`;
51
+ let n = 2;
52
+ while (taken.has(candidate)) {
53
+ candidate = `${prefix}${slug}-${n++}`;
54
+ }
55
+ taken.add(candidate);
56
+ return candidate;
57
+ };
58
+ }
package/package.json CHANGED
@@ -1,9 +1,25 @@
1
1
  {
2
2
  "name": "@idomatic/parser-html",
3
- "version": "1.0.43",
3
+ "version": "1.1.1",
4
4
  "description": "HTML/Vue/Angular parser for idomatic CLI",
5
5
  "type": "module",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/AmarSmajlovic/idomatic.git",
9
+ "directory": "packages/parser-html"
10
+ },
11
+ "homepage": "https://www.idomatic.dev",
12
+ "bugs": {
13
+ "url": "https://github.com/AmarSmajlovic/idomatic/issues"
14
+ },
6
15
  "main": "index.js",
16
+ "files": [
17
+ "index.js",
18
+ "idGen.js",
19
+ "utils",
20
+ "consts",
21
+ "helpers"
22
+ ],
7
23
  "exports": {
8
24
  "default": "./index.js"
9
25
  },
@@ -12,11 +28,22 @@
12
28
  "parser",
13
29
  "html",
14
30
  "vue",
15
- "angular"
31
+ "angular",
32
+ "data-testid",
33
+ "test-automation",
34
+ "playwright",
35
+ "cypress",
36
+ "testing-library"
16
37
  ],
17
38
  "author": "Amar Smajlovic",
18
39
  "license": "MIT",
40
+ "scripts": {
41
+ "test": "vitest run"
42
+ },
19
43
  "dependencies": {
20
44
  "magic-string": "^0.30.17"
45
+ },
46
+ "devDependencies": {
47
+ "vitest": "^2.1.8"
21
48
  }
22
49
  }
package/utils/index.js CHANGED
@@ -1,68 +1,98 @@
1
1
  import MagicString from "magic-string";
2
- import { randomUUID } from "crypto";
3
- import { TAG_REGEX } from "../consts/index.js";
2
+ import { createIdFactory } from "../idGen.js";
4
3
 
5
- /**
6
- * Processes a Vue template content by inserting unique IDs while preserving formatting.
7
- *
8
- * @param {string} templateContent - The content inside the Vue <template> tag.
9
- * @param {object} config - The configuration object.
10
- * @param {string} config.attributeName - The attribute name to add (e.g. "id" or "data-id").
11
- * @param {string} config.prefix - The prefix for generated IDs.
12
- * @param {string[]} [config.excludeTags] - An array of tag names to exclude (optional).
13
- * @returns {string} - The updated template content.
14
- */
15
- export function processVueTemplate(templateContent, config) {
16
- const { attributeName, prefix, excludeTags = [] } = config;
17
- const ms = new MagicString(templateContent);
4
+ // Attributes that describe an element well, in priority order.
5
+ const DESCRIPTIVE_ATTRS = [
6
+ "aria-label",
7
+ "name",
8
+ "placeholder",
9
+ "alt",
10
+ "title",
11
+ "label",
12
+ "role",
13
+ "type",
14
+ ];
18
15
 
19
- let match;
20
- while ((match = TAG_REGEX.exec(templateContent)) !== null) {
21
- const fullMatch = match[0];
22
- const tagName = match[1];
23
- const attributesStr = match[2];
24
- const selfClosing = match[3] || "";
16
+ const TAG_REGEX = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
25
17
 
26
- if (excludeTags.includes(tagName)) continue;
18
+ function escapeRegExp(str) {
19
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
20
+ }
27
21
 
28
- const idRegex = new RegExp(`\\s${attributeName}\\s*=`);
29
- if (idRegex.test(attributesStr)) continue;
22
+ function getAttrValue(attributesStr, attrName) {
23
+ const re = new RegExp(
24
+ `${escapeRegExp(attrName)}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`,
25
+ "i"
26
+ );
27
+ const match = attributesStr.match(re);
28
+ if (!match) return "";
29
+ return match[1] ?? match[2] ?? "";
30
+ }
30
31
 
31
- const insertPosition =
32
- match.index + fullMatch.length - (selfClosing.length + 1);
33
- ms.appendLeft(
34
- insertPosition,
35
- ` ${attributeName}="${prefix}${randomUUID()}"`
36
- );
32
+ function describe(attributesStr) {
33
+ for (const name of DESCRIPTIVE_ATTRS) {
34
+ const value = getAttrValue(attributesStr, name);
35
+ if (value) return value;
37
36
  }
37
+ return "";
38
+ }
38
39
 
39
- return ms.toString();
40
+ function collectExistingIds(content, attributeName) {
41
+ const ids = [];
42
+ const re = new RegExp(
43
+ `\\s${escapeRegExp(attributeName)}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`,
44
+ "gi"
45
+ );
46
+ let match;
47
+ while ((match = re.exec(content)) !== null) {
48
+ const value = match[1] ?? match[2];
49
+ if (value) ids.push(value);
50
+ }
51
+ return ids;
40
52
  }
41
53
 
42
- export function processHtmlContent(htmlContent, config) {
43
- const { attributeName, prefix, excludeTags = [] } = config;
44
- const ms = new MagicString(htmlContent);
54
+ /**
55
+ * Inserts semantic, unique ids into every eligible tag of an HTML-like string
56
+ * while preserving the original formatting.
57
+ *
58
+ * @param {string} content - HTML or Vue template markup.
59
+ * @param {object} config - The configuration object.
60
+ * @param {string} config.attributeName - The attribute name to add.
61
+ * @param {string} [config.prefix] - The prefix for generated ids.
62
+ * @param {string[]} [config.excludeTags] - Tag names to skip.
63
+ * @returns {string} - The updated markup.
64
+ */
65
+ function processTemplate(content, config) {
66
+ const { attributeName, excludeTags = [] } = config;
67
+ const ms = new MagicString(content);
68
+ const makeId = createIdFactory(config, collectExistingIds(content, attributeName));
69
+
70
+ const idRegex = new RegExp(`\\s${escapeRegExp(attributeName)}\\s*=`);
45
71
 
46
- const tagRegex = /<([a-zA-Z][^\s/>]*)([^>]*?)(\/?)>/g;
47
72
  let match;
48
- while ((match = tagRegex.exec(htmlContent)) !== null) {
73
+ while ((match = TAG_REGEX.exec(content)) !== null) {
49
74
  const fullMatch = match[0];
50
75
  const tagName = match[1];
51
76
  const attributesStr = match[2];
52
77
  const selfClosing = match[3] || "";
53
78
 
54
79
  if (excludeTags.includes(tagName)) continue;
55
-
56
- const idRegex = new RegExp(`\\s${attributeName}\\s*=`);
57
80
  if (idRegex.test(attributesStr)) continue;
58
81
 
82
+ const idValue = makeId([tagName, describe(attributesStr)]);
59
83
  const insertPosition =
60
84
  match.index + fullMatch.length - (selfClosing.length + 1);
61
- ms.appendLeft(
62
- insertPosition,
63
- ` ${attributeName}="${prefix}${randomUUID()}"`
64
- );
85
+ ms.appendLeft(insertPosition, ` ${attributeName}="${idValue}"`);
65
86
  }
66
87
 
88
+ TAG_REGEX.lastIndex = 0;
67
89
  return ms.toString();
68
90
  }
91
+
92
+ export function processVueTemplate(templateContent, config) {
93
+ return processTemplate(templateContent, config);
94
+ }
95
+
96
+ export function processHtmlContent(htmlContent, config) {
97
+ return processTemplate(htmlContent, config);
98
+ }
package/CHANGELOG.md DELETED
@@ -1,13 +0,0 @@
1
- # Changelog
2
-
3
- ## [1.0.43] - 2024-12-22
4
-
5
- ### Fixed
6
- - Fixed HTML parser import/export bug where `templateRegex` was not properly exported from consts/index.js
7
- - Resolved module loading issues that prevented proper functionality
8
-
9
- ### Changed
10
- - Updated import statement to use correct export name (`TEMPLATE_REGEX as templateRegex`)
11
-
12
- ## [1.0.42] - Previous releases
13
- - Previous functionality and features