Skip to content

Commit

Permalink
Make @page pseudo-class format correctly
Browse files Browse the repository at this point in the history
Fixes #661
  • Loading branch information
bitwiseman committed Apr 1, 2015
1 parent d8fe5ac commit d5da0ad
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
18 changes: 11 additions & 7 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,24 @@
output.push(ch);

// strip trailing space, if present, for hash property checks
var variableOrRule = peekString(": ,;{}()[]/='\"").replace(/\s$/, '');
var variableOrRule = peekString(": ,;{}()[]/='\"");

if (variableOrRule.match(/[ :]$/)) {
// we have a variable or pseudo-class, add it and insert one space before continuing
next();
variableOrRule = eatString(": ").replace(/\s$/, '');
output.push(variableOrRule);
print.singleSpace();
}

variableOrRule = variableOrRule.replace(/\s$/, '')

// might be a nesting at-rule
if (variableOrRule in css_beautify.NESTED_AT_RULE) {
nestedLevel += 1;
if (variableOrRule in css_beautify.CONDITIONAL_GROUP_RULE) {
enteringConditionalGroup = true;
}
} else if (': '.indexOf(variableOrRule[variableOrRule.length - 1]) >= 0) {
//we have a variable, add it and insert one space before continuing
next();
variableOrRule = eatString(": ").replace(/\s$/, '');
output.push(variableOrRule);
print.singleSpace();
}
} else if (ch === '{') {
if (peek(true) === '}') {
Expand Down
7 changes: 7 additions & 0 deletions js/test/beautify-css-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
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}');

// Psuedo-classes vs Variables
t('@page :first {}');

// Assume the colon goes with the @name. If we're in LESS, this is required regardless of the at-string.
t('@page:first {}', '@page: first {}');
t('@page: first {}');

//

// test basic css beautifier
Expand Down
19 changes: 10 additions & 9 deletions python/cssbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,24 @@ def beautify(self):

# strip trailing space, if present, for hash property check
variableOrRule = self.peekString(": ,;{}()[]/='\"")
if variableOrRule[-1].isspace():
variableOrRule = variableOrRule[:-1]

# might be a nesting at-rule
if variableOrRule in self.NESTED_AT_RULE:
printer.nestedLevel += 1
if variableOrRule in self.CONDITIONAL_GROUP_RULE:
enteringConditionalGroup = True
elif variableOrRule[-1] in ": ":
# we have a variable, add it and a space after
if variableOrRule[-1] in ": ":
# wwe have a variable or pseudo-class, add it and insert one space before continuing
self.next()
variableOrRule = self.eatString(": ")
if variableOrRule[-1].isspace():
variableOrRule = variableOrRule[:-1]
printer.push(variableOrRule)
printer.singleSpace();

if variableOrRule[-1].isspace():
variableOrRule = variableOrRule[:-1]

# might be a nesting at-rule
if variableOrRule in self.NESTED_AT_RULE:
printer.nestedLevel += 1
if variableOrRule in self.CONDITIONAL_GROUP_RULE:
enteringConditionalGroup = True

elif self.ch == '{':
if self.peek(True) == '}':
Expand Down
7 changes: 7 additions & 0 deletions python/cssbeautifier/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def testGenerated(self):
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}')

# Psuedo-classes vs Variables
t('@page :first {}')

# Assume the colon goes with the @name. If we're in LESS, this is required regardless of the at-string.
t('@page:first {}', '@page: first {}')
t('@page: first {}')

#


Expand Down
10 changes: 10 additions & 0 deletions test/data/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ exports.test_data = {
{ 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}' }
],
}, {
name: "Psuedo-classes vs Variables",
description: "",
tests: [
{ unchanged: '@page :first {}' },
{ comment: "Assume the colon goes with the @name. If we're in LESS, this is required regardless of the at-string.",
input: '@page:first {}',
output: '@page: first {}' },
{ unchanged: '@page: first {}' }
],
}, {

}]
}

0 comments on commit d5da0ad

Please sign in to comment.