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

use minify

This commit is contained in:
s2
2019-04-15 10:35:12 +02:00
parent 9528e1e8e1
commit 86e1333c3e
9417 changed files with 70210 additions and 462173 deletions

View File

@@ -19,6 +19,8 @@ var Marker = require('../../tokenizer/marker');
var formatPosition = require('../../utils/format-position');
var split = require('../../utils/split');
var serializeRules = require('../../writer/one-time').rules;
var IgnoreProperty = 'ignore-property';
var CHARSET_TOKEN = '@charset';
@@ -334,7 +336,7 @@ function optimizeZeroUnits(name, value) {
}
function removeQuotes(name, value) {
if (name == 'content' || name.indexOf('font-feature-settings') > -1 || name.indexOf('grid-') > -1) {
if (name == 'content' || name.indexOf('font-variation-settings') > -1 || name.indexOf('font-feature-settings') > -1 || name.indexOf('grid-') > -1) {
return value;
}
@@ -349,8 +351,9 @@ function removeUrlQuotes(value) {
value;
}
function transformValue(propertyName, propertyValue, transformCallback) {
var transformedValue = transformCallback(propertyName, propertyValue);
function transformValue(propertyName, propertyValue, rule, transformCallback) {
var selector = serializeRules(rule);
var transformedValue = transformCallback(propertyName, propertyValue, selector);
if (transformedValue === undefined) {
return propertyValue;
@@ -363,7 +366,7 @@ function transformValue(propertyName, propertyValue, transformCallback) {
//
function optimizeBody(properties, context) {
function optimizeBody(rule, properties, context) {
var options = context.options;
var levelOptions = options.level[OptimizationLevel.One];
var property, name, type, value;
@@ -408,7 +411,7 @@ function optimizeBody(properties, context) {
}
if (property.block) {
optimizeBody(property.value[0][1], context);
optimizeBody(rule, property.value[0][1], context);
continue;
}
@@ -467,7 +470,7 @@ function optimizeBody(properties, context) {
}
}
value = transformValue(name, value, levelOptions.transform);
value = transformValue(name, value, rule, levelOptions.transform);
if (value === IgnoreProperty) {
property.unused = true;
@@ -637,7 +640,7 @@ function level1Optimize(tokens, context) {
mayHaveCharset = true;
break;
case Token.AT_RULE_BLOCK:
optimizeBody(token[2], context);
optimizeBody(token[1], token[2], context);
afterRules = true;
break;
case Token.NESTED_BLOCK:
@@ -651,7 +654,7 @@ function level1Optimize(tokens, context) {
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[2], context);
optimizeBody(token[1], token[2], context);
afterRules = true;
break;
}

View File

@@ -68,7 +68,7 @@ function removeWhitespace(value, format) {
character = value[i];
isNewLineNix = character == Marker.NEW_LINE_NIX;
isNewLineWin = character == Marker.NEW_LINE_NIX && value[i - 1] == Marker.NEW_LINE_WIN;
isNewLineWin = character == Marker.NEW_LINE_NIX && value[i - 1] == Marker.CARRIAGE_RETURN;
isQuoted = isSingleQuoted || isDoubleQuoted;
isRelation = !isAttribute && !isEscaped && roundBracketLevel === 0 && RELATION_PATTERN.test(character);
isWhitespace = WHITESPACE_PATTERN.test(character);

View File

@@ -105,7 +105,7 @@ function animation(property, compactable, validator) {
} else if (validator.isTime(value[1]) && !delaySet) {
delay.value = [value];
delaySet = true;
} else if ((validator.isGlobal(value[1]) || validator.isAnimationTimingFunction(value[1])) && !timingSet) {
} else if ((validator.isGlobal(value[1]) || validator.isTimingFunction(value[1])) && !timingSet) {
timing.value = [value];
timingSet = true;
} else if ((validator.isAnimationIterationCountKeyword(value[1]) || validator.isPositiveNumber(value[1])) && !iterationSet) {
@@ -523,6 +523,53 @@ function listStyle(property, compactable, validator) {
return components;
}
function transition(property, compactable, validator) {
var prop = _wrapDefault(property.name + '-property', property, compactable);
var duration = _wrapDefault(property.name + '-duration', property, compactable);
var timing = _wrapDefault(property.name + '-timing-function', property, compactable);
var delay = _wrapDefault(property.name + '-delay', property, compactable);
var components = [prop, duration, timing, delay];
var values = property.value;
var value;
var durationSet = false;
var delaySet = false;
var propSet = false;
var timingSet = false;
var i;
var l;
if (property.value.length == 1 && property.value[0][1] == 'inherit') {
prop.value = duration.value = timing.value = delay.value = property.value;
return components;
}
if (values.length > 1 && _anyIsInherit(values)) {
throw new InvalidPropertyError('Invalid animation values at ' + formatPosition(values[0][2][0]) + '. Ignoring.');
}
for (i = 0, l = values.length; i < l; i++) {
value = values[i];
if (validator.isTime(value[1]) && !durationSet) {
duration.value = [value];
durationSet = true;
} else if (validator.isTime(value[1]) && !delaySet) {
delay.value = [value];
delaySet = true;
} else if ((validator.isGlobal(value[1]) || validator.isTimingFunction(value[1])) && !timingSet) {
timing.value = [value];
timingSet = true;
} else if (validator.isIdentifier(value[1]) && !propSet) {
prop.value = [value];
propSet = true;
} else {
throw new InvalidPropertyError('Invalid animation value at ' + formatPosition(value[2][0]) + '. Ignoring.');
}
}
return components;
}
function widthStyleColor(property, compactable, validator) {
var descriptor = compactable[property.name];
var components = [
@@ -592,5 +639,6 @@ module.exports = {
fourValues: fourValues,
listStyle: listStyle,
multiplex: multiplex,
outline: widthStyleColor
outline: widthStyleColor,
transition: transition
};

View File

@@ -20,16 +20,6 @@ function animationName(validator, value1, value2) {
return validator.isAnimationNameKeyword(value2) || validator.isIdentifier(value2);
}
function animationTimingFunction(validator, value1, value2) {
if (!understandable(validator, value1, value2, 0, true) && !(validator.isAnimationTimingFunction(value2) || validator.isGlobal(value2))) {
return false;
} else if (validator.isVariable(value1) && validator.isVariable(value2)) {
return true;
}
return validator.isAnimationTimingFunction(value2) || validator.isGlobal(value2);
}
function areSameFunction(validator, value1, value2) {
if (!validator.isFunction(value1) || !validator.isFunction(value2)) {
return false;
@@ -129,14 +119,22 @@ function keywordWithGlobal(propertyName) {
};
}
function propertyName(validator, value1, value2) {
if (!understandable(validator, value1, value2, 0, true) && !validator.isIdentifier(value2)) {
return false;
} else if (validator.isVariable(value1) && validator.isVariable(value2)) {
return true;
}
return validator.isIdentifier(value2);
}
function sameFunctionOrValue(validator, value1, value2) {
return areSameFunction(validator, value1, value2) ?
true :
value1 === value2;
}
function textShadow(validator, value1, value2) {
if (!understandable(validator, value1, value2, 0, true) && !(validator.isUnit(value2) || validator.isColor(value2) || validator.isGlobal(value2))) {
return false;
@@ -165,6 +163,16 @@ function time(validator, value1, value2) {
return sameFunctionOrValue(validator, value1, value2);
}
function timingFunction(validator, value1, value2) {
if (!understandable(validator, value1, value2, 0, true) && !(validator.isTimingFunction(value2) || validator.isGlobal(value2))) {
return false;
} else if (validator.isVariable(value1) && validator.isVariable(value2)) {
return true;
}
return validator.isTimingFunction(value2) || validator.isGlobal(value2);
}
function unit(validator, value1, value2) {
if (!understandable(validator, value1, value2, 0, true) && !validator.isUnit(value2)) {
return false;
@@ -224,7 +232,9 @@ module.exports = {
color: color,
components: components,
image: image,
propertyName: propertyName,
time: time,
timingFunction: timingFunction,
unit: unit,
unitOrNumber: unitOrNumber
},
@@ -234,7 +244,6 @@ module.exports = {
animationIterationCount: animationIterationCount,
animationName: animationName,
animationPlayState: keywordWithGlobal('animation-play-state'),
animationTimingFunction: animationTimingFunction,
backgroundAttachment: keyword('background-attachment'),
backgroundClip: keywordWithGlobal('background-clip'),
backgroundOrigin: keyword('background-origin'),

View File

@@ -38,7 +38,7 @@ var compactable = {
'animation': {
canOverride: canOverride.generic.components([
canOverride.generic.time,
canOverride.property.animationTimingFunction,
canOverride.generic.timingFunction,
canOverride.generic.time,
canOverride.property.animationIterationCount,
canOverride.property.animationDirection,
@@ -159,7 +159,7 @@ var compactable = {
]
},
'animation-timing-function': {
canOverride: canOverride.property.animationTimingFunction,
canOverride: canOverride.generic.timingFunction,
componentOf: [
'animation'
],
@@ -918,6 +918,82 @@ var compactable = {
'-webkit-'
]
},
'transition': {
breakUp: breakUp.multiplex(breakUp.transition),
canOverride: canOverride.generic.components([
canOverride.property.transitionProperty,
canOverride.generic.time,
canOverride.generic.timingFunction,
canOverride.generic.time
]),
components: [
'transition-property',
'transition-duration',
'transition-timing-function',
'transition-delay'
],
defaultValue: 'none',
restore: restore.multiplex(restore.withoutDefaults),
shorthand: true,
vendorPrefixes: [
'-moz-',
'-o-',
'-webkit-'
]
},
'transition-delay': {
canOverride: canOverride.generic.time,
componentOf: [
'transition'
],
defaultValue: '0s',
intoMultiplexMode: 'real',
vendorPrefixes: [
'-moz-',
'-o-',
'-webkit-'
]
},
'transition-duration': {
canOverride: canOverride.generic.time,
componentOf: [
'transition'
],
defaultValue: '0s',
intoMultiplexMode: 'real',
vendorPrefixes: [
'-moz-',
'-o-',
'-webkit-'
]
},
'transition-property': {
canOverride: canOverride.generic.propertyName,
componentOf: [
'transition'
],
defaultValue: 'all',
intoMultiplexMode: 'placeholder',
placeholderValue: '_', // it's a short value that won't match any property and still be a valid `transition-property`
vendorPrefixes: [
'-moz-',
'-o-',
'-webkit-'
]
},
'transition-timing-function': {
canOverride: canOverride.generic.timingFunction,
componentOf: [
'transition'
],
defaultValue: 'ease',
intoMultiplexMode: 'real',
vendorPrefixes: [
'-moz-',
'-o-',
'-webkit-'
]
},
'vertical-align': {
canOverride: canOverride.property.verticalAlign,
defaultValue: 'baseline'

View File

@@ -8,7 +8,6 @@ var sameVendorPrefixesIn = require('./vendor-prefixes').same;
var compactable = require('../compactable');
var deepClone = require('../clone').deep;
var deepClone = require('../clone').deep;
var restoreWithComponents = require('../restore-with-components');
var shallowClone = require('../clone').shallow;
@@ -95,10 +94,11 @@ function turnShorthandValueIntoMultiplex(property, size) {
}
function turnLonghandValueIntoMultiplex(property, size) {
var withRealValue = compactable[property.name].intoMultiplexMode == 'real';
var withValue = withRealValue ?
var descriptor = compactable[property.name];
var withRealValue = descriptor.intoMultiplexMode == 'real';
var withValue = descriptor.intoMultiplexMode == 'real' ?
property.value.slice(0) :
compactable[property.name].defaultValue;
(descriptor.intoMultiplexMode == 'placeholder' ? descriptor.placeholderValue : descriptor.defaultValue);
var i = multiplexSize(property);
var j;
var m = withValue.length;

View File

@@ -3,21 +3,24 @@ var functionVendorRegexStr = '\\-(\\-|[A-Z]|[0-9])+\\(.*?\\)';
var variableRegexStr = 'var\\(\\-\\-[^\\)]+\\)';
var functionAnyRegexStr = '(' + variableRegexStr + '|' + functionNoVendorRegexStr + '|' + functionVendorRegexStr + ')';
var animationTimingFunctionRegex = /^(cubic\-bezier|steps)\([^\)]+\)$/;
var calcRegex = new RegExp('^(\\-moz\\-|\\-webkit\\-)?calc\\([^\\)]+\\)$', 'i');
var decimalRegex = /[0-9]/;
var functionAnyRegex = new RegExp('^' + functionAnyRegexStr + '$', 'i');
var hslColorRegex = /^hsl\(\s{0,31}[\-\.]?\d+\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+%\s{0,31}\)|hsla\(\s{0,31}[\-\.]?\d+\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+%\s{0,31},\s{0,31}\.?\d+\s{0,31}\)$/;
var identifierRegex = /^(\-[a-z0-9_][a-z0-9\-_]*|[a-z][a-z0-9\-_]*)$/i;
var longHexColorRegex = /^#[0-9a-f]{6}$/i;
var namedEntityRegex = /^[a-z]+$/i;
var prefixRegex = /^-([a-z0-9]|-)*$/i;
var rgbColorRegex = /^rgb\(\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31}\)|rgba\(\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\d]{1,3}\s{0,31},\s{0,31}[\.\d]+\s{0,31}\)$/;
var shortHexColorRegex = /^#[0-9a-f]{3}$/i;
var timingFunctionRegex = /^(cubic\-bezier|steps)\([^\)]+\)$/;
var validTimeUnits = ['ms', 's'];
var urlRegex = /^url\([\s\S]+\)$/i;
var variableRegex = new RegExp('^' + variableRegexStr + '$', 'i');
var eightValueColorRegex = /^#[0-9a-f]{8}$/i;
var fourValueColorRegex = /^#[0-9a-f]{4}$/i;
var sixValueColorRegex = /^#[0-9a-f]{6}$/i;
var threeValueColorRegex = /^#[0-9a-f]{3}$/i;
var DECIMAL_DOT = '.';
var MINUS_SIGN = '-';
var PLUS_SIGN = '+';
@@ -41,6 +44,15 @@ var Keywords = {
'ridge',
'solid'
],
'*-timing-function': [
'ease',
'ease-in',
'ease-in-out',
'ease-out',
'linear',
'step-end',
'step-start'
],
'animation-direction': [
'alternate',
'alternate-reverse',
@@ -63,15 +75,6 @@ var Keywords = {
'paused',
'running'
],
'animation-timing-function': [
'ease',
'ease-in',
'ease-in-out',
'ease-out',
'linear',
'step-end',
'step-start'
],
'background-attachment': [
'fixed',
'inherit',
@@ -342,14 +345,6 @@ var Units = [
'vw'
];
function isAnimationTimingFunction() {
var isTimingFunctionKeyword = isKeyword('animation-timing-function');
return function (value) {
return isTimingFunctionKeyword(value) || animationTimingFunctionRegex.test(value);
};
}
function isColor(value) {
return value != 'auto' &&
(
@@ -373,7 +368,7 @@ function isFunction(value) {
}
function isHexColor(value) {
return shortHexColorRegex.test(value) || longHexColorRegex.test(value);
return threeValueColorRegex.test(value) || fourValueColorRegex.test(value) || sixValueColorRegex.test(value) || eightValueColorRegex.test(value);
}
function isHslColor(value) {
@@ -426,6 +421,14 @@ function isTime(value) {
numberUpTo > -1 && validTimeUnits.indexOf(value.slice(numberUpTo + 1)) > -1;
}
function isTimingFunction() {
var isTimingFunctionKeyword = isKeyword('*-timing-function');
return function (value) {
return isTimingFunctionKeyword(value) || timingFunctionRegex.test(value);
};
}
function isUnit(validUnits, value) {
var numberUpTo = scanForNumber(value);
@@ -484,7 +487,7 @@ function validator(compatibility) {
isAnimationIterationCountKeyword: isKeyword('animation-iteration-count'),
isAnimationNameKeyword: isKeyword('animation-name'),
isAnimationPlayStateKeyword: isKeyword('animation-play-state'),
isAnimationTimingFunction: isAnimationTimingFunction(),
isTimingFunction: isTimingFunction(),
isBackgroundAttachmentKeyword: isKeyword('background-attachment'),
isBackgroundClipKeyword: isKeyword('background-clip'),
isBackgroundOriginKeyword: isKeyword('background-origin'),