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 7 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
80 changes: 77 additions & 3 deletions lib/replace/Dimension.js
Expand Up @@ -21,13 +21,87 @@ var LENGTH_UNIT = {
'vmax': true,
'vm': true
};
var PERCENTAGE_LENGTH_DELC = {
'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_FN = {
'inset': true,
'polygon': true,
'translate': true,
'translate3d': true,
'translateX': true,
'translateY': true
};

module.exports = function compressDimension(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 (value === '0' && this.declaration !== null && this.atrulePrelude === null) {
if (node.type === 'Percentage') {
if (PERCENTAGE_LENGTH_DELC.hasOwnProperty(decl) || PERCENTAGE_LENGTH_FN.hasOwnProperty(fnName)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lahmatiy оно?

// Zero-value workaround
if (value === '0') {
item.data = {
type: 'Number',
loc: node.loc,
value: value
};
}
}
} else if (value === '0' && decl !== null && this.atrulePrelude === null) {
var unit = node.unit.toLowerCase();

// only length values can be compressed
Expand All @@ -36,12 +110,12 @@ module.exports = function compressDimension(node, item) {
}

// issue #200: don't remove units in flex property as it could change value meaning
if (this.declaration.property === 'flex') {
if (decl === 'flex') {
return;
}

// issue #222: don't remove units inside calc
if (this['function'] && this['function'].name === 'calc') {
if (fnName === 'calc') {
return;
}

Expand Down
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('./Dimension'),
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}