Skip to content

Commit 564b84a

Browse files
committed
Added python implementation with tests
1 parent 9789983 commit 564b84a

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

python/cssbeautifier/__init__.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __repr__(self):
4242
indent_char = [%s]
4343
separate_selectors_newline = [%s]
4444
end_with_newline = [%s]
45-
""" % (self.indent_size, self.indent_char,
45+
""" % (self.indent_size, self.indent_char,
4646
self.separate_selectors, self.end_with_newline)
4747

4848

@@ -91,7 +91,7 @@ def __init__(self, indent_char, indent_size, default_indent=""):
9191

9292
def __lastCharWhitespace(self):
9393
return WHITE_RE.search(self.output[len(self.output) - 1]) is not None
94-
94+
9595
def indent(self):
9696
self.indentString += self.singleIndent
9797

@@ -129,7 +129,7 @@ def newLine(self, keepWhitespace=False):
129129

130130
if len(self.output) > 0:
131131
self.output.append("\n")
132-
132+
133133
if len(self.indentString) > 0:
134134
self.output.append(self.indentString)
135135

@@ -189,19 +189,28 @@ def skipWhitespace(self):
189189
pass
190190
return self.pos != start + 1
191191

192-
def eatComment(self):
192+
def eatComment(self, singleLine):
193193
start = self.pos
194194
self.next()
195195
while self.next():
196196
if self.ch == "*" and self.peek() == "/":
197197
self.pos = self.pos + 1
198198
break
199+
elif singleLine and self.ch == "\n":
200+
break
199201
return self.source_text[start:self.pos + 1]
200202

201203
def lookBack(self, string):
202204
past = self.source_text[self.pos - len(string):self.pos]
203205
return past.lower() == string
204206

207+
def isCommentOnLine(self):
208+
endOfLine = self.source_text.find('\n', self.pos)
209+
if endOfLine == -1:
210+
return False;
211+
restOfLine = self.source_text[self.pos:endOfLine]
212+
return restOfLine.find('//') != -1
213+
205214
def beautify(self):
206215
m = re.search("^[\r\n]*[\t ]*", self.source_text)
207216
indentString = m.group(0)
@@ -214,11 +223,14 @@ def beautify(self):
214223
if not self.ch:
215224
break
216225
elif self.ch == '/' and self.peek() == '*':
217-
comment = self.eatComment()
226+
comment = self.eatComment(False)
218227
printer.comment(comment)
219228
header = self.lookBack("")
220229
if header:
221230
printer.push("\n\n")
231+
elif self.ch == '/' and self.peek() == '/':
232+
printer.comment(self.eatComment(True)[0:-1])
233+
printer.newLine()
222234
elif self.ch == '{':
223235
self.eatWhitespace()
224236
if self.peek() == '}':
@@ -238,7 +250,14 @@ def beautify(self):
238250
elif self.ch == '"' or self.ch == '\'':
239251
printer.push(self.eatString(self.ch))
240252
elif self.ch == ';':
241-
printer.semicolon()
253+
if self.isCommentOnLine():
254+
beforeComment = self.eatString('/')
255+
comment = self.eatComment(True)
256+
printer.push(beforeComment)
257+
printer.push(comment[1:-1])
258+
printer.newLine()
259+
else:
260+
printer.semicolon()
242261
elif self.ch == '(':
243262
# may be a url
244263
if self.lookBack("url"):

python/cssbeautifier/tests/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def testComments(self):
3232
t(".tabs{/* test */}", ".tabs {\n\t/* test */\n}\n")
3333
t("/* header */.tabs {}", "/* header */\n\n.tabs {}\n")
3434

35+
#single line comment support (less/sass)
36+
t(".tabs{\n// comment\nwidth:10px;\n}", ".tabs {\n\t// comment\n\twidth: 10px;\n}\n")
37+
t(".tabs{// comment\nwidth:10px;\n}", ".tabs {\n\t// comment\n\twidth: 10px;\n}\n")
38+
t("//comment\n.tabs{width:10px;}", "//comment\n.tabs {\n\twidth: 10px;\n}\n")
39+
t(".tabs{//comment\n//2nd single line comment\nwidth:10px;}", ".tabs {\n\t//comment\n\t//2nd single line comment\n\twidth: 10px;\n}\n")
40+
t(".tabs{width:10px;//end of line comment\n}", ".tabs {\n\twidth: 10px;//end of line comment\n}\n")
41+
t(".tabs{width:10px;//end of line comment\nheight:10px;}", ".tabs {\n\twidth: 10px;//end of line comment\n\theight: 10px;\n}\n")
42+
t(".tabs{width:10px;//end of line comment\nheight:10px;//another\n}", ".tabs {\n\twidth: 10px;//end of line comment\n\theight: 10px;//another\n}\n")
43+
3544

3645
def testSeperateSelectors(self):
3746
self.resetOptions()

0 commit comments

Comments
 (0)