Skip to content

Commit

Permalink
Fix class handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Feb 11, 2020
1 parent 4ae5b2f commit 7bbc423
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,25 @@ export class PugPrinter {
val = val.replace(/\s\s+/g, ' ');
const classes: string[] = val.split(' ');
const specialClasses: string[] = [];
const normalClasses: string[] = [];
const validClassNameRegex: RegExp = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/;
for (const className of classes) {
if (!validClassNameRegex.test(className)) {
specialClasses.push(className);
continue;
} else {
normalClasses.push(className);
}
}
if (normalClasses.length > 0) {
// Write css-class in front of attributes
const position: number = this.possibleClassPosition;
this.result = [
this.result.slice(0, position),
`.${className}`,
'.',
normalClasses.join('.'),
this.result.slice(position)
].join('');
this.possibleClassPosition += 1 + className.length;
this.possibleClassPosition += 1 + normalClasses.join('.').length;
this.result = this.result.replace(/div\./, '.');
}
if (specialClasses.length > 0) {
Expand Down
4 changes: 4 additions & 0 deletions test/attributes/class-attributes/formatted.pug
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ a.class(href="index.html") Text
.class Text
#id Text
.class Text
.bg-red-100.border-l-4.border-red-600.text-red-700.p-4.mb-4
.bg-red-100.border-l-4.border-red-600.text-red-700.p-4.mb-4(
data-other="other"
)
2 changes: 2 additions & 0 deletions test/attributes/class-attributes/unformatted.pug
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ div(id="id") Text
div(class="class") Text
#id Text
.class Text
div(class="bg-red-100 border-l-4 border-red-600 text-red-700 p-4 mb-4")
div(class="bg-red-100 border-l-4 border-red-600 text-red-700 p-4 mb-4", data-other="other")

0 comments on commit 7bbc423

Please sign in to comment.