From 3bc65921165a5a78e0c5aee18874ece2186f42ec Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 11:41:42 +0200 Subject: [PATCH 1/8] Modification for issue 2967 --- packages/pug-lexer/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/pug-lexer/index.js b/packages/pug-lexer/index.js index 434e3a048..77d9e581e 100644 --- a/packages/pug-lexer/index.js +++ b/packages/pug-lexer/index.js @@ -1220,7 +1220,9 @@ Lexer.prototype = { // a part of the value if((!characterParser.isPunctuator(str[x]) || quoteRe.test(str[x]) || str[x] === ':') && this.assertExpression(val, true)){ done = true; - } + }else{ + this.error('INVALID_ATTRIBUTE_START', 'Attribute name starts with invalid character "' + str[0] + '", to use special characters in attribute names, wrap the attribute name in double quotes ""'); + } break; } } From 4f71f204879857ac57e33d07c2852c64ee46d1f8 Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 11:43:07 +0200 Subject: [PATCH 2/8] Modification for issue 2967 --- packages/pug-lexer/index.js | 88 ++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/packages/pug-lexer/index.js b/packages/pug-lexer/index.js index 77d9e581e..8462ddc46 100644 --- a/packages/pug-lexer/index.js +++ b/packages/pug-lexer/index.js @@ -108,10 +108,10 @@ Lexer.prototype = { tok: function(type, val){ var res = { - type: type, + type: type, loc: { start: { - line: this.lineno, + line: this.lineno, column: this.colno }, filename: this.filename @@ -122,15 +122,15 @@ Lexer.prototype = { return res; }, - + /** * Set the token's `loc.end` value. - * + * * @param {Object} tok * @returns {Object} * @api private */ - + tokEnd: function(tok){ tok.loc.end = { line: this.lineno, @@ -1052,7 +1052,7 @@ Lexer.prototype = { return true; } }, - + /** * Attribute Name. */ @@ -1061,7 +1061,7 @@ Lexer.prototype = { var quoteRe = /['"]/; var key = ''; var i; - + // consume all whitespace before the key for(i = 0; i < str.length; i++){ if(!this.whitespaceRe.test(str[i])) break; @@ -1071,23 +1071,23 @@ Lexer.prototype = { this.incrementColumn(1); } } - + if(i === str.length){ return ''; } - + var tok = this.tok('attribute'); - + // quote? if(quoteRe.test(str[i])){ quote = str[i]; this.incrementColumn(1); i++; } - + // start looping through the key for (; i < str.length; i++) { - + if(quote){ if (str[i] === quote) { this.incrementColumn(1); @@ -1099,20 +1099,20 @@ Lexer.prototype = { break; } } - + key += str[i]; - + if (str[i] === '\n') { this.incrementLine(1); } else { this.incrementColumn(1); } } - + tok.name = key; - + var valueResponse = this.attributeValue(str.substr(i)); - + if (valueResponse.val) { tok.val = valueResponse.val; tok.mustEscape = valueResponse.mustEscape; @@ -1121,11 +1121,11 @@ Lexer.prototype = { tok.val = true; tok.mustEscape = true; } - + str = valueResponse.remainingSource; - + this.tokens.push(this.tokEnd(tok)); - + for(i = 0; i < str.length; i++){ if(!this.whitespaceRe.test(str[i])) { break; @@ -1136,15 +1136,15 @@ Lexer.prototype = { this.incrementColumn(1); } } - + if(str[i] === ','){ this.incrementColumn(1); i++; } - + return str.substr(i); }, - + /** * Attribute Value. */ @@ -1156,7 +1156,7 @@ Lexer.prototype = { var state = characterParser.defaultState(); var col = this.colno; var line = this.lineno; - + // consume all whitespace before the equals sign for(i = 0; i < str.length; i++){ if(!this.whitespaceRe.test(str[i])) break; @@ -1167,18 +1167,18 @@ Lexer.prototype = { col++; } } - + if(i === str.length){ return { remainingSource: str }; } - + if(str[i] === '!'){ escapeAttr = false; col++; i++; if (str[i] !== '=') this.error('INVALID_KEY_CHARACTER', 'Unexpected character ' + str[i] + ' expected `=`'); } - + if(str[i] !== '='){ // check for anti-pattern `div("foo"bar)` if (i === 0 && str && !this.whitespaceRe.test(str[0]) && str[0] !== ','){ @@ -1187,11 +1187,11 @@ Lexer.prototype = { return { remainingSource: str }; } } - + this.lineno = line; this.colno = col + 1; i++; - + // consume all whitespace before the value for(; i < str.length; i++){ if(!this.whitespaceRe.test(str[i])) break; @@ -1201,18 +1201,18 @@ Lexer.prototype = { this.incrementColumn(1); } } - + line = this.lineno; col = this.colno; - + // start looping through the value for (; i < str.length; i++) { // if the character is in a string or in parentheses/brackets/braces if (!(state.isNesting() || state.isString())){ - + if (this.whitespaceRe.test(str[i])) { done = false; - + // find the first non-whitespace character for (x = i; x < str.length; x++) { if (!this.whitespaceRe.test(str[x])) { @@ -1221,29 +1221,29 @@ Lexer.prototype = { if((!characterParser.isPunctuator(str[x]) || quoteRe.test(str[x]) || str[x] === ':') && this.assertExpression(val, true)){ done = true; }else{ - this.error('INVALID_ATTRIBUTE_START', 'Attribute name starts with invalid character "' + str[0] + '", to use special characters in attribute names, wrap the attribute name in double quotes ""'); - } + this.error('INVALID_ATTRIBUTE_START', 'Attribute name starts with invalid character "' + str[0] + '", to use special characters in attribute names, wrap the attribute name in double quotes ""'); + } break; } } - + // if everything else is whitespace, return now so last attribute // does not include trailing whitespace if(done || x === str.length){ break; } } - + // if there's no whitespace and the character is not ',', the // attribute did not end. if(str[i] === ',' && this.assertExpression(val, true)){ break; } } - + state = characterParser.parseChar(str[i], state); val += str[i]; - + if (str[i] === '\n') { line++; col = 1; @@ -1251,22 +1251,22 @@ Lexer.prototype = { col++; } } - + this.assertExpression(val); - + this.lineno = line; this.colno = col; - + return { val: val, mustEscape: escapeAttr, remainingSource: str.substr(i) }; }, /** * Attributes. */ - + attrs: function() { var tok; - + if ('(' == this.input.charAt(0)) { tok = this.tok('start-attributes'); var index = this.bracketExpression().end; From 94b182f4f5b6e7d4f4a6e525d55e7a92383d3027 Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 11:44:45 +0200 Subject: [PATCH 3/8] Modification for issue 2967 --- packages/pug-lexer/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/pug-lexer/index.js b/packages/pug-lexer/index.js index 8462ddc46..862a5d978 100644 --- a/packages/pug-lexer/index.js +++ b/packages/pug-lexer/index.js @@ -1221,7 +1221,8 @@ Lexer.prototype = { if((!characterParser.isPunctuator(str[x]) || quoteRe.test(str[x]) || str[x] === ':') && this.assertExpression(val, true)){ done = true; }else{ - this.error('INVALID_ATTRIBUTE_START', 'Attribute name starts with invalid character "' + str[0] + '", to use special characters in attribute names, wrap the attribute name in double quotes ""'); + this.error('INVALID_ATTRIBUTE_START', 'Attribute name starts with invalid character "' + str[0] + + '", to use special characters in attribute names, wrap the attribute name in double quotes ""'); } break; } From 8068f02ca990638a11fff933b225f5432cb9bb65 Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 12:00:12 +0200 Subject: [PATCH 4/8] test case --- packages/pug/test/anti-cases/invalid-character.pug | 1 + 1 file changed, 1 insertion(+) create mode 100644 packages/pug/test/anti-cases/invalid-character.pug diff --git a/packages/pug/test/anti-cases/invalid-character.pug b/packages/pug/test/anti-cases/invalid-character.pug new file mode 100644 index 000000000..43008b602 --- /dev/null +++ b/packages/pug/test/anti-cases/invalid-character.pug @@ -0,0 +1 @@ +p({{bar}}) foo From cb216483f9a880a2f152cf559515235b6fbd99c0 Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 18:39:39 +0200 Subject: [PATCH 5/8] New warn message when invalid character and test for Issue 2967 --- packages/pug-lexer/index.js | 8 ++++---- .../__snapshots__/invalid.test.js.snap | 5 +++++ .../invalid-character.pug | 0 .../Invalid-Attribute-test/invalid.test.js | 12 ++++++++++++ 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap rename packages/pug/test/anti-cases/{ => Invalid-Attribute-test}/invalid-character.pug (100%) create mode 100644 packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js diff --git a/packages/pug-lexer/index.js b/packages/pug-lexer/index.js index 862a5d978..5c5a9742e 100644 --- a/packages/pug-lexer/index.js +++ b/packages/pug-lexer/index.js @@ -1083,6 +1083,9 @@ Lexer.prototype = { quote = str[i]; this.incrementColumn(1); i++; + }else if(characterParser.isPunctuator(str[i]) && str[i] !== ':'){ + console.warn(this.filename + ', line ' + tok.loc.start.line + 'Attribute name starts with invalid character "' + str[i] + + '", to use special characters in attribute names, wrap the attribute name in double quotes ""') } // start looping through the key @@ -1220,10 +1223,7 @@ Lexer.prototype = { // a part of the value if((!characterParser.isPunctuator(str[x]) || quoteRe.test(str[x]) || str[x] === ':') && this.assertExpression(val, true)){ done = true; - }else{ - this.error('INVALID_ATTRIBUTE_START', 'Attribute name starts with invalid character "' + str[0] + - '", to use special characters in attribute names, wrap the attribute name in double quotes ""'); - } + } break; } } diff --git a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap new file mode 100644 index 000000000..cd3418172 --- /dev/null +++ b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap @@ -0,0 +1,5 @@ +exports[`test invalid attribute name should log warning 1`] = ` +Array [ + "C:\\Users\\ivan\\Desktop\\Nueva carpeta\\Pug pull request\\pug\\packages\\pug\\test\\anti-cases\\Invalid-Attribute-test/invalid-character.pug, line 1Attribute name starts with invalid character \"{\", to use special characters in attribute names, wrap the attribute name in double quotes \"\"", +] +`; diff --git a/packages/pug/test/anti-cases/invalid-character.pug b/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug similarity index 100% rename from packages/pug/test/anti-cases/invalid-character.pug rename to packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug diff --git a/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js b/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js new file mode 100644 index 000000000..86bccc358 --- /dev/null +++ b/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js @@ -0,0 +1,12 @@ +const pug = require('../../../'); + +test('invalid attribute name should log warning', () => { + const oldWarn = console.warn; + const warnings = []; + console.warn = warnings.push.bind(warnings); + pug.compileFile( + __dirname + '/invalid-character.pug' + ); + console.warn = oldWarn; + expect(warnings).toMatchSnapshot(); +}); From d6a701da94f8cd619211ab588b5fbd89ae3c1eb1 Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 18:49:18 +0200 Subject: [PATCH 6/8] Issue with the information in the snapshot --- .../Invalid-Attribute-test/__snapshots__/invalid.test.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap index cd3418172..c5453c9f8 100644 --- a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap +++ b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap @@ -1,5 +1,5 @@ exports[`test invalid attribute name should log warning 1`] = ` Array [ - "C:\\Users\\ivan\\Desktop\\Nueva carpeta\\Pug pull request\\pug\\packages\\pug\\test\\anti-cases\\Invalid-Attribute-test/invalid-character.pug, line 1Attribute name starts with invalid character \"{\", to use special characters in attribute names, wrap the attribute name in double quotes \"\"", +"/home/travis/build/pugjs/pug/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug, line 1Attribute name starts with invalid character "{", to use special characters in attribute names, wrap the attribute name in double quotes """, ] `; From be343a1d9e7710df010a7150d6640bd871e37a7d Mon Sep 17 00:00:00 2001 From: drexpp Date: Thu, 19 Jul 2018 19:04:11 +0200 Subject: [PATCH 7/8] Issue with the information in the snapshot --- packages/pug-lexer/index.js | 2 +- .../Invalid-Attribute-test/__snapshots__/invalid.test.js.snap | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/pug-lexer/index.js b/packages/pug-lexer/index.js index 5c5a9742e..79c2f244f 100644 --- a/packages/pug-lexer/index.js +++ b/packages/pug-lexer/index.js @@ -1084,7 +1084,7 @@ Lexer.prototype = { this.incrementColumn(1); i++; }else if(characterParser.isPunctuator(str[i]) && str[i] !== ':'){ - console.warn(this.filename + ', line ' + tok.loc.start.line + 'Attribute name starts with invalid character "' + str[i] + + console.warn(this.filename + ', line ' + tok.loc.start.line + ', attribute name starts with invalid character "' + str[i] + '", to use special characters in attribute names, wrap the attribute name in double quotes ""') } diff --git a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap index c5453c9f8..25daffd91 100644 --- a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap +++ b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap @@ -1,5 +1,4 @@ exports[`test invalid attribute name should log warning 1`] = ` Array [ -"/home/travis/build/pugjs/pug/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug, line 1Attribute name starts with invalid character "{", to use special characters in attribute names, wrap the attribute name in double quotes """, -] +"/home/travis/build/pugjs/pug/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug, line 1, attribute name starts with invalid character "{", to use special characters in attribute names, wrap the attribute name in double quotes """,] `; From 6ea6887f9f6313bd32f43f3b20b55e759b59b1ec Mon Sep 17 00:00:00 2001 From: drexpp Date: Fri, 27 Jul 2018 10:02:21 +0200 Subject: [PATCH 8/8] Added fix --- .../Invalid-Attribute-test/__snapshots__/invalid.test.js.snap | 3 ++- .../pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap index 25daffd91..5fe8d75b7 100644 --- a/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap +++ b/packages/pug/test/anti-cases/Invalid-Attribute-test/__snapshots__/invalid.test.js.snap @@ -1,4 +1,5 @@ exports[`test invalid attribute name should log warning 1`] = ` Array [ -"/home/travis/build/pugjs/pug/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug, line 1, attribute name starts with invalid character "{", to use special characters in attribute names, wrap the attribute name in double quotes """,] +"/home/travis/build/pugjs/pug/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid-character.pug, line 1, attribute name starts with invalid character "{", to use special characters in attribute names, wrap the attribute name in double quotes """, +] `; diff --git a/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js b/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js index 86bccc358..82ceeef9e 100644 --- a/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js +++ b/packages/pug/test/anti-cases/Invalid-Attribute-test/invalid.test.js @@ -8,5 +8,6 @@ test('invalid attribute name should log warning', () => { __dirname + '/invalid-character.pug' ); console.warn = oldWarn; + warnings.map(warning => warning.replace(/\\/g, '/').split(process.cwd().replace(/\\/g, '/')).join('')); expect(warnings).toMatchSnapshot(); });