html-component-engine 0.1.2 → 0.1.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.
@@ -1,74 +1,76 @@
1
- // Utility functions for the HTML Component Engine
2
-
3
- /**
4
- * Parse a self-closing Component tag to extract attributes
5
- * @param {string} tag - The full component tag, e.g., <Component src="Button" text="Click" />
6
- * @returns {object|null} - Object with attribute key-value pairs, or null if invalid
7
- */
8
- export function parseSelfClosingComponentTag(tag) {
9
- const regex = /<Component\s+([^>]+)\s*\/>/;
10
- const match = tag.match(regex);
11
- if (!match) return null;
12
-
13
- const attrs = {};
14
- // Support hyphenated attributes like data-test, aria-label
15
- const attrRegex = /([\w-]+)="([^"]*)"/g;
16
- let attrMatch;
17
- while ((attrMatch = attrRegex.exec(match[1])) !== null) {
18
- attrs[attrMatch[1]] = attrMatch[2];
19
- }
20
- return attrs;
21
- }
22
-
23
- /**
24
- * Parse a Component tag to extract attributes (legacy alias)
25
- * @param {string} tag - The full component tag
26
- * @returns {object|null} - Object with attribute key-value pairs, or null if invalid
27
- */
28
- export function parseComponentTag(tag) {
29
- return parseSelfClosingComponentTag(tag);
30
- }
31
-
32
- /**
33
- * Parse variants from component HTML
34
- * Looks for <!-- variants: primary=class1 class2, secondary=class3 -->
35
- * @param {string} html - The component HTML
36
- * @returns {object} - Map of variant name to classes
37
- */
38
- export function parseVariants(html) {
39
- const variants = {};
40
- // Match HTML comment with variants - use non-greedy match
41
- const regex = /<!--\s*variants:\s*(.+?)\s*-->/;
42
- const match = html.match(regex);
43
- if (match) {
44
- const variantsStr = match[1];
45
- const variantPairs = variantsStr.split(',');
46
- for (const pair of variantPairs) {
47
- const [name, ...classParts] = pair.split('=');
48
- const classes = classParts.join('='); // Handle = in class names (unlikely but safe)
49
- if (name && classes) {
50
- variants[name.trim()] = classes.trim();
51
- }
52
- }
53
- }
54
- return variants;
55
- }
56
-
57
- /**
58
- * Clean unused placeholders from compiled HTML
59
- * @param {string} html - The compiled HTML
60
- * @returns {string} - Cleaned HTML
61
- */
62
- export function cleanUnusedPlaceholders(html) {
63
- // Remove any remaining {{ ... }} placeholders
64
- return html.replace(/\{\{\s*\w+\s*\}\}/g, '');
65
- }
66
-
67
- /**
68
- * Normalize path separators to forward slashes
69
- * @param {string} p - Path to normalize
70
- * @returns {string} - Normalized path
71
- */
72
- export function normalizePath(p) {
73
- return p.replace(/\\/g, '/');
74
- }
1
+ // Utility functions for the HTML Component Engine
2
+
3
+ /**
4
+ * Parse a self-closing Component tag to extract attributes
5
+ * @param {string} tag - The full component tag, e.g., <Component src="Button" text="Click" />
6
+ * @returns {object|null} - Object with attribute key-value pairs, or null if invalid
7
+ */
8
+ export function parseSelfClosingComponentTag(tag) {
9
+ const regex = /<Component\s+([^>]+)\s*\/>/;
10
+ const match = tag.match(regex);
11
+ if (!match) return null;
12
+
13
+ const attrs = {};
14
+ // Support hyphenated attributes with both quote styles and boolean attributes
15
+ const attrRegex = /([\w-]+)(?:=(?:"([^"]*)"|'([^']*)'))?/g;
16
+ let attrMatch;
17
+ while ((attrMatch = attrRegex.exec(match[1])) !== null) {
18
+ const key = attrMatch[1];
19
+ const value = attrMatch[2] ?? attrMatch[3] ?? 'true';
20
+ attrs[key] = value;
21
+ }
22
+ return attrs;
23
+ }
24
+
25
+ /**
26
+ * Parse a Component tag to extract attributes (legacy alias)
27
+ * @param {string} tag - The full component tag
28
+ * @returns {object|null} - Object with attribute key-value pairs, or null if invalid
29
+ */
30
+ export function parseComponentTag(tag) {
31
+ return parseSelfClosingComponentTag(tag);
32
+ }
33
+
34
+ /**
35
+ * Parse variants from component HTML
36
+ * Looks for <!-- variants: primary=class1 class2, secondary=class3 -->
37
+ * @param {string} html - The component HTML
38
+ * @returns {object} - Map of variant name to classes
39
+ */
40
+ export function parseVariants(html) {
41
+ const variants = {};
42
+ // Match HTML comment with variants - use non-greedy match
43
+ const regex = /<!--\s*variants:\s*(.+?)\s*-->/;
44
+ const match = html.match(regex);
45
+ if (match) {
46
+ const variantsStr = match[1];
47
+ const variantPairs = variantsStr.split(',');
48
+ for (const pair of variantPairs) {
49
+ const [name, ...classParts] = pair.split('=');
50
+ const classes = classParts.join('='); // Handle = in class names (unlikely but safe)
51
+ if (name && classes) {
52
+ variants[name.trim()] = classes.trim();
53
+ }
54
+ }
55
+ }
56
+ return variants;
57
+ }
58
+
59
+ /**
60
+ * Clean unused placeholders from compiled HTML
61
+ * @param {string} html - The compiled HTML
62
+ * @returns {string} - Cleaned HTML
63
+ */
64
+ export function cleanUnusedPlaceholders(html) {
65
+ // Remove any remaining {{ ... }} placeholders
66
+ return html.replace(/\{\{\s*\w+\s*\}\}/g, '');
67
+ }
68
+
69
+ /**
70
+ * Normalize path separators to forward slashes
71
+ * @param {string} p - Path to normalize
72
+ * @returns {string} - Normalized path
73
+ */
74
+ export function normalizePath(p) {
75
+ return p.replace(/\\/g, '/');
76
+ }