Skip to content

Commit fa1ef00

Browse files
committed
Add leeway for commas between parenthesis (usually for shorthand CSS statements)
Add check for extra commas. Needed to check for this otherwise extra commas would render CSS as valid.
1 parent 956c18c commit fa1ef00

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

locales/en/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
"selector-missing": "Start every block of CSS with a selector, such as an element name or class name.\nTry: p {\n color: red;\n}",
124124
"block-expected": "Start a block using { after your selector.\nTry: {{error}} {",
125125
"extra-tokens-after-value": "The {{token}} at the end of this line doesn’t belong there.",
126+
"extra-commas": "Looks like you have extra commas on this line",
126127
"illegal-token-after-combinator": "After a + or > in a selector, you need to specify the name of another element, class, or ID",
127128
"invalid-token": "This line doesn't look like valid CSS.",
128129
"invalid-token-in-selector": "The {{token}} on this line doesn’t belong here.",

src/validations/css/prettycss.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ const errorMap = {
5353
},
5454

5555
'extra-tokens-after-value': (error, source) => {
56-
const lineNumber = error.token.line;
56+
const errorToken = error.token;
57+
const lineNumber = errorToken.line;
5758

5859
if (lineNumber > 1) {
5960
const lines = source.split('\n');
6061
const previousLine = lines[lineNumber - 2];
6162
const thisLine = lines[lineNumber - 1];
6263

6364
if (
64-
error.token.charNum - 1 === /\S/.exec(thisLine).index &&
65+
errorToken.charNum - 1 === /\S/.exec(thisLine).index &&
6566
!endsWith(trim(previousLine), ';')
6667
) {
6768
return {
@@ -70,11 +71,25 @@ const errorMap = {
7071
column: previousLine.length - 1,
7172
};
7273
}
74+
if (/,(?=\s*,)/.test(thisLine)) {
75+
return {
76+
reason: 'extra-commas',
77+
row: lineNumber - 1,
78+
column: errorToken.charNum,
79+
};
80+
}
81+
if (
82+
errorToken.content === ',' &&
83+
errorToken.charNum - 1 !== /,/.exec(thisLine).index &&
84+
endsWith(trim(thisLine), ';')
85+
) {
86+
return null;
87+
}
7388
}
7489

7590
return ({
7691
reason: 'extra-tokens-after-value',
77-
payload: {token: error.token.content},
92+
payload: {token: errorToken.content},
7893
});
7994
},
8095

test/unit/validations/css.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ test('valid filter', validationTest(
2626
css,
2727
));
2828

29+
test('valid text-shadow declaration', validationTest(
30+
`p {
31+
text-shadow: rgba(0,0,0,0.1) 0 -5px, rgba(0,0,0,0.1) 0 -1px, \
32+
rgba(255,255,255,0.1) 1px 0, rgba(255,255,255,0.1) 0 1px, \
33+
rgba(0,0,0,0.1) -1px -1px, rgba(255,255,255,0.1) 1px 1px;
34+
}`,
35+
css,
36+
));
37+
2938
test('bogus flex value', validationTest(
3039
'.flex-item { flex: bogus; }',
3140
css,
@@ -78,6 +87,16 @@ test('missing semicolon at end of block', validationTest(
7887
{reason: 'missing-semicolon', row: 1},
7988
));
8089

90+
test('extra commas in value', validationTest(
91+
`p {
92+
text-shadow: rgba(0,0,0,0.1) 0 -5px,, rgba(0,0,0,0.1) 0 -1px, \
93+
rgba(255,255,255,0.1) 1px 0, rgba(255,255,255,0.1) 0 1px, \
94+
rgba(0,0,0,0.1) -1px -1px, rgba(255,255,255,0.1) 1px 1px;
95+
}`,
96+
css,
97+
{reason: 'extra-commas', row: 1},
98+
));
99+
81100
test('missing semicolon within block', validationTest(
82101
`
83102
p {

0 commit comments

Comments
 (0)