Skip to content

Commit

Permalink
Fix partial quoted surrounded attribute token
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Nov 23, 2019
1 parent bfb90c6 commit 20be986
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 49 deletions.
105 changes: 56 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,57 +184,64 @@ export const plugin: Plugin = {
}
break;
case 'attribute': {
if (
token.name === 'class' &&
typeof token.val === 'string' &&
(token.val.startsWith('"') || token.val.startsWith("'"))
) {
// Handle class attribute
let val = token.val;
val = val.substring(1, val.length - 1);
val = val.trim();
val = val.replace(/\s\s+/g, ' ');
const classes: string[] = val.split(' ');
const specialClasses: string[] = [];
const validClassNameRegex: RegExp = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/;
for (const className of classes) {
if (!validClassNameRegex.test(className)) {
specialClasses.push(className);
continue;
if (typeof token.val === 'string') {
const surroundedByQuotes: boolean =
(token.val.startsWith('"') && token.val.endsWith('"')) ||
(token.val.startsWith("'") && token.val.endsWith("'"));
if (surroundedByQuotes) {
if (token.name === 'class') {
// Handle class attribute
let val = token.val;
val = val.substring(1, val.length - 1);
val = val.trim();
val = val.replace(/\s\s+/g, ' ');
const classes: string[] = val.split(' ');
const specialClasses: string[] = [];
const validClassNameRegex: RegExp = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/;
for (const className of classes) {
if (!validClassNameRegex.test(className)) {
specialClasses.push(className);
continue;
}
// Write css-class in front of attributes
const position: number = startAttributePosition;
result = [
result.slice(0, position),
`.${className}`,
result.slice(position)
].join('');
startAttributePosition += 1 + className.length;
result = result.replace(/div\./, '.');
}
if (specialClasses.length > 0) {
token.val = makeString(
specialClasses.join(' '),
singleQuote ? "'" : '"',
false
);
previousAttributeRemapped = false;
} else {
previousAttributeRemapped = true;
break;
}
} else if (token.name === 'id') {
// Handle id attribute
let val = token.val;
val = val.substring(1, val.length - 1);
val = val.trim();
// Write css-id in front of css-classes
const position: number = startTagPosition;
result = [result.slice(0, position), `#${val}`, result.slice(position)].join(
''
);
startAttributePosition += 1 + val.length;
result = result.replace(/div#/, '#');
if (previousToken.type === 'attribute' && previousToken.name !== 'class') {
previousAttributeRemapped = true;
}
break;
}
// Write css-class in front of attributes
const position: number = startAttributePosition;
result = [result.slice(0, position), `.${className}`, result.slice(position)].join(
''
);
startAttributePosition += 1 + className.length;
result = result.replace(/div\./, '.');
}
if (specialClasses.length > 0) {
token.val = makeString(specialClasses.join(' '), singleQuote ? "'" : '"', false);
previousAttributeRemapped = false;
} else {
previousAttributeRemapped = true;
break;
}
} else if (
token.name === 'id' &&
typeof token.val === 'string' &&
(token.val.startsWith('"') || token.val.startsWith("'"))
) {
// Handle id attribute
let val = token.val;
val = val.substring(1, val.length - 1);
val = val.trim();
// Write css-id in front of css-classes
const position: number = startTagPosition;
result = [result.slice(0, position), `#${val}`, result.slice(position)].join('');
startAttributePosition += 1 + val.length;
result = result.replace(/div#/, '#');
if (previousToken.type === 'attribute' && previousToken.name !== 'class') {
previousAttributeRemapped = true;
}
break;
}

const hasNormalPreviousToken: AttributeToken | undefined = previousNormalAttributeToken(
Expand Down
35 changes: 35 additions & 0 deletions test/pug-tests/code.iteration.formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

- var items = [1, 2, 3];

ul
- items.forEach(function(item){
li= item
- })

- var items = [1, 2, 3];

ul
each item, i in items
li(class='item-' + i)= item

ul
each item, i in items
li= item

ul
each $item in items
li= $item

- var nums = [1, 2, 3];
- var letters = ['a', 'b', 'c'];

ul
each l in letters
each n in nums
li #{n}: #{l}

- var count = 1;
- var counter = function() { return [count++, count++, count++] }
ul
each n in counter()
li #{n}
35 changes: 35 additions & 0 deletions test/pug-tests/code.iteration.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

- var items = [1,2,3]

ul
- items.forEach(function(item){
li= item
- })

- var items = [1,2,3]

ul
for item, i in items
li(class='item-' + i)= item

ul
each item, i in items
li= item

ul
each $item in items
li= $item

- var nums = [1, 2, 3]
- var letters = ['a', 'b', 'c']

ul
for l in letters
for n in nums
li #{n}: #{l}

- var count = 1
- var counter = function() { return [count++, count++, count++] }
ul
for n in counter()
li #{n}

0 comments on commit 20be986

Please sign in to comment.