Skip to content

Commit

Permalink
fix: formatting void elements to jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko Nowak committed Jan 31, 2024
1 parent 583856f commit 5a2b9e4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
36 changes: 36 additions & 0 deletions src/app/core/services/formatter.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,42 @@ describe('Service: Formatter', () => {
}
));

it('should add closing slash to col elements but not colgroup', inject(
[FormatterService],
(service: FormatterService) => {
const str = `
<colgroup>
<col class="w-5">
<col>
<col>
<col>
<col>
<col>
<col class="w-5">
</colgroup>`;
const result = `
<colgroup>
<col className="w-5" />
<col />
<col />
<col />
<col />
<col />
<col className="w-5" />
</colgroup>`;
expect(service.useReactSyntax(str)).toBe(result);
}
));

it('should not add a closing slash to col elements if it already has one', inject(
[FormatterService],
(service: FormatterService) => {
const str = `<col class="w-5" /><col />`;
const result = `<col className="w-5" /><col />`;
expect(service.useReactSyntax(str)).toBe(result);
}
));

it('should change svg properties to camelCase', inject(
[FormatterService],
(service: FormatterService) => {
Expand Down
37 changes: 26 additions & 11 deletions src/app/core/services/formatter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,32 @@ export class FormatterService {
}

useReactSyntax(codeStr: string): string {
const cleanStroke = codeStr.replace(/(stroke)-[a-z]/g, prop => {
const parts = prop.split('-');
return parts[0] + parts[1].toUpperCase();
});
const cleanSVGs = cleanStroke.replace(/(fill|clip)-rule/g, prop => {
const parts = prop.split('-');
return parts[0] + parts[1].charAt(0).toUpperCase() + parts[1].slice(1);
});
const closeImgTags = cleanSVGs.replace(/<img ([^<]*[^/\s])[\s]?>/g, '<img $1 />');
const closeInputTags = closeImgTags.replace(/<input ([^<]*[^/\s])[\s]?>/g, '<input $1 />');
return closeInputTags.replace(/class=/gm, 'className=');
const reactSyntax = codeStr
.replace(/(stroke)-[a-z]/g, prop => {
const parts = prop.split('-');
return parts[0] + parts[1].toUpperCase();
})
.replace(/(fill|clip)-rule/g, prop => {
const parts = prop.split('-');
return parts[0] + parts[1].charAt(0).toUpperCase() + parts[1].slice(1);
})
.replace(/<img ([^<]*[^/\s])[\s]?>/g, '<img $1 />')
.replace(/<input ([^<]*[^/\s])[\s]?>/g, '<input $1 />')
.replace(/<area([^<]*[^/\s])[\s]?>/g, '<area $1 />')
.replace(/<base([^<]*[^/\s])[\s]?>/g, '<base $1 />')
.replace(/<br([^<]*[^/\s])[\s]?>/g, '<br $1 />')
.replace(/<col[^group]([^<]*[^/\s])[\s]?>/g, '<col $1 />')
.replace(/<col>/g, '<col />')
.replace(/<embed([^<]*[^/\s])[\s]?>/g, '<embed $1 />')
.replace(/<hr([^<]*[^/\s])[\s]?>/g, '<hr $1 />')
.replace(/<link([^<]*[^/\s])[\s]?>/g, '<link $1 />')
.replace(/<meta([^<]*[^/\s])[\s]?>/g, '<meta $1 />')
.replace(/<param([^<]*[^/\s])[\s]?>/g, '<param $1 />')
.replace(/<source([^<]*[^/\s])[\s]?>/g, '<source $1 />')
.replace(/<track([^<]*[^/\s])[\s]?>/g, '<track $1 />')
.replace(/<wbr([^<]*[^/\s])[\s]?>/g, '<wbr />')
.replace(/class=/gm, 'className=');
return reactSyntax;
}

toVue(codeStr: string): string {
Expand Down

0 comments on commit 5a2b9e4

Please sign in to comment.