sublimetext-codeformatter icon indicating copy to clipboard operation
sublimetext-codeformatter copied to clipboard

HTML: support closing empty tags on the same line

Open bostrom opened this issue 9 years ago • 9 comments

HTML formatter now closes an empty tag on the next line, effectively adding a space into an originally empty tag. An option to close empty tags on the same line (or leave empty tags untouched) would be nice. Example:

<i class="fa fa-warning"></i>

becomes:

<i class="fa fa-warning">
</i>

bostrom avatar Feb 15 '16 10:02 bostrom

Looking at the source code, apparently the undocumented option reduce_empty_tags does exactly this. This option should probably be added to the readme and default settings?

bostrom avatar Feb 15 '16 10:02 bostrom

However, the current implementation uses \w to match the tag name, which doesn't match hyphens used in e.g. AngularJS custom tags (directives). So the reduce_empty_tags option wouldn't recognize the following AngularJS directive:

<my-special-tag></my-special-tag>

Are these cases outside the scope of the HTML formatter, or would it be acceptable to add the hyphen character to the tag name matching regexp?

bostrom avatar Feb 15 '16 10:02 bostrom

When I originally re-wrote the HTML formatter I was writing it to the HTML5 spec, and so did not think about including the ability to format non-standard tag names such as the ones that AngularJS allows users to implement via directives.

I'm currently working on a fix to one of the other options (expand_tags has problems with attributes that don't have values or use single quotes instead of double quotes because I was an idiot and didn't think ahead), so I'll look into supporting tags with hyphens in them while I work on it, and I'll also fix the lack of documented options while I'm at it.

RevanProdigalKnight avatar Feb 29 '16 21:02 RevanProdigalKnight

@RevanProdigalKnight Much appreciated, again :)

bostrom avatar Mar 02 '16 17:03 bostrom

@RevanProdigalKnight I see you've managed to fix these issues and the PR in included in the latest version of CodeFormatter. However, using these options

  "codeformatter_html_options": {
    "syntaxes": "html,asp,xml", // Syntax names which must process HTML formatter
    "indent_size": 2,
    "indent_char": " ", // Indent character
    "preserve_newlines": true, // Preserve existing line-breaks,
    "reduce_empty_tags": true, // Remove spaces from within empty tags
  }

it seems that not even standard empty html tags are kept on the same line when formatting. I.e. now

<i></i>

becomes

<i>
</i>

Am I missing something or did something break along the way? I even uninstalled and re-installed CodeFormatter for ST3, but still the same problem.

bostrom avatar Apr 05 '16 14:04 bostrom

No, nothing broke. @akalongman added a new HTML beautifier using BeautifulSoup 4 that only implements the indent_size option. I have still been working on more bugfixes / improvements for my formatter, though, and intend to submit another pull request soon that adds the option to pick which formatter to use while @akalongman finishes the new beautifier.

RevanProdigalKnight avatar Apr 05 '16 16:04 RevanProdigalKnight

@RevanProdigalKnight I am still finding better solution for formatting html/xml.. Still no result :) Your pull requests will be very welcomed

akalongman avatar Apr 05 '16 16:04 akalongman

I too am looking forward to this functionality!

wadewegner avatar Apr 12 '16 04:04 wadewegner