Sha256: 12057ef8a8af9040b0ca938d9d96fbe25af4cd6832e583930afabaa9b020daea

Contents?: true

Size: 1.3 KB

Versions: 23

Compression:

Stored size: 1.3 KB

Contents

'use strict';

const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const styleSearch = require('style-search');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'color-no-hex';

const messages = ruleMessages(ruleName, {
	rejected: (hex) => `Unexpected hex color "${hex}"`,
});

function rule(actual) {
	return (root, result) => {
		const validOptions = validateOptions(result, ruleName, { actual });

		if (!validOptions) {
			return;
		}

		root.walkDecls((decl) => {
			const declString = decl.toString();

			styleSearch({ source: declString, target: '#' }, (match) => {
				// If there's not a colon, comma, or whitespace character before, we'll assume this is
				// not intended to be a hex color, but is instead something like the
				// hash in a url() argument
				if (!/[:,\s]/.test(declString[match.startIndex - 1])) {
					return;
				}

				const hexMatch = /^#[0-9A-Za-z]+/.exec(declString.substr(match.startIndex));

				if (!hexMatch) {
					return;
				}

				const hexValue = hexMatch[0];

				report({
					message: messages.rejected(hexValue),
					node: decl,
					index: match.startIndex,
					result,
					ruleName,
				});
			});
		});
	};
}

rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.6 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.5 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.4 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.3 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.2 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.1 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.3.5 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-5.0.0 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.3.4 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.3.3 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.3.2 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.3.1 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.3 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.2.3 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.2.2 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.2.1 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.2 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.1.9 node_modules/stylelint/lib/rules/color-no-hex/index.js
trusty-cms-4.1.8 node_modules/stylelint/lib/rules/color-no-hex/index.js