From 20be986be4303c379f76acdd30d8b1a99bc2e30b Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg Date: Sat, 23 Nov 2019 17:53:36 +0100 Subject: [PATCH] Fix partial quoted surrounded attribute token --- src/index.ts | 105 +++++++++++--------- test/pug-tests/code.iteration.formatted.pug | 35 +++++++ test/pug-tests/code.iteration.pug | 35 +++++++ 3 files changed, 126 insertions(+), 49 deletions(-) create mode 100644 test/pug-tests/code.iteration.formatted.pug create mode 100644 test/pug-tests/code.iteration.pug diff --git a/src/index.ts b/src/index.ts index 0f7bd045..daadaab4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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( diff --git a/test/pug-tests/code.iteration.formatted.pug b/test/pug-tests/code.iteration.formatted.pug new file mode 100644 index 00000000..4035ad02 --- /dev/null +++ b/test/pug-tests/code.iteration.formatted.pug @@ -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} diff --git a/test/pug-tests/code.iteration.pug b/test/pug-tests/code.iteration.pug new file mode 100644 index 00000000..45946b49 --- /dev/null +++ b/test/pug-tests/code.iteration.pug @@ -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}