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

Razor does not respect html comments inside a razor block #302

Closed
yishaigalatzer opened this issue Feb 11, 2015 · 12 comments
Closed

Razor does not respect html comments inside a razor block #302

yishaigalatzer opened this issue Feb 11, 2015 · 12 comments

Comments

@yishaigalatzer
Copy link
Contributor

See original comment - https://aspnetwebstack.codeplex.com/workitem/2233

Does not work

@{
    <!-- Hello, I'm a comment that will break razor --->
}

Works

    <!-- Hello, I'm a comment that will break razor --->

If you have more than 2 dashes at the end of the comment - Razor shows compilation error. Only 2 dashes are accepted. However, if this comment is outside a code block, it works fine.

@dougbu
Copy link
Member

dougbu commented Feb 11, 2015

@yishaigalatzer HTML 5 (like XML) disallows ending a comment with --->. should Razor ignore the issue (everywhere), warn, or do something else? I agree the behaviour should be consistent regardless of C# block versus HTML context.

just out of interest, does

@{
  <text><!-- Hello, I'm a comment that will break razor --></text>
}

or the same with the not-well-formed triple-dash comment result in a generated HTML comment?

@yishaigalatzer
Copy link
Contributor Author

Browsers do allow it, so we can be nicer about it

@am11
Copy link

am11 commented Apr 14, 2016

This issue is scoped to odd number of hyphens before CloseAngle in endSequence.
I think this patch will fix the issue:

diff --git a/src/Microsoft.AspNet.Razor/Parser/HtmlLanguageCharacteristics.cs b/src/Microsoft.AspNet.Razor/Parser/HtmlLanguageCharacteristics.cs
index dfb1c5e..4a35c33 100644
--- a/src/Microsoft.AspNet.Razor/Parser/HtmlLanguageCharacteristics.cs
+++ b/src/Microsoft.AspNet.Razor/Parser/HtmlLanguageCharacteristics.cs
@@ -43,6 +43,8 @@ public override string GetSample(HtmlSymbolType type)
                     return "?";
                 case HtmlSymbolType.DoubleHyphen:
                     return "--";
+                case HtmlSymbolType.SingleHyphen:
+                    return "-";
                 case HtmlSymbolType.LeftBracket:
                     return "[";
                 case HtmlSymbolType.CloseAngle:
diff --git a/src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs b/src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs
index 0f6a6d5..4ecd074 100644
--- a/src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs
+++ b/src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs
@@ -252,7 +252,11 @@ private bool BangTag()
                 if (CurrentSymbol.Type == HtmlSymbolType.DoubleHyphen)
                 {
                     AcceptAndMoveNext();
-                    return AcceptUntilAll(HtmlSymbolType.DoubleHyphen, HtmlSymbolType.CloseAngle);
+                    while(CurrentSymbol.Type == HtmlSymbolType.SingleHyphen)
+                    {
+                        AcceptAndMoveNext();
+                    }
+                    return AcceptUntilAll(HtmlSymbolType.CloseAngle);
                 }
                 else if (CurrentSymbol.Type == HtmlSymbolType.LeftBracket)
                 {
diff --git a/src/Microsoft.AspNet.Razor/Tokenizer/Symbols/HtmlSymbolType.cs b/src/Microsoft.AspNet.Razor/Tokenizer/Symbols/HtmlSymbolType.cs
index 27cbd7e..ab27157 100644
--- a/src/Microsoft.AspNet.Razor/Tokenizer/Symbols/HtmlSymbolType.cs
+++ b/src/Microsoft.AspNet.Razor/Tokenizer/Symbols/HtmlSymbolType.cs
@@ -17,6 +17,7 @@ public enum HtmlSymbolType
         ForwardSlash, // /
         QuestionMark, // ?
         DoubleHyphen, // --
+        SingleHyphen, // -
         LeftBracket, // [
         CloseAngle, // >
         RightBracket, // ]

@Eilon Eilon removed this from the Backlog milestone Jan 9, 2017
@Eilon Eilon removed the 0 - Backlog label Jan 9, 2017
@Eilon
Copy link
Member

Eilon commented Mar 14, 2017

@NTaylorMullen can you verify if this is fixed? If so, please add a test for it.

@NTaylorMullen
Copy link
Contributor

Yup, this is fixed 😄

@Eilon
Copy link
Member

Eilon commented Mar 15, 2017

And there's a test for this exact case?

@NTaylorMullen
Copy link
Contributor

Yup 😄

@Eilon
Copy link
Member

Eilon commented Mar 15, 2017

Actually I don't see that case there. This particular case has 2 dashes <!-- and then 3 dashes --->. Can we add this specific case in case that has some bizarre impact on this case?

@Eilon
Copy link
Member

Eilon commented Mar 15, 2017

(All the cases in that test have a matching number of dashes.)

@NTaylorMullen
Copy link
Contributor

Can we add this specific case in case that has some bizarre impact on this case?

Sure

@rynowak
Copy link
Member

rynowak commented Apr 10, 2017

Let's just add this test and close this out.

@NTaylorMullen
Copy link
Contributor

49e3533

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

6 participants