3 const blurFunctionArguments = require("../../utils/blurFunctionArguments");
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-hex-length";
11 const messages = ruleMessages(ruleName, {
12 expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`
15 const rule = function(expectation, _, context) {
16 return (root, result) => {
17 const validOptions = validateOptions(result, ruleName, {
19 possible: ["short", "long"]
25 root.walkDecls(decl => {
26 const declString = blurFunctionArguments(decl.toString(), "url");
27 const fixPositions = [];
29 styleSearch({ source: declString, target: "#" }, match => {
30 const hexMatch = /^#[0-9A-Za-z]+/.exec(
31 declString.substr(match.startIndex)
37 const hexValue = hexMatch[0];
40 expectation === "long" &&
41 hexValue.length !== 4 &&
48 expectation === "short" &&
49 (hexValue.length < 6 || !canShrink(hexValue))
54 const variant = expectation === "long" ? longer : shorter;
55 const expectedHex = variant(hexValue);
58 fixPositions.unshift({
61 startIndex: match.startIndex
68 message: messages.expected(hexValue, expectedHex),
70 index: match.startIndex,
76 if (fixPositions.length) {
77 const declProp = decl.prop;
78 const declBetween = decl.raws.between;
80 fixPositions.forEach(function(fixPosition) {
81 // 1 — it's a # length
82 decl.value = replaceHex(
84 fixPosition.currentHex,
85 fixPosition.expectedHex,
86 fixPosition.startIndex - declProp.length - declBetween.length - 1
94 function canShrink(hex) {
95 hex = hex.toLowerCase();
101 (hex.length === 7 || (hex.length === 9 && hex[7] === hex[8]))
105 function shorter(hex) {
106 let hexVariant = "#";
107 for (let i = 1; i < hex.length; i = i + 2) {
108 hexVariant += hex[i];
113 function longer(hex) {
114 let hexVariant = "#";
115 for (let i = 1; i < hex.length; i++) {
116 hexVariant += hex[i] + hex[i];
121 function replaceHex(input, searchString, replaceString, startIndex) {
122 const offset = startIndex + 1;
123 const stringStart = input.slice(0, offset);
124 const stringEnd = input.slice(offset + searchString.length);
126 return stringStart + replaceString + stringEnd;
129 rule.ruleName = ruleName;
130 rule.messages = messages;
131 module.exports = rule;