stylelint-gamut 1.1.3 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-gamut",
3
- "version": "1.1.3",
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": {
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
- const parsedValue = valueParser(getDeclarationValue(decl))
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
- parsedValue.walk((node, _index, nodes) => {
43
- if (node.type !== 'function') return
46
+ function checkColor (color) {
47
+ const parsedValue = valueParser(color)
44
48
 
45
- if (!COLOR_FUNCTIONS.has(node.value)) {
46
- return
47
- }
49
+ parsedValue.walk((node, _index, nodes) => {
50
+ if (node.type !== 'function') return
48
51
 
49
- const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
50
- 'srgb'
51
- )
52
+ const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
53
+ 'srgb'
54
+ )
52
55
 
53
- if (isInSrgbGamut) return
56
+ if (isInSrgbGamut) return
54
57
 
55
- if (isInColorGamutP3MediaQuery(decl)) return
58
+ if (isInColorGamutP3MediaQuery(decl)) return
56
59
 
57
- const index = declarationValueIndex(decl) + node.sourceIndex
58
- const endIndex = index + decl.value.length
60
+ const index = declarationValueIndex(decl) + node.sourceIndex
61
+ const endIndex = index + decl.value.length
59
62
 
60
- stylelint.utils.report({
61
- message: messages.rejected(decl.value),
62
- node: decl,
63
- index,
64
- endIndex,
65
- result,
66
- ruleName
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' && parent.params === '(color-gamut: p3)'
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: p3) { a { color: lch(48% 82 283); } }',
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: oklch(48% 82 283 / 73%); } }',
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: 'a { color: oklch(98% 49 129); }',
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(98% 49 129)'),
103
+ message: messages.rejected('oklch(85% 0.1 354 / 73%)'),
64
104
  line: 1,
65
105
  column: 12,
66
106
  endLine: 1,
67
- endColumn: 29
107
+ endColumn: 36
68
108
  },
69
109
 
70
110
  {
@@ -1,11 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @param {import('postcss').Declaration} decl
5
- * @returns {string}
6
- */
7
- module.exports = function getDeclarationValue (decl) {
8
- const raws = decl.raws
9
-
10
- return (raws.value && raws.value.raw) || decl.value
11
- }