stylelint-gamut 1.1.5 → 1.2.1

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.5",
3
+ "version": "1.2.1",
4
4
  "description": "Stylelint plugin for working with different color spaces",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "homepage": "https://github.com/fpetrakov/stylelint-gamut#readme",
35
35
  "peerDependencies": {
36
- "stylelint": ">=14.10.0"
36
+ "stylelint": "^14.0.0"
37
37
  },
38
38
  "devDependencies": {
39
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,51 @@ 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)
48
46
 
49
- parsedValue.walk((node, _index, nodes) => {
50
- if (node.type !== 'function') return
47
+ if (customProperties[varName] && !customProperties[varName].inQuery) {
48
+ customPropValue = customProperties[varName].value
49
+ } else {
50
+ return
51
+ }
52
+ }
53
+
54
+ const isInSrgbGamut = new Color(customPropValue || value).inGamut('srgb')
51
55
 
52
- const isInSrgbGamut = new Color(valueParser.stringify(nodes)).inGamut(
53
- 'srgb'
54
- )
56
+ if (isInSrgbGamut) return
55
57
 
56
- if (isInSrgbGamut) return
58
+ if (isInColorGamutP3MediaQuery(decl)) return
57
59
 
58
- if (isInColorGamutP3MediaQuery(decl)) return
60
+ if (decl.prop && decl.prop.startsWith('--')) {
61
+ customProperties[decl.prop] = { value: decl.value.trim(), inQuery: false }
62
+ return
63
+ }
59
64
 
60
- const index = declarationValueIndex(decl) + node.sourceIndex
61
- const endIndex = index + decl.value.length
65
+ const index = declarationValueIndex(decl)
66
+ const endIndex = index + decl.value.length
62
67
 
63
- stylelint.utils.report({
64
- message: messages.rejected(color),
65
- node: decl,
66
- index,
67
- endIndex,
68
- result,
69
- ruleName
70
- })
68
+ stylelint.utils.report({
69
+ message: messages.rejected(value),
70
+ node: decl,
71
+ index,
72
+ endIndex,
73
+ result,
74
+ ruleName
71
75
  })
72
76
  }
73
77
  })
@@ -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
- }