Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#286: Remove percent sign for zero-values percentages when safe #361

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/node_modules/
/coverage/
.idea
82 changes: 82 additions & 0 deletions 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decl -> property

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
};
}
}
};
2 changes: 1 addition & 1 deletion lib/replace/index.js
Expand Up @@ -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'),
Expand Down
6 changes: 6 additions & 0 deletions test/fixture/compress/restructure.margin/4.css
@@ -0,0 +1,6 @@
.a {
margin: 0%
}
.b {
margin: 0% 0px 0em 0rem
}
1 change: 1 addition & 0 deletions test/fixture/compress/restructure.margin/4.min.css
@@ -0,0 +1 @@
.a,.b{margin:0}