remove "remove trailing zeros"

add new option "selector_separator" which determines which string should separate selectors, e.g. setting it to '\n' results in 'a,\nbr', setting it to ' ' in 'a, br'
This commit is contained in:
Thorben Bochenek 2013-10-15 10:57:01 +02:00
parent 93604b1b31
commit 51ac378392
2 changed files with 31 additions and 21 deletions

View File

@ -33,17 +33,17 @@ class BeautifierOptions:
def __init__(self):
self.indent_size = 4
self.indent_char = ' '
self.remove_trailing_zero = True
self.selector_separator = '\n'
self.end_with_newline = False
def __repr__(self):
return \
"""indent_size = %d
indent_char = [%s]
remove_trailing_zero = [%s]
separate_selectors = [%s]
end_with_newline = [%s]
""" % (self.indent_size, self.indent_char,
self.remove_trailing_zero, self.end_with_newline)
self.separate_selectors, self.end_with_newline)
def default_options():
@ -207,6 +207,7 @@ class Beautifier:
indentString = m.group(0)
printer = Printer(self.indentChar, self.indentSize, indentString)
insideRule = False
while True:
isAfterSpace = self.skipWhitespace()
@ -229,9 +230,11 @@ class Beautifier:
elif self.ch == '}':
printer.outdent()
printer.closeBracket()
insideRule = False
elif self.ch == ":":
self.eatWhitespace()
printer.colon()
insideRule = True
elif self.ch == '"' or self.ch == '\'':
printer.push(self.eatString(self.ch))
elif self.ch == ';':
@ -257,17 +260,16 @@ class Beautifier:
elif self.ch == ',':
self.eatWhitespace()
printer.push(self.ch)
printer.singleSpace()
if insideRule:
printer.singleSpace()
else:
printer.push(self.opts.selector_separator)
elif self.ch == ']':
printer.push(self.ch)
elif self.ch == '[' or self.ch == '=':
# no whitespace before or after
self.eatWhitespace()
printer.push(self.ch)
elif self.opts.remove_trailing_zero and \
self.ch == "0" and self.peek() == "." and \
(self.lookBack(":") or self.lookBack(" ")):
pass # skip dot
else:
if isAfterSpace:
printer.singleSpace()

View File

@ -4,45 +4,53 @@ import cssbeautifier
class CSSBeautifierTest(unittest.TestCase):
options = cssbeautifier.default_options()
options.indent_size = 1
options.indent_char = '\t'
options.end_with_newline = True
def resetOptions(self):
self.options = cssbeautifier.default_options()
self.options.indent_size = 1
self.options.indent_char = '\t'
self.options.selector_separator = '\n'
self.options.end_with_newline = True
def testBasics(self):
self.resetOptions()
t = self.decodesto
t("", "\n")
t(".tabs{}", ".tabs {}\n")
t(".tabs{color:red}", ".tabs {\n\tcolor: red\n}\n")
t(".tabs{color:rgb(255, 255, 0)}", ".tabs {\n\tcolor: rgb(255, 255, 0)\n}\n")
t(".tabs{background:url('back.jpg')}", ".tabs {\n\tbackground: url('back.jpg')\n}\n")
t("#bla, #foo{color:red}", "#bla, #foo {\n\tcolor: red\n}\n")
t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}\n")
t("@media print {.tab{}}", "@media print {\n\t.tab {}\n}\n")
def testComments(self):
self.resetOptions()
t = self.decodesto
t("/* test */", "/* test */\n")
t(".tabs{/* test */}", ".tabs {\n\t/* test */\n}\n")
t("/* header */.tabs {}", "/* header */\n.tabs {}\n")
def testLeadingZero(self):
def testSeperateSelectors(self):
self.resetOptions()
t = self.decodesto
t(".tabs{opacity: 0.9}", ".tabs {\n\topacity: .9\n}\n")
t(".tabs{opacity:0.9}", ".tabs {\n\topacity: .9\n}\n")
t(".tabs{padding: 10.3px}", ".tabs {\n\tpadding: 10.3px\n}\n")
t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}\n")
t("a, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}\n")
def testSpaceIndent(self):
t = self.decodesto
def testOptions(self):
self.resetOptions()
self.options.indent_size = 2
self.options.indent_char = ' '
self.options.selector_separator = ' '
t = self.decodesto
t("#bla, #foo{color:red}", "#bla, #foo {\n color: red\n}\n")
t("#bla, #foo{color:green}", "#bla, #foo {\n color: green\n}\n")
t("@media print {.tab{}}", "@media print {\n .tab {}\n}\n")
t("#bla, #foo{color:black}", "#bla, #foo {\n color: black\n}\n")
def decodesto(self, input, expectation=None):
self.assertEqual(