Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Razor Comments Introduce Carriage Returns #428

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

Razor Comments Introduce Carriage Returns #428

RehanSaeed opened this issue Jun 23, 2015 · 8 comments

Comments

@RehanSaeed
Copy link

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
Copy link

@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
Copy link
Author

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
Copy link

@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
Copy link

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
Copy link
Member

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
Copy link
Author

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

@danroth27
Copy link
Member

😄

@RehanSaeed
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants