@idomatic/parser-html 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/index.js +13 -5
- package/package.json +14 -2
- package/utils/index.js +133 -31
package/index.js
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { TEMPLATE_REGEX as templateRegex } from "./consts/index.js";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
processComponentContent,
|
|
6
|
+
processHtmlContent,
|
|
7
|
+
processVueTemplate,
|
|
8
|
+
} from "./utils/index.js";
|
|
9
|
+
|
|
10
|
+
const COMPONENT_EXTS = new Set([".svelte", ".astro"]);
|
|
11
|
+
const MARKUP_EXTS = new Set([".html", ".hbs", ".handlebars"]);
|
|
5
12
|
|
|
6
13
|
/**
|
|
7
|
-
* Parses an HTML
|
|
14
|
+
* Parses an HTML, Vue, Svelte, Astro or Handlebars file and adds unique ids.
|
|
8
15
|
*
|
|
9
16
|
* @param {string} filePath - The path to the file.
|
|
10
17
|
* @param {object} config - The configuration object.
|
|
11
18
|
* @param {boolean} dry - If true, does not write changes back to disk.
|
|
12
19
|
* @returns {boolean} - Returns true if changes were made.
|
|
13
20
|
*/
|
|
14
|
-
|
|
15
21
|
export default async function parseHTML(filePath, config, dry = false) {
|
|
16
22
|
const ext = path.extname(filePath).toLowerCase();
|
|
17
23
|
const originalContent = fs.readFileSync(filePath, "utf-8");
|
|
@@ -23,10 +29,12 @@ export default async function parseHTML(filePath, config, dry = false) {
|
|
|
23
29
|
const newTemplate = processVueTemplate(match[1], config);
|
|
24
30
|
updatedContent = originalContent.replace(
|
|
25
31
|
templateRegex,
|
|
26
|
-
`<template>${newTemplate}</template>`
|
|
32
|
+
() => `<template>${newTemplate}</template>`
|
|
27
33
|
);
|
|
28
34
|
}
|
|
29
|
-
} else if (
|
|
35
|
+
} else if (COMPONENT_EXTS.has(ext)) {
|
|
36
|
+
updatedContent = processComponentContent(originalContent, config);
|
|
37
|
+
} else if (MARKUP_EXTS.has(ext)) {
|
|
30
38
|
updatedContent = processHtmlContent(originalContent, config);
|
|
31
39
|
}
|
|
32
40
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idomatic/parser-html",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "HTML
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "HTML, Vue, Angular, Svelte, Astro and Handlebars 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",
|
|
7
16
|
"files": [
|
|
8
17
|
"index.js",
|
|
@@ -20,6 +29,9 @@
|
|
|
20
29
|
"html",
|
|
21
30
|
"vue",
|
|
22
31
|
"angular",
|
|
32
|
+
"svelte",
|
|
33
|
+
"astro",
|
|
34
|
+
"handlebars",
|
|
23
35
|
"data-testid",
|
|
24
36
|
"test-automation",
|
|
25
37
|
"playwright",
|
package/utils/index.js
CHANGED
|
@@ -13,15 +13,92 @@ const DESCRIPTIVE_ATTRS = [
|
|
|
13
13
|
"type",
|
|
14
14
|
];
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
// Elements whose content is raw text, never markup.
|
|
17
|
+
const RAW_TEXT_TAGS = new Set(["script", "style", "textarea", "title"]);
|
|
18
|
+
|
|
19
|
+
// Elements that render nothing of their own or reject attributes in
|
|
20
|
+
// component templates (Svelte, Astro), so an id there is wrong or a no-op.
|
|
21
|
+
const NON_RENDERING_TAGS = new Set(["slot", "Fragment", "script", "style"]);
|
|
17
22
|
|
|
18
23
|
function escapeRegExp(str) {
|
|
19
24
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
20
25
|
}
|
|
21
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Walks markup and returns every opening tag. Unlike a single regex, it treats
|
|
29
|
+
* quoted strings and `{...}` / `{{...}}` expressions inside a tag as opaque, so
|
|
30
|
+
* `onclick={() => a > b}` (Svelte, Astro) or `{{#if x}}class="a"{{/if}}`
|
|
31
|
+
* (Handlebars) do not end the tag early. Comments and raw-text element bodies
|
|
32
|
+
* (script, style, ...) are skipped.
|
|
33
|
+
*/
|
|
34
|
+
function findOpeningTags(content) {
|
|
35
|
+
const tags = [];
|
|
36
|
+
let i = 0;
|
|
37
|
+
|
|
38
|
+
while (i < content.length) {
|
|
39
|
+
const lt = content.indexOf("<", i);
|
|
40
|
+
if (lt === -1) break;
|
|
41
|
+
|
|
42
|
+
if (content.startsWith("<!--", lt)) {
|
|
43
|
+
const end = content.indexOf("-->", lt + 4);
|
|
44
|
+
i = end === -1 ? content.length : end + 3;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const nameMatch = /^<([a-zA-Z][\w:.-]*)/.exec(content.slice(lt, lt + 200));
|
|
49
|
+
if (!nameMatch) {
|
|
50
|
+
i = lt + 1;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const tagName = nameMatch[1];
|
|
55
|
+
let j = lt + nameMatch[0].length;
|
|
56
|
+
let quote = null;
|
|
57
|
+
let depth = 0;
|
|
58
|
+
|
|
59
|
+
for (; j < content.length; j++) {
|
|
60
|
+
const ch = content[j];
|
|
61
|
+
if (quote) {
|
|
62
|
+
if (ch === quote) quote = null;
|
|
63
|
+
} else if (ch === '"' || ch === "'" || (ch === "`" && depth > 0)) {
|
|
64
|
+
quote = ch;
|
|
65
|
+
} else if (ch === "{") {
|
|
66
|
+
depth++;
|
|
67
|
+
} else if (ch === "}") {
|
|
68
|
+
depth = Math.max(0, depth - 1);
|
|
69
|
+
} else if (ch === ">" && depth === 0) {
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (j >= content.length) break; // unterminated tag
|
|
75
|
+
|
|
76
|
+
const selfClosing = content[j - 1] === "/";
|
|
77
|
+
const attrsStart = lt + nameMatch[0].length;
|
|
78
|
+
const attributesStr = content.slice(attrsStart, selfClosing ? j - 1 : j);
|
|
79
|
+
// Insert right after the last attribute so existing spacing before `>` or
|
|
80
|
+
// `/>` is kept: `<input name="a" />` -> `<input name="a" id="x" />`.
|
|
81
|
+
tags.push({
|
|
82
|
+
start: lt,
|
|
83
|
+
tagName,
|
|
84
|
+
attributesStr,
|
|
85
|
+
insertAt: attrsStart + attributesStr.trimEnd().length,
|
|
86
|
+
});
|
|
87
|
+
i = j + 1;
|
|
88
|
+
|
|
89
|
+
if (RAW_TEXT_TAGS.has(tagName.toLowerCase()) && !selfClosing) {
|
|
90
|
+
const close = new RegExp(`</${escapeRegExp(tagName)}\\s*>`, "i");
|
|
91
|
+
const rest = close.exec(content.slice(i));
|
|
92
|
+
i = rest ? i + rest.index + rest[0].length : content.length;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return tags;
|
|
97
|
+
}
|
|
98
|
+
|
|
22
99
|
function getAttrValue(attributesStr, attrName) {
|
|
23
100
|
const re = new RegExp(
|
|
24
|
-
|
|
101
|
+
`(?:^|\\s)${escapeRegExp(attrName)}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`,
|
|
25
102
|
"i"
|
|
26
103
|
);
|
|
27
104
|
const match = attributesStr.match(re);
|
|
@@ -32,60 +109,71 @@ function getAttrValue(attributesStr, attrName) {
|
|
|
32
109
|
function describe(attributesStr) {
|
|
33
110
|
for (const name of DESCRIPTIVE_ATTRS) {
|
|
34
111
|
const value = getAttrValue(attributesStr, name);
|
|
35
|
-
|
|
112
|
+
// Skip values that are template expressions, e.g. "{{label}}".
|
|
113
|
+
if (value && !/[{}]/.test(value)) return value;
|
|
36
114
|
}
|
|
37
115
|
return "";
|
|
38
116
|
}
|
|
39
117
|
|
|
40
|
-
function
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
118
|
+
function hasAttribute(attributesStr, attributeName) {
|
|
119
|
+
const name = escapeRegExp(attributeName);
|
|
120
|
+
// `id="x"`, `id={x}`, Vue `:id="x"` / `v-bind:id`, and Svelte shorthand `{id}`.
|
|
121
|
+
return new RegExp(
|
|
122
|
+
`(?:^|\\s)(?::|v-bind:)?${name}\\s*=|\\{\\s*${name}\\s*\\}`
|
|
123
|
+
).test(attributesStr);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function collectExistingIds(tags, attributeName) {
|
|
127
|
+
return tags
|
|
128
|
+
.map((t) => getAttrValue(t.attributesStr, attributeName))
|
|
129
|
+
.filter(Boolean);
|
|
52
130
|
}
|
|
53
131
|
|
|
54
132
|
/**
|
|
55
133
|
* Inserts semantic, unique ids into every eligible tag of an HTML-like string
|
|
56
134
|
* while preserving the original formatting.
|
|
57
135
|
*
|
|
58
|
-
* @param {string} content - HTML
|
|
136
|
+
* @param {string} content - Markup (HTML, Vue template, Svelte, Astro, Handlebars).
|
|
59
137
|
* @param {object} config - The configuration object.
|
|
60
138
|
* @param {string} config.attributeName - The attribute name to add.
|
|
61
139
|
* @param {string} [config.prefix] - The prefix for generated ids.
|
|
62
140
|
* @param {string[]} [config.excludeTags] - Tag names to skip.
|
|
141
|
+
* @param {object} [options]
|
|
142
|
+
* @param {boolean} [options.component] - Skip non-rendering component tags
|
|
143
|
+
* (`slot`, `Fragment`, `svelte:*`) and top-level script/style blocks.
|
|
63
144
|
* @returns {string} - The updated markup.
|
|
64
145
|
*/
|
|
65
|
-
function processTemplate(content, config) {
|
|
146
|
+
function processTemplate(content, config, { component = false } = {}) {
|
|
66
147
|
const { attributeName, excludeTags = [] } = config;
|
|
67
148
|
const ms = new MagicString(content);
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
149
|
+
const tags = findOpeningTags(content);
|
|
150
|
+
const makeId = createIdFactory(
|
|
151
|
+
config,
|
|
152
|
+
collectExistingIds(tags, attributeName)
|
|
153
|
+
);
|
|
71
154
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
155
|
+
// `<svelte:head>` content goes into document.head, not the rendered page.
|
|
156
|
+
const headRanges = component
|
|
157
|
+
? [...content.matchAll(/<svelte:head[\s\S]*?<\/svelte:head>/g)].map(
|
|
158
|
+
(m) => [m.index, m.index + m[0].length]
|
|
159
|
+
)
|
|
160
|
+
: [];
|
|
78
161
|
|
|
162
|
+
for (const { start, tagName, attributesStr, insertAt } of tags) {
|
|
79
163
|
if (excludeTags.includes(tagName)) continue;
|
|
80
|
-
if (
|
|
164
|
+
if (headRanges.some(([from, to]) => start > from && start < to)) continue;
|
|
165
|
+
if (
|
|
166
|
+
component &&
|
|
167
|
+
(NON_RENDERING_TAGS.has(tagName) || tagName.startsWith("svelte:"))
|
|
168
|
+
) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (hasAttribute(attributesStr, attributeName)) continue;
|
|
81
172
|
|
|
82
173
|
const idValue = makeId([tagName, describe(attributesStr)]);
|
|
83
|
-
|
|
84
|
-
match.index + fullMatch.length - (selfClosing.length + 1);
|
|
85
|
-
ms.appendLeft(insertPosition, ` ${attributeName}="${idValue}"`);
|
|
174
|
+
ms.appendLeft(insertAt, ` ${attributeName}="${idValue}"`);
|
|
86
175
|
}
|
|
87
176
|
|
|
88
|
-
TAG_REGEX.lastIndex = 0;
|
|
89
177
|
return ms.toString();
|
|
90
178
|
}
|
|
91
179
|
|
|
@@ -96,3 +184,17 @@ export function processVueTemplate(templateContent, config) {
|
|
|
96
184
|
export function processHtmlContent(htmlContent, config) {
|
|
97
185
|
return processTemplate(htmlContent, config);
|
|
98
186
|
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Svelte and Astro components: markup plus script/style blocks (and, for
|
|
190
|
+
* Astro, a `---` frontmatter fence). Only the markup is touched.
|
|
191
|
+
*/
|
|
192
|
+
export function processComponentContent(content, config) {
|
|
193
|
+
const frontmatter = /^\s*---\r?\n[\s\S]*?\r?\n---/.exec(content);
|
|
194
|
+
if (!frontmatter) return processTemplate(content, config, { component: true });
|
|
195
|
+
|
|
196
|
+
const head = frontmatter[0];
|
|
197
|
+
return (
|
|
198
|
+
head + processTemplate(content.slice(head.length), config, { component: true })
|
|
199
|
+
);
|
|
200
|
+
}
|