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

HTML formatter does not send comments to a new line when they start with non-whitespace character #1915

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

carolinekistler
Copy link

@carolinekistler carolinekistler commented Apr 14, 2021

Description

Fixes Issue:
Fixes #1823 HTML formatter sends comments to a new line when the comment does not include a space as the first character

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
  • [ NA] Python implementation (NA if HTML beautifier)
  • [ NA Added Tests to data file(s)
  • [ NA] Added command-line option(s) (NA if
  • [NA ] README.md documents new feature/option(s)

@bitwiseman bitwiseman changed the title fixed issue 1823 HTML formatter does not send comments to a new line when they start with non-whitespace character Apr 14, 2021
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.

Here is where tag_name get calculated:
https://github.com/beautify-web/js-beautify/blob/bc48f5b54556bfcb3d2baefb2902ccb44958c9ed/js/src/html/beautifier.js#L607

That is what is causing the bug - it is using whitespace as the delimiter. That is reasonable for regular elements but not for comments. You probably want to do a special case right there for TOKEN.COMMENT and check for whether you are dealing with !-- or CDATA.

It is interesting that the change you did make did not break any tests.
Speaking of which, please add tests to this PR to cover the new behavior.

Thanks!

@@ -695,7 +695,7 @@ Beautifier.prototype._set_tag_position = function(printer, raw_token, parser_tok
}

// Don't add a newline before elements that should remain where they are.
if (parser_token.tag_name === '!--' && last_token.type === TOKEN.TAG_CLOSE &&
if ((parser_token.tag_name !== '!--' || parser_token.tag_name === '!--') && last_token.type === TOKEN.TAG_CLOSE &&
Copy link
Member

Choose a reason for hiding this comment

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

When does the expression (parser_token.tag_name !== '!--' || parser_token.tag_name === '!--') not evaluate to true? What happens if you remove this expression?

Copy link
Author

Choose a reason for hiding this comment

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

It has to deal with the spacing issue that it is reading when reading the comments. It is reading the tag name that does not have a space after as the tag name is not equal to '!== 'and vice versa. This expression covers both edge cases.

Copy link
Author

Choose a reason for hiding this comment

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

How would you like me to add tests?

Copy link
Member

Choose a reason for hiding this comment

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

Tests: See #1844. You only the to update the test data file. The generated files are generated by the build.

Copy link
Member

Choose a reason for hiding this comment

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

@carolinekistler
Please fetch and merge the latest changes from main. I've just removed the generated files from tracking to improve clarity.

@bitwiseman
Copy link
Member

@carolinekistler
Do you think you'll get to adding the tests for this?

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.

HTML formatter sends comments to a new line when the comment does not include a space as the first character
2 participants