Skip to content
This repository has been archived by the owner. It is now read-only.

Razor Comments Introduce Carriage Returns #428

Closed
RehanSaeed opened this issue Jun 23, 2015 · 8 comments

Comments

Projects
None yet
5 participants
@RehanSaeed
Copy link

commented Jun 23, 2015

Using Razor comments, can cause extra carriage returns to appear in the generated HTML. This has been a problem in MVC 5 also.

<p>Paragraph 1</p>
@* Razor Comment *@
<p>Paragraph 2</p>

The above code generates the following HTML. You can imagine that with a lot of comments you get a lot of blank lines appearing in your code. If you really wanted a blank line to appear, you could add one yourself before the comment.

<p>Paragraph 1</p>

<p>Paragraph 2</p>

Ideally it should generate this HTML:

<p>Paragraph 1</p>
<p>Paragraph 2</p>
@NTaylorMullen

This comment has been minimized.

Copy link
Member

commented Jun 23, 2015

@RehanSaeed The reason why you're seeing the extra carriage returns is due to the newline following the *@. To work around the issue you can do:

<p>Paragraph 1</p>
@* Razor Comment 
*@<p>Paragraph 2</p>

As for if this is right or not: Currently the only Razor comment we have only consumes the content it surrounds. @* to denote the begin of the comment and *@ to denote the end. Therefore the @* comment is working as intended.

You're definitely not the first person to bring up such an issue however. Maybe filing an issue to add the ability to provide single line Razor comments (like C#'s //) would work well.

@RehanSaeed

This comment has been minimized.

Copy link
Author

commented Jun 23, 2015

Adding single line comments sounds nice but that will still mean multi-line comments does not do what most developers expect. I can think of no reason when a developer would want it to add blank lines. The only way to get it to behave as expected is to use your workaround which is pretty ugly.

@NTaylorMullen

This comment has been minimized.

Copy link
Member

commented Jun 23, 2015

@RehanSaeed The Razor comment clearly distinguishes were it starts and where it ends. HTML is white-space sensitive so adding newline consumption to a type of comment that has a start/end would lead to other issues. Take the following examples:

<p>
Hello World @* abc *@
</p>
<p>
Hello 
World @* abc *@
</p>

I'd definitely expect to see a newline in each of these examples.

You could say that if @* ... *@ is on its own line that it could then and only then consume the newline after it however that's why languages like C# invented things like // to deal with comments that consume a line to avoid magicish behavior.

@danroth27 danroth27 added this to the 4.0.0-beta6 milestone Jun 24, 2015

@danroth27 danroth27 modified the milestones: 4.0.0-beta7, 4.0.0-beta6 Jun 24, 2015

@Eilon Eilon modified the milestones: 4.0.0-beta6, 4.0.0-beta7 Jul 8, 2015

@danroth27 danroth27 modified the milestones: 4.0.0-beta6, 4.0.0-beta7 Jul 15, 2015

@NTaylorMullen

This comment has been minimized.

Copy link
Member

commented Jul 16, 2015

Created a "Create Razor single line comment alternative" issue: #451

@NTaylorMullen NTaylorMullen removed this from the 4.0.0-beta7 milestone Jul 16, 2015

@danroth27 danroth27 reopened this Jul 22, 2015

@danroth27

This comment has been minimized.

Copy link
Member

commented Jul 22, 2015

We've decided to be smart about removing whitespace for any non-HTML content that occupies the entire line. So in addition to @RehanSaeed's example above, the following example:

@{
}
<!DOCTYPE html>

Should render just the doctype without any leading whitespace (which would be invalid). Also:

    @foreach (var item in items)
    {
        <li>@item</li>
    }

Should not render a line with four spaces before the list item.

@danroth27 danroth27 added this to the 4.0.0-beta7 milestone Jul 22, 2015

@RehanSaeed

This comment has been minimized.

Copy link
Author

commented Jul 23, 2015

@danroth27 Excellent! Imagine how much wasted bandwidth this change will save over the next decade!

@danroth27

This comment has been minimized.

Copy link
Member

commented Jul 23, 2015

😄

@RehanSaeed

This comment has been minimized.

Copy link
Author

commented Aug 6, 2015

I wrote a blog post about how much bandwidth this feature might have saved the internet here.

@ajaybhargavb ajaybhargavb added 3 - Done and removed 2 - Working labels Aug 13, 2015

ajaybhargavb added a commit to aspnet/Mvc that referenced this issue Aug 13, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.