Skip to content

Commit

Permalink
Update with css preserve-newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Mar 20, 2017
1 parent aa578be commit 794ffa0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
46 changes: 33 additions & 13 deletions js/lib/beautify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,10 @@ function css_beautify(source_text, options) {

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 @@ -654,12 +656,16 @@ function css_beautify(source_text, options) {
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) {
output.add_new_line(true);
result++;
}
}
newlinesFromLastWSEat = result;
return result;
}

Expand Down Expand Up @@ -834,15 +840,20 @@ function css_beautify(source_text, options) {
next();
output.space_before_token = true;
print_string("{}");
output.add_new_line();
if (newline_between_rules && indentLevel === 0) {
if (!eatWhitespace(true)) {
output.add_new_line();
}

if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
output.add_new_line(true);
}
} else {
indent();
output.space_before_token = true;
print_string(ch);
output.add_new_line();
if (!eatWhitespace(true)) {
output.add_new_line();
}

// when entering conditional groups, only rulesets are allowed
if (enteringConditionalGroup) {
Expand All @@ -857,13 +868,17 @@ function css_beautify(source_text, options) {
outdent();
output.add_new_line();
print_string(ch);
output.add_new_line();
insideRule = false;
insidePropertyValue = false;
if (nestedLevel) {
nestedLevel--;
}
if (newline_between_rules && indentLevel === 0) {

if (!eatWhitespace(true)) {
output.add_new_line();
}

if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
output.add_new_line(true);
}
} else if (ch === ":") {
Expand Down Expand Up @@ -901,7 +916,9 @@ function css_beautify(source_text, options) {
} else if (ch === ';') {
insidePropertyValue = false;
print_string(ch);
output.add_new_line();
if (!eatWhitespace(true)) {
output.add_new_line();
}
} else if (ch === '(') { // may be a url
if (lookBack("url")) {
print_string(ch);
Expand All @@ -924,8 +941,7 @@ function css_beautify(source_text, options) {
parenLevel--;
} else if (ch === ',') {
print_string(ch);
eatWhitespace();
if (selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
if (!eatWhitespace(true) && selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
output.add_new_line();
} else {
output.space_before_token = true;
Expand All @@ -952,8 +968,11 @@ function css_beautify(source_text, options) {
print_string(ch);
} else if (ch === '=') { // no whitespace before or after
eatWhitespace();
ch = '=';
print_string(ch);
print_string('=');
if (whiteRe.test(ch)) {
ch = '';
}

} else {
preserveSingleSpace();
print_string(ch);
Expand Down Expand Up @@ -983,6 +1002,7 @@ css_beautify.CONDITIONAL_GROUP_RULE = {

module.exports = css_beautify;


/***/ })
/******/ ]);
var css_beautify = legacy_beautify_css;
Expand Down
45 changes: 32 additions & 13 deletions js/src/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ function css_beautify(source_text, options) {

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 @@ -115,12 +117,16 @@ function css_beautify(source_text, options) {
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) {
output.add_new_line(true);
result++;
}
}
newlinesFromLastWSEat = result;
return result;
}

Expand Down Expand Up @@ -295,15 +301,20 @@ function css_beautify(source_text, options) {
next();
output.space_before_token = true;
print_string("{}");
output.add_new_line();
if (newline_between_rules && indentLevel === 0) {
if (!eatWhitespace(true)) {
output.add_new_line();
}

if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
output.add_new_line(true);
}
} else {
indent();
output.space_before_token = true;
print_string(ch);
output.add_new_line();
if (!eatWhitespace(true)) {
output.add_new_line();
}

// when entering conditional groups, only rulesets are allowed
if (enteringConditionalGroup) {
Expand All @@ -318,13 +329,17 @@ function css_beautify(source_text, options) {
outdent();
output.add_new_line();
print_string(ch);
output.add_new_line();
insideRule = false;
insidePropertyValue = false;
if (nestedLevel) {
nestedLevel--;
}
if (newline_between_rules && indentLevel === 0) {

if (!eatWhitespace(true)) {
output.add_new_line();
}

if (newlinesFromLastWSEat < 2 && newline_between_rules && indentLevel === 0) {
output.add_new_line(true);
}
} else if (ch === ":") {
Expand Down Expand Up @@ -362,7 +377,9 @@ function css_beautify(source_text, options) {
} else if (ch === ';') {
insidePropertyValue = false;
print_string(ch);
output.add_new_line();
if (!eatWhitespace(true)) {
output.add_new_line();
}
} else if (ch === '(') { // may be a url
if (lookBack("url")) {
print_string(ch);
Expand All @@ -385,8 +402,7 @@ function css_beautify(source_text, options) {
parenLevel--;
} else if (ch === ',') {
print_string(ch);
eatWhitespace();
if (selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
if (!eatWhitespace(true) && selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
output.add_new_line();
} else {
output.space_before_token = true;
Expand All @@ -413,8 +429,11 @@ function css_beautify(source_text, options) {
print_string(ch);
} else if (ch === '=') { // no whitespace before or after
eatWhitespace();
ch = '=';
print_string(ch);
print_string('=');
if (whiteRe.test(ch)) {
ch = '';
}

} else {
preserveSingleSpace();
print_string(ch);
Expand Down

0 comments on commit 794ffa0

Please sign in to comment.