Skip to content

Commit

Permalink
Merge branch 'main' into in-class-method
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Apr 21, 2022
2 parents a5995d0 + 508e803 commit 4b6ba1b
Show file tree
Hide file tree
Showing 18 changed files with 1,269 additions and 336 deletions.
459 changes: 459 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -58,17 +58,17 @@ JS Beautifier is hosted on two CDN services: [cdnjs](https://cdnjs.com/libraries

To pull the latest version from one of these services include one set of the script tags below in your document:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.2/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.2/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.2/beautify-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-html.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.2/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.2/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.2/beautify-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.3/beautify-html.min.js"></script>

<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.2/js/lib/beautify.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.2/js/lib/beautify-css.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.2/js/lib/beautify-html.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.3/js/lib/beautify.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.3/js/lib/beautify-css.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.14.3/js/lib/beautify-html.js"></script>
```

Older versions are available by changing the version number.
Expand Down Expand Up @@ -401,4 +401,4 @@ Thanks also to Jason Diamond, Patrick Hof, Nochum Sossonko, Andreas Schneider, D
Vasilevsky, Vital Batmanov, Ron Baldwin, Gabriel Harrison, Chris J. Shull,
Mathias Bynens, Vittorio Gambaletta and others.
(README.md: js-beautify@1.14.2)
(README.md: js-beautify@1.14.3)
409 changes: 200 additions & 209 deletions index.html

Large diffs are not rendered by default.

51 changes: 44 additions & 7 deletions js/src/css/beautifier.js
Expand Up @@ -67,6 +67,10 @@ function Beautifier(source_text, options) {
"@supports": true,
"@document": true
};
this.NON_SEMICOLON_NEWLINE_PROPERTY = [
"grid-template-areas",
"grid-template"
];

}

Expand Down Expand Up @@ -191,7 +195,9 @@ Beautifier.prototype.beautify = function() {
var enteringConditionalGroup = false;
var insideAtExtend = false;
var insideAtImport = false;
var insideScssMap = false;
var topCharacter = this._ch;
var insideNonSemiColonValues = false;
var whitespace;
var isAfterSpace;
var previous_ch;
Expand Down Expand Up @@ -243,7 +249,7 @@ Beautifier.prototype.beautify = function() {

// Ensures any new lines following the comment are preserved
this.eatWhitespace(true);
} else if (this._ch === '@') {
} else if (this._ch === '@' || this._ch === '$') {
this.preserveSingleSpace(isAfterSpace);

// deal with less propery mixins @{...}
Expand Down Expand Up @@ -358,6 +364,14 @@ Beautifier.prototype.beautify = function() {
}
}
} else if (this._ch === ":") {

for (var i = 0; i < this.NON_SEMICOLON_NEWLINE_PROPERTY.length; i++) {
if (this._input.lookBack(this.NON_SEMICOLON_NEWLINE_PROPERTY[i])) {
insideNonSemiColonValues = true;
break;
}
}

if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend && parenLevel === 0) {
// 'property: value' delimiter
// which could be in a conditional group query
Expand Down Expand Up @@ -390,6 +404,7 @@ Beautifier.prototype.beautify = function() {
this.print_string(this._ch + this.eatString(this._ch));
this.eatWhitespace(true);
} else if (this._ch === ';') {
insideNonSemiColonValues = false;
if (parenLevel === 0) {
if (insidePropertyValue) {
this.outdent();
Expand Down Expand Up @@ -429,22 +444,39 @@ Beautifier.prototype.beautify = function() {
}
}
} else {
this.preserveSingleSpace(isAfterSpace);
var space_needed = false;
if (this._input.lookBack("with")) {
// look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = true;
}
this.preserveSingleSpace(isAfterSpace || space_needed);
this.print_string(this._ch);
this.eatWhitespace();
parenLevel++;
this.indent();

// handle scss/sass map
if (insidePropertyValue && previous_ch === "$" && this._options.selector_separator_newline) {
this._output.add_new_line();
insideScssMap = true;
} else {
this.eatWhitespace();
parenLevel++;
this.indent();
}
}
} else if (this._ch === ')') {
if (parenLevel) {
parenLevel--;
this.outdent();
}
if (insideScssMap && this._input.peek() === ";" && this._options.selector_separator_newline) {
insideScssMap = false;
this.outdent();
this._output.add_new_line();
}
this.print_string(this._ch);
} else if (this._ch === ',') {
this.print_string(this._ch);
this.eatWhitespace(true);
if (this._options.selector_separator_newline && !insidePropertyValue && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
if (this._options.selector_separator_newline && (!insidePropertyValue || insideScssMap) && parenLevel === 0 && !insideAtImport && !insideAtExtend) {
this._output.add_new_line();
} else {
this._output.space_before_token = true;
Expand Down Expand Up @@ -478,8 +510,13 @@ Beautifier.prototype.beautify = function() {
this.print_string(' ');
this.print_string(this._ch);
} else {
this.preserveSingleSpace(isAfterSpace);
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';
this.preserveSingleSpace(preserveAfterSpace || isAfterSpace);
this.print_string(this._ch);

if (!this._output.just_added_newline() && this._input.peek() === '\n' && insideNonSemiColonValues) {
this._output.add_new_line();
}
}
}

Expand Down
23 changes: 18 additions & 5 deletions js/src/html/beautifier.js
Expand Up @@ -607,14 +607,19 @@ var TagOpenParserToken = function(parent, raw_token) {
tag_check_match = raw_token.text.match(/^<([^\s>]*)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';
} else {
tag_check_match = raw_token.text.match(/^{{(?:[\^]|#\*?)?([^\s}]+)/);
tag_check_match = raw_token.text.match(/^{{~?(?:[\^]|#\*?)?([^\s}]+)/);
this.tag_check = tag_check_match ? tag_check_match[1] : '';

// handle "{{#> myPartial}}
if (raw_token.text === '{{#>' && this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
// handle "{{#> myPartial}}" or "{{~#> myPartial}}"
if ((raw_token.text.startsWith('{{#>') || raw_token.text.startsWith('{{~#>')) && this.tag_check[0] === '>') {
if (this.tag_check === '>' && raw_token.next !== null) {
this.tag_check = raw_token.next.text.split(' ')[0];
} else {
this.tag_check = raw_token.text.split('>')[1];
}
}
}

this.tag_check = this.tag_check.toLowerCase();

if (raw_token.type === TOKEN.COMMENT) {
Expand All @@ -626,9 +631,17 @@ var TagOpenParserToken = function(parent, raw_token) {
this.is_end_tag = !this.is_start_tag ||
(raw_token.closed && raw_token.closed.text === '/>');

// if whitespace handler ~ included (i.e. {{~#if true}}), handlebars tags start at pos 3 not pos 2
var handlebar_starts = 2;
if (this.tag_start_char === '{' && this.text.length >= 3) {
if (this.text.charAt(2) === '~') {
handlebar_starts = 3;
}
}

// handlebars tags that don't start with # or ^ are single_tags, and so also start and end.
this.is_end_tag = this.is_end_tag ||
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(2)))));
(this.tag_start_char === '{' && (this.text.length < 3 || (/[^#\^]/.test(this.text.charAt(handlebar_starts)))));
}
};

Expand Down
17 changes: 9 additions & 8 deletions js/src/javascript/beautifier.js
Expand Up @@ -1151,13 +1151,6 @@ Beautifier.prototype.handle_operator = function(current_token) {
this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
}

if (reserved_array(this._flags.last_token, special_words)) {
// "return" had a special handling in TK_WORD. Now we need to return the favor
this._output.space_before_token = true;
this.print_token(current_token);
return;
}

// hack for actionscript's import .*;
if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
this.print_token(current_token);
Expand Down Expand Up @@ -1285,7 +1278,11 @@ Beautifier.prototype.handle_operator = function(current_token) {
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (current_token.newlines && (current_token.text === '--' || current_token.text === '++' || current_token.text === '~')) {
this.print_newline(false, true);
var new_line_needed = reserved_array(this._flags.last_token, special_words) && current_token.newlines;
if (new_line_needed && (this._previous_flags.if_block || this._previous_flags.else_block)) {
this.restore_mode();
}
this.print_newline(new_line_needed, true);
}

if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
Expand Down Expand Up @@ -1425,6 +1422,10 @@ Beautifier.prototype.handle_dot = function(current_token) {
this.handle_whitespace_and_comments(current_token, true);
}

if (this._flags.last_token.text.match('^[0-9]+$')) {
this._output.space_before_token = true;
}

if (reserved_array(this._flags.last_token, special_words)) {
this._output.space_before_token = false;
} else {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "js-beautify",
"version": "1.14.2",
"version": "1.14.3",
"description": "beautifier.io for node",
"main": "js/index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion python/cssbeautifier/__version__.py
@@ -1 +1 @@
__version__ = "1.14.2"
__version__ = "1.14.3"
55 changes: 48 additions & 7 deletions python/cssbeautifier/css/beautifier.py
Expand Up @@ -119,6 +119,7 @@ def __init__(self, source_text, opts=default_options()):
"@document",
}
self.CONDITIONAL_GROUP_RULE = {"@media", "@supports", "@document"}
self.NON_SEMICOLON_NEWLINE_PROPERTY = ["grid-template-areas", "grid-template"]

def eatString(self, endChars):
result = ""
Expand Down Expand Up @@ -222,7 +223,9 @@ def beautify(self):
enteringConditionalGroup = False
insideAtExtend = False
insideAtImport = False
insideScssMap = False
topCharacter = self._ch
insideNonSemiColonValues = False

while True:
whitespace = self._input.read(whitespacePattern)
Expand Down Expand Up @@ -268,7 +271,7 @@ def beautify(self):

# Ensures any new lines following the comment are preserved
self.eatWhitespace(True)
elif self._ch == "@":
elif self._ch == "@" or self._ch == "$":
self.preserveSingleSpace(isAfterSpace)

# deal with less propery mixins @{...}
Expand Down Expand Up @@ -381,6 +384,12 @@ def beautify(self):
if self._options.brace_style == "expand":
self._output.add_new_line(True)
elif self._ch == ":":

for i in range(0, len(self.NON_SEMICOLON_NEWLINE_PROPERTY)):
if self._input.lookBack(self.NON_SEMICOLON_NEWLINE_PROPERTY[i]):
insideNonSemiColonValues = True
break

if (
(insideRule or enteringConditionalGroup)
and not (self._input.lookBack("&") or self.foundNestedPseudoClass())
Expand Down Expand Up @@ -417,6 +426,7 @@ def beautify(self):
self.print_string(self._ch + self.eatString(self._ch))
self.eatWhitespace(True)
elif self._ch == ";":
insideNonSemiColonValues = False
if parenLevel == 0:
if insidePropertyValue:
self.outdent()
Expand Down Expand Up @@ -452,22 +462,45 @@ def beautify(self):
parenLevel -= 1
self.outdent()
else:
self.preserveSingleSpace(isAfterSpace)
space_needed = False
if self._input.lookBack("with"):
# look back is not an accurate solution, we need tokens to confirm without whitespaces
space_needed = True
self.preserveSingleSpace(isAfterSpace or space_needed)
self.print_string(self._ch)
self.eatWhitespace()
parenLevel += 1
self.indent()

# handle scss/sass map
if (
insidePropertyValue
and previous_ch == "$"
and self._options.selector_separator_newline
):
self._output.add_new_line()
insideScssMap = True
else:
self.eatWhitespace()
parenLevel += 1
self.indent()
elif self._ch == ")":
if parenLevel:
parenLevel -= 1
self.outdent()

if (
insideScssMap
and self._input.peek() == ";"
and self._options.selector_separator_newline
):
insideScssMap = False
self.outdent()
self._output.add_new_line()
self.print_string(self._ch)
elif self._ch == ",":
self.print_string(self._ch)
self.eatWhitespace(True)
if (
self._options.selector_separator_newline
and not insidePropertyValue
and (not insidePropertyValue or insideScssMap)
and parenLevel == 0
and not insideAtImport
and not insideAtExtend
Expand Down Expand Up @@ -507,9 +540,17 @@ def beautify(self):
self.print_string(" ")
self.print_string(self._ch)
else:
self.preserveSingleSpace(isAfterSpace)
preserveAfterSpace = previous_ch == '"' or previous_ch == "'"
self.preserveSingleSpace(preserveAfterSpace or isAfterSpace)
self.print_string(self._ch)

if (
not self._output.just_added_newline()
and self._input.peek() == "\n"
and insideNonSemiColonValues
):
self._output.add_new_line()

sweet_code = self._output.get_code(self._options.eol)

return sweet_code
2 changes: 1 addition & 1 deletion python/jsbeautifier/__version__.py
@@ -1 +1 @@
__version__ = "1.14.2"
__version__ = "1.14.3"

0 comments on commit 4b6ba1b

Please sign in to comment.