Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug with css pseudo-classes #431

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions python/cssbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ def closeBracket(self):
self.output.append("}")
self.newLine()

def colon(self):
def colon(self, addWhitespace=False):
self.output.append(":")
self.singleSpace()
if (addWhitespace):
self.singleSpace()

def semicolon(self):
self.output.append(";")
Expand Down Expand Up @@ -150,6 +151,7 @@ def __init__(self, source_text, opts=default_options()):
self.indentChar = opts.indent_char
self.pos = -1
self.ch = None
self.pseudo_classes = ["link", "visited", "active", "hover", "focus", "first-letter", "first-line", "first-child", "before", "after", "lang", "language"]

def next(self):
self.pos = self.pos + 1
Expand Down Expand Up @@ -203,13 +205,25 @@ def eatComment(self, singleLine):
def lookBack(self, string):
past = self.source_text[self.pos - len(string):self.pos]
return past.lower() == string

def lookFront(self, string):
front = self.source_text[self.pos + 1:self.pos + len(string) + 1]
return front.lower() == string

def isCommentOnLine(self):
endOfLine = self.source_text.find('\n', self.pos)
if endOfLine == -1:
return False;
restOfLine = self.source_text[self.pos:endOfLine]
return restOfLine.find('//') != -1

def nextIsPseudoClass(self):
status = False
for pseudo_class in self.pseudo_classes:
if (self.lookFront(pseudo_class)):
status = True
break
return status

def beautify(self):
m = re.search("^[\r\n]*[\t ]*", self.source_text)
Expand Down Expand Up @@ -245,7 +259,8 @@ def beautify(self):
insideRule = False
elif self.ch == ":":
self.eatWhitespace()
printer.colon()
addWhitespace = not self.nextIsPseudoClass()
printer.colon(addWhitespace)
insideRule = True
elif self.ch == '"' or self.ch == '\'':
printer.push(self.eatString(self.ch))
Expand Down Expand Up @@ -306,4 +321,3 @@ def beautify(self):
sweet_code = sweet_code[:-1]

return sweet_code