stylelint-gamut 1.1.6 → 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
@@ -4,7 +4,7 @@
4
4
 
5
5
  **stylelint-gamut** is a [Stylelint] plugin that helps you to work with different color spaces.
6
6
 
7
- 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**.
8
8
 
9
9
  # Installation
10
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-gamut",
3
- "version": "1.1.6",
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": {
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
  })
@@ -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
- }