deanhume / html-minifier

A simple command line tool to minify your HTML, Razor views & Web Forms views
http://deanhume.github.io/html-minifier/
MIT License
134 stars 86 forks source link

Razor error comments "//" in @{} #34

Closed jaryway closed 5 years ago

jaryway commented 7 years ago

test:

@{
/*Layout = "~/Views/Shared/_layout.cshtml";*/
// Layout = "~/Views/Shared/_layout.cshtml";
//Layout = "~/Views/Shared/_layout.cshtml";
}

to

@{ //Layout = "~/Views/Shared/_layout.cshtml"; }
nevaldas commented 7 years ago

It's so because regex checks for comments that has space after //: file: StreamReaderExtension.cs Lines 148-149

 // Replace line comments
htmlContents = Regex.Replace(htmlContents, @"// (.*?)\r?\n", "", RegexOptions.Singleline);

I have simply removed the space and everything is fine for my case.

yangar commented 7 years ago

@nevaldas your solution remove also some code like:

heldersepu commented 7 years ago

This opens and interesting question, what should happen on something like:

<div>
<pre>test // Hello World </pre>
</div>
heldersepu commented 7 years ago

At the very least the replacement of line comments: https://github.com/deanhume/html-minifier/blob/master/ViewMinifier/StreamReaderExtension.cs#L148

Should be inside the

if (!features.IgnoreHtmlComments)
deanhume commented 5 years ago

Closing this issue as it is not something that will likely be fixed.

This is due to a number of reasons:

  1. In order to truly 'minify' code, it shouldn't really include comments anyway
  2. This is really tricky from a logic point of view - under what conditions do we keep / remove comments? (see the reasons why above)
nevaldas commented 5 years ago

The 1st reason is true, however, it does not take into account hardcoded texts that might contain such things, for instance, auto-generated html files that might contain product description, which might contain // (which would not represent any comment, especially when HTML comments don't start in such a way). A great example was given by heldersepu. Also, another reason to have HTML comments left would be usage of libraries like Knockout (that you support in this library). I truly understand that this issue is not easy (or nearly impossible) to fix, but yet, there are some reasons for such things to be left. One solution might be to allow overriding rules for specific files. For instance, skip "//" replacement in auto-generated/*/.cshtml files. Not sure how useful it would be for others, but would have been very useful in my case.