diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
commit | bbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch) | |
tree | c58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/clean-css/lib/optimizer | |
parent | 003fb34971cf63466184351b4db5f7c67df4f444 (diff) |
update packages
Diffstat (limited to 'node_modules/clean-css/lib/optimizer')
9 files changed, 337 insertions, 72 deletions
diff --git a/node_modules/clean-css/lib/optimizer/level-1/optimize.js b/node_modules/clean-css/lib/optimizer/level-1/optimize.js index 82cb9531d..13cfd8c52 100644 --- a/node_modules/clean-css/lib/optimizer/level-1/optimize.js +++ b/node_modules/clean-css/lib/optimizer/level-1/optimize.js @@ -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'; @@ -29,6 +31,7 @@ var DEFAULT_ROUNDING_PRECISION = require('../../options/rounding-precision').DEF var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/; var TIME_VALUE = /^(\-?[\d\.]+)(m?s)$/; +var HEX_VALUE_PATTERN = /[0-9a-f]/i; var PROPERTY_NAME_PATTERN = /^(?:\-chrome\-|\-[\w\-]+\w|\w[\w\-]+\w|\-\-\S+)$/; var IMPORT_PREFIX_PATTERN = /^@import/i; var QUOTED_PATTERN = /^('.*'|".*")$/; @@ -98,11 +101,15 @@ function optimizeColors(name, value, compatibility) { .replace(/hsl\((-?\d+),(-?\d+)%?,(-?\d+)%?\)/g, function (match, hue, saturation, lightness) { return shortenHsl(hue, saturation, lightness); }) - .replace(/(^|[^='"])#([0-9a-f]{6})($|[^0-9a-f])/gi, function (match, prefix, color, suffix) { - if (color[0] == color[1] && color[2] == color[3] && color[4] == color[5]) { - return (prefix + '#' + color[0] + color[2] + color[4]).toLowerCase() + suffix; + .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() + suffix; + return (prefix + '#' + color).toLowerCase(); } }) .replace(/(^|[^='"])#([0-9a-f]{3})/gi, function (match, prefix, color) { @@ -329,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; } @@ -344,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; @@ -358,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; @@ -403,7 +411,7 @@ function optimizeBody(properties, context) { } if (property.block) { - optimizeBody(property.value[0][1], context); + optimizeBody(rule, property.value[0][1], context); continue; } @@ -462,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; @@ -632,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: @@ -646,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; } diff --git a/node_modules/clean-css/lib/optimizer/level-1/tidy-rules.js b/node_modules/clean-css/lib/optimizer/level-1/tidy-rules.js index 0af3b2fe7..d046d0efd 100644 --- a/node_modules/clean-css/lib/optimizer/level-1/tidy-rules.js +++ b/node_modules/clean-css/lib/optimizer/level-1/tidy-rules.js @@ -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); diff --git a/node_modules/clean-css/lib/optimizer/level-2/break-up.js b/node_modules/clean-css/lib/optimizer/level-2/break-up.js index b48ccf23d..5301cb898 100644 --- a/node_modules/clean-css/lib/optimizer/level-2/break-up.js +++ b/node_modules/clean-css/lib/optimizer/level-2/break-up.js @@ -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) { @@ -299,6 +299,10 @@ function font(property, compactable, validator) { return components; } + if (values.length < 2 || !_anyIsFontSize(values, validator) || !_anyIsFontFamily(values, validator)) { + throw new InvalidPropertyError('Invalid font values at ' + formatPosition(property.all[property.position][1][2][0]) + '. Ignoring.'); + } + if (values.length > 1 && _anyIsInherit(values)) { throw new InvalidPropertyError('Invalid font values at ' + formatPosition(values[0][2][0]) + '. Ignoring.'); } @@ -377,6 +381,36 @@ function font(property, compactable, validator) { return components; } +function _anyIsFontSize(values, validator) { + var value; + var i, l; + + for (i = 0, l = values.length; i < l; i++) { + value = values[i]; + + if (validator.isFontSizeKeyword(value[1]) || validator.isUnit(value[1]) && !validator.isDynamicUnit(value[1]) || validator.isFunction(value[1])) { + return true; + } + } + + return false; +} + +function _anyIsFontFamily(values, validator) { + var value; + var i, l; + + for (i = 0, l = values.length; i < l; i++) { + value = values[i]; + + if (validator.isIdentifier(value[1])) { + return true; + } + } + + return false; +} + function fourValues(property, compactable) { var componentNames = compactable[property.name].components; var components = []; @@ -489,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 = [ @@ -558,5 +639,6 @@ module.exports = { fourValues: fourValues, listStyle: listStyle, multiplex: multiplex, - outline: widthStyleColor + outline: widthStyleColor, + transition: transition }; diff --git a/node_modules/clean-css/lib/optimizer/level-2/can-override.js b/node_modules/clean-css/lib/optimizer/level-2/can-override.js index 10d3bba17..3dae08f0e 100644 --- a/node_modules/clean-css/lib/optimizer/level-2/can-override.js +++ b/node_modules/clean-css/lib/optimizer/level-2/can-override.js @@ -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; @@ -191,6 +199,24 @@ function unitOrKeywordWithGlobal(propertyName) { }; } +function unitOrNumber(validator, value1, value2) { + if (!understandable(validator, value1, value2, 0, true) && !(validator.isUnit(value2) || validator.isNumber(value2))) { + return false; + } else if (validator.isVariable(value1) && validator.isVariable(value2)) { + return true; + } else if ((validator.isUnit(value1) || validator.isNumber(value1)) && !(validator.isUnit(value2) || validator.isNumber(value2))) { + return false; + } else if (validator.isUnit(value2) || validator.isNumber(value2)) { + return true; + } else if (validator.isUnit(value1) || validator.isNumber(value1)) { + return false; + } else if (validator.isFunction(value1) && !validator.isPrefixed(value1) && validator.isFunction(value2) && !validator.isPrefixed(value2)) { + return true; + } + + return sameFunctionOrValue(validator, value1, value2); +} + function zIndex(validator, value1, value2) { if (!understandable(validator, value1, value2, 0, true) && !validator.isZIndex(value2)) { return false; @@ -206,8 +232,11 @@ module.exports = { color: color, components: components, image: image, + propertyName: propertyName, time: time, - unit: unit + timingFunction: timingFunction, + unit: unit, + unitOrNumber: unitOrNumber }, property: { animationDirection: keywordWithGlobal('animation-direction'), @@ -215,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'), diff --git a/node_modules/clean-css/lib/optimizer/level-2/compactable.js b/node_modules/clean-css/lib/optimizer/level-2/compactable.js index 97e7e2aca..73f42a10e 100644 --- a/node_modules/clean-css/lib/optimizer/level-2/compactable.js +++ b/node_modules/clean-css/lib/optimizer/level-2/compactable.js @@ -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, @@ -99,6 +99,7 @@ var compactable = { ], defaultValue: '0s', intoMultiplexMode: 'real', + keepUnlessDefault: 'animation-delay', vendorPrefixes: [ '-moz-', '-o-', @@ -158,7 +159,7 @@ var compactable = { ] }, 'animation-timing-function': { - canOverride: canOverride.property.animationTimingFunction, + canOverride: canOverride.generic.timingFunction, componentOf: [ 'animation' ], @@ -680,7 +681,7 @@ var compactable = { defaultValue: 'auto' }, 'line-height': { - canOverride: canOverride.generic.unit, + canOverride: canOverride.generic.unitOrNumber, defaultValue: 'normal', shortestValue: '0' }, @@ -917,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' @@ -955,6 +1032,10 @@ function cloneDescriptor(propertyName, prefix) { }); } + if ('keepUnlessDefault' in clonedDescriptor) { + clonedDescriptor.keepUnlessDefault = prefix + clonedDescriptor.keepUnlessDefault; + } + return clonedDescriptor; } diff --git a/node_modules/clean-css/lib/optimizer/level-2/properties/override-properties.js b/node_modules/clean-css/lib/optimizer/level-2/properties/override-properties.js index 3749720c9..0f7b97a9f 100644 --- a/node_modules/clean-css/lib/optimizer/level-2/properties/override-properties.js +++ b/node_modules/clean-css/lib/optimizer/level-2/properties/override-properties.js @@ -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; diff --git a/node_modules/clean-css/lib/optimizer/level-2/remove-unused-at-rules.js b/node_modules/clean-css/lib/optimizer/level-2/remove-unused-at-rules.js index 7285991a4..798d3939f 100644 --- a/node_modules/clean-css/lib/optimizer/level-2/remove-unused-at-rules.js +++ b/node_modules/clean-css/lib/optimizer/level-2/remove-unused-at-rules.js @@ -8,6 +8,14 @@ var Token = require('../../tokenizer/token'); var animationNameRegex = /^(\-moz\-|\-o\-|\-webkit\-)?animation-name$/; var animationRegex = /^(\-moz\-|\-o\-|\-webkit\-)?animation$/; var keyframeRegex = /^@(\-moz\-|\-o\-|\-webkit\-)?keyframes /; +var importantRegex = /\s{0,31}!important$/; +var optionalMatchingQuotesRegex = /^(['"]?)(.*)\1$/; + +function normalize(value) { + return value + .replace(optionalMatchingQuotesRegex, '$2') + .replace(importantRegex, ''); +} function removeUnusedAtRules(tokens, context) { removeUnusedAtRule(tokens, matchCounterStyle, markCounterStylesAsUsed, context); @@ -107,7 +115,7 @@ function matchFontFace(token, atRules) { property = token[2][i]; if (property[1][1] == 'font-family') { - match = property[2][1].toLowerCase(); + match = normalize(property[2][1].toLowerCase()); atRules[match] = atRules[match] || []; atRules[match].push(token); break; @@ -134,7 +142,7 @@ function markFontFacesAsUsed(atRules) { component = wrappedProperty.components[6]; for (j = 0, m = component.value.length; j < m; j++) { - normalizedMatch = component.value[j][1].toLowerCase(); + normalizedMatch = normalize(component.value[j][1].toLowerCase()); if (normalizedMatch in atRules) { delete atRules[normalizedMatch]; @@ -146,7 +154,7 @@ function markFontFacesAsUsed(atRules) { if (property[1][1] == 'font-family') { for (j = 2, m = property.length; j < m; j++) { - normalizedMatch = property[j][1].toLowerCase(); + normalizedMatch = normalize(property[j][1].toLowerCase()); if (normalizedMatch in atRules) { delete atRules[normalizedMatch]; diff --git a/node_modules/clean-css/lib/optimizer/level-2/restore.js b/node_modules/clean-css/lib/optimizer/level-2/restore.js index 13f12e496..f9c2f0d33 100644 --- a/node_modules/clean-css/lib/optimizer/level-2/restore.js +++ b/node_modules/clean-css/lib/optimizer/level-2/restore.js @@ -264,8 +264,9 @@ function withoutDefaults(property, compactable) { var component = components[i]; var descriptor = compactable[component.name]; - if (component.value[0][1] != descriptor.defaultValue) + if (component.value[0][1] != descriptor.defaultValue || ('keepUnlessDefault' in descriptor) && !isDefault(components, compactable, descriptor.keepUnlessDefault)) { restored.unshift(component.value[0]); + } } if (restored.length === 0) @@ -277,6 +278,21 @@ function withoutDefaults(property, compactable) { return restored; } +function isDefault(components, compactable, propertyName) { + var component; + var i, l; + + for (i = 0, l = components.length; i < l; i++) { + component = components[i]; + + if (component.name == propertyName && component.value[0][1] == compactable[propertyName].defaultValue) { + return true; + } + } + + return false; +} + module.exports = { background: background, borderRadius: borderRadius, diff --git a/node_modules/clean-css/lib/optimizer/validator.js b/node_modules/clean-css/lib/optimizer/validator.js index cfccd0f9d..fd3fb97e4 100644 --- a/node_modules/clean-css/lib/optimizer/validator.js +++ b/node_modules/clean-css/lib/optimizer/validator.js @@ -3,20 +3,28 @@ 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*[\-\.\d]+\s*,\s*[\.\d]+%\s*,\s*[\.\d]+%\s*\)|hsla\(\s*[\-\.\d]+\s*,\s*[\.\d]+%\s*,\s*[\.\d]+%\s*,\s*[\.\d]+\s*\)$/; +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*[\d]{1,3}\s*,\s*[\d]{1,3}\s*,\s*[\d]{1,3}\s*\)|rgba\(\s*[\d]{1,3}\s*,\s*[\d]{1,3}\s*,\s*[\d]{1,3}\s*,\s*[\.\d]+\s*\)$/; -var shortHexColorRegex = /^#[0-9a-f]{3}$/i; -var timeRegex = new RegExp('^(\\-?\\+?\\.?\\d+\\.?\\d*(s|ms))$'); +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 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 = '+'; + var Keywords = { '^': [ 'inherit', @@ -36,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', @@ -58,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', @@ -337,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' && ( @@ -368,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) { @@ -394,7 +394,7 @@ function isNamedEntity(value) { } function isNumber(value) { - return value.length > 0 && ('' + parseFloat(value)) === value; + return scanForNumber(value) == value.length; } function isRgbColor(value) { @@ -415,11 +415,27 @@ function isVariable(value) { } function isTime(value) { - return timeRegex.test(value); + var numberUpTo = scanForNumber(value); + + return numberUpTo == value.length && parseInt(value) === 0 || + 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(compatibleCssUnitRegex, value) { - return compatibleCssUnitRegex.test(value); +function isUnit(validUnits, value) { + var numberUpTo = scanForNumber(value); + + return numberUpTo == value.length && parseInt(value) === 0 || + numberUpTo > -1 && validUnits.indexOf(value.slice(numberUpTo + 1)) > -1 || + value == 'auto' || + value == 'inherit'; } function isUrl(value) { @@ -432,13 +448,38 @@ function isZIndex(value) { isKeyword('^')(value); } +function scanForNumber(value) { + var hasDot = false; + var hasSign = false; + var character; + var i, l; + + for (i = 0, l = value.length; i < l; i++) { + character = value[i]; + + if (i === 0 && (character == PLUS_SIGN || character == MINUS_SIGN)) { + hasSign = true; + } else if (i > 0 && hasSign && (character == PLUS_SIGN || character == MINUS_SIGN)) { + return i - 1; + } else if (character == DECIMAL_DOT && !hasDot) { + hasDot = true; + } else if (character == DECIMAL_DOT && hasDot) { + return i - 1; + } else if (decimalRegex.test(character)) { + continue; + } else { + return i - 1; + } + } + + return i; +} + function validator(compatibility) { var validUnits = Units.slice(0).filter(function (value) { return !(value in compatibility.units) || compatibility.units[value] === true; }); - var compatibleCssUnitRegex = new RegExp('^(\\-?\\.?\\d+\\.?\\d*(' + validUnits.join('|') + '|)|auto|inherit)$', 'i'); - return { colorOpacity: compatibility.colors.opacity, isAnimationDirectionKeyword: isKeyword('animation-direction'), @@ -446,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'), @@ -471,12 +512,13 @@ function validator(compatibility) { isLineHeightKeyword: isKeyword('line-height'), isListStylePositionKeyword: isKeyword('list-style-position'), isListStyleTypeKeyword: isKeyword('list-style-type'), + isNumber: isNumber, isPrefixed: isPrefixed, isPositiveNumber: isPositiveNumber, isRgbColor: isRgbColor, isStyleKeyword: isKeyword('*-style'), isTime: isTime, - isUnit: isUnit.bind(null, compatibleCssUnitRegex), + isUnit: isUnit.bind(null, validUnits), isUrl: isUrl, isVariable: isVariable, isWidth: isKeyword('width'), |