Skip to content

Commit bd9e0de

Browse files
meatcarForbesLindesay
authored andcommitted
[pug-lexer] Relax class name requirements (#2948)
* Allow class names to begin with single dash, 0-9, a-z, and underscore * Add test case that passes with change, fails without change Fixes #2907
1 parent af94a9f commit bd9e0de

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

packages/pug-lexer/index.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,17 @@ Lexer.prototype = {
403403
*/
404404

405405
className: function() {
406-
var tok = this.scan(/^\.(-?-?[_a-z][_a-z0-9\-]*)/i, 'class');
406+
var tok = this.scan(/^\.([_a-z0-9\-]*[_a-z][_a-z0-9\-]*)/i, 'class');
407407
if (tok) {
408408
this.tokens.push(tok);
409409
this.incrementColumn(tok.val.length);
410410
return true;
411411
}
412-
if (/^\.\-/i.test(this.input)) {
413-
this.error('INVALID_CLASS_NAME', 'If a class name begins with a "-" or "--", it must be followed by a letter or underscore.');
414-
}
415-
if (/^\.[0-9]/i.test(this.input)) {
416-
this.error('INVALID_CLASS_NAME', 'Class names must begin with "-", "_" or a letter.');
412+
if (/^\.[_a-z0-9\-]+/i.test(this.input)) {
413+
this.error('INVALID_CLASS_NAME', 'Class names must contain at least one letter or underscore.');
417414
}
418415
if (/^\./.test(this.input)) {
419-
this.error('INVALID_CLASS_NAME', '"' + /.[^ \t\(\#\.\:]*/.exec(this.input.substr(1))[0] + '" is not a valid class name. Class names must begin with "-", "_" or a letter and can only contain "_", "-", a-z and 0-9.');
416+
this.error('INVALID_CLASS_NAME', '"' + /.[^ \t\(\#\.\:]*/.exec(this.input.substr(1))[0] + '" is not a valid class name. Class names can only contain "_", "-", a-z and 0-9, and must contain at least one of "_", or a-z');
420417
}
421418
},
422419

packages/pug-lexer/test/__snapshots__/index.test.js.snap

+39-5
Original file line numberDiff line numberDiff line change
@@ -3464,12 +3464,46 @@ Array [
34643464
},
34653465
Object {
34663466
"col": 1,
3467-
"line": 12,
3467+
"line": 13,
34683468
"type": "newline",
34693469
},
34703470
Object {
34713471
"col": 1,
3472-
"line": 12,
3472+
"line": 13,
3473+
"type": "tag",
3474+
"val": "a",
3475+
},
3476+
Object {
3477+
"col": 2,
3478+
"line": 13,
3479+
"type": "class",
3480+
"val": "-foo",
3481+
},
3482+
Object {
3483+
"col": 1,
3484+
"line": 14,
3485+
"type": "newline",
3486+
},
3487+
Object {
3488+
"col": 1,
3489+
"line": 14,
3490+
"type": "tag",
3491+
"val": "a",
3492+
},
3493+
Object {
3494+
"col": 2,
3495+
"line": 14,
3496+
"type": "class",
3497+
"val": "3foo",
3498+
},
3499+
Object {
3500+
"col": 1,
3501+
"line": 15,
3502+
"type": "newline",
3503+
},
3504+
Object {
3505+
"col": 1,
3506+
"line": 15,
34733507
"type": "eos",
34743508
},
34753509
]
@@ -10689,7 +10723,7 @@ Object {
1068910723
"code": "PUG:INVALID_CLASS_NAME",
1069010724
"column": 1,
1069110725
"line": 1,
10692-
"msg": "Class names must begin with \"-\", \"_\" or a letter.",
10726+
"msg": "Class names must contain at least one letter or underscore.",
1069310727
}
1069410728
`;
1069510729

@@ -10698,7 +10732,7 @@ Object {
1069810732
"code": "PUG:INVALID_CLASS_NAME",
1069910733
"column": 1,
1070010734
"line": 1,
10701-
"msg": "If a class name begins with a \"-\" or \"--\", it must be followed by a letter or underscore.",
10735+
"msg": "Class names must contain at least one letter or underscore.",
1070210736
}
1070310737
`;
1070410738

@@ -10707,7 +10741,7 @@ Object {
1070710741
"code": "PUG:INVALID_CLASS_NAME",
1070810742
"column": 1,
1070910743
"line": 1,
10710-
"msg": "\"ä\" is not a valid class name. Class names must begin with \"-\", \"_\" or a letter and can only contain \"_\", \"-\", a-z and 0-9.",
10744+
"msg": "\"ä\" is not a valid class name. Class names can only contain \"_\", \"-\", a-z and 0-9, and must contain at least one of \"_\", or a-z",
1071110745
}
1071210746
`;
1071310747

packages/pug-lexer/test/cases/classes.pug

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ a.foo(class='bar').baz
99
a.foo-bar_baz
1010

1111
a(class={foo: true, bar: false, baz: true})
12+
13+
a.-foo
14+
a.3foo

0 commit comments

Comments
 (0)