Skip to content

Commit

Permalink
Fix escape id for complex id values
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Aug 26, 2020
1 parent cd8a64e commit 559ee6d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ tests/pug-tests/attrs.formatted.pug
tests/pug-tests/attrs.js.formatted.pug
tests/pug-tests/attrs.js.pug
tests/pug-tests/attrs.pug
tests/pug-tests/attrs.unescaped.formatted.pug
tests/pug-tests/attrs.unescaped.pug
tests/pug-tests/block-code.formatted.pug
tests/pug-tests/block-code.pug
tests/pug-tests/comments.formatted.pug
Expand Down
6 changes: 5 additions & 1 deletion src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,11 @@ export class PugPrinter {
const validIdNameRegex: RegExp = /^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$/;
if (!validIdNameRegex.test(val)) {
val = makeString(val, this.quotes);
this.result += `id=${val}`;
this.result += 'id';
if (token.mustEscape === false) {
this.result += '!';
}
this.result += `=${val}`;
return;
}
// Write css-id in front of css-classes
Expand Down
3 changes: 3 additions & 0 deletions tests/pug-tests/attrs.unescaped.formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
script(type="text/x-template")
div(id!="user-<%= user.id %>")
h1 <%= user.title %>
3 changes: 3 additions & 0 deletions tests/pug-tests/attrs.unescaped.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
script(type='text/x-template')
div(id!='user-<%= user.id %>')
h1 <%= user.title %>
41 changes: 35 additions & 6 deletions tests/pug-tests/pug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,45 @@ import { plugin } from './../../src/index';

describe('Pug Tests', () => {
const filenames = readdirSync(resolve(__dirname), 'utf8');

const ignores: string[] = [
'attrs.pug',
'attrs.js.pug',
'block-code.pug',
'comments.pug',
'comments.source.pug',
'escaping-class-attribute.pug',
'filters.coffeescript.pug',
'filters.custom.pug',
'filters.include.pug',
'inheritance.extend.mixins.pug',
'inline-block-comment.pug',
'inline-tag.pug',
'layout.append.without-block.pug',
'layout.multi.append.prepend.block.pug',
'layout.prepend.without-block.pug',
'mixin.merge.pug',
'pipeless-filters.pug',
'pre.pug',
'script.whitespace.pug',
'styles.pug',
'tags.self-closing.pug',
'template.pug',
'text.pug'
];

for (const filename of filenames) {
if (filename.endsWith('.formatted.pug')) {
const unformattedFilename = filename.replace('.formatted', '');
test(unformattedFilename, () => {
const expected: string = readFileSync(resolve(__dirname, filename), 'utf8');
const code: string = readFileSync(resolve(__dirname, unformattedFilename), 'utf8');
const actual: string = format(code, { parser: 'pug' as any, plugins: [plugin] });
if (!ignores.includes(unformattedFilename)) {
test(unformattedFilename, () => {
const expected: string = readFileSync(resolve(__dirname, filename), 'utf8');
const code: string = readFileSync(resolve(__dirname, unformattedFilename), 'utf8');
const actual: string = format(code, { parser: 'pug' as any, plugins: [plugin] });

expect(actual).toBe(expected);
});
expect(actual).toBe(expected);
});
}
}
}
});

0 comments on commit 559ee6d

Please sign in to comment.