Using package validate-color to validate css colors.

This commit is contained in:
John Livingston 2021-11-22 13:45:19 +01:00
parent ede36695cd
commit bcdee7eb2b
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
3 changed files with 9 additions and 6 deletions

6
package-lock.json generated
View File

@ -7031,6 +7031,12 @@
"integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==",
"dev": true
},
"validate-color": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/validate-color/-/validate-color-2.2.1.tgz",
"integrity": "sha512-1eDb1zqP6W6bbfKKl6dRXObelNoQpW7aF3BUTh2AivWuhcD0pa3ejwURWqrVsyKJMLBMlHLFcM3sj5J+dSFhbg==",
"dev": true
},
"validate-npm-package-license": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz",

View File

@ -64,6 +64,7 @@
"svgo": "^2.8.0",
"ts-loader": "^8.3.0",
"typescript": "^4.3.5",
"validate-color": "^2.2.1",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12"
},

View File

@ -1,4 +1,5 @@
import type { ChatType } from './types'
const validateColor = require('validate-color').default
type AutoColorValue = string
@ -38,12 +39,7 @@ function areAutoColorsValid (autocolors: AutoColors): true | string[] {
for (const k in autocolors) {
const color = autocolors[k as keyof AutoColors]
// FIXME: there are missing cases. For now there are only basic values formats.
if (
!/^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$/.test(color) &&
!/^rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*(1|0|0?.\d+)\)$/.test(color) &&
!/^#[0-9a-fA-F]{3,6}$/.test(color) &&
!/^[a-z]+$/
) {
if (!validateColor(color)) {
errors.push(color)
}
}