1
0
mirror of https://github.com/S2-/minifyfromhtml.git synced 2025-08-03 20:30:04 +02:00

update packages to latest version

This commit is contained in:
s2
2022-08-20 18:51:33 +02:00
parent 09663a35a5
commit 806ebf9a57
4513 changed files with 366205 additions and 92512 deletions

View File

@@ -25,10 +25,10 @@ var CHARSET_REGEXP = new RegExp('^' + CHARSET_TOKEN, 'i');
var DEFAULT_ROUNDING_PRECISION = require('../../options/rounding-precision').DEFAULT;
var PROPERTY_NAME_PATTERN = /^(?:\-chrome\-|\-[\w\-]+\w|\w[\w\-]+\w|\w{1,}|\-\-\S+)$/;
var VARIABLE_PROPERTY_NAME_PATTERN = /^--\S+$/;
var PROPERTY_NAME_PATTERN = /^(?:-chrome-|-[\w-]+\w|\w[\w-]+\w|\w{1,})$/;
var IMPORT_PREFIX_PATTERN = /^@import/i;
var URL_PREFIX_PATTERN = /^url\(/i;
var VARIABLE_NAME_PATTERN = /^--\S+$/;
function startsAsUrl(value) {
return URL_PREFIX_PATTERN.test(value);
@@ -44,16 +44,17 @@ function isLegacyFilter(property) {
if (property.name == 'filter' || property.name == '-ms-filter') {
value = property.value[0][1];
return value.indexOf('progid') > -1 ||
value.indexOf('alpha') === 0 ||
value.indexOf('chroma') === 0;
} else {
return false;
return value.indexOf('progid') > -1
|| value.indexOf('alpha') === 0
|| value.indexOf('chroma') === 0;
}
return false;
}
function noop() {}
function noopValueOptimizer(_name, value, _options) { return value; }
function optimizeBody(rule, properties, context) {
var options = context.options;
var valueOptimizers;
@@ -64,9 +65,9 @@ function optimizeBody(rule, properties, context) {
var _properties = wrapForOptimizing(properties);
var pluginValueOptimizers = context.options.plugins.level1Value;
var pluginPropertyOptimizers = context.options.plugins.level1Property;
var isVariable;
var i, l;
propertyLoop:
for (i = 0, l = _properties.length; i < l; i++) {
var j, k, m, n;
@@ -74,8 +75,15 @@ function optimizeBody(rule, properties, context) {
name = property.name;
propertyOptimizer = configuration[name] && configuration[name].propertyOptimizer || noop;
valueOptimizers = configuration[name] && configuration[name].valueOptimizers || [optimizers.whiteSpace];
isVariable = VARIABLE_PROPERTY_NAME_PATTERN.test(name);
if (!PROPERTY_NAME_PATTERN.test(name)) {
if (isVariable) {
valueOptimizers = options.variableOptimizers.length > 0
? options.variableOptimizers
: [optimizers.whiteSpace];
}
if (!isVariable && !PROPERTY_NAME_PATTERN.test(name)) {
propertyToken = property.all[property.position];
context.warnings.push('Invalid property name \'' + name + '\' at ' + formatPosition(propertyToken[1][2][0]) + '. Ignoring.');
property.unused = true;
@@ -90,9 +98,10 @@ function optimizeBody(rule, properties, context) {
}
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.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;
continue;
}
@@ -107,11 +116,6 @@ function optimizeBody(rule, properties, context) {
continue;
}
if (VARIABLE_NAME_PATTERN.test(name)) {
continue;
}
valuesLoop:
for (j = 0, m = property.value.length; j < m; j++) {
type = property.value[j][0];
value = property.value[j][1];
@@ -186,11 +190,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);
@@ -208,7 +210,7 @@ function buildUnitRegexp(options) {
var units = ['px', 'em', 'ex', 'cm', 'mm', 'in', 'pt', 'pc', '%'];
var otherUnits = ['ch', 'rem', 'vh', 'vm', 'vmax', 'vmin', 'vw'];
otherUnits.forEach(function (unit) {
otherUnits.forEach(function(unit) {
if (options.compatibility.units[unit]) {
units.push(unit);
}
@@ -220,7 +222,7 @@ function buildUnitRegexp(options) {
function buildPrecisionOptions(roundingPrecision) {
var precisionOptions = {
matcher: null,
units: {},
units: {}
};
var optimizable = [];
var unit;
@@ -232,7 +234,7 @@ function buildPrecisionOptions(roundingPrecision) {
if (value != DEFAULT_ROUNDING_PRECISION) {
precisionOptions.units[unit] = {};
precisionOptions.units[unit].value = value;
precisionOptions.units[unit].multiplier = Math.pow(10, value);
precisionOptions.units[unit].multiplier = 10 ** value;
optimizable.push(unit);
}
@@ -240,13 +242,23 @@ function buildPrecisionOptions(roundingPrecision) {
if (optimizable.length > 0) {
precisionOptions.enabled = true;
precisionOptions.decimalPointMatcher = new RegExp('(\\d)\\.($|' + optimizable.join('|') + ')($|\W)', 'g');
precisionOptions.decimalPointMatcher = new RegExp('(\\d)\\.($|' + optimizable.join('|') + ')($|\\W)', 'g');
precisionOptions.zeroMatcher = new RegExp('(\\d*)(\\.\\d+)(' + optimizable.join('|') + ')', 'g');
}
return precisionOptions;
}
function buildVariableOptimizers(options) {
return options.level[OptimizationLevel.One].variableValueOptimizers.map(function(optimizer) {
if (typeof (optimizer) == 'string') {
return optimizers[optimizer] || noopValueOptimizer;
}
return optimizer;
});
}
function level1Optimize(tokens, context) {
var options = context.options;
var levelOptions = options.level[OptimizationLevel.One];
@@ -260,37 +272,43 @@ function level1Optimize(tokens, context) {
options.unitsRegexp = options.unitsRegexp || buildUnitRegexp(options);
options.precision = options.precision || buildPrecisionOptions(levelOptions.roundingPrecision);
options.commentsKept = options.commentsKept || 0;
options.variableOptimizers = options.variableOptimizers || buildVariableOptimizers(options);
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
switch (token[0]) {
case Token.AT_RULE:
token[1] = isImport(token) && afterRules ? '' : token[1];
token[1] = levelOptions.tidyAtRules ? tidyAtRule(token[1]) : token[1];
mayHaveCharset = true;
break;
case Token.AT_RULE_BLOCK:
optimizeBody(token[1], token[2], context);
afterRules = true;
break;
case Token.NESTED_BLOCK:
token[1] = levelOptions.tidyBlockScopes ? tidyBlock(token[1], spaceAfterClosingBrace) : token[1];
level1Optimize(token[2], context);
afterRules = true;
break;
case Token.COMMENT:
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];
optimizeBody(token[1], token[2], context);
afterRules = true;
break;
case Token.AT_RULE:
token[1] = isImport(token) && afterRules ? '' : token[1];
token[1] = levelOptions.tidyAtRules ? tidyAtRule(token[1]) : token[1];
mayHaveCharset = true;
break;
case Token.AT_RULE_BLOCK:
optimizeBody(token[1], token[2], context);
afterRules = true;
break;
case Token.NESTED_BLOCK:
token[1] = levelOptions.tidyBlockScopes ? tidyBlock(token[1], spaceAfterClosingBrace) : token[1];
level1Optimize(token[2], context);
afterRules = true;
break;
case Token.COMMENT:
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];
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--;

View File

@@ -16,9 +16,12 @@ var plugin = {
}
if (property.value.length == 1) {
property.value[0][1] = property.value[0][1].replace(ALPHA_OR_CHROMA_FILTER_PATTERN, function (match, filter, suffix) {
return filter.toLowerCase() + suffix;
});
property.value[0][1] = property.value[0][1].replace(
ALPHA_OR_CHROMA_FILTER_PATTERN,
function(match, filter, suffix) {
return filter.toLowerCase() + suffix;
}
);
}
property.value[0][1] = property.value[0][1]

View File

@@ -16,7 +16,13 @@ var plugin = {
}
// remove negative paddings
if (options.level[OptimizationLevel.One].removeNegativePaddings && (isNegative(property.value[0]) || isNegative(property.value[1]) || isNegative(property.value[2]) || isNegative(property.value[3]))) {
if (options.level[OptimizationLevel.One].removeNegativePaddings
&& (
isNegative(property.value[0])
|| isNegative(property.value[1])
|| isNegative(property.value[2])
|| isNegative(property.value[3])
)) {
property.unused = true;
}
}

View File

@@ -10,13 +10,13 @@ function standardSorter(scope1, scope2) {
function sortSelectors(selectors, method) {
switch (method) {
case 'natural':
return selectors.sort(naturalSorter);
case 'standard':
return selectors.sort(standardSorter);
case 'none':
case false:
return selectors;
case 'natural':
return selectors.sort(naturalSorter);
case 'standard':
return selectors.sort(standardSorter);
case 'none':
case false:
return selectors;
}
}

View File

@@ -9,7 +9,7 @@ var DOUBLE_QUOTE_PATTERN = /="([a-zA-Z][a-zA-Z\d\-_]+)"(\s|\])/g;
var HTML_COMMENT_PATTERN = /^(?:(?:<!--|-->)\s*)+/;
var SINGLE_QUOTE_CASE_PATTERN = /='([a-zA-Z][a-zA-Z\d\-_]+)'([iI])/g;
var SINGLE_QUOTE_PATTERN = /='([a-zA-Z][a-zA-Z\d\-_]+)'(\s|\])/g;
var RELATION_PATTERN = /[>\+~]/;
var RELATION_PATTERN = /[>+~]/;
var WHITESPACE_PATTERN = /\s/;
var ASTERISK_PLUS_HTML_HACK = '*+html ';
@@ -42,7 +42,12 @@ function hasInvalidCharacters(value) {
// continue as always
} else if (character == Marker.SINGLE_QUOTE || character == Marker.DOUBLE_QUOTE) {
isQuote = !isQuote;
} else if (!isQuote && (character == Marker.CLOSE_CURLY_BRACKET || character == Marker.EXCLAMATION || character == LESS_THAN || character == Marker.SEMICOLON)) {
} else if (!isQuote
&& (character == Marker.CLOSE_CURLY_BRACKET
|| character == Marker.EXCLAMATION
|| character == LESS_THAN
|| character == Marker.SEMICOLON)
) {
isInvalid = true;
break;
} else if (!isQuote && i === 0 && RELATION_PATTERN.test(character)) {
@@ -86,9 +91,10 @@ function removeWhitespace(value, format) {
isQuoted = isSingleQuoted || isDoubleQuoted;
isRelation = !isAttribute && !isEscaped && roundBracketLevel === 0 && RELATION_PATTERN.test(character);
isWhitespace = WHITESPACE_PATTERN.test(character);
isSpaceAwarePseudoClass = roundBracketLevel == 1 && character == Marker.CLOSE_ROUND_BRACKET ?
false :
isSpaceAwarePseudoClass || (roundBracketLevel === 0 && character == Marker.COLON && isPseudoClassWithSelectors(value, i));
isSpaceAwarePseudoClass = roundBracketLevel == 1 && character == Marker.CLOSE_ROUND_BRACKET
? false
: isSpaceAwarePseudoClass
|| (roundBracketLevel === 0 && character == Marker.COLON && isPseudoClassWithSelectors(value, i));
if (wasEscaped && isQuoted && isNewLineWin) {
// swallow escaped new windows lines in comments
@@ -157,9 +163,9 @@ function removeWhitespace(value, format) {
wasComma = character == Marker.COMMA;
}
return withCaseAttribute ?
stripped.join('').replace(CASE_RESTORE_PATTERN, '$1 $2]') :
stripped.join('');
return withCaseAttribute
? stripped.join('').replace(CASE_RESTORE_PATTERN, '$1 $2]')
: stripped.join('');
}
function isPseudoClassWithSelectors(value, colonPosition) {
@@ -180,6 +186,22 @@ function removeQuotes(value) {
.replace(DOUBLE_QUOTE_PATTERN, '=$1$2');
}
function replacePseudoClasses(value) {
return value
.replace('nth-child(1)', 'first-child')
.replace('nth-of-type(1)', 'first-of-type')
.replace('nth-of-type(even)', 'nth-of-type(2n)')
.replace('nth-child(even)', 'nth-child(2n)')
.replace('nth-of-type(2n+1)', 'nth-of-type(odd)')
.replace('nth-child(2n+1)', 'nth-child(odd)')
.replace('nth-last-child(1)', 'last-child')
.replace('nth-last-of-type(1)', 'last-of-type')
.replace('nth-last-of-type(even)', 'nth-last-of-type(2n)')
.replace('nth-last-child(even)', 'nth-last-child(2n)')
.replace('nth-last-of-type(2n+1)', 'nth-last-of-type(odd)')
.replace('nth-last-child(2n+1)', 'nth-last-child(odd)');
}
function tidyRules(rules, removeUnsupported, adjacentSpace, format, warnings) {
var list = [];
var repeated = [];
@@ -217,14 +239,16 @@ function tidyRules(rules, removeUnsupported, adjacentSpace, format, warnings) {
if (reduced.indexOf('*') > -1) {
reduced = reduced
.replace(/\*([:#\.\[])/g, '$1')
.replace(/^(\:first\-child)?\+html/, '*$1+html');
.replace(/\*([:#.[])/g, '$1')
.replace(/^(:first-child)?\+html/, '*$1+html');
}
if (repeated.indexOf(reduced) > -1) {
continue;
}
reduced = replacePseudoClasses(reduced);
rule[1] = reduced;
repeated.push(reduced);
list.push(rule);

View File

@@ -4,14 +4,14 @@ var shortenRgb = require('./color/shorten-rgb');
var split = require('../../../utils/split');
var ANY_COLOR_FUNCTION_PATTERN = /(rgb|rgba|hsl|hsla)\(([^\(\)]+)\)/gi;
var ANY_COLOR_FUNCTION_PATTERN = /(rgb|rgba|hsl|hsla)\(([^()]+)\)/gi;
var COLOR_PREFIX_PATTERN = /#|rgb|hsl/gi;
var HEX_LONG_PATTERN = /(^|[^='"])#([0-9a-f]{6})/gi;
var HEX_SHORT_PATTERN = /(^|[^='"])#([0-9a-f]{3})/gi;
var HEX_VALUE_PATTERN = /[0-9a-f]/i;
var HSL_PATTERN = /hsl\((-?\d+),(-?\d+)%?,(-?\d+)%?\)/gi;
var RGBA_HSLA_PATTERN = /(rgb|hsl)a?\((\-?\d+),(\-?\d+\%?),(\-?\d+\%?),(0*[1-9]+[0-9]*(\.?\d*)?)\)/gi;
var RGB_PATTERN = /rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/gi;
var RGBA_HSLA_PATTERN = /(rgb|hsl)a?\((-?\d+),(-?\d+%?),(-?\d+%?),(0*[1-9]+[0-9]*(\.?\d*)?)\)/gi;
var RGB_PATTERN = /rgb\((-?\d+),(-?\d+),(-?\d+)\)/gi;
var TRANSPARENT_FUNCTION_PATTERN = /(?:rgba|hsla)\(0,0%?,0%?,0\)/g;
var plugin = {
@@ -26,36 +26,35 @@ var plugin = {
}
value = value
.replace(RGBA_HSLA_PATTERN, function (match, colorFn, p1, p2, p3, alpha) {
return (parseInt(alpha, 10) >= 1 ? colorFn + '(' + [p1,p2,p3].join(',') + ')' : match);
.replace(RGBA_HSLA_PATTERN, function(match, colorFn, p1, p2, p3, alpha) {
return (parseInt(alpha) >= 1 ? colorFn + '(' + [p1, p2, p3].join(',') + ')' : match);
})
.replace(RGB_PATTERN, function (match, red, green, blue) {
.replace(RGB_PATTERN, function(match, red, green, blue) {
return shortenRgb(red, green, blue);
})
.replace(HSL_PATTERN, function (match, hue, saturation, lightness) {
.replace(HSL_PATTERN, function(match, hue, saturation, lightness) {
return shortenHsl(hue, saturation, lightness);
})
.replace(HEX_LONG_PATTERN, function (match, prefix, color, at, inputValue) {
.replace(HEX_LONG_PATTERN, 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]) {
} 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();
}
return (prefix + '#' + color).toLowerCase();
})
.replace(HEX_SHORT_PATTERN, function (match, prefix, color) {
.replace(HEX_SHORT_PATTERN, function(match, prefix, color) {
return prefix + '#' + color.toLowerCase();
})
.replace(ANY_COLOR_FUNCTION_PATTERN, function (match, colorFunction, colorDef) {
.replace(ANY_COLOR_FUNCTION_PATTERN, 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);
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;
@@ -73,7 +72,7 @@ var plugin = {
});
if (options.compatibility.colors.opacity && name.indexOf('background') == -1) {
value = value.replace(TRANSPARENT_FUNCTION_PATTERN, function (match) {
value = value.replace(TRANSPARENT_FUNCTION_PATTERN, function(match) {
if (split(value, ',').pop().indexOf('gradient(') > -1) {
return match;
}

View File

@@ -181,9 +181,9 @@ function shortenHex(value) {
shortened = shortened.replace(toHexPattern, hexConverter);
}
return hasHex ?
shortened.replace(toNamePattern, nameConverter) :
shortened;
return hasHex
? shortened.replace(toNamePattern, nameConverter)
: shortened;
}
module.exports = shortenHex;

View File

@@ -5,44 +5,37 @@ function hslToRgb(h, s, l) {
var r, g, b;
// normalize hue orientation b/w 0 and 360 degrees
h = h % 360;
if (h < 0)
h += 360;
h %= 360;
if (h < 0) { h += 360; }
h = ~~h / 360;
if (s < 0)
s = 0;
else if (s > 100)
s = 100;
if (s < 0) { s = 0; } else if (s > 100) { s = 100; }
s = ~~s / 100;
if (l < 0)
l = 0;
else if (l > 100)
l = 100;
if (l < 0) { l = 0; } else if (l > 100) { l = 100; }
l = ~~l / 100;
if (s === 0) {
r = g = b = l; // achromatic
} else {
var q = l < 0.5 ?
l * (1 + s) :
l + s - l * s;
var q = l < 0.5
? l * (1 + s)
: l + s - l * s;
var p = 2 * l - q;
r = hueToRgb(p, q, h + 1/3);
r = hueToRgb(p, q, h + 1 / 3);
g = hueToRgb(p, q, h);
b = hueToRgb(p, q, h - 1/3);
b = hueToRgb(p, q, h - 1 / 3);
}
return [~~(r * 255), ~~(g * 255), ~~(b * 255)];
}
function hueToRgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
if (t < 0) { t += 1; }
if (t > 1) { t -= 1; }
if (t < 1 / 6) { return p + (q - p) * 6 * t; }
if (t < 1 / 2) { return q; }
if (t < 2 / 3) { return p + (q - p) * (2 / 3 - t) * 6; }
return p;
}
@@ -52,10 +45,10 @@ function shortenHsl(hue, saturation, lightness) {
var greenAsHex = asRgb[1].toString(16);
var blueAsHex = asRgb[2].toString(16);
return '#' +
((redAsHex.length == 1 ? '0' : '') + redAsHex) +
((greenAsHex.length == 1 ? '0' : '') + greenAsHex) +
((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
return '#'
+ ((redAsHex.length == 1 ? '0' : '') + redAsHex)
+ ((greenAsHex.length == 1 ? '0' : '') + greenAsHex)
+ ((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
}
module.exports = shortenHsl;

View File

@@ -1,13 +1,62 @@
var split = require('../../../utils/split');
var startsAsUrl = require('./starts-as-url');
var OptimizationLevel = require('../../../options/optimization-level').OptimizationLevel;
var EXPRESSION_PATTERN = /^expression\(.*\)$/;
var ANY_FUNCTION_PATTERN = /^(-(?:moz|ms|o|webkit)-[a-z-]+|[a-z-]+)\((.+)\)$/;
var TOKEN_SEPARATOR_PATTERN = /([\s,/])/;
var DOT_ZERO_PATTERN = /(^|\D)\.0+(\D|$)/g;
var FRACTION_PATTERN = /\.([1-9]*)0+(\D|$)/g;
var LEADING_ZERO_FRACTION_PATTERN = /(^|\D)0\.(\d)/g;
var MINUS_ZERO_FRACTION_PATTERN = /([^\w\d\-]|^)\-0([^\.]|$)/g;
var MINUS_ZERO_FRACTION_PATTERN = /([^\w\d-]|^)-0([^.]|$)/g;
var ZERO_PREFIXED_UNIT_PATTERN = /(^|\s)0+([1-9])/g;
function optimizeRecursively(value) {
var functionTokens;
var tokens;
if (startsAsUrl(value)) {
return value;
}
if (EXPRESSION_PATTERN.test(value)) {
return value;
}
functionTokens = ANY_FUNCTION_PATTERN.exec(value);
if (!functionTokens) {
return optimizeFractions(value);
}
tokens = split(functionTokens[2], TOKEN_SEPARATOR_PATTERN)
.map(function(token) { return optimizeRecursively(token); });
return functionTokens[1] + '(' + tokens.join('') + ')';
}
function optimizeFractions(value) {
if (value.indexOf('0') == -1) {
return value;
}
if (value.indexOf('-') > -1) {
value = value
.replace(MINUS_ZERO_FRACTION_PATTERN, '$10$2')
.replace(MINUS_ZERO_FRACTION_PATTERN, '$10$2');
}
return value
.replace(ZERO_PREFIXED_UNIT_PATTERN, '$1$2')
.replace(DOT_ZERO_PATTERN, '$10$2')
.replace(FRACTION_PATTERN, function(match, nonZeroPart, suffix) {
return (nonZeroPart.length > 0 ? '.' : '') + nonZeroPart + suffix;
})
.replace(LEADING_ZERO_FRACTION_PATTERN, '$1.$2');
}
var plugin = {
level1: {
value: function fraction(name, value, options) {
@@ -15,27 +64,7 @@ var plugin = {
return value;
}
if (startsAsUrl(value)) {
return value;
}
if (value.indexOf('0') == -1) {
return value;
}
if (value.indexOf('-') > -1) {
value = value
.replace(MINUS_ZERO_FRACTION_PATTERN, '$10$2')
.replace(MINUS_ZERO_FRACTION_PATTERN, '$10$2');
}
return value
.replace(ZERO_PREFIXED_UNIT_PATTERN, '$1$2')
.replace(DOT_ZERO_PATTERN, '$10$2')
.replace(FRACTION_PATTERN, function (match, nonZeroPart, suffix) {
return (nonZeroPart.length > 0 ? '.' : '') + nonZeroPart + suffix;
})
.replace(LEADING_ZERO_FRACTION_PATTERN, '$1.$2');
return optimizeRecursively(value);
}
}
};

View File

@@ -7,10 +7,10 @@ var plugin = {
return value
.replace(options.precision.decimalPointMatcher, '$1$2$3')
.replace(options.precision.zeroMatcher, function (match, integerPart, fractionPart, unit) {
.replace(options.precision.zeroMatcher, function(match, integerPart, fractionPart, unit) {
var multiplier = options.precision.units[unit].multiplier;
var parsedInteger = parseInt(integerPart);
var integer = isNaN(parsedInteger) ? 0 : parsedInteger;
var integer = Number.isNaN(parsedInteger) ? 0 : parsedInteger;
var fraction = parseFloat(fractionPart);
return Math.round((integer + fraction) * multiplier) / multiplier + unit;

View File

@@ -3,10 +3,16 @@ var OptimizationLevel = require('../../../options/optimization-level').Optimizat
var LOCAL_PREFIX_PATTERN = /^local\(/i;
var QUOTED_PATTERN = /^('.*'|".*")$/;
var QUOTED_BUT_SAFE_PATTERN = /^['"][a-zA-Z][a-zA-Z\d\-_]+['"]$/;
// eslint-disable-next-line max-len
var GENERIC_FONT_FAMILY_PATTERN = /^['"](?:cursive|default|emoji|fangsong|fantasy|inherit|initial|math|monospace|revert|revert-layer|sans-serif|serif|system-ui|ui-monospace|ui-rounded|ui-sans-serif|ui-serif|unset)['"]$/;
var plugin = {
level1: {
value: function textQuotes(_name, value, options) {
value: function textQuotes(name, value, options) {
if ((name == 'font-family' || name == 'font') && GENERIC_FONT_FAMILY_PATTERN.test(value)) {
return value;
}
if (!options.level[OptimizationLevel.One].removeQuotes) {
return value;
}
@@ -15,9 +21,9 @@ var plugin = {
return value;
}
return QUOTED_BUT_SAFE_PATTERN.test(value) ?
value.substring(1, value.length - 1) :
value;
return QUOTED_BUT_SAFE_PATTERN.test(value)
? value.substring(1, value.length - 1)
: value;
}
}
};

View File

@@ -1,6 +1,6 @@
var OptimizationLevel = require('../../../options/optimization-level').OptimizationLevel;
var TIME_VALUE = /^(\-?[\d\.]+)(m?s)$/;
var TIME_VALUE = /^(-?[\d.]+)(m?s)$/;
var plugin = {
level1: {
@@ -13,7 +13,7 @@ var plugin = {
return value;
}
return value.replace(TIME_VALUE, function (match, val, unit) {
return value.replace(TIME_VALUE, function(match, val, unit) {
var newValue;
if (unit == 'ms') {

View File

@@ -7,7 +7,7 @@ var plugin = {
return value;
}
return value.replace(WHOLE_PIXEL_VALUE, function (match, val) {
return value.replace(WHOLE_PIXEL_VALUE, function(match, val) {
var newValue;
var intVal = parseInt(val);
@@ -15,15 +15,21 @@ var plugin = {
return match;
}
if (options.compatibility.properties.shorterLengthUnits && options.compatibility.units.pt && intVal * 3 % 4 === 0) {
if (options.compatibility.properties.shorterLengthUnits
&& options.compatibility.units.pt
&& intVal * 3 % 4 === 0) {
newValue = intVal * 3 / 4 + 'pt';
}
if (options.compatibility.properties.shorterLengthUnits && options.compatibility.units.pc && intVal % 16 === 0) {
if (options.compatibility.properties.shorterLengthUnits
&& options.compatibility.units.pc
&& intVal % 16 === 0) {
newValue = intVal / 16 + 'pc';
}
if (options.compatibility.properties.shorterLengthUnits && options.compatibility.units.in && intVal % 96 === 0) {
if (options.compatibility.properties.shorterLengthUnits
&& options.compatibility.units.in
&& intVal % 96 === 0) {
newValue = intVal / 96 + 'in';
}

View File

@@ -1,5 +1,5 @@
var QUOTED_URL_PATTERN = /^url\(['"].+['"]\)$/;
var QUOTED_URL_WITH_WHITESPACE_PATTERN = /^url\(['"].*[\*\s\(\)'"].*['"]\)$/;
var QUOTED_URL_WITH_WHITESPACE_PATTERN = /^url\(['"].*[*\s()'"].*['"]\)$/;
var QUOTES_PATTERN = /["']/g;
var URL_DATA_PATTERN = /^url\(['"]data:[^;]+;charset/;
@@ -10,9 +10,11 @@ var plugin = {
return value;
}
return QUOTED_URL_PATTERN.test(value) && !QUOTED_URL_WITH_WHITESPACE_PATTERN.test(value) && !URL_DATA_PATTERN.test(value) ?
value.replace(QUOTES_PATTERN, '') :
value;
return QUOTED_URL_PATTERN.test(value)
&& !QUOTED_URL_WITH_WHITESPACE_PATTERN.test(value)
&& !URL_DATA_PATTERN.test(value)
? value.replace(QUOTES_PATTERN, '')
: value;
}
}
};

View File

@@ -1,6 +1,8 @@
var startsAsUrl = require('./starts-as-url');
var WHITESPACE_PATTERN = /\\?\n|\\?\r\n/g;
var WHITESPACE_PREFIX_PATTERN = /(\()\s+/g;
var WHITESPACE_SUFFIX_PATTERN = /\s+(\))/g;
var plugin = {
level1: {
@@ -9,7 +11,10 @@ var plugin = {
return value;
}
return value.replace(WHITESPACE_PATTERN, '');
return value
.replace(WHITESPACE_PATTERN, '')
.replace(WHITESPACE_PREFIX_PATTERN, '$1')
.replace(WHITESPACE_SUFFIX_PATTERN, '$1');
}
}
};

View File

@@ -4,9 +4,12 @@ var Marker = require('../../../tokenizer/marker');
var CALC_DIVISION_WHITESPACE_PATTERN = /\) ?\/ ?/g;
var COMMA_AND_SPACE_PATTERN = /, /g;
var LINE_BREAK_PATTERN = /\r?\n/g;
var MULTI_WHITESPACE_PATTERN = /\s+/g;
var FUNCTION_CLOSING_BRACE_WHITESPACE_PATTERN = /\s+(;?\))/g;
var FUNCTION_OPENING_BRACE_WHITESPACE_PATTERN = /(\(;?)\s+/g;
var VARIABLE_NAME_PATTERN = /^--\S+$/;
var VARIABLE_VALUE_PATTERN = /^var\(\s*--\S+\s*\)$/;
var plugin = {
level1: {
@@ -15,7 +18,11 @@ var plugin = {
return value;
}
if (value.indexOf(' ') == -1 || value.indexOf('expression') === 0) {
if (VARIABLE_NAME_PATTERN.test(name) && !VARIABLE_VALUE_PATTERN.test(value)) {
return value;
}
if ((value.indexOf(' ') == -1 && value.indexOf('\n') == -1) || value.indexOf('expression') === 0) {
return value;
}
@@ -23,6 +30,7 @@ var plugin = {
return value;
}
value = value.replace(LINE_BREAK_PATTERN, '');
value = value.replace(MULTI_WHITESPACE_PATTERN, ' ');
if (value.indexOf('calc') > -1) {

View File

@@ -1,4 +1,34 @@
var FUNCTION_PATTERN = /^(?:\-moz\-calc|\-webkit\-calc|calc|rgb|hsl|rgba|hsla|min|max|clamp)\(/;
var split = require('../../../utils/split');
var ANY_FUNCTION_PATTERN = /^(-(?:moz|ms|o|webkit)-[a-z-]+|[a-z-]+)\((.+)\)$/;
var SKIP_FUNCTION_PATTERN = /^(?:-moz-calc|-webkit-calc|calc|rgb|hsl|rgba|hsla|min|max|clamp|expression)\(/;
var TOKEN_SEPARATOR_PATTERN = /([\s,/])/;
function removeRecursively(value, options) {
var functionTokens;
var tokens;
if (SKIP_FUNCTION_PATTERN.test(value)) {
return value;
}
functionTokens = ANY_FUNCTION_PATTERN.exec(value);
if (!functionTokens) {
return removeZeros(value, options);
}
tokens = split(functionTokens[2], TOKEN_SEPARATOR_PATTERN)
.map(function(token) { return removeRecursively(token, options); });
return functionTokens[1] + '(' + tokens.join('') + ')';
}
function removeZeros(value, options) {
return value
.replace(options.unitsRegexp, '$10$2')
.replace(options.unitsRegexp, '$10$2');
}
var plugin = {
level1: {
@@ -7,17 +37,11 @@ var plugin = {
return value;
}
if (FUNCTION_PATTERN.test(value)) {
return value;
}
if (value.indexOf('%') > 0 && (name == 'height' || name == 'max-height' || name == 'width' || name == 'max-width')) {
return value;
}
return value
.replace(options.unitsRegexp, '$1' + '0' + '$2')
.replace(options.unitsRegexp, '$1' + '0' + '$2');
return removeRecursively(value, options);
}
}
};