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