format: prettify entire project

This commit is contained in:
Rim
2025-04-02 06:50:39 -04:00
parent 86f0782a98
commit 7ccc0be712
1711 changed files with 755867 additions and 235931 deletions

View File

@ -11,7 +11,8 @@ var removeUnused = require('../remove-unused');
var restoreFromOptimizing = require('../restore-from-optimizing');
var wrapForOptimizing = require('../wrap-for-optimizing').all;
var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;
var OptimizationLevel =
require('../../options/optimization-level').OptimizationLevel;
var Token = require('../../tokenizer/token');
var Marker = require('../../tokenizer/marker');
@ -26,7 +27,8 @@ var IgnoreProperty = 'ignore-property';
var CHARSET_TOKEN = '@charset';
var CHARSET_REGEXP = new RegExp('^' + CHARSET_TOKEN, 'i');
var DEFAULT_ROUNDING_PRECISION = require('../../options/rounding-precision').DEFAULT;
var DEFAULT_ROUNDING_PRECISION =
require('../../options/rounding-precision').DEFAULT;
var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/;
var TIME_VALUE = /^(\-?[\d\.]+)(m?s)$/;
@ -40,7 +42,7 @@ var URL_PREFIX_PATTERN = /^url\(/i;
var LOCAL_PREFIX_PATTERN = /^local\(/i;
var VARIABLE_NAME_PATTERN = /^--\S+$/;
function isLocal(value){
function isLocal(value) {
return LOCAL_PREFIX_PATTERN.test(value);
}
@ -78,13 +80,35 @@ function optimizeBorderRadius(property) {
var values = property.value;
var spliceAt;
if (values.length == 3 && values[1][1] == '/' && values[0][1] == values[2][1]) {
if (
values.length == 3 &&
values[1][1] == '/' &&
values[0][1] == values[2][1]
) {
spliceAt = 1;
} else if (values.length == 5 && values[2][1] == '/' && values[0][1] == values[3][1] && values[1][1] == values[4][1]) {
} else if (
values.length == 5 &&
values[2][1] == '/' &&
values[0][1] == values[3][1] &&
values[1][1] == values[4][1]
) {
spliceAt = 2;
} else if (values.length == 7 && values[3][1] == '/' && values[0][1] == values[4][1] && values[1][1] == values[5][1] && values[2][1] == values[6][1]) {
} else if (
values.length == 7 &&
values[3][1] == '/' &&
values[0][1] == values[4][1] &&
values[1][1] == values[5][1] &&
values[2][1] == values[6][1]
) {
spliceAt = 3;
} else if (values.length == 9 && values[4][1] == '/' && values[0][1] == values[5][1] && values[1][1] == values[6][1] && values[2][1] == values[7][1] && values[3][1] == values[8][1]) {
} else if (
values.length == 9 &&
values[4][1] == '/' &&
values[0][1] == values[5][1] &&
values[1][1] == values[6][1] &&
values[2][1] == values[7][1] &&
values[3][1] == values[8][1]
) {
spliceAt = 4;
}
@ -106,51 +130,77 @@ function optimizeColors(name, value, compatibility) {
}
value = value
.replace(/(rgb|hsl)a?\((\-?\d+),(\-?\d+\%?),(\-?\d+\%?),(0*[1-9]+[0-9]*(\.?\d*)?)\)/gi, function (match, colorFn, p1, p2, p3, alpha) {
return (parseInt(alpha, 10) >= 1 ? colorFn + '(' + [p1,p2,p3].join(',') + ')' : match);
})
.replace(/rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/gi, function (match, red, green, blue) {
return shortenRgb(red, green, blue);
})
.replace(/hsl\((-?\d+),(-?\d+)%?,(-?\d+)%?\)/gi, function (match, hue, saturation, lightness) {
return shortenHsl(hue, saturation, lightness);
})
.replace(/(^|[^='"])#([0-9a-f]{6})/gi, function (match, prefix, color, at, inputValue) {
var suffix = inputValue[at + match.length];
if (suffix && HEX_VALUE_PATTERN.test(suffix)) {
return match;
} else if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) {
return (prefix + '#' + color[0] + color[2] + color[4]).toLowerCase();
} else {
return (prefix + '#' + color).toLowerCase();
.replace(
/(rgb|hsl)a?\((\-?\d+),(\-?\d+\%?),(\-?\d+\%?),(0*[1-9]+[0-9]*(\.?\d*)?)\)/gi,
function (match, colorFn, p1, p2, p3, alpha) {
return parseInt(alpha, 10) >= 1 ?
colorFn + '(' + [p1, p2, p3].join(',') + ')'
: match;
}
})
)
.replace(
/rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/gi,
function (match, red, green, blue) {
return shortenRgb(red, green, blue);
}
)
.replace(
/hsl\((-?\d+),(-?\d+)%?,(-?\d+)%?\)/gi,
function (match, hue, saturation, lightness) {
return shortenHsl(hue, saturation, lightness);
}
)
.replace(
/(^|[^='"])#([0-9a-f]{6})/gi,
function (match, prefix, color, at, inputValue) {
var suffix = inputValue[at + match.length];
if (suffix && HEX_VALUE_PATTERN.test(suffix)) {
return match;
} else if (
color[0] == color[1] &&
color[2] == color[3] &&
color[4] == color[5]
) {
return (prefix + '#' + color[0] + color[2] + color[4]).toLowerCase();
} else {
return (prefix + '#' + color).toLowerCase();
}
}
)
.replace(/(^|[^='"])#([0-9a-f]{3})/gi, function (match, prefix, color) {
return prefix + '#' + color.toLowerCase();
})
.replace(/(rgb|rgba|hsl|hsla)\(([^\)]+)\)/gi, function (match, colorFunction, colorDef) {
var tokens = colorDef.split(',');
var colorFnLowercase = colorFunction && colorFunction.toLowerCase();
var applies = (colorFnLowercase == 'hsl' && tokens.length == 3) ||
(colorFnLowercase == 'hsla' && tokens.length == 4) ||
(colorFnLowercase == 'rgb' && tokens.length === 3 && colorDef.indexOf('%') > 0) ||
(colorFnLowercase == 'rgba' && tokens.length == 4 && colorDef.indexOf('%') > 0);
.replace(
/(rgb|rgba|hsl|hsla)\(([^\)]+)\)/gi,
function (match, colorFunction, colorDef) {
var tokens = colorDef.split(',');
var colorFnLowercase = colorFunction && colorFunction.toLowerCase();
var applies =
(colorFnLowercase == 'hsl' && tokens.length == 3) ||
(colorFnLowercase == 'hsla' && tokens.length == 4) ||
(colorFnLowercase == 'rgb' &&
tokens.length === 3 &&
colorDef.indexOf('%') > 0) ||
(colorFnLowercase == 'rgba' &&
tokens.length == 4 &&
colorDef.indexOf('%') > 0);
if (!applies) {
return match;
if (!applies) {
return match;
}
if (tokens[1].indexOf('%') == -1) {
tokens[1] += '%';
}
if (tokens[2].indexOf('%') == -1) {
tokens[2] += '%';
}
return colorFunction + '(' + tokens.join(',') + ')';
}
if (tokens[1].indexOf('%') == -1) {
tokens[1] += '%';
}
if (tokens[2].indexOf('%') == -1) {
tokens[2] += '%';
}
return colorFunction + '(' + tokens.join(',') + ')';
});
);
if (compatibility.colors.opacity && name.indexOf('background') == -1) {
value = value.replace(/(?:rgba|hsla)\(0,0%?,0%?,0\)/g, function (match) {
@ -167,9 +217,12 @@ function optimizeColors(name, value, compatibility) {
function optimizeFilter(property) {
if (property.value.length == 1) {
property.value[0][1] = property.value[0][1].replace(/progid:DXImageTransform\.Microsoft\.(Alpha|Chroma)(\W)/, function (match, filter, suffix) {
return filter.toLowerCase() + suffix;
});
property.value[0][1] = property.value[0][1].replace(
/progid:DXImageTransform\.Microsoft\.(Alpha|Chroma)(\W)/,
function (match, filter, suffix) {
return filter.toLowerCase() + suffix;
}
);
}
property.value[0][1] = property.value[0][1]
@ -193,7 +246,13 @@ function optimizeMultipleZeros(property) {
var values = property.value;
var spliceAt;
if (values.length == 4 && values[0][1] === '0' && values[1][1] === '0' && values[2][1] === '0' && values[3][1] === '0') {
if (
values.length == 4 &&
values[0][1] === '0' &&
values[1][1] === '0' &&
values[2][1] === '0' &&
values[3][1] === '0'
) {
if (property.name.indexOf('box-shadow') > -1) {
spliceAt = 2;
} else {
@ -228,15 +287,27 @@ function optimizePixelLengths(_, value, compatibility) {
return match;
}
if (compatibility.properties.shorterLengthUnits && compatibility.units.pt && intVal * 3 % 4 === 0) {
newValue = intVal * 3 / 4 + 'pt';
if (
compatibility.properties.shorterLengthUnits &&
compatibility.units.pt &&
(intVal * 3) % 4 === 0
) {
newValue = (intVal * 3) / 4 + 'pt';
}
if (compatibility.properties.shorterLengthUnits && compatibility.units.pc && intVal % 16 === 0) {
if (
compatibility.properties.shorterLengthUnits &&
compatibility.units.pc &&
intVal % 16 === 0
) {
newValue = intVal / 16 + 'pc';
}
if (compatibility.properties.shorterLengthUnits && compatibility.units.in && intVal % 96 === 0) {
if (
compatibility.properties.shorterLengthUnits &&
compatibility.units.in &&
intVal % 96 === 0
) {
newValue = intVal / 96 + 'in';
}
@ -255,19 +326,23 @@ function optimizePrecision(_, value, precisionOptions) {
return value
.replace(precisionOptions.decimalPointMatcher, '$1$2$3')
.replace(precisionOptions.zeroMatcher, function (match, integerPart, fractionPart, unit) {
var multiplier = precisionOptions.units[unit].multiplier;
var parsedInteger = parseInt(integerPart);
var integer = isNaN(parsedInteger) ? 0 : parsedInteger;
var fraction = parseFloat(fractionPart);
.replace(
precisionOptions.zeroMatcher,
function (match, integerPart, fractionPart, unit) {
var multiplier = precisionOptions.units[unit].multiplier;
var parsedInteger = parseInt(integerPart);
var integer = isNaN(parsedInteger) ? 0 : parsedInteger;
var fraction = parseFloat(fractionPart);
return Math.round((integer + fraction) * multiplier) / multiplier + unit;
});
return (
Math.round((integer + fraction) * multiplier) / multiplier + unit
);
}
);
}
function optimizeTimeUnits(_, value) {
if (!TIME_VALUE.test(value))
return value;
if (!TIME_VALUE.test(value)) return value;
return value.replace(TIME_VALUE, function (match, val, unit) {
var newValue;
@ -287,11 +362,23 @@ function optimizeUnits(name, value, unitsRegexp) {
return value;
}
if (name == 'flex' || name == '-ms-flex' || name == '-webkit-flex' || name == 'flex-basis' || name == '-webkit-flex-basis') {
if (
name == 'flex' ||
name == '-ms-flex' ||
name == '-webkit-flex' ||
name == 'flex-basis' ||
name == '-webkit-flex-basis'
) {
return value;
}
if (value.indexOf('%') > 0 && (name == 'height' || name == 'max-height' || name == 'width' || name == 'max-width')) {
if (
value.indexOf('%') > 0 &&
(name == 'height' ||
name == 'max-height' ||
name == 'width' ||
name == 'max-width')
) {
return value;
}
@ -301,11 +388,18 @@ function optimizeUnits(name, value, unitsRegexp) {
}
function optimizeWhitespace(name, value) {
if (name.indexOf('filter') > -1 || value.indexOf(' ') == -1 || value.indexOf('expression') === 0) {
if (
name.indexOf('filter') > -1 ||
value.indexOf(' ') == -1 ||
value.indexOf('expression') === 0
) {
return value;
}
if (value.indexOf(Marker.SINGLE_QUOTE) > -1 || value.indexOf(Marker.DOUBLE_QUOTE) > -1) {
if (
value.indexOf(Marker.SINGLE_QUOTE) > -1 ||
value.indexOf(Marker.DOUBLE_QUOTE) > -1
) {
return value;
}
@ -351,24 +445,38 @@ function optimizeZeroUnits(name, value) {
}
function removeQuotes(name, value) {
if (name == 'content' || name.indexOf('font-variation-settings') > -1 || name.indexOf('font-feature-settings') > -1 || name == 'grid' || name.indexOf('grid-') > -1) {
if (
name == 'content' ||
name.indexOf('font-variation-settings') > -1 ||
name.indexOf('font-feature-settings') > -1 ||
name == 'grid' ||
name.indexOf('grid-') > -1
) {
return value;
}
return QUOTED_BUT_SAFE_PATTERN.test(value) ?
value.substring(1, value.length - 1) :
value;
value.substring(1, value.length - 1)
: value;
}
function removeUrlQuotes(value) {
return /^url\(['"].+['"]\)$/.test(value) && !/^url\(['"].*[\*\s\(\)'"].*['"]\)$/.test(value) && !/^url\(['"]data:[^;]+;charset/.test(value) ?
value.replace(/["']/g, '') :
value;
return (
/^url\(['"].+['"]\)$/.test(value) &&
!/^url\(['"].*[\*\s\(\)'"].*['"]\)$/.test(value) &&
!/^url\(['"]data:[^;]+;charset/.test(value)
) ?
value.replace(/["']/g, '')
: value;
}
function transformValue(propertyName, propertyValue, rule, transformCallback) {
var selector = serializeRules(rule);
var transformedValue = transformCallback(propertyName, propertyValue, selector);
var transformedValue = transformCallback(
propertyName,
propertyValue,
selector
);
if (transformedValue === undefined) {
return propertyValue;
@ -389,35 +497,62 @@ function optimizeBody(rule, properties, context) {
var propertyToken;
var _properties = wrapForOptimizing(properties, true);
propertyLoop:
for (var i = 0, l = _properties.length; i < l; i++) {
propertyLoop: for (var i = 0, l = _properties.length; i < l; i++) {
property = _properties[i];
name = property.name;
if (!PROPERTY_NAME_PATTERN.test(name)) {
propertyToken = property.all[property.position];
context.warnings.push('Invalid property name \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.');
context.warnings.push(
"Invalid property name '" +
name +
"' at " +
formatPosition(propertyToken[1][2][0]) +
'. Ignoring.'
);
property.unused = true;
}
if (property.value.length === 0) {
propertyToken = property.all[property.position];
context.warnings.push('Empty property \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.');
context.warnings.push(
"Empty property '" +
name +
"' at " +
formatPosition(propertyToken[1][2][0]) +
'. Ignoring.'
);
property.unused = true;
}
if (property.hack && (
(property.hack[0] == Hack.ASTERISK || property.hack[0] == Hack.UNDERSCORE) && !options.compatibility.properties.iePrefixHack ||
property.hack[0] == Hack.BACKSLASH && !options.compatibility.properties.ieSuffixHack ||
property.hack[0] == Hack.BANG && !options.compatibility.properties.ieBangHack)) {
if (
property.hack &&
(((property.hack[0] == Hack.ASTERISK ||
property.hack[0] == Hack.UNDERSCORE) &&
!options.compatibility.properties.iePrefixHack) ||
(property.hack[0] == Hack.BACKSLASH &&
!options.compatibility.properties.ieSuffixHack) ||
(property.hack[0] == Hack.BANG &&
!options.compatibility.properties.ieBangHack))
) {
property.unused = true;
}
if (levelOptions.removeNegativePaddings && name.indexOf('padding') === 0 && (isNegative(property.value[0]) || isNegative(property.value[1]) || isNegative(property.value[2]) || isNegative(property.value[3]))) {
if (
levelOptions.removeNegativePaddings &&
name.indexOf('padding') === 0 &&
(isNegative(property.value[0]) ||
isNegative(property.value[1]) ||
isNegative(property.value[2]) ||
isNegative(property.value[3]))
) {
property.unused = true;
}
if (!options.compatibility.properties.ieFilters && isLegacyFilter(property)) {
if (
!options.compatibility.properties.ieFilters &&
isLegacyFilter(property)
) {
property.unused = true;
}
@ -441,39 +576,49 @@ function optimizeBody(rule, properties, context) {
if (type == Token.PROPERTY_BLOCK) {
property.unused = true;
context.warnings.push('Invalid value token at ' + formatPosition(value[0][1][2][0]) + '. Ignoring.');
context.warnings.push(
'Invalid value token at ' +
formatPosition(value[0][1][2][0]) +
'. Ignoring.'
);
break;
}
if (valueIsUrl && !context.validator.isUrl(value)) {
property.unused = true;
context.warnings.push('Broken URL \'' + value + '\' at ' + formatPosition(property.value[j][2][0]) + '. Ignoring.');
context.warnings.push(
"Broken URL '" +
value +
"' at " +
formatPosition(property.value[j][2][0]) +
'. Ignoring.'
);
break;
}
if (valueIsUrl) {
value = levelOptions.normalizeUrls ?
normalizeUrl(value) :
value;
value = !options.compatibility.properties.urlQuotes ?
removeUrlQuotes(value) :
value;
value = levelOptions.normalizeUrls ? normalizeUrl(value) : value;
value =
!options.compatibility.properties.urlQuotes ?
removeUrlQuotes(value)
: value;
} else if (isQuoted(value) || isLocal(value)) {
value = levelOptions.removeQuotes ?
removeQuotes(name, value) :
value;
value = levelOptions.removeQuotes ? removeQuotes(name, value) : value;
} else {
value = levelOptions.removeWhitespace ?
optimizeWhitespace(name, value) :
value;
value =
levelOptions.removeWhitespace ?
optimizeWhitespace(name, value)
: value;
value = optimizePrecision(name, value, options.precision);
value = optimizePixelLengths(name, value, options.compatibility);
value = levelOptions.replaceTimeUnits ?
optimizeTimeUnits(name, value) :
value;
value = levelOptions.replaceZeroUnits ?
optimizeZeroUnits(name, value) :
value;
value =
levelOptions.replaceTimeUnits ?
optimizeTimeUnits(name, value)
: value;
value =
levelOptions.replaceZeroUnits ?
optimizeZeroUnits(name, value)
: value;
if (options.compatibility.properties.zeroUnits) {
value = optimizeZeroDegUnit(name, value);
@ -501,9 +646,17 @@ function optimizeBody(rule, properties, context) {
if (name == 'background' && levelOptions.optimizeBackground) {
optimizeBackground(property);
} else if (name.indexOf('border') === 0 && name.indexOf('radius') > 0 && levelOptions.optimizeBorderRadius) {
} else if (
name.indexOf('border') === 0 &&
name.indexOf('radius') > 0 &&
levelOptions.optimizeBorderRadius
) {
optimizeBorderRadius(property);
} else if (name == 'filter'&& levelOptions.optimizeFilter && options.compatibility.properties.ieFilters) {
} else if (
name == 'filter' &&
levelOptions.optimizeFilter &&
options.compatibility.properties.ieFilters
) {
optimizeFilter(property);
} else if (name == 'font-weight' && levelOptions.optimizeFontWeight) {
optimizeFontWeight(property, 0);
@ -538,7 +691,12 @@ function removeComments(tokens, options) {
}
function optimizeComment(token, options) {
if (token[1][2] == Marker.EXCLAMATION && (options.level[OptimizationLevel.One].specialComments == 'all' || options.commentsKept < options.level[OptimizationLevel.One].specialComments)) {
if (
token[1][2] == Marker.EXCLAMATION &&
(options.level[OptimizationLevel.One].specialComments == 'all' ||
options.commentsKept <
options.level[OptimizationLevel.One].specialComments)
) {
options.commentsKept++;
return;
}
@ -552,11 +710,9 @@ function cleanupCharsets(tokens) {
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
if (token[0] != Token.AT_RULE)
continue;
if (token[0] != Token.AT_RULE) continue;
if (!CHARSET_REGEXP.test(token[1]))
continue;
if (!CHARSET_REGEXP.test(token[1])) continue;
if (hasCharset || token[1].indexOf(CHARSET_TOKEN) == -1) {
tokens.splice(i, 1);
@ -565,7 +721,10 @@ function cleanupCharsets(tokens) {
} else {
hasCharset = true;
tokens.splice(i, 1);
tokens.unshift([Token.AT_RULE, token[1].replace(CHARSET_REGEXP, CHARSET_TOKEN)]);
tokens.unshift([
Token.AT_RULE,
token[1].replace(CHARSET_REGEXP, CHARSET_TOKEN),
]);
}
}
}
@ -606,8 +765,14 @@ function buildPrecisionOptions(roundingPrecision) {
if (optimizable.length > 0) {
precisionOptions.enabled = true;
precisionOptions.decimalPointMatcher = new RegExp('(\\d)\\.($|' + optimizable.join('|') + ')($|\\W)', 'g');
precisionOptions.zeroMatcher = new RegExp('(\\d*)(\\.\\d+)(' + optimizable.join('|') + ')', 'g');
precisionOptions.decimalPointMatcher = new RegExp(
'(\\d)\\.($|' + optimizable.join('|') + ')($|\\W)',
'g'
);
precisionOptions.zeroMatcher = new RegExp(
'(\\d*)(\\.\\d+)(' + optimizable.join('|') + ')',
'g'
);
}
return precisionOptions;
@ -623,9 +788,11 @@ function isLegacyFilter(property) {
if (property.name == 'filter' || property.name == '-ms-filter') {
value = property.value[0][1];
return value.indexOf('progid') > -1 ||
return (
value.indexOf('progid') > -1 ||
value.indexOf('alpha') === 0 ||
value.indexOf('chroma') === 0;
value.indexOf('chroma') === 0
);
} else {
return false;
}
@ -636,13 +803,15 @@ function level1Optimize(tokens, context) {
var levelOptions = options.level[OptimizationLevel.One];
var ie7Hack = options.compatibility.selectors.ie7Hack;
var adjacentSpace = options.compatibility.selectors.adjacentSpace;
var spaceAfterClosingBrace = options.compatibility.properties.spaceAfterClosingBrace;
var spaceAfterClosingBrace =
options.compatibility.properties.spaceAfterClosingBrace;
var format = options.format;
var mayHaveCharset = false;
var afterRules = false;
options.unitsRegexp = options.unitsRegexp || buildUnitRegexp(options);
options.precision = options.precision || buildPrecisionOptions(levelOptions.roundingPrecision);
options.precision =
options.precision || buildPrecisionOptions(levelOptions.roundingPrecision);
options.commentsKept = options.commentsKept || 0;
for (var i = 0, l = tokens.length; i < l; i++) {
@ -659,7 +828,10 @@ function level1Optimize(tokens, context) {
afterRules = true;
break;
case Token.NESTED_BLOCK:
token[1] = levelOptions.tidyBlockScopes ? tidyBlock(token[1], spaceAfterClosingBrace) : token[1];
token[1] =
levelOptions.tidyBlockScopes ?
tidyBlock(token[1], spaceAfterClosingBrace)
: token[1];
level1Optimize(token[2], context);
afterRules = true;
break;
@ -667,14 +839,30 @@ function level1Optimize(tokens, context) {
optimizeComment(token, options);
break;
case Token.RULE:
token[1] = levelOptions.tidySelectors ? tidyRules(token[1], !ie7Hack, adjacentSpace, format, context.warnings) : token[1];
token[1] = token[1].length > 1 ? sortSelectors(token[1], levelOptions.selectorsSortingMethod) : token[1];
token[1] =
levelOptions.tidySelectors ?
tidyRules(
token[1],
!ie7Hack,
adjacentSpace,
format,
context.warnings
)
: token[1];
token[1] =
token[1].length > 1 ?
sortSelectors(token[1], levelOptions.selectorsSortingMethod)
: token[1];
optimizeBody(token[1], token[2], context);
afterRules = true;
break;
}
if (token[0] == Token.COMMENT && token[1].length === 0 || levelOptions.removeEmpty && (token[1].length === 0 || (token[2] && token[2].length === 0))) {
if (
(token[0] == Token.COMMENT && token[1].length === 0) ||
(levelOptions.removeEmpty &&
(token[1].length === 0 || (token[2] && token[2].length === 0)))
) {
tokens.splice(i, 1);
i--;
l--;