diff --git a/.gitignore b/.gitignore index e61051f3..1dd46ecb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ /coverage/ +.idea diff --git a/lib/replace/Percentage.js b/lib/replace/Percentage.js new file mode 100644 index 00000000..e7886632 --- /dev/null +++ b/lib/replace/Percentage.js @@ -0,0 +1,82 @@ +var packNumber = require('./Number').pack; +var PERCENTAGE_LENGTH_PROPERTY = { + 'margin': true, + 'margin-top': true, + 'margin-left': true, + 'margin-bottom': true, + 'margin-right': true, + + 'padding': true, + 'padding-top': true, + 'padding-left': true, + 'padding-bottom': true, + 'padding-right': true, + + 'top': true, + 'left': true, + 'botton': true, + 'right': true, + 'position': true, + + '-webkit-mask-box-image': true, + '-webkit-mask-position-x': true, + '-webkit-mask-position-y': true, + + 'background-position-x': true, + 'background-position-y': true, + + 'border': true, + 'border-width': true, + 'border-top-width': true, + 'border-left-width': true, + 'border-bottom-width': true, + 'border-right-width': true, + 'border-image-width': true, + + 'border-radius': true, + 'border-bottom-left-radius': true, + 'border-bottom-right-radius': true, + 'border-top-left-radius': true, + 'border-top-right-radius': true, + + 'font-size': true, + 'grid-column-gap': true, + 'grid-row-gap': true, + 'letter-spacing': true, + 'offset-distance': true, + 'scroll-snap-points-x': true, + 'scroll-snap-points-y': true, + 'shape-margin': true, + 'text-indent': true, + 'transform-origin': true, + 'word-spacing': true +}; +var PERCENTAGE_LENGTH_FUNCTION = { + 'inset': true, + 'polygon': true, + 'translate': true, + 'translate3d': true, + 'translateX': true, + 'translateY': true +}; + +module.exports = function compressPercentage(node, item) { + var value = packNumber(node.value, item); + var decl = this.declaration !== null ? this.declaration.property : null; + var fnName = this['function'] !== null ? this['function'].name : null; + + node.value = value; + + if ( + (decl !== null && PERCENTAGE_LENGTH_PROPERTY.hasOwnProperty(decl)) || + (fnName !== null && PERCENTAGE_LENGTH_FUNCTION.hasOwnProperty(fnName)) + ) { + if (value === '0') { + item.data = { + type: 'Number', + loc: node.loc, + value: value + }; + } + } +}; diff --git a/lib/replace/index.js b/lib/replace/index.js index 72b867ad..d9007d45 100644 --- a/lib/replace/index.js +++ b/lib/replace/index.js @@ -4,7 +4,7 @@ var handlers = { AttributeSelector: require('./AttributeSelector'), Value: require('./Value'), Dimension: require('./Dimension'), - Percentage: require('./Number'), + Percentage: require('./Percentage'), Number: require('./Number'), String: require('./String'), Url: require('./Url'), diff --git a/test/fixture/compress/restructure.margin/4.css b/test/fixture/compress/restructure.margin/4.css new file mode 100644 index 00000000..ee37bced --- /dev/null +++ b/test/fixture/compress/restructure.margin/4.css @@ -0,0 +1,6 @@ +.a { + margin: 0% +} +.b { + margin: 0% 0px 0em 0rem +} diff --git a/test/fixture/compress/restructure.margin/4.min.css b/test/fixture/compress/restructure.margin/4.min.css new file mode 100644 index 00000000..904b8858 --- /dev/null +++ b/test/fixture/compress/restructure.margin/4.min.css @@ -0,0 +1 @@ +.a,.b{margin:0}