Skip to content

Commit

Permalink
Add support for less functions with parameters
Browse files Browse the repository at this point in the history
Closes #568
  • Loading branch information
lejenome authored and bitwiseman committed Mar 6, 2015
1 parent 6b90203 commit 2b13664
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
15 changes: 9 additions & 6 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

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

function next() {
ch = source_text.charAt(++pos);
Expand Down Expand Up @@ -123,7 +124,7 @@
function eatWhitespace() {
var result = '';
while (whiteRe.test(peek())) {
next()
next();
result += ch;
}
return result;
Expand All @@ -135,14 +136,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 @@ -249,8 +250,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 @@ -364,6 +365,7 @@
}
}
} else {
parenLevel++;
if (isAfterSpace) {
print.singleSpace();
}
Expand All @@ -372,10 +374,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();
Expand Down
10 changes: 10 additions & 0 deletions js/test/beautify-css-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
t('@font-face {\n\tfont-family: "Bitstream Vera Serif Bold";\n\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n}\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: "Helvetica Neue"\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url([email protected]);\n\t\t}\n\t}\n}');
t('a:first-child{color:red;div:first-child{color:black;}}\n.div{height:15px;}', 'a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}\n.div {\n\theight: 15px;\n}');

// Functions braces
t('.tabs(){}', '.tabs() {}');
t('.tabs (){}', '.tabs () {}');
t('.tabs (pa, pa(1,2)), .cols { }', '.tabs (pa, pa(1, 2)),\n.cols {}');
t('.tabs(pa, pa(1,2)), .cols { }', '.tabs(pa, pa(1, 2)),\n.cols {}');
t('.tabs ( ) { }', '.tabs () {}');
t('.tabs( ) { }', '.tabs() {}');
t('.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}');
t('.box-shadow(@shadow: 0 1px 3px rgba(0, 0, 0, .25)) {\n\t-webkit-box-shadow: @shadow;\n\t-moz-box-shadow: @shadow;\n\tbox-shadow: @shadow;\n}');

//

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

while True:
whitespace = self.skipWhitespace();
Expand Down Expand Up @@ -384,16 +385,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()
Expand Down
10 changes: 10 additions & 0 deletions python/cssbeautifier/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def testGenerated(self):
t('@font-face {\n\tfont-family: "Bitstream Vera Serif Bold";\n\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n}\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: "Helvetica Neue"\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url([email protected]);\n\t\t}\n\t}\n}')
t('a:first-child{color:red;div:first-child{color:black;}}\n.div{height:15px;}', 'a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}\n.div {\n\theight: 15px;\n}')

# Functions braces
t('.tabs(){}', '.tabs() {}')
t('.tabs (){}', '.tabs () {}')
t('.tabs (pa, pa(1,2)), .cols { }', '.tabs (pa, pa(1, 2)),\n.cols {}')
t('.tabs(pa, pa(1,2)), .cols { }', '.tabs(pa, pa(1, 2)),\n.cols {}')
t('.tabs ( ) { }', '.tabs () {}')
t('.tabs( ) { }', '.tabs() {}')
t('.tabs (t, t2) \n{\n key: val(p1 ,p2); \n }', '.tabs (t, t2) {\n\tkey: val(p1, p2);\n}')
t('.box-shadow(@shadow: 0 1px 3px rgba(0, 0, 0, .25)) {\n\t-webkit-box-shadow: @shadow;\n\t-moz-box-shadow: @shadow;\n\tbox-shadow: @shadow;\n}')

#


Expand Down
13 changes: 13 additions & 0 deletions test/data/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ exports.test_data = {
{ input: 'a:first-child{color:red;div:first-child{color:black;}}\n.div{height:15px;}', output: 'a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}\n{{separator}}.div {\n\theight: 15px;\n}'},
],
}, {
name: "Functions braces",
description: "",
tests: [
{ input: '.tabs(){}', output: '.tabs() {}' },
{ input: '.tabs (){}', output: '.tabs () {}' },
{ input: '.tabs (pa, pa(1,2)), .cols { }', output: '.tabs (pa, pa(1, 2)),\n.cols {}' },
{ input: '.tabs(pa, pa(1,2)), .cols { }', output: '.tabs(pa, pa(1, 2)),\n.cols {}' },
{ input: '.tabs ( ) { }', output: '.tabs () {}' },
{ 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}' },
{ input: '.box-shadow(@shadow: 0 1px 3px rgba(0, 0, 0, .25)) {\n\t-webkit-box-shadow: @shadow;\n\t-moz-box-shadow: @shadow;\n\tbox-shadow: @shadow;\n}' }
],
}, {

}]
}

0 comments on commit 2b13664

Please sign in to comment.