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

add support to less functions paramters braces #568

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
17 changes: 10 additions & 7 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

var pos = -1,
ch;
var roundBraceLevel = 0;

function next() {
ch = source_text.charAt(++pos);
Expand Down Expand Up @@ -120,7 +121,7 @@
function eatWhitespace() {
var result = '';
while (whiteRe.test(peek())) {
next()
next();
result += ch;
}
return result;
Expand All @@ -132,14 +133,14 @@
result = ch;
}
while (whiteRe.test(next())) {
result += ch
result += ch;
}
return result;
}

function eatComment(singleLine) {
var start = pos;
var singleLine = peek() === "/";
singleLine = peek() === "/";
next();
while (next()) {
if (!singleLine && ch === "*" && peek() === "/") {
Expand Down Expand Up @@ -230,7 +231,7 @@
}
};


var output = [];
if (basebaseIndentString) {
output.push(basebaseIndentString);
Expand All @@ -246,8 +247,8 @@
var whitespace = skipWhitespace();
var isAfterSpace = whitespace !== '';
var isAfterNewline = whitespace.indexOf('\n') !== -1;
var last_top_ch = top_ch;
var top_ch = ch;
last_top_ch = top_ch;
top_ch = ch;

if (!ch) {
break;
Expand Down Expand Up @@ -354,6 +355,7 @@
}
}
} else {
roundBraceLevel++;
if (isAfterSpace) {
print.singleSpace();
}
Expand All @@ -362,10 +364,11 @@
}
} else if (ch === ')') {
output.push(ch);
roundBraceLevel--;
} else if (ch === ',') {
output.push(ch);
eatWhitespace();
if (!insideRule && selectorSeparatorNewline) {
if (!insideRule && selectorSeparatorNewline && roundBraceLevel < 1) {
Copy link
Member

Choose a reason for hiding this comment

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

"Round brace"... do you mean parenthesis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

Copy link
Member

Choose a reason for hiding this comment

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

Let's call them that then. 😄
You can say call it parenLevel if you like. roundBraceLevel is odd.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok done

print.newLine();
} else {
print.singleSpace();
Expand Down
5 changes: 5 additions & 0 deletions js/test/beautify-css-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
t('.tabs { }', '.tabs {}');
t('.tabs \n{\n \n }', '.tabs {}');

// Functions braces
t('.tabs(){}', '.tabs() {}');
t('.tabs (pa, pa(1,2)), .cols { }', '.tabs (pa, pa(1, 2)),\n.cols {}');
t('.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}');

//

// test basic css beautifier
Expand Down
7 changes: 5 additions & 2 deletions python/cssbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def beautify(self):
enteringConditionalGroup = False
top_ch = ''
last_top_ch = ''
roundBraceLevel = 0

while True:
whitespace = self.skipWhitespace();
Expand Down Expand Up @@ -377,16 +378,18 @@ def beautify(self):
else:
self.pos = self.pos - 1
else:
roundBraceLevel += 1
if isAfterSpace:
printer.singleSpace()
printer.push(self.ch)
self.eatWhitespace()
elif self.ch == ')':
printer.push(self.ch)
roundBraceLevel -= 1
elif self.ch == ',':
printer.push(self.ch)
self.eatWhitespace()
if not insideRule and self.opts.selector_separator_newline:
if not insideRule and self.opts.selector_separator_newline and roundBraceLevel < 1:
printer.newLine()
else:
printer.singleSpace()
Expand All @@ -412,4 +415,4 @@ def beautify(self):
if self.opts.end_with_newline:
sweet_code += "\n"

return sweet_code
return sweet_code
5 changes: 5 additions & 0 deletions python/cssbeautifier/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def testGenerated(self):
t('.tabs { }', '.tabs {}')
t('.tabs \n{\n \n }', '.tabs {}')

# Functions braces
t('.tabs(){}', '.tabs() {}')
t('.tabs (pa, pa(1,2)), .cols { }', '.tabs (pa, pa(1, 2)),\n.cols {}')
t('.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}')

#


Expand Down
10 changes: 10 additions & 0 deletions test/data/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ exports.test_data = {
{ input: '.tabs \n{\n \n }', output: '.tabs {}' }
],
}, {
name: "Functions braces",
description: "",
tests: [
{ input: '.tabs(){}', output: '.tabs() {}' },
{ input: '.tabs (pa, pa(1,2)), .cols { }', output: '.tabs (pa, pa(1, 2)),\n.cols {}' },
// Still not supported to check if there are opening roundBracs after name
// { input: '.tabs ( ) { }', output: '.tabs() {}' },
{ input: '.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', output: '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}' }
],
}, {

}]
}