Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives for hex with alpha-channel and false negatives for modern syntax in color-named #5718

Merged
merged 10 commits into from Nov 16, 2021
2 changes: 0 additions & 2 deletions lib/reference/keywordSets.js
Expand Up @@ -51,8 +51,6 @@ keywordSets.lengthUnits = new Set([

keywordSets.units = uniteSets(keywordSets.nonLengthUnits, keywordSets.lengthUnits);

keywordSets.colorFunctionNames = new Set(['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'gray']);

keywordSets.camelCaseFunctionNames = new Set([
'translateX',
'translateY',
Expand Down
153 changes: 0 additions & 153 deletions lib/reference/namedColorData.js

This file was deleted.

177 changes: 177 additions & 0 deletions lib/rules/color-named/__tests__/colord-utils.test.js
@@ -0,0 +1,177 @@
'use strict';

const { colord } = require('../colord-utils');

describe('colord gray', () => {
const TESTS = [
{
input: 'gray(100)',
output: '#ffffff',
},
{
input: 'gray(0)',
output: '#000000',
},
{
input: 'gray(10)',
output: '#1b1b1b',
},
{
input: 'gray(100%)',
output: '#ffffff',
},
{
input: 'gray(0%)',
output: '#000000',
},
{
input: 'gray(10%)',
output: '#1b1b1b',
},
{
input: 'gray(100, 1)',
output: '#ffffff',
},
{
input: 'gray(0, 1)',
output: '#000000',
},
{
input: 'gray(10, 1)',
output: '#1b1b1b',
},
{
input: 'gray(100%, 1)',
output: '#ffffff',
},
{
input: 'gray(0%, 1)',
output: '#000000',
},
{
input: 'gray(10%, 1)',
output: '#1b1b1b',
},
{
input: 'gray(100, 100%)',
output: '#ffffff',
},
{
input: 'gray(0, 100%)',
output: '#000000',
},
{
input: 'gray(10, 100%)',
output: '#1b1b1b',
},
{
input: 'gray(100%, 100%)',
output: '#ffffff',
},
{
input: 'gray(0%, 100%)',
output: '#000000',
},
{
input: 'gray(10%, 100%)',
output: '#1b1b1b',
},
{
input: 'gray(100, .5)',
output: '#ffffff80',
},
{
input: 'gray(0, .5)',
output: '#00000080',
},
{
input: 'gray(10, 0.5)',
output: '#1b1b1b80',
},
{
input: 'gray(100%, .5)',
output: '#ffffff80',
},
{
input: 'gray(0%, .5)',
output: '#00000080',
},
{
input: 'gray(10%, .5)',
output: '#1b1b1b80',
},
{
input: 'gray(100, 50%)',
output: '#ffffff80',
},
{
input: 'gray(0, 50%)',
output: '#00000080',
},
{
input: 'gray(10, 50%)',
output: '#1b1b1b80',
},
{
input: 'gray(100%, 50%)',
output: '#ffffff80',
},
{
input: 'gray(0%, 50%)',
output: '#00000080',
},
{
input: 'gray(10%, 50%)',
output: '#1b1b1b80',
},
{
input: 'gray(0% / 50%)',
valid: false,
},
{
input: 'gray(100 / 50%)',
valid: false,
},
];

for (const { input, output, valid } of TESTS) {
it(`input: ${input}`, () => {
const result = colord(input);

expect(result.isValid()).toBe(valid == null ? true : valid);

expect(result.toHex()).toEqual(output || '#000000');
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
});
}
});

describe('colord hwb with comma', () => {
const TESTS = [
{
input: 'hwb(0, 0%, 0%)',
output: '#ff0000',
},
{
input: 'hwb(0, 0%, 0% , 0)',
output: '#ff000000',
},
{
input: 'hwb(0, 0%, 0% / 0%)',
valid: false,
},
{
input: 'hwb(0, 0%)',
valid: false,
},
];

for (const { input, output, valid } of TESTS) {
it(`input: ${input}`, () => {
const result = colord(input);

expect(result.isValid()).toBe(valid == null ? true : valid);

expect(result.toHex()).toEqual(output || '#000000');
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
});
}
});
16 changes: 12 additions & 4 deletions lib/rules/color-named/__tests__/index.js
Expand Up @@ -75,6 +75,10 @@ testRule({
code: '$colors: (white: #fff, blue: #00f);',
description: 'ignore non-standard list and map syntax',
},
{
code: 'a { color: transparent; }',
description: 'ignore transparent',
},
],

reject: [
Expand Down Expand Up @@ -174,6 +178,10 @@ testRule({
code: 'a { background-image: url(./thing.png#000); }',
description: 'ignore representations within urls',
},
{
code: '.a { color: #fff0ffff; };',
description: 'https://github.com/stylelint/stylelint/issues/5716',
},
],

reject: [
Expand All @@ -196,14 +204,14 @@ testRule({
column: 12,
},
{
code: 'a { background: #f000 }',
message: messages.expected('black', '#f000'),
code: 'a { background: #000f }',
message: messages.expected('black', '#000f'),
line: 1,
column: 17,
},
{
code: 'a { color: #ff000000 }',
message: messages.expected('black', '#ff000000'),
code: 'a { color: #000000ff }',
message: messages.expected('black', '#000000ff'),
line: 1,
column: 12,
},
Expand Down