stylelint-gamut 1.1.4 → 1.2.0

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/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # stylelint-gamut
2
2
 
3
+ [![npm version][npm-version-img]][npm] [![npm downloads last month][npm-downloads-img]][npm]
4
+
3
5
  **stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
4
6
 
5
- Right now it's working with **node version ≥ 16.0.0**.
7
+ Right now it's working with **node version ≥ 16.0.0** and **Stylelint version ≥ 14.0.0**.
6
8
 
7
9
  # Installation
8
10
 
@@ -38,3 +40,6 @@ Add `stylelint-gamut` to your Stylelint config `plugins` array, then add rules y
38
40
  * [`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) {}`.
39
41
 
40
42
  [Stylelint]: https://stylelint.io/
43
+ [npm-version-img]: https://img.shields.io/npm/v/stylelint-gamut.svg
44
+ [npm-downloads-img]: https://img.shields.io/npm/dm/stylelint-gamut.svg
45
+ [npm]: https://www.npmjs.com/package/stylelint-gamut
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-gamut",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
4
4
  "description": "Stylelint plugin for working with different color spaces",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -13,6 +13,10 @@
13
13
  "type": "git",
14
14
  "url": "git+https://github.com/fpetrakov/stylelint-gamut.git"
15
15
  },
16
+ "files": [
17
+ "src/**/*.js",
18
+ "!**/**/*.test.js"
19
+ ],
16
20
  "keywords": [
17
21
  "stylelint",
18
22
  "stylelint-plugin",
@@ -29,7 +33,7 @@
29
33
  },
30
34
  "homepage": "https://github.com/fpetrakov/stylelint-gamut#readme",
31
35
  "peerDependencies": {
32
- "stylelint": ">=14.10.0"
36
+ "stylelint": "^14.0.0"
33
37
  },
34
38
  "devDependencies": {
35
39
  "eslint": "^8.22.0",
package/src/index.js CHANGED
@@ -1,10 +1,8 @@
1
1
  'use strict'
2
2
 
3
- const valueParser = require('postcss-value-parser')
4
3
  const stylelint = require('stylelint')
5
4
  const Color = require('colorjs.io').default
6
5
  const isStandardSyntaxProperty = require('./utils/isStandardSyntaxProperty')
7
- const isCustomProperty = require('./utils/isCustomProperty')
8
6
  const isAtRule = require('./utils/isAtRule')
9
7
  const declarationValueIndex = require('./utils/declarationValueIndex')
10
8
 
@@ -29,45 +27,47 @@ const ruleFunction = (primary) => {
29
27
  return
30
28
  }
31
29
 
30
+ const customProperties = {}
31
+
32
32
  root.walkDecls((decl) => {
33
33
  if (!isStandardSyntaxProperty(decl.prop)) return
34
34
 
35
- if (isCustomProperty(decl.prop)) return
36
-
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
- }
35
+ const values = decl.value.match(/(oklch|oklab|lab|lch|var)\([^)]+\)/g)
36
+ if (values) {
37
+ for (const value of values) {
38
+ check(value)
43
39
  }
44
40
  }
45
41
 
46
- function checkColor (color) {
47
- const parsedValue = valueParser(color)
42
+ function check (value) {
43
+ let customPropValue
44
+ if (value.startsWith('var(--')) {
45
+ const varName = value.slice(4, -1)
46
+ customPropValue = customProperties[varName]
47
+ if (!customPropValue) return
48
+ }
48
49
 
49
- parsedValue.walk((node, _index, nodes) => {
50
- if (node.type !== 'function') return
50
+ const isInSrgbGamut = new Color(customPropValue || value).inGamut('srgb')
51
51
 
52
- const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
53
- 'srgb'
54
- )
52
+ if (isInSrgbGamut) return
55
53
 
56
- if (isInSrgbGamut) return
54
+ if (decl.prop && decl.prop.startsWith('--')) {
55
+ customProperties[decl.prop] = decl.value.trim()
56
+ return
57
+ }
57
58
 
58
- if (isInColorGamutP3MediaQuery(decl)) return
59
+ if (isInColorGamutP3MediaQuery(decl)) return
59
60
 
60
- const index = declarationValueIndex(decl) + node.sourceIndex
61
- const endIndex = index + decl.value.length
61
+ const index = declarationValueIndex(decl)
62
+ const endIndex = index + decl.value.length
62
63
 
63
- stylelint.utils.report({
64
- message: messages.rejected(color),
65
- node: decl,
66
- index,
67
- endIndex,
68
- result,
69
- ruleName
70
- })
64
+ stylelint.utils.report({
65
+ message: messages.rejected(value),
66
+ node: decl,
67
+ index,
68
+ endIndex,
69
+ result,
70
+ ruleName
71
71
  })
72
72
  }
73
73
  })
package/.eslintrc.js DELETED
@@ -1,15 +0,0 @@
1
- module.exports = {
2
- env: {
3
- browser: true,
4
- commonjs: true,
5
- es2021: true
6
- },
7
- extends: [
8
- 'standard'
9
- ],
10
- parserOptions: {
11
- ecmaVersion: 'latest'
12
- },
13
- rules: {
14
- }
15
- }
@@ -1,30 +0,0 @@
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/jest.config.js DELETED
@@ -1,7 +0,0 @@
1
- module.exports = {
2
- preset: 'jest-preset-stylelint',
3
- clearMocks: true,
4
- setupFiles: ['./jest.setup.js'],
5
- testEnvironment: 'node',
6
- roots: ['src']
7
- }
package/jest.setup.js DELETED
@@ -1,5 +0,0 @@
1
- 'use strict'
2
-
3
- const { getTestRule } = require('jest-preset-stylelint')
4
-
5
- global.testRule = getTestRule({ plugins: ['./src'] })
package/src/README.md DELETED
@@ -1,88 +0,0 @@
1
- # color-no-out-gamut-range
2
-
3
- Disallow out of gamut range colors.
4
-
5
- <!-- prettier-ignore -->
6
- ```css
7
- a { color: lch(48% 82 283) }
8
- /** ↑
9
- * This color is out of sRGB gamut range */
10
- ```
11
-
12
- Colors declared using lch, oklch, lab and oklab functions can move from sRGB to P3 color space, which is not supported by most of screens right now. It can happen by mistake. For instance, by converting colors from one space to another.
13
-
14
- This rule checks if the color is in sRGB space. If it's not, you should wrap it in media query like this:
15
-
16
- ```css
17
- @media (color-gamut: p3) {
18
- a {
19
- color: lch(48% 82 283);
20
- }
21
- }
22
- ```
23
-
24
- Please note that conditional imports are not checked, e.g.:
25
-
26
- ```html
27
- <link href="p3-custom-props.css" rel="stylesheet" media="(color-gamut: p3)" />
28
- ```
29
-
30
- ```css
31
- @import url("p3-custom-props.css") (color-gamut: p3);
32
- ```
33
-
34
- ## Options
35
-
36
- ### `true`
37
-
38
- The following patterns are considered problems:
39
-
40
- <!-- prettier-ignore -->
41
- ```css
42
- a { color: lch(48% 82 283) }
43
- ```
44
-
45
- <!-- prettier-ignore -->
46
- ```css
47
- a { color: oklch(48% 82 283) }
48
- ```
49
-
50
- <!-- prettier-ignore -->
51
- ```css
52
- a { color: lab(48% 82 283) }
53
- ```
54
-
55
- <!-- prettier-ignore -->
56
- ```css
57
- a { color: oklab(48% 82 283) }
58
- ```
59
-
60
- The following patterns are _not_ considered problems:
61
-
62
- <!-- prettier-ignore -->
63
- ```css
64
- @media (color-gamut: p3) {
65
- a { color: lch(48% 82 283) }
66
- }
67
- ```
68
-
69
- <!-- prettier-ignore -->
70
- ```css
71
- @media (color-gamut: p3) {
72
- a { color: oklch(48% 82 283) }
73
- }
74
- ```
75
-
76
- <!-- prettier-ignore -->
77
- ```css
78
- @media (color-gamut: p3) {
79
- a { color: lab(48% 82 283) }
80
- }
81
- ```
82
-
83
- <!-- prettier-ignore -->
84
- ```css
85
- @media (color-gamut: p3) {
86
- a { color: oklab(48% 82 283) }
87
- }
88
- ```
package/src/index.test.js DELETED
@@ -1,129 +0,0 @@
1
- 'use strict'
2
-
3
- const { rule: { messages, ruleName } } = require('./index.js')
4
-
5
- // eslint-disable-next-line no-undef
6
- testRule({
7
- ruleName,
8
- config: [true],
9
- plugins: ['./index.js'],
10
- accept: [
11
- {
12
- code: '@media (color-gamut:p3) { a { color: lch(48% 82 283); } }',
13
- description: 'lch out of srgb gamut but wrapped in media query'
14
- },
15
- {
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%); } }',
21
- description:
22
- 'oklch with alpha out of srgb gamut but wrapped in media query'
23
- },
24
- {
25
- code: 'a { color: lch(50% 0 0); }',
26
- description: 'in srgb gamut'
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
- },
36
- {
37
- code: 'a { color: red; }',
38
- description: 'ignore not lch color declaration'
39
- },
40
- {
41
- code: ':root { --clr: lch(48% 82 283 / 75%) } a { color: var(--clr) }',
42
- description: 'ignore css variables'
43
- },
44
- {
45
- code: '@myVariable: lch(48% 82 283); a { color: @myVariable; }',
46
- description: 'ignore Less variable'
47
- },
48
- {
49
- code: ':root { $clr: lch(48% 82 283 / 67%) } a { color: $clr; }',
50
- description: 'ignore scss variables'
51
- }
52
- ],
53
-
54
- reject: [
55
- {
56
- code: 'a { color: lch(48% 82 283); }',
57
- description: 'lch',
58
- message: messages.rejected('lch(48% 82 283)'),
59
- line: 1,
60
- column: 12,
61
- endLine: 1,
62
- endColumn: 27
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
- },
82
- {
83
- code: 'a { color: lch(48% 82 283 / 72%); }',
84
- description: 'lch with alpha',
85
- message: messages.rejected('lch(48% 82 283 / 72%)'),
86
- line: 1,
87
- column: 12,
88
- endLine: 1,
89
- endColumn: 33
90
- },
91
- {
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%); }',
102
- description: 'oklch',
103
- message: messages.rejected('oklch(85% 0.1 354 / 73%)'),
104
- line: 1,
105
- column: 12,
106
- endLine: 1,
107
- endColumn: 36
108
- },
109
-
110
- {
111
- code: 'a { color: lab(98% 49 129); }',
112
- description: 'lab',
113
- message: messages.rejected('lab(98% 49 129)'),
114
- line: 1,
115
- column: 12,
116
- endLine: 1,
117
- endColumn: 27
118
- },
119
- {
120
- code: 'a { color: oklab(98% 49 129); }',
121
- description: 'oklab',
122
- message: messages.rejected('oklab(98% 49 129)'),
123
- line: 1,
124
- column: 12,
125
- endLine: 1,
126
- endColumn: 29
127
- }
128
- ]
129
- })
@@ -1,10 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * Check whether a property is a custom one
5
- * @param {string} property
6
- * @returns {boolean}
7
- */
8
- module.exports = function isCustomProperty (property) {
9
- return property.startsWith('--')
10
- }