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

Partial fix https://github.com/morishitter/stylefmt/issues/86 #320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/formatDecls.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ function formatDecls (rule, indent, indentWidth, stylelint) {
decl.prop = decl.raws.before.trim().replace(/\n/g, '') + decl.prop
}

decl.raws.between = ': '
if (decl.raws.between.indexOf('\n') !== -1) {
decl.raws.between = ':\n' + indent + indentWidth.repeat(2)
} else {
decl.raws.between = ': '
}
decl.raws.before = declarationEmptyLineBefore(stylelint, decl, indent, indentWidth, isSingleLine)

if (getProperty(stylelint, 'declaration-colon-space-before')) {
Expand All @@ -33,7 +37,7 @@ function formatDecls (rule, indent, indentWidth, stylelint) {
decl.raws.between = declarationColonSpaceAfter(stylelint, decl.raws.between)
}

formatValues(decl, stylelint)
formatValues(decl, indent, indentWidth, stylelint)
})
}

Expand Down
30 changes: 25 additions & 5 deletions lib/formatValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ var formatTransforms = require('./formatTransforms')
var formatColors = require('./formatColors')
var formatZeros = require('./formatZeros')
var formatShorthand = require('./formatShorthand')
var valueParser = require('postcss-value-parser')
var getProperty = require('./util').getProperty

function formatvalues (decl, stylelint) {
function formatValues (decl, indent, indentWidth, stylelint) {
var isDataUrl = /data:.+\/(.+);base64,(.*)/.test(decl.value)
var isVarNotation = /var\s*\(.*\)/.test(decl.value)
var isString = /^("|').*("|')$/.test(decl.value)
Expand All @@ -15,7 +16,25 @@ function formatvalues (decl, stylelint) {
}

if (!isString) {
decl.value = decl.value.trim().replace(/\s+/g, ' ')
var parsedValue = valueParser(decl.value)
decl.value = ''
parsedValue.nodes.forEach(function (node) {
var value = valueParser.stringify(node)
value = value.replace(/\s+/g, ' ')
if (node.before && node.before.indexOf('\n') !== -1) {
decl.value += '\n'
}
decl.value += value
if (node.after && node.after.indexOf('\n') !== -1) {
decl.value += '\n'
}
})

decl.value = decl.value.trim().replace(/[^\S\n]+/g, ' ')
decl.value = decl.value.replace(/\n /, '\n')
decl.value = decl.value.replace(/ \n/, '\n')
decl.value = decl.value.replace(
/\n+/g, '\n' + indent + indentWidth.repeat(2))
}

switch (getProperty(stylelint, 'string-quotes')) {
Expand All @@ -32,13 +51,14 @@ function formatvalues (decl, stylelint) {
}

if (decl.prop === 'font-family') {
decl.value = decl.value.trim().replace(/\s+,\s/g, ', ')
decl.value = decl.value.trim().replace(/[^\S\n]+,[^\S\n]/g, ', ')
return decl
}

if (!isDataUrl) {
// Remove spaces before commas and keep only one space after.
decl.value = decl.value.trim().replace(/(\s+)?,(\s)*/g, ', ')
decl.value = decl.value.trim().replace(/([^\S\n]+)?,[^\S\n]*/g, ',')
decl.value = decl.value.replace(/,([^\n])/g, ', $1')
}

if (isVarNotation) {
Expand Down Expand Up @@ -75,4 +95,4 @@ function formatvalues (decl, stylelint) {
}


module.exports = formatvalues
module.exports = formatValues
8 changes: 6 additions & 2 deletions test/stylelint/extends/extends.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@

@media screen and (min-resolution: 192dpi), screen and (min-resolution: 2dppx) {
.selector {
background-image: repeating-linear-gradient(-45deg, transparent, #fff 25px, rgba(255, 255, 255, 1) 50px);
background-image:
repeating-linear-gradient(-45deg, transparent, #fff 25px, rgba(255, 255, 255, 1) 50px);
margin: 10px;
margin-bottom: 5px;
box-shadow: 0 1px 1px #000, 0 1px 0 #fff, 2px 2px 1px 1px #ccc inset;
box-shadow:
0 1px 1px #000,
0 1px 0 #fff,
2px 2px 1px 1px #ccc inset;
height: 10rem;
}
/* Flush nested single line comment */
Expand Down
8 changes: 6 additions & 2 deletions test/stylelint/extends2/extends2.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@

@media screen and (min-resolution: 192dpi), screen and (min-resolution: 2dppx) {
.selector {
background-image: repeating-linear-gradient(-45deg, transparent, #fff 25px, rgba(255, 255, 255, 1) 50px);
background-image:
repeating-linear-gradient(-45deg, transparent, #fff 25px, rgba(255, 255, 255, 1) 50px);
margin: 10px;
margin-bottom: 5px;
box-shadow: 0 1px 1px #000, 0 1px 0 #fff, 2px 2px 1px 1px #ccc inset;
box-shadow:
0 1px 1px #000,
0 1px 0 #fff,
2px 2px 1px 1px #ccc inset;
height: 10rem;
}
/* Flush nested single line comment */
Expand Down
8 changes: 6 additions & 2 deletions test/stylelint/extends3/extends3.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@

@media screen and (min-resolution: 192dpi), screen and (min-resolution: 2dppx) {
.selector {
background-image: repeating-linear-gradient(-45deg, transparent, #fff 25px, rgba(255, 255, 255, 1) 50px);
background-image:
repeating-linear-gradient(-45deg, transparent, #fff 25px, rgba(255, 255, 255, 1) 50px);
margin: 10px;
margin-bottom: 5px;
box-shadow: 0 1px 1px #000, 0 1px 0 #fff, 2px 2px 1px 1px #ccc inset;
box-shadow:
0 1px 1px #000,
0 1px 0 #fff,
2px 2px 1px 1px #ccc inset;
height: 10rem;
}
/* Flush nested single line comment */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@media print {
a {
background-position: top left, top right;
background-position: top left,
top right;
}
}
3 changes: 2 additions & 1 deletion test/stylelint/indentation-2space/indentation-2space.out.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@media print {
a {
background-position: top left, top right;
background-position: top left,
top right;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@media print {
a {
background-position: top left, top right;
background-position: top left,
top right;
}
}
3 changes: 2 additions & 1 deletion test/stylelint/indentation-4space/indentation-4space.out.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@media print {
a {
background-position: top left, top right;
background-position: top left,
top right;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@media print {
a {
background-position: top left, top right;
background-position: top left,
top right;
}
}
3 changes: 2 additions & 1 deletion test/stylelint/indentation-tab/indentation-tab.out.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@media print {
a {
background-position: top left, top right;
background-position: top left,
top right;
}
}