Skip to content

Commit

Permalink
Fix wrong percentage sign removal for zero values (fixes #395)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Oct 28, 2019
1 parent 27c8903 commit 522cc37
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 52 deletions.
73 changes: 24 additions & 49 deletions lib/replace/Percentage.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,30 @@
var lexer = require('css-tree').lexer;
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,
'bottom': true,
'right': true,

'background-position': true,
'background-position-x': true,
'background-position-y': true,
'background-size': 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
};
var blacklist = new Set([
// see https://github.com/jakubpawlowicz/clean-css/issues/957
'width',
'min-width',
'max-width',
'height',
'min-height',
'max-height'
]);

module.exports = function compressPercentage(node, item) {
var value = packNumber(node.value, item);
var property = this.declaration !== null ? this.declaration.property : null;

node.value = value;

if (property !== null && PERCENTAGE_LENGTH_PROPERTY.hasOwnProperty(property)) {
if (value === '0') {
item.data = {
type: 'Number',
loc: node.loc,
value: value
};
node.value = packNumber(node.value, item);

if (node.value === '0' && this.declaration && !blacklist.has(this.declaration.property)) {
// try to convert a number
item.data = {
type: 'Number',
loc: node.loc,
value: node.value
};

// that's ok only when new value matches on length
if (!lexer.matchDeclaration(this.declaration).isType(item.data, 'length')) {
// otherwise rollback changes
item.data = node;
}
}
};
11 changes: 9 additions & 2 deletions test/fixture/compress/issue/286.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
}

/* safe properties */
.test3 {
.test3 {
top: 0%;
bottom: 0%;
right: 0%;
left: 0%;

line-height: 0%;

background-position: 0%;
background-position-x: 0%;
background-position-y: 0%;
Expand Down Expand Up @@ -50,5 +52,10 @@

height: 0%;
min-height: 0%;
max-height: 0%
max-height: 0%;

flex: 0 0%;

/* issue #395 */
color: hsla(0, 0%, 82.4%, .8);
}
2 changes: 1 addition & 1 deletion test/fixture/compress/issue/286.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 522cc37

Please sign in to comment.