scancss
Version:
A robust CSS stylesheet statistics collector and analyzer
125 lines (88 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleFonts = handleFonts;
var _postcss = require('postcss');
var _postcss2 = _interopRequireDefault(_postcss);
var _cssPropertyParser = require('css-property-parser');
var _cssGenericFonts = require('../../../constants/cssGenericFonts');
var _cssSystemFonts = require('../../../constants/cssSystemFonts');
var _cssFontSizeKeywords = require('../../../constants/cssFontSizeKeywords');
var _reExistingVendorPrefix = require('../../../constants/reExistingVendorPrefix');
var _countUsage = require('../../../calculators/countUsage');
var _transformString = require('../../../converters/transformString');
var _trimExtraSpaces = require('../../../converters/trimExtraSpaces');
var _trimSpacesNearCommas = require('../../../converters/trimSpacesNearCommas');
var _trimSpacesNearParentheses = require('../../../converters/trimSpacesNearParentheses');
var _trimQuotes = require('../../../converters/trimQuotes');
var _handleVendorPrefix = require('../../handleVendorPrefix');
var _isNumber = require('../../../predicates/isNumber');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function countFontSizes(propValue, report) {
report.fontSizes.total++;
(0, _countUsage.countUsage)(propValue, report.fontSizes.usage);
if (_cssFontSizeKeywords.cssFontSizeKeywords.includes(propValue)) {
report.fontSizes.keywords.total++;
(0, _countUsage.countUsage)(propValue, report.fontSizes.keywords.usage);
}
}
function countLineHeights(propValue, report) {
report.lineHeights.total++;
(0, _countUsage.countUsage)(propValue, report.lineHeights.usage);
/** Count hard-coded line-heights, i.e `line-height: 16px` */
if ((0, _isNumber.isNumber)(Number(propValue)) === false) {
report.lineHeights.hardCoded++;
}
}
function countFontFamilies(propValue, report) {
const familiesList = _postcss2.default.list.comma(propValue);
familiesList.map(family => (0, _trimQuotes.trimQuotes)(family).trim()).forEach(family => {
report.fontFamilies.total++;
if (_reExistingVendorPrefix.reExistingVendorPrefix.test(family)) {
(0, _handleVendorPrefix.handleVendorPrefix)(family, report);
}
if (_cssGenericFonts.cssGenericFonts.includes(family)) {
(0, _countUsage.countUsage)(family, report.fontFamilies.generic);
}
(0, _countUsage.countUsage)(family, report.fontFamilies.usage);
});
if (familiesList.length === 1) {
report.fontFamilies.withoutFallbackFonts++;
}
}
function handleFonts(decl, report) {
const cleanedValue = (0, _transformString.transformString)(decl.value, [_trimExtraSpaces.trimExtraSpaces, _trimSpacesNearCommas.trimSpacesNearCommas, _trimSpacesNearParentheses.trimSpacesNearParentheses]);
if (decl.prop === 'font') {
// http://nicolasgallagher.com/another-css-image-replacement-technique/
if (/^0\/0 [a-z]/.test(cleanedValue)) {
report.fontFamilies.imageReplacementHacks++;
}
if (_cssSystemFonts.cssSystemFonts.includes(decl.value)) {
(0, _countUsage.countUsage)(decl.value, report.fontFamilies.system);
return;
}
const fontLonghand = (0, _cssPropertyParser.expandShorthandProperty)(decl.prop, cleanedValue);
/* istanbul ignore else */
if (typeof fontLonghand['font-size'] === 'string') {
countFontSizes(fontLonghand['font-size'].trim(), report);
}
/* istanbul ignore else */
if (typeof fontLonghand['line-height'] === 'string') {
countLineHeights(fontLonghand['line-height'].trim(), report);
}
/* istanbul ignore else */
if (typeof fontLonghand['font-family'] === 'string') {
countFontFamilies(fontLonghand['font-family'].trim(), report);
}
}
if (decl.prop === 'font-size') {
countFontSizes(cleanedValue, report);
}
if (decl.prop === 'line-height') {
countLineHeights(cleanedValue, report);
}
if (decl.prop === 'font-family') {
countFontFamilies(cleanedValue, report);
}
}