scancss
Version:
A robust CSS stylesheet statistics collector and analyzer
223 lines (156 loc) • 7.1 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleDeclaration = handleDeclaration;
var _postcssValuesParser = require('postcss-values-parser');
var _postcssValuesParser2 = _interopRequireDefault(_postcssValuesParser);
var _cssColorableProperties = require('../../constants/cssColorableProperties');
var _cssExplicitDefaultingKeywords = require('../../constants/cssExplicitDefaultingKeywords');
var _rePrefixedString = require('../../constants/rePrefixedString');
var _isSafeAst = require('../../predicates/isSafeAst');
var _isShorthandProperty = require('../../predicates/isShorthandProperty');
var _isCustomProperty = require('../../predicates/isCustomProperty');
var _countUsage = require('../../calculators/countUsage');
var _transformString = require('../../converters/transformString');
var _trimExtraSpaces = require('../../converters/trimExtraSpaces');
var _trimSpacesNearColon = require('../../converters/trimSpacesNearColon');
var _trimSpacesNearCommas = require('../../converters/trimSpacesNearCommas');
var _trimSpacesNearParentheses = require('../../converters/trimSpacesNearParentheses');
var _handleEngineTriggers = require('../properties/handleEngineTriggers');
var _handleColorable = require('../properties/handleColorable');
var _handleFonts = require('../properties/handleFonts');
var _handleTransitionsAndAnimations = require('../properties/handleTransitionsAndAnimations');
var _handleDisplay = require('../properties/handleDisplay');
var _handlePosition = require('../properties/handlePosition');
var _handleZIndex = require('../properties/handleZIndex');
var _handleFloat = require('../properties/handleFloat');
var _handleBorderRadiuses = require('../properties/handleBorderRadiuses');
var _handleWidth = require('../properties/handleWidth');
var _handleHeight = require('../properties/handleHeight');
var _handleLetterSpacing = require('../properties/handleLetterSpacing');
var _handlePerformanceHacks = require('../properties/handlePerformanceHacks');
var _handleFunctions = require('../handleFunctions');
var _handleUnits = require('../handleUnits');
var _handleVariables = require('../handleVariables');
var _handleVendorPrefix = require('../handleVendorPrefix');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const supportedCssFontProperties = ['font', 'font-size', 'line-height', 'font-family'];
/* eslint-disable-next-line complexity */
function handleDeclaration(decl, report, options) {
report.declarations.total++;
const declarationByteLength = Buffer.byteLength(decl.toString(), 'utf8');
report.declarations.length.total += declarationByteLength;
const normalizedDecl = (0, _transformString.transformString)(decl.toString(), [_trimExtraSpaces.trimExtraSpaces, _trimSpacesNearColon.trimSpacesNearColon, _trimSpacesNearCommas.trimSpacesNearCommas, _trimSpacesNearParentheses.trimSpacesNearParentheses]);
if (report.declarations.length.longest < declarationByteLength) {
report.declarations.length.longest = declarationByteLength;
report.declarations.length.longestDeclaration = normalizedDecl;
}
if (report.declarations.list.includes(normalizedDecl) === false) {
report.declarations.list.push(normalizedDecl);
}
if (decl.important) {
report.declarations.important++;
}
const prop = decl.prop;
if (options.properties) {
/** Count properties excluding variables */
/* istanbul ignore else */
if ((0, _isCustomProperty.isCustomProperty)(prop) === false) {
report.properties.total++;
(0, _countUsage.countUsage)(prop, report.properties.usage);
}
/** Count properties with vendor prefixes */
if (_rePrefixedString.rePrefixedString.test(prop)) {
report.properties.prefixed++;
(0, _handleVendorPrefix.handleVendorPrefix)(prop, report);
}
/** Count property shorthands */
if ((0, _isShorthandProperty.isShorthandProperty)(prop)) {
report.properties.shorthands++;
}
try {
const ast = (0, _postcssValuesParser2.default)(decl.value).parse();
/* istanbul ignore else */
if ((0, _isSafeAst.isSafeAst)(ast)) {
ast.nodes[0].nodes.forEach(node => {
const nodeValue = node.value.toLowerCase();
if (node.type === 'word' && _cssExplicitDefaultingKeywords.cssExplicitDefaultingKeywords.includes(nodeValue)) {
const keyword = nodeValue + 'Keyword';
report.properties[keyword]++;
}
if (node.type === 'word' && nodeValue === 'auto') {
report.properties.autoKeyword++;
}
});
}
} catch (err) {
/* istanbul ignore next */
/* eslint-disable-next-line no-console */
console.log(`'postcss-values-parser' module error\n${err}`);
}
if (options.engineTriggerProperties) {
(0, _handleEngineTriggers.handleEngineTriggers)(prop, report);
}
/**
* Count property values resets via `all` property
* https://drafts.csswg.org/css-cascade-4/#all-shorthand
*/
if (prop === 'all') {
report.properties.resetsViaAll++;
}
/**
* Count anonymous replaced elements
* https://developer.mozilla.org/en-US/docs/Web/CSS/Replaced_element
*/
if (prop === 'content') {
report.properties.anonymousReplacedElements++;
}
if (options.performanceHacks) {
(0, _handlePerformanceHacks.handlePerformanceHacks)(decl, report);
}
}
const shouldHandleAnyColors = options.colors || options.backgroundColors || options.allColors;
if (shouldHandleAnyColors && _cssColorableProperties.cssColorableProperties.includes(prop)) {
(0, _handleColorable.handleColorable)(decl, report, options);
}
if (prop === 'display' && options.displays) {
(0, _handleDisplay.handleDisplay)(decl, report);
}
if (prop === 'position' && options.positions) {
(0, _handlePosition.handlePosition)(decl, report);
}
if (prop === 'z-index' && options.zIndices) {
(0, _handleZIndex.handleZIndex)(decl, report);
}
if (prop === 'float' && options.floats) {
(0, _handleFloat.handleFloat)(decl, report);
}
if ((0, _isCustomProperty.isCustomProperty)(prop) === false && prop.includes('border-') && prop.endsWith('-radius') && options.borderRadiuses) {
(0, _handleBorderRadiuses.handleBorderRadiuses)(decl, report);
}
if (prop === 'width' && options.widths) {
(0, _handleWidth.handleWidth)(decl, report);
}
if (prop === 'height' && options.heights) {
(0, _handleHeight.handleHeight)(decl, report);
}
if (prop === 'letter-spacing' && options.letterSpacings) {
(0, _handleLetterSpacing.handleLetterSpacing)(decl, report);
}
if (supportedCssFontProperties.includes(prop) && options.fonts) {
(0, _handleFonts.handleFonts)(decl, report);
}
if (options.functions) {
(0, _handleFunctions.handleFunctions)(decl, report, options);
}
if ((prop.includes('transition') || prop.includes('animation')) && options.transitionsAndAnimations) {
(0, _handleTransitionsAndAnimations.handleTransitionsAndAnimations)(decl, report);
}
if (options.variables) {
(0, _handleVariables.handleVariables)(decl, report);
}
if (options.units) {
(0, _handleUnits.handleUnits)(decl, report, options);
}
}