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

Added option max_preserve_newlines in CSS beautifier #1867

Merged
merged 7 commits into from
Jan 8, 2021

Conversation

JuanFML
Copy link
Contributor

@JuanFML JuanFML commented Dec 21, 2020

Description

Added option max_preserve_newlines to CSS beautifier, as well tests with more lines, less lines and the same as the ones requested in the option.

  • Source branch in your fork has meaningful name (not master)

Fixes #1863:

Before Merge Checklist

These items can be completed after PR is created.

(Check any items that are not applicable (NA) for this PR)

  • JavaScript implementation
  • Python implementation (NA if HTML beautifier)
  • Added Tests to data file(s)
  • Added command-line option(s) (NA if
  • README.md documents new feature/option(s)

Copy link
Member

@bitwiseman bitwiseman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing.
Take a look at my suggestion and see if they seem right to you.

@@ -92,11 +92,15 @@ Beautifier.prototype.eatString = function(endChars) {
Beautifier.prototype.eatWhitespace = function(allowAtLeastOneNewLine) {
var result = whitespaceChar.test(this._input.peek());
var isFirstNewLine = true;

var newlines = this._options.max_preserve_newlines;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete var isFirstNewLine = true; and then

Suggested change
var newlines = this._options.max_preserve_newlines;
var newline_count = 0;

Comment on lines 99 to 103
if ((this._options.preserve_newlines || isFirstNewLine) && (this._options.max_preserve_newlines === 32786 || this._options.max_preserve_newlines === 0)) {
isFirstNewLine = false;
this._output.add_new_line(true);
} else if ((this._options.preserve_newlines || isFirstNewLine) && newlines > 0) {
newlines--;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_preserve_newlines is already sanitized by the options object:

https://github.com/beautify-web/js-beautify/blob/aa9e365aed991080193c2bced411f132990c9d82/js/src/core/options.js#L44-47

Suggested change
if ((this._options.preserve_newlines || isFirstNewLine) && (this._options.max_preserve_newlines === 32786 || this._options.max_preserve_newlines === 0)) {
isFirstNewLine = false;
this._output.add_new_line(true);
} else if ((this._options.preserve_newlines || isFirstNewLine) && newlines > 0) {
newlines--;
if (newline_count === 0 || newline_count < this._options.max_preserve_newlines) {
newline_count++;

Also delete isFirstNewLine = false; below.

And implement same in python, of course.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I liked this way better, is way cleaner, thank you!

@bitwiseman
Copy link
Member

@JuanFML
Please remove 95e29b8 and the revert b14e245 from the branch for this PR. Otherwise, #1860 will become an empty PR when this is merged: all the changes in it will already be in master.

@JuanFML
Copy link
Contributor Author

JuanFML commented Dec 24, 2020

@JuanFML
Please remove 95e29b8 and the revert b14e245 from the branch for this PR. Otherwise, #1860 will become an empty PR when this is merged: all the changes in it will already be in master.

I think I already fix it, although I don't know how I can erase the commits at the top, but I could also close this and open a new one.

@bitwiseman bitwiseman merged commit b6af0d0 into beautifier:master Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option 'max_preserve_newlines' not working on beautify_css.js CSS Beautifier
2 participants