diff --git a/js/lib/beautify-css.js b/js/lib/beautify-css.js index 1e3a5c14a..26194725c 100644 --- a/js/lib/beautify-css.js +++ b/js/lib/beautify-css.js @@ -78,6 +78,7 @@ var pos = -1, ch; + var parenLevel = 0; function next() { ch = source_text.charAt(++pos); @@ -120,7 +121,7 @@ function eatWhitespace() { var result = ''; while (whiteRe.test(peek())) { - next() + next(); result += ch; } return result; @@ -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() === "/") { @@ -230,7 +231,7 @@ } }; - + var output = []; if (basebaseIndentString) { output.push(basebaseIndentString); @@ -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; @@ -354,6 +355,7 @@ } } } else { + parenLevel++; if (isAfterSpace) { print.singleSpace(); } @@ -362,10 +364,11 @@ } } else if (ch === ')') { output.push(ch); + parenLevel--; } else if (ch === ',') { output.push(ch); eatWhitespace(); - if (!insideRule && selectorSeparatorNewline) { + if (!insideRule && selectorSeparatorNewline && parenLevel < 1) { print.newLine(); } else { print.singleSpace(); diff --git a/js/test/beautify-css-tests.js b/js/test/beautify-css-tests.js index d51981ecd..0a1a81d53 100644 --- a/js/test/beautify-css-tests.js +++ b/js/test/beautify-css-tests.js @@ -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 diff --git a/python/cssbeautifier/__init__.py b/python/cssbeautifier/__init__.py index 7debc732a..937607910 100644 --- a/python/cssbeautifier/__init__.py +++ b/python/cssbeautifier/__init__.py @@ -268,6 +268,7 @@ def beautify(self): enteringConditionalGroup = False top_ch = '' last_top_ch = '' + parenLevel = 0 while True: whitespace = self.skipWhitespace(); @@ -377,16 +378,18 @@ def beautify(self): else: self.pos = self.pos - 1 else: + parenLevel += 1 if isAfterSpace: printer.singleSpace() printer.push(self.ch) self.eatWhitespace() elif self.ch == ')': printer.push(self.ch) + parenLevel -= 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 parenLevel < 1: printer.newLine() else: printer.singleSpace() @@ -412,4 +415,4 @@ def beautify(self): if self.opts.end_with_newline: sweet_code += "\n" - return sweet_code \ No newline at end of file + return sweet_code diff --git a/python/cssbeautifier/tests/test.py b/python/cssbeautifier/tests/test.py index 07ab8f531..4993da771 100644 --- a/python/cssbeautifier/tests/test.py +++ b/python/cssbeautifier/tests/test.py @@ -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}') + # diff --git a/test/data/css.js b/test/data/css.js index d610ddac3..5f124fc9b 100644 --- a/test/data/css.js +++ b/test/data/css.js @@ -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}' } + ], + }, { }] }