-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix wrong percentage sign removal for zero values (fixes #395)
- Loading branch information
Showing
3 changed files
with
34 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.