question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor: The comments added to the html can ruin the markup with a pre tag

See original GitHub issue

Describe the bug

Blazor automatically adds markup to the final html in the form of empty comments <!--!--> this is usually not a problem. However, in the case of the <pre /> tag it can be. When creating a component that encapsulates the <pre /> tag in it, extra lines are added to the rendered output in the html.

To Reproduce

  1. Create a component that has a pre tag (and in my case which makes it worse a code tag)
  2. Render the ChildContent inside the pre (and code)
  3. Consider setting the background color of the pre tag to make the issue more visible
  4. You can also see this in action at: https://chanan.github.io/BlazorStyled/
  5. Editing the html in the browser dev tools, removing the extra <!--!--> tags fixes the output

Expected behavior

I am not sure if this possible as I assume Blazor uses them for something, but removing those extra tags would be great

Screenshots

image

Additional context

3.0.0-preview5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveSandersonMScommented, Jun 3, 2019

Your problem isn’t the comment nodes. Comment nodes don’t affect the layout. Your problem is with whitespace text nodes inside your components.

If you have a component CodeBlock.razor:

<pre style="color: white; background-color: blue;">
    @ChildContent
</pre>

@functions {
    [Parameter] RenderFragment ChildContent { get; set; }
}

… and use it like this:

<CodeBlock>
    This is my code
    And another line
</CodeBlock>

… then you have leading and trailing whitespace in both places. This shows up in the UI. This isn’t unique to Blazor, but is an aspect of HTML generally: https://stackoverflow.com/questions/14946908/how-can-i-ignore-leading-and-trailing-linebreaks-within-an-html-code-block-usi In some cases it’s trickier to work around with Blazor components than it would be with plain HTML, but the underlying principle is the same.

You can avoid the whitespace, e.g.:

<pre style="color: white; background-color: blue;">@ChildContent</pre>

@functions {
    [Parameter] RenderFragment ChildContent { get; set; }
}

… and use it like this:

<CodeBlock>This is my code
And another line</CodeBlock>

I know it’s annoying to have to format your source code like that. One possible alternative would be to use Razor comments to hide the whitespace from the browser:

<CodeBlock>@*              
*@This is my code
Another line@*
*@</CodeBlock>

Arguably we should consider stripping whitespace from the start/end of RenderFragment but it’s not an easy choice to make. Sometimes you might be using the whitespace on purpose because you need it. cc @rynowak in case he has thoughts on that.


Sidenote: The comment nodes are how Blazor tracks which part of the DOM corresponds to which Blazor components. To avoid having them, we’d need to invent and implement a totally different way of tracking this, which would be a significant block of work that could cause issues in other areas. I’m not sure we’d have any reason to do this for the forseeable future, as the comment nodes don’t cause problems in known cases, or at least not mainstream ones.

1reaction
egilcommented, May 30, 2019

It would be nice if the comments could be removed from the output, if not from all generated code, then from select components that match certain conditions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

so many comment tags in blazor rendered html file
The comment tags are generated by the Blazor framework to help track which parts of the DOM correspond to which Blazor Components.
Read more >
Can I disable the use of html tags in Blazor pages?
It is normal to distinguish framework from markup. Then do so. Nothing's stopping you. I want to remove the ability to add html...
Read more >
Comparing Native Blazor Components
We compare native Blazor components to wrapped JavaScript components by understanding how Blazor works, including what native vs. interop means ...
Read more >
<pre>: The Preformatted Text element - HTML - MDN Web Docs
The HTML element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered ......
Read more >
How do I render raw HTML in Blazor?
Raw HTML can be rendered in Blazor by using the MarkupString. You can set the raw HTML as a string to any parameter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found