Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman authored Mar 19, 2017
2 parents ba3221c + 977b47d commit 0811a2c
Show file tree
Hide file tree
Showing 6 changed files with 843 additions and 455 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ can be overridden. This works for settings passed directly to the API in either
In the Javascript implementation, settings loaded from a config file, such as .jsbeautifyrc,
can also use inheritance/overriding.

Below is an example configuration tree showing all the the supported locations
Below is an example configuration tree showing all the supported locations
for language override nodes. We'll use `indent_size` to discuss how this configuration
would behave, but any number of settings can be inherited or overridden:

Expand Down
55 changes: 36 additions & 19 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
The options are (default in brackets):
indent_size (4) — indentation size,
indent_char (space) — character to indent with,
preserve_newlines (default false) - whether existing line breaks should be preserved,
selector_separator_newline (true) - separate selectors with newline or
not (e.g. "a,\nbr" or "a, br")
end_with_newline (false) - end with a newline
Expand Down Expand Up @@ -96,8 +97,10 @@

source_text = source_text || '';

var newlinesFromLastWSEat = 0;
var indentSize = options.indent_size ? parseInt(options.indent_size, 10) : 4;
var indentCharacter = options.indent_char || ' ';
var preserve_newlines = (options.preserve_newlines === undefined) ? false : options.preserve_newlines;
var selectorSeparatorNewline = (options.selector_separator_newline === undefined) ? true : options.selector_separator_newline;
var end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline;
var newline_between_rules = (options.newline_between_rules === undefined) ? true : options.newline_between_rules;
Expand Down Expand Up @@ -168,12 +171,16 @@
return str;
}

function eatWhitespace() {
var result = '';
function eatWhitespace(preserve_newlines_local) {
var result = 0;
while (whiteRe.test(peek())) {
next();
result += ch;
if (ch === '\n' && preserve_newlines_local && preserve_newlines) {
print.newLine(true);
result++;
}
}
newlinesFromLastWSEat = result;
return result;
}

Expand Down Expand Up @@ -254,12 +261,18 @@
print["{"] = function(ch) {
print.singleSpace();
output.push(ch);
print.newLine();
if (!eatWhitespace(true)) {
print.newLine();
}
};
print["}"] = function(ch) {
print.newLine();
output.push(ch);
print.newLine();
print["}"] = function(newline) {
if (newline) {
print.newLine();
}
output.push('}');
if (!eatWhitespace(true)) {
print.newLine();
}
};

print._lastCharWhitespace = function() {
Expand All @@ -270,8 +283,9 @@
if (output.length) {
if (!keepWhitespace && output[output.length - 1] !== '\n') {
print.trim();
} else if (output[output.length - 1] === basebaseIndentString) {
output.pop();
}

output.push('\n');

if (basebaseIndentString) {
Expand Down Expand Up @@ -373,9 +387,9 @@
eatWhitespace();
next();
print.singleSpace();
output.push("{}");
print.newLine();
if (newline_between_rules && indentLevel === 0) {
output.push("{");
print['}'](false);
if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
print.newLine(true);
}
} else {
Expand All @@ -392,13 +406,13 @@
}
} else if (ch === '}') {
outdent();
print["}"](ch);
print["}"](true);
insideRule = false;
insidePropertyValue = false;
if (nestedLevel) {
nestedLevel--;
}
if (newline_between_rules && indentLevel === 0) {
if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
print.newLine(true);
}
} else if (ch === ":") {
Expand Down Expand Up @@ -436,7 +450,9 @@
} else if (ch === ';') {
insidePropertyValue = false;
output.push(ch);
print.newLine();
if (!eatWhitespace(true)) {
print.newLine();
}
} else if (ch === '(') { // may be a url
if (lookBack("url")) {
output.push(ch);
Expand All @@ -459,8 +475,7 @@
parenLevel--;
} else if (ch === ',') {
output.push(ch);
eatWhitespace();
if (selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
if (!eatWhitespace(true) && selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
print.newLine();
} else {
print.singleSpace();
Expand All @@ -487,8 +502,10 @@
output.push(ch);
} else if (ch === '=') { // no whitespace before or after
eatWhitespace();
ch = '=';
output.push(ch);
output.push('=');
if (whiteRe.test(ch)) {
ch = '';
}
} else {
print.preserveSingleSpace();
output.push(ch);
Expand Down
Loading

0 comments on commit 0811a2c

Please sign in to comment.