<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users663friends</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/663/friends" rel="self"/>
  <title>GateKiller friends</title>
  <updated>Tue Nov 11 01:14:04 +0000 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code597</id>
    <published>2008-11-11T01:14:04+00:00</published>
    <updated>2008-11-11T05:14:29+00:00</updated>
    <title>[C#] Strip Html Comments</title>
    <content type="html">&lt;p&gt;Procedural style code to strip HTML comments. No attempt was made to make this code more OO/patterned base so it could be readable/maintainable. ;)&lt;/p&gt;

&lt;pre&gt;public static class HtmlHelper {
    public static string StripHtmlComments(string html) {
        if (html == null) {
            throw new ArgumentNullException(&amp;quot;html&amp;quot;);
        }

        if (html.IndexOf(&amp;quot;&amp;lt;!&amp;quot;, StringComparison.Ordinal) &amp;lt; 0) {
            return html;
        }

        var cleanedHtml = new char[html.Length];
        bool inHtmlComment = false;
        bool inHtmlTag = false;
        int cleanCount = 0;

        for (int i = 0; i &amp;lt; html.Length; i++) {
            char current = html[i];

            if (!inHtmlComment &amp;amp;&amp;amp; !inHtmlTag) {
                if (current == '&amp;lt;') {
                    if (i + 1 &amp;lt; html.Length) {
                        char nextChar = html[i + 1];
                        if (nextChar == '!') {
                                inHtmlComment = true;
                                continue;
                            }
                            else {
                                if (IsEnglishLetter(nextChar)) {
                                    inHtmlTag = true;
                                }
                            }
                        }
                    }
                }
                else if(inHtmlComment) {
                    if (current == '&amp;gt;') {
                        if (inHtmlComment) {
                            inHtmlComment = false;
                            continue;
                        }
                    }
                    continue;
                }
                else if (inHtmlTag) {
                    if (current == '&amp;gt;') {
                        inHtmlTag = false;
                    }
                }

                cleanedHtml[cleanCount++] = current;
            }

            return new String(cleanedHtml, 0, cleanCount);
        }

        private static bool IsEnglishLetter(char nextChar) {
            return ('a' &amp;lt;= nextChar &amp;amp;&amp;amp; nextChar &amp;lt;= 'z') || ('A' &amp;lt;= nextChar &amp;amp;&amp;amp; nextChar &amp;lt;= 'Z');
        }

}

//Unit Tests
[TestMethod]
public void NullStringReturnsThrowsArgumentNullException() {
    try {
        HtmlHelper.StripHtmlComments(null);
        Assert.Fail();
    }
    catch (ArgumentNullException) { 
    }
}

[TestMethod]
public void EmptyStringReturnsEmpty() {
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(string.Empty));
}

[TestMethod]
public void StringWithoutCommentReturnsSameString() {
    string s = &amp;quot;This has &amp;lt;strong&amp;gt;No Comments&amp;lt;/strong&amp;gt;!&amp;quot;;
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void StringWithOnlyCommentReturnsEmptyString() {
    string s = &amp;quot;&amp;lt;!-- this go bye bye&amp;gt;&amp;quot;;
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithNonDashDashComment_ReturnsEmptyString() {
    string s = &amp;quot;&amp;lt;! this go bye bye&amp;gt;&amp;quot;;
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void StringWithTwoConsecutiveCommentsReturnsEmptyString() {
    string s = &amp;quot;&amp;lt;!-- this go bye bye&amp;gt;&amp;lt;!-- another comment&amp;gt;&amp;quot;;
    Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void CommentWithStringBeforeReturnsString() {
    string s = &amp;quot;Hello&amp;lt;!-- this go bye bye --&amp;gt;&amp;quot;;
    Assert.AreEqual(&amp;quot;Hello&amp;quot;, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void CommentWithStringAfterReturnsString() {
    string s = &amp;quot;&amp;lt;!-- this go bye bye --&amp;gt;World&amp;quot;;
    Assert.AreEqual(&amp;quot;World&amp;quot;, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithAngleBracketsButNotHtml_NotSripped() {
    string s = &amp;quot;&amp;lt;$)*(@&amp;amp;$(@*&amp;gt;&amp;quot;;
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInterleavedWithText_RendersText() {
    string s = &amp;quot;Hello &amp;lt;!-- this go bye bye --&amp;gt; World &amp;lt;!--&amp;gt; This is fun&amp;quot;;
    Assert.AreEqual(&amp;quot;Hello  World  This is fun&amp;quot;, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithHtmlTags_DoesNotStripHtml() {
    string s = &amp;quot;&amp;lt;strong&amp;gt;Hello&amp;lt;/strong&amp;gt;&amp;lt;!this go bye bye&amp;gt;&amp;quot;;
    Assert.AreEqual(&amp;quot;&amp;lt;strong&amp;gt;Hello&amp;lt;/strong&amp;gt;&amp;quot;, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInAttribute_DoesNotStripAttributeValue() {
    string s = &amp;quot;&amp;lt;img alt=\&amp;quot;&amp;lt;!-- This should remain --&amp;gt;\&amp;quot; /&amp;gt;&amp;quot;;
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInSingleQuotedAttribute_DoesNotStripAttributeValue() {
    string s = &amp;quot;&amp;lt;img alt=\'&amp;lt;!-- This should remain --&amp;gt;\' /&amp;gt;&amp;quot;;
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentInNonQuotedAttribute_DoesNotStripAttributeValue() {
    string s = &amp;quot;&amp;lt;p title=&amp;lt;!--Thisshouldremain--&amp;gt;Test&amp;lt;/p&amp;gt;&amp;quot;;
    Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}

[TestMethod]
public void Html_WithCommentBetweenNonTagButLooksLikeTag_DoesStripComment() {
    string s = @&amp;quot;&amp;lt;&#231;123 title=&amp;quot;&amp;quot;&amp;lt;!bc def&amp;gt;&amp;quot;&amp;quot;&amp;gt;&amp;quot;;
    Assert.AreEqual(@&amp;quot;&amp;lt;&#231;123 title=&amp;quot;&amp;quot;&amp;quot;&amp;quot;&amp;gt;&amp;quot;, HtmlHelper.StripHtmlComments(s));
}

&lt;/pre&gt;</content>
    <author>
      <name>Haacked</name>
      <email>haacked@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/597-strip-html-comments" rel="alternate"/>
  </entry>
</feed>
