stylelint-gamut 1.1.1 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/test.yml +30 -0
- package/README.md +13 -11
- package/package.json +2 -2
- package/src/index.js +29 -25
- package/src/index.test.js +45 -5
- package/src/utils/getDeclarationValue.js +0 -11
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
pull_request:
|
|
7
|
+
env:
|
|
8
|
+
FORCE_COLOR: 2
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
node-version:
|
|
15
|
+
- 18
|
|
16
|
+
- 16
|
|
17
|
+
name: Node.js Latest Full
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
steps:
|
|
20
|
+
- name: Checkout the repository
|
|
21
|
+
uses: actions/checkout@v3
|
|
22
|
+
- name: Install Node.js ${{ matrix.node-version }}
|
|
23
|
+
uses: actions/setup-node@v3
|
|
24
|
+
with:
|
|
25
|
+
node-version: ${{ matrix.node-version }}
|
|
26
|
+
cache: npm
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: npm install --ignore-scripts
|
|
29
|
+
- name: Run tests
|
|
30
|
+
run: npm test
|
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# stylelint-gamut
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
**stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
|
|
4
|
+
|
|
5
|
+
Right now it's working with **node version ≥ 16.0.0**.
|
|
4
6
|
|
|
5
7
|
# Installation
|
|
6
8
|
|
|
@@ -18,21 +20,21 @@ npm install stylelint-gamut --save-dev
|
|
|
18
20
|
|
|
19
21
|
## Usage
|
|
20
22
|
|
|
21
|
-
Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules you need to the rules list. All rules from stylelint-gamut need to be namespaced with `gamut`.
|
|
23
|
+
Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules you need to the rules list. All rules from `stylelint-gamut` need to be namespaced with `gamut`.
|
|
22
24
|
|
|
23
25
|
```json
|
|
24
26
|
{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
"plugins": [
|
|
28
|
+
"stylelint-gamut"
|
|
29
|
+
],
|
|
30
|
+
"rules": {
|
|
31
|
+
"gamut/color-no-out-gamut-range": true,
|
|
32
|
+
}
|
|
31
33
|
}
|
|
32
34
|
```
|
|
33
35
|
|
|
34
36
|
## Rules
|
|
35
37
|
|
|
36
|
-
* [`color-no-out-gamut-range`](./src/README.md): Throw warning if color goes out of sRGB color space and is not wrapped in
|
|
38
|
+
* [`color-no-out-gamut-range`](./src/README.md): Throw warning if color goes out of sRGB color space and is not wrapped in `@media (color-gamut: p3) {}`.
|
|
37
39
|
|
|
38
|
-
[Stylelint]: https://stylelint.io/
|
|
40
|
+
[Stylelint]: https://stylelint.io/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stylelint-gamut",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Stylelint plugin for working with different color spaces",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://github.com/fpetrakov/stylelint-gamut#readme",
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"stylelint": "
|
|
32
|
+
"stylelint": ">=14.10.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"eslint": "^8.22.0",
|
package/src/index.js
CHANGED
|
@@ -5,7 +5,6 @@ const stylelint = require('stylelint')
|
|
|
5
5
|
const Color = require('colorjs.io').default
|
|
6
6
|
const isStandardSyntaxProperty = require('./utils/isStandardSyntaxProperty')
|
|
7
7
|
const isCustomProperty = require('./utils/isCustomProperty')
|
|
8
|
-
const getDeclarationValue = require('./utils/getDeclarationValue')
|
|
9
8
|
const isAtRule = require('./utils/isAtRule')
|
|
10
9
|
const declarationValueIndex = require('./utils/declarationValueIndex')
|
|
11
10
|
|
|
@@ -19,8 +18,6 @@ const meta = {
|
|
|
19
18
|
url: 'https://stylelint.io/user-guide/rules/list/color-no-out-gamut-range'
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
const COLOR_FUNCTIONS = new Set(['lch', 'lab', 'oklch', 'oklab'])
|
|
23
|
-
|
|
24
21
|
/** @type {import('stylelint').Rule} */
|
|
25
22
|
const ruleFunction = (primary) => {
|
|
26
23
|
return (root, result) => {
|
|
@@ -37,35 +34,42 @@ const ruleFunction = (primary) => {
|
|
|
37
34
|
|
|
38
35
|
if (isCustomProperty(decl.prop)) return
|
|
39
36
|
|
|
40
|
-
|
|
37
|
+
if (decl.value.includes('lch(') || decl.value.includes('lab(')) {
|
|
38
|
+
const colors = decl.value.match(/(oklch|oklab|lab|lch)\([^)]+\)/g)
|
|
39
|
+
if (colors) {
|
|
40
|
+
for (const color of colors) {
|
|
41
|
+
checkColor(color)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
function checkColor (color) {
|
|
47
|
+
const parsedValue = valueParser(color)
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
return
|
|
47
|
-
}
|
|
49
|
+
parsedValue.walk((node, _index, nodes) => {
|
|
50
|
+
if (node.type !== 'function') return
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
|
|
53
|
+
'srgb'
|
|
54
|
+
)
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
if (isInSrgbGamut) return
|
|
54
57
|
|
|
55
|
-
|
|
58
|
+
if (isInColorGamutP3MediaQuery(decl)) return
|
|
56
59
|
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
const index = declarationValueIndex(decl) + node.sourceIndex
|
|
61
|
+
const endIndex = index + decl.value.length
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
stylelint.utils.report({
|
|
64
|
+
message: messages.rejected(color),
|
|
65
|
+
node: decl,
|
|
66
|
+
index,
|
|
67
|
+
endIndex,
|
|
68
|
+
result,
|
|
69
|
+
ruleName
|
|
70
|
+
})
|
|
67
71
|
})
|
|
68
|
-
}
|
|
72
|
+
}
|
|
69
73
|
})
|
|
70
74
|
}
|
|
71
75
|
}
|
|
@@ -78,7 +82,7 @@ function isInColorGamutP3MediaQuery (decl) {
|
|
|
78
82
|
if (decl.parent && decl.parent.parent && isAtRule(decl.parent.parent)) {
|
|
79
83
|
const parent = decl.parent.parent
|
|
80
84
|
|
|
81
|
-
return parent.name === 'media' &&
|
|
85
|
+
return parent.name === 'media' && /\(color-gamut:\s*p3\)/.test(parent.params)
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
return false
|
package/src/index.test.js
CHANGED
|
@@ -2,17 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
const { rule: { messages, ruleName } } = require('./index.js')
|
|
4
4
|
|
|
5
|
+
// eslint-disable-next-line no-undef
|
|
5
6
|
testRule({
|
|
6
7
|
ruleName,
|
|
7
8
|
config: [true],
|
|
8
9
|
plugins: ['./index.js'],
|
|
9
10
|
accept: [
|
|
10
11
|
{
|
|
11
|
-
code: '@media (color-gamut:
|
|
12
|
+
code: '@media (color-gamut:p3) { a { color: lch(48% 82 283); } }',
|
|
12
13
|
description: 'lch out of srgb gamut but wrapped in media query'
|
|
13
14
|
},
|
|
14
15
|
{
|
|
15
|
-
code: '@media (color-gamut: p3) { a { color:
|
|
16
|
+
code: '@media (prefers-color-scheme: dark) and (color-gamut: p3) { a { color: lch(48% 82 283); } }',
|
|
17
|
+
description: 'lch out of srgb gamut but wrapped in a long media query'
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
code: '@media (color-gamut: p3) { a { color: oklch(85% 0.1 354 / 73%); } }',
|
|
16
21
|
description:
|
|
17
22
|
'oklch with alpha out of srgb gamut but wrapped in media query'
|
|
18
23
|
},
|
|
@@ -20,6 +25,14 @@ testRule({
|
|
|
20
25
|
code: 'a { color: lch(50% 0 0); }',
|
|
21
26
|
description: 'in srgb gamut'
|
|
22
27
|
},
|
|
28
|
+
{
|
|
29
|
+
code: '.foo { border: 4px solid oklch(100% 0 0); }',
|
|
30
|
+
description: 'in srgb gamut long property'
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
code: '.foo {background-image: linear-gradient(red lch(50% 0 0 )); }',
|
|
34
|
+
description: 'multicolor property in gamut range'
|
|
35
|
+
},
|
|
23
36
|
{
|
|
24
37
|
code: 'a { color: red; }',
|
|
25
38
|
description: 'ignore not lch color declaration'
|
|
@@ -48,6 +61,24 @@ testRule({
|
|
|
48
61
|
endLine: 1,
|
|
49
62
|
endColumn: 27
|
|
50
63
|
},
|
|
64
|
+
{
|
|
65
|
+
code: '@media (prefers-color-scheme: dark) and (color-gamut: srgb) { a { color: lch(48% 82 283); } }',
|
|
66
|
+
description: 'lch out of srgb gamut and wrapped in a long and wrong media query',
|
|
67
|
+
message: messages.rejected('lch(48% 82 283)'),
|
|
68
|
+
line: 1,
|
|
69
|
+
column: 74,
|
|
70
|
+
endLine: 1,
|
|
71
|
+
endColumn: 89
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
code: '.foo { background: content-box oklch(82.6% 0.087 262.26); }',
|
|
75
|
+
description: 'out of srgb gamut long property',
|
|
76
|
+
message: messages.rejected('oklch(82.6% 0.087 262.26)'),
|
|
77
|
+
line: 1,
|
|
78
|
+
column: 20,
|
|
79
|
+
endLine: 1,
|
|
80
|
+
endColumn: 57
|
|
81
|
+
},
|
|
51
82
|
{
|
|
52
83
|
code: 'a { color: lch(48% 82 283 / 72%); }',
|
|
53
84
|
description: 'lch with alpha',
|
|
@@ -58,13 +89,22 @@ testRule({
|
|
|
58
89
|
endColumn: 33
|
|
59
90
|
},
|
|
60
91
|
{
|
|
61
|
-
code: '
|
|
92
|
+
code: '.foo {background-image: linear-gradient(red lch(48% 82 283)); }',
|
|
93
|
+
description: 'multicolor property out of gamut range',
|
|
94
|
+
message: messages.rejected('lch(48% 82 283)'),
|
|
95
|
+
line: 1,
|
|
96
|
+
column: 25,
|
|
97
|
+
endLine: 1,
|
|
98
|
+
endColumn: 61
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
code: 'a { color: oklch(85% 0.1 354 / 73%); }',
|
|
62
102
|
description: 'oklch',
|
|
63
|
-
message: messages.rejected('oklch(
|
|
103
|
+
message: messages.rejected('oklch(85% 0.1 354 / 73%)'),
|
|
64
104
|
line: 1,
|
|
65
105
|
column: 12,
|
|
66
106
|
endLine: 1,
|
|
67
|
-
endColumn:
|
|
107
|
+
endColumn: 36
|
|
68
108
|
},
|
|
69
109
|
|
|
70
110
|
{
|