Skip to content

Commit 5b94390

Browse files
fix: ensure initial class object renders correctly (#67)
* fix: ensure initial class object renders correctly * fix(parser): improve attribute value stringification for class and style handling * fix(parser): remove redundant code * Removing UTF-8 with BOM encoding from import * fix: ensure initial class object renders correctly * fix: ensure initial class object renders correctly
1 parent c3aa159 commit 5b94390

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

src/parser/parser.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ const getEventName = (eventAttributeString: RMLEventAttributeName): [RMLEventNam
8484
* };
8585
* ```
8686
**/
87+
function stringifyInitialValue(
88+
initialValue: string | number | boolean | Record<string, any> | null | undefined,
89+
attributeName?: string
90+
): string {
91+
if (initialValue == null) return '';
92+
93+
const obj = initialValue as Record<string, any>;
94+
95+
if (attributeName === 'class') {
96+
return Object.entries(obj)
97+
.filter(([_, v]) => v)
98+
.map(([k]) => k)
99+
.join(' ');
100+
}
101+
102+
if (attributeName === 'style') {
103+
return Object.entries(obj)
104+
.map(kvp => kvp.join(':'))
105+
.join('; ');
106+
}
107+
return String(initialValue ?? '');
108+
}
109+
87110
export function rml(strings: TemplateStringsArray, ...expressions: RMLTemplateExpression[]): HTMLString {
88111
let acc = '';
89112
const strlen = strings.length -1;
@@ -212,8 +235,9 @@ export function rml(strings: TemplateStringsArray, ...expressions: RMLTemplateEx
212235
? accPlusString.replace(new RegExp(`${attributeName}=['"]+$`), `_${attributeName}="`) // TODO: or maybe clean it up completely?
213236
: accPlusString
214237
;
238+
const formattedInitial = stringifyInitialValue(initialValue, attributeName);
215239

216-
acc = (prefix +(initialValue ?? '')).replace(/<(\w[\w-]*)\s+([^>]+)$/, `<$1 ${existingRef?'':`${RESOLVE_ATTRIBUTE}="${ref}" `}$2`);
240+
acc = (prefix + formattedInitial).replace(/<(\w[\w-]*)\s+([^>]+)$/, `<$1 ${existingRef?'':`${RESOLVE_ATTRIBUTE}="${ref}" `}$2`);
217241
}
218242
} else if(/<[a-z_][a-z0-9_-]*[^>]*(?:\s+\.\.\.)?$/ig.test(accPlusString.substring(lastTag))) {
219243

0 commit comments

Comments
 (0)