3 const isValidHex = require("../../utils/isValidHex");
4 const report = require("../../utils/report");
5 const ruleMessages = require("../../utils/ruleMessages");
6 const styleSearch = require("style-search");
7 const validateOptions = require("../../utils/validateOptions");
9 const ruleName = "color-no-invalid-hex";
11 const messages = ruleMessages(ruleName, {
12 rejected: hex => `Unexpected invalid hex color "${hex}"`
15 const rule = function(actual) {
16 return (root, result) => {
17 const validOptions = validateOptions(result, ruleName, { actual });
22 root.walkDecls(decl => {
23 const declString = decl.toString();
25 styleSearch({ source: declString, target: "#" }, match => {
26 // If there's not a colon, comma, or whitespace character before, we'll assume this is
27 // not intended to be a hex color, but is instead something like the
28 // hash in a url() argument
29 if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
33 const hexMatch = /^#[0-9A-Za-z]+/.exec(
34 declString.substr(match.startIndex)
40 const hexValue = hexMatch[0];
41 if (isValidHex(hexValue)) {
46 message: messages.rejected(hexValue),
48 index: match.startIndex,
57 rule.ruleName = ruleName;
58 rule.messages = messages;
59 module.exports = rule;