Skip to content

Commit

Permalink
jsbeautify version update for 1.5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefjester committed Jun 1, 2015
1 parent 5622eb5 commit b6fa9e0
Show file tree
Hide file tree
Showing 28 changed files with 1,876 additions and 1,129 deletions.
Empty file modified libs/jsbeautifier/MANIFEST.in
100644 → 100755
Empty file.
93 changes: 58 additions & 35 deletions libs/jsbeautifier/cssbeautifier/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,21 @@ class BeautifierOptions:
def __init__(self):
self.indent_size = 4
self.indent_char = ' '
self.indent_with_tabs = False
self.selector_separator_newline = True
self.end_with_newline = False
self.newline_between_rules = True

def __repr__(self):
return \
"""indent_size = %d
indent_char = [%s]
indent_with_tabs = [%s]
separate_selectors_newline = [%s]
end_with_newline = [%s]
""" % (self.indent_size, self.indent_char,
self.separate_selectors, self.end_with_newline)
newline_between_rules = [%s]
""" % (self.indent_size, self.indent_char, self.indent_with_tabs,
self.selector_separator_newline, self.end_with_newline, self.newline_between_rules)


def default_options():
Expand Down Expand Up @@ -88,11 +92,9 @@ def __init__(self, indent_char, indent_size, default_indent=""):
self.singleIndent = (indent_size) * indent_char
self.indentLevel = 0
self.nestedLevel = 0

self.baseIndentString = default_indent
self.output = []
if self.baseIndentString:
self.push(self.baseIndentString)

def __lastCharWhitespace(self):
return len(self.output) > 0 and WHITE_RE.search(self.output[-1]) is not None
Expand Down Expand Up @@ -127,14 +129,14 @@ def comment(self, comment):
self.output.append(comment)

def newLine(self, keepWhitespace=False):
if not keepWhitespace:
self.trim()
if len(self.output) > 0 :
if not keepWhitespace and self.output[-1] != '\n':
self.trim()

if len(self.output) > 0:
self.output.append("\n")

if len(self.baseIndentString) > 0:
self.output.append(self.baseIndentString)
if len(self.baseIndentString) > 0:
self.output.append(self.baseIndentString)

def trim(self):
while self.__lastCharWhitespace():
Expand All @@ -145,7 +147,10 @@ def singleSpace(self):
self.output.append(" ")

def result(self):
return "".join(self.output)
if self.baseIndentString:
return self.baseIndentString + "".join(self.output);
else:
return "".join(self.output)


class Beautifier:
Expand All @@ -157,7 +162,11 @@ def __init__(self, source_text, opts=default_options()):
self.indentChar = opts.indent_char
self.pos = -1
self.ch = None


if self.opts.indent_with_tabs:
self.indentChar = "\t"
self.indentSize = 1

# https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
# also in CONDITIONAL_GROUP_RULE below
self.NESTED_AT_RULE = [ \
Expand Down Expand Up @@ -222,7 +231,7 @@ def skipWhitespace(self):
result = ''
if self.ch and WHITE_RE.search(self.ch):
result = self.ch

while WHITE_RE.search(self.next()) is not None:
result += self.ch
return result
Expand Down Expand Up @@ -255,9 +264,9 @@ def foundNestedPseudoClass(self):
elif ch == ";" or ch == "}" or ch == ")":
return False
i += 1;

return False


def beautify(self):
m = re.search("^[\t ]*", self.source_text)
Expand All @@ -266,30 +275,35 @@ def beautify(self):

insideRule = False
enteringConditionalGroup = False
top_ch = ''
last_top_ch = ''

top_ch = ''
last_top_ch = ''
parenLevel = 0

while True:
whitespace = self.skipWhitespace();
isAfterSpace = whitespace != ''
isAfterNewline = '\n' in whitespace;
last_top_ch = top_ch
top_ch = self.ch

if not self.ch:
break
elif self.ch == '/' and self.peek() == '*':
printer.newLine()
header = printer.indentLevel == 0

if not isAfterNewline or header:
printer.newLine()


comment = self.eatComment()
printer.comment(comment)
printer.newLine()
header = self.lookBack("")
printer.newLine()
if header:
printer.newLine(True)
elif self.ch == '/' and self.peek() == '/':
if not isAfterNewline and last_top_ch != '{':
printer.trim()

printer.singleSpace()
printer.comment(self.eatComment())
printer.newLine()
Expand All @@ -301,30 +315,34 @@ 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) == '}':
self.eatWhitespace()
self.next()
printer.singleSpace()
printer.push("{}")
printer.newLine()
if self.opts.newline_between_rules and printer.indentLevel == 0:
printer.newLine(True)
else:
printer.indent()
printer.openBracket()
Expand All @@ -341,6 +359,8 @@ def beautify(self):
insideRule = False
if printer.nestedLevel:
printer.nestedLevel -= 1
if self.opts.newline_between_rules and printer.indentLevel == 0:
printer.newLine(True)
elif self.ch == ":":
self.eatWhitespace()
if (insideRule or enteringConditionalGroup) and \
Expand All @@ -358,7 +378,7 @@ def beautify(self):
printer.push("::")
else:
# pseudo-element
printer.push(":")
printer.push(":")
elif self.ch == '"' or self.ch == '\'':
if isAfterSpace:
printer.singleSpace()
Expand All @@ -377,16 +397,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 All @@ -399,6 +421,7 @@ def beautify(self):
elif self.ch == '=':
# no whitespace before or after
self.eatWhitespace()
self.ch = '='
printer.push(self.ch)
else:
if isAfterSpace:
Expand All @@ -412,4 +435,4 @@ def beautify(self):
if self.opts.end_with_newline:
sweet_code += "\n"

return sweet_code
return sweet_code
Empty file modified libs/jsbeautifier/cssbeautifier/__version__.py
100644 → 100755
Empty file.
Empty file modified libs/jsbeautifier/cssbeautifier/tests/__init__.py
100644 → 100755
Empty file.
139 changes: 110 additions & 29 deletions libs/jsbeautifier/cssbeautifier/tests/test.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,116 @@
class CSSBeautifierTest(unittest.TestCase):

def resetOptions(self):
self.options = cssbeautifier.default_options()
self.options.indent_size = 1
self.options.indent_char = '\t'
self.options.selector_separator_newline = True
self.options.end_with_newline = False
false = False
true = True
self.options = cssbeautifier.default_options()
self.options.indent_size = 1
self.options.indent_char = '\t'
self.options.selector_separator_newline = true
self.options.end_with_newline = false
self.options.newline_between_rules = false

def testGenerated(self):
self.resetOptions()
test_fragment = self.decodesto
t = self.decodesto

false = False
true = True

self.options.indent_size = 1
self.options.indent_char = '\t'
self.options.selector_separator_newline = true
self.options.end_with_newline = false
self.options.newline_between_rules = false

# End With Newline - (eof = "\n")
self.options.end_with_newline = true
test_fragment('', '\n')
test_fragment(' .tabs{}', ' .tabs {}\n')
test_fragment(' \n\n.tabs{}\n\n\n\n', ' .tabs {}\n')
test_fragment('\n')

# End With Newline - (eof = "")
self.options.end_with_newline = false
test_fragment('')
test_fragment(' .tabs{}', ' .tabs {}')
test_fragment(' \n\n.tabs{}\n\n\n\n', ' .tabs {}')
test_fragment('\n', '')

# Empty braces
t('.tabs{}', '.tabs {}')
t('.tabs { }', '.tabs {}')
t('.tabs { }', '.tabs {}')
t('.tabs \n{\n \n }', '.tabs {}')

#
t('#cboxOverlay {\n\tbackground: url(images/overlay.png) repeat 0 0;\n\topacity: 0.9;\n\tfilter: alpha(opacity = 90);\n}', '#cboxOverlay {\n\tbackground: url(images/overlay.png) repeat 0 0;\n\topacity: 0.9;\n\tfilter: alpha(opacity=90);\n}')

# Newline Between Rules - (separator = "\n")
self.options.newline_between_rules = true
t('.div {}\n.span {}', '.div {}\n\n.span {}')
t('.div{}\n \n.span{}', '.div {}\n\n.span {}')
t('.div {} \n \n.span { } \n', '.div {}\n\n.span {}')
t('.div {\n \n} \n .span {\n } ', '.div {}\n\n.span {}')
t('.selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n.div{height:15px;}', '.selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n\n.div {\n\theight: 15px;\n}')
t('.tabs{width:10px;//end of line comment\nheight:10px;//another\n}\n.div{height:15px;}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px; //another\n}\n\n.div {\n\theight: 15px;\n}')
t('#foo {\n\tbackground-image: url([email protected]);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '#foo {\n\tbackground-image: url([email protected]);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n\n.div {\n\theight: 15px;\n}')
t('@media screen {\n\t#foo:hover {\n\t\tbackground-image: url([email protected]);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '@media screen {\n\t#foo:hover {\n\t\tbackground-image: url([email protected]);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n\n.div {\n\theight: 15px;\n}')
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}', '@font-face {\n\tfont-family: "Bitstream Vera Serif Bold";\n\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n}\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\n.div {\n\theight: 15px;\n}')

# Newline Between Rules - (separator = "")
self.options.newline_between_rules = false
t('.div {}\n.span {}')
t('.div{}\n \n.span{}', '.div {}\n.span {}')
t('.div {} \n \n.span { } \n', '.div {}\n.span {}')
t('.div {\n \n} \n .span {\n } ', '.div {}\n.span {}')
t('.selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n.div{height:15px;}', '.selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}\n.div {\n\theight: 15px;\n}')
t('.tabs{width:10px;//end of line comment\nheight:10px;//another\n}\n.div{height:15px;}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px; //another\n}\n.div {\n\theight: 15px;\n}')
t('#foo {\n\tbackground-image: url([email protected]);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '#foo {\n\tbackground-image: url([email protected]);\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div {\n\theight: 15px;\n}')
t('@media screen {\n\t#foo:hover {\n\t\tbackground-image: url([email protected]);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div{height:15px;}', '@media screen {\n\t#foo:hover {\n\t\tbackground-image: url([email protected]);\n\t}\n\t@font-face {\n\t\tfont-family: "Bitstream Vera Serif Bold";\n\t\tsrc: url("http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf");\n\t}\n}\n.div {\n\theight: 15px;\n}')
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}')

# Comments
t('/* test */')
t('.tabs{/* test */}', '.tabs {\n\t/* test */\n}')
t('.tabs{/* test */}', '.tabs {\n\t/* test */\n}')
t('/* header */.tabs {}', '/* header */\n\n.tabs {}')
t('.tabs {\n/* non-header */\nwidth:10px;}', '.tabs {\n\t/* non-header */\n\twidth: 10px;\n}')
t('/* header')
t('// comment')
t('.selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}', '.selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}')

# single line comment support (less/sass)
t('.tabs{\n// comment\nwidth:10px;\n}', '.tabs {\n\t// comment\n\twidth: 10px;\n}')
t('.tabs{// comment\nwidth:10px;\n}', '.tabs {\n\t// comment\n\twidth: 10px;\n}')
t('//comment\n.tabs{width:10px;}', '//comment\n.tabs {\n\twidth: 10px;\n}')
t('.tabs{//comment\n//2nd single line comment\nwidth:10px;}', '.tabs {\n\t//comment\n\t//2nd single line comment\n\twidth: 10px;\n}')
t('.tabs{width:10px;//end of line comment\n}', '.tabs {\n\twidth: 10px; //end of line comment\n}')
t('.tabs{width:10px;//end of line comment\nheight:10px;}', '.tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px;\n}')
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}')

# 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 {}')

#


def testNewline(self):
self.resetOptions()
Expand Down Expand Up @@ -51,30 +156,6 @@ def testBasics(self):
t(" \t \na, img {padding: 0.2px}", " \t a,\n \t img {\n \t \tpadding: 0.2px\n \t }")
t("\n\n a, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}")


def testComments(self):
self.resetOptions()
t = self.decodesto

t("/* test */", "/* test */")
t(".tabs{/* test */}", ".tabs {\n\t/* test */\n}")
t("/* header */.tabs {}", "/* header */\n\n.tabs {}")
t("/* header", "/* header");
t("// comment", "// comment");
t(".selector1 {\n\tmargin: 0; /* This is a comment including an url http://domain.com/path/to/file.ext */\n}",
".selector1 {\n\tmargin: 0;\n\t/* This is a comment including an url http://domain.com/path/to/file.ext */\n}")

#single line comment support (less/sass)
t(".tabs{\n// comment\nwidth:10px;\n}", ".tabs {\n\t// comment\n\twidth: 10px;\n}")
t(".tabs{// comment\nwidth:10px;\n}", ".tabs {\n\t// comment\n\twidth: 10px;\n}")
t("//comment\n.tabs{width:10px;}", "//comment\n.tabs {\n\twidth: 10px;\n}")
t(".tabs{//comment\n//2nd single line comment\nwidth:10px;}", ".tabs {\n\t//comment\n\t//2nd single line comment\n\twidth: 10px;\n}")
t(".tabs{width:10px;//end of line comment\n}", ".tabs {\n\twidth: 10px; //end of line comment\n}")
t(".tabs{width:10px;//end of line comment\nheight:10px;}", ".tabs {\n\twidth: 10px; //end of line comment\n\theight: 10px;\n}")
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}")



def testSeperateSelectors(self):
self.resetOptions()
t = self.decodesto
Expand Down
Loading

0 comments on commit b6fa9e0

Please sign in to comment.