Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Ultra lightweight message "popup" at top of parent element
Good way to output menu with submenu through a module?!
Moving Code from Controller to Model
Get Site Information with PHP
Help with module?!
Node.js: Calculating total filesize of 3 files
convert single itemed sub-arrays into string
Access HashMap key by value
Language Selection
Generating a list of 3 entries with the rest hidden
Popular
Good way to output menu with submenu through a module?!
Ultra lightweight message "popup" at top of parent element
Moving Code from Controller to Model
Double 'if'
Language Selection
Help with module?!
Fetch and parse feeds with feedzirra
Generating a list of 3 entries with the rest hidden
Access HashMap key by value
Node.js: Calculating total filesize of 3 files
Pastable version of
Strip Html Comments
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155</pre> <pre class="sunburst"><span style="color:#E28964;">public</span> <span style="color:#E28964;">static</span> <span style="color:#99CF50;">class</span> HtmlHelper { <span style="color:#E28964;">public</span> <span style="color:#E28964;">static</span> <span style="color:#99CF50;">string</span> StripHtmlComments(<span style="color:#99CF50;">string</span> html) { <span style="color:#E28964;">if</span> (html == <span style="color:#E28964;">null</span>) { <span style="color:#E28964;">throw</span> <span style="color:#E28964;">new</span> ArgumentNullException(<span style="color:#65B042;"><span style="color:#65B042;">"</span>html<span style="color:#65B042;">"</span></span>); } <span style="color:#E28964;">if</span> (html.IndexOf(<span style="color:#65B042;"><span style="color:#65B042;">"</span><!<span style="color:#65B042;">"</span></span>, StringComparison.Ordinal) < <span style="color:#3387CC;">0</span>) { <span style="color:#E28964;">return</span> html; } var cleanedHtml = <span style="color:#E28964;">new</span> <span style="color:#99CF50;">char</span>[html.Length]; <span style="color:#99CF50;">bool</span> inHtmlComment = <span style="color:#E28964;">false</span>; <span style="color:#99CF50;">bool</span> inHtmlTag = <span style="color:#E28964;">false</span>; <span style="color:#99CF50;">int</span> cleanCount = <span style="color:#3387CC;">0</span>; <span style="color:#E28964;">for</span> (<span style="color:#99CF50;">int</span> i = <span style="color:#3387CC;">0</span>; i < html.Length; i++) { <span style="color:#99CF50;">char</span> current = html[i]; <span style="color:#E28964;">if</span> (!inHtmlComment && !inHtmlTag) { <span style="color:#E28964;">if</span> (current == <span style="color:#65B042;"><span style="color:#65B042;">'</span><<span style="color:#65B042;">'</span></span>) { <span style="color:#E28964;">if</span> (i + <span style="color:#3387CC;">1</span> < html.Length) { <span style="color:#99CF50;">char</span> nextChar = html[i + <span style="color:#3387CC;">1</span>]; <span style="color:#E28964;">if</span> (nextChar == <span style="color:#65B042;"><span style="color:#65B042;">'</span>!<span style="color:#65B042;">'</span></span>) { inHtmlComment = <span style="color:#E28964;">true</span>; <span style="color:#E28964;">continue</span>; } <span style="color:#E28964;">else</span> { <span style="color:#E28964;">if</span> (IsEnglishLetter(nextChar)) { inHtmlTag = <span style="color:#E28964;">true</span>; } } } } } <span style="color:#E28964;">else</span> <span style="color:#E28964;">if</span>(inHtmlComment) { <span style="color:#E28964;">if</span> (current == <span style="color:#65B042;"><span style="color:#65B042;">'</span>><span style="color:#65B042;">'</span></span>) { <span style="color:#E28964;">if</span> (inHtmlComment) { inHtmlComment = <span style="color:#E28964;">false</span>; <span style="color:#E28964;">continue</span>; } } <span style="color:#E28964;">continue</span>; } <span style="color:#E28964;">else</span> <span style="color:#E28964;">if</span> (inHtmlTag) { <span style="color:#E28964;">if</span> (current == <span style="color:#65B042;"><span style="color:#65B042;">'</span>><span style="color:#65B042;">'</span></span>) { inHtmlTag = <span style="color:#E28964;">false</span>; } } cleanedHtml[cleanCount++] = current; } <span style="color:#E28964;">return</span> <span style="color:#E28964;">new</span> String(cleanedHtml, <span style="color:#3387CC;">0</span>, cleanCount); } <span style="color:#E28964;">private</span> <span style="color:#E28964;">static</span> <span style="color:#99CF50;">bool</span> IsEnglishLetter(<span style="color:#99CF50;">char</span> nextChar) { <span style="color:#E28964;">return</span> (<span style="color:#65B042;"><span style="color:#65B042;">'</span>a<span style="color:#65B042;">'</span></span> <= nextChar && nextChar <= <span style="color:#65B042;"><span style="color:#65B042;">'</span>z<span style="color:#65B042;">'</span></span>) || (<span style="color:#65B042;"><span style="color:#65B042;">'</span>A<span style="color:#65B042;">'</span></span> <= nextChar && nextChar <= <span style="color:#65B042;"><span style="color:#65B042;">'</span>Z<span style="color:#65B042;">'</span></span>); } } <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span>Unit Tests</span> [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> NullStringReturnsThrowsArgumentNullException() { <span style="color:#E28964;">try</span> { HtmlHelper.StripHtmlComments(<span style="color:#E28964;">null</span>); Assert.Fail(); } <span style="color:#E28964;">catch</span> (ArgumentNullException) { } } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> EmptyStringReturnsEmpty() { Assert.AreEqual(<span style="color:#99CF50;">string</span>.Empty, HtmlHelper.StripHtmlComments(<span style="color:#99CF50;">string</span>.Empty)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> StringWithoutCommentReturnsSameString() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span>This has <strong>No Comments</strong>!<span style="color:#65B042;">"</span></span>; Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> StringWithOnlyCommentReturnsEmptyString() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><!-- this go bye bye><span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#99CF50;">string</span>.Empty, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithNonDashDashComment_ReturnsEmptyString() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><! this go bye bye><span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#99CF50;">string</span>.Empty, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> StringWithTwoConsecutiveCommentsReturnsEmptyString() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><!-- this go bye bye><!-- another comment><span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#99CF50;">string</span>.Empty, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> CommentWithStringBeforeReturnsString() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span>Hello<!-- this go bye bye --><span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#65B042;"><span style="color:#65B042;">"</span>Hello<span style="color:#65B042;">"</span></span>, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> CommentWithStringAfterReturnsString() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><!-- this go bye bye -->World<span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#65B042;"><span style="color:#65B042;">"</span>World<span style="color:#65B042;">"</span></span>, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithAngleBracketsButNotHtml_NotSripped() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><$)*(@&$(@*><span style="color:#65B042;">"</span></span>; Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithCommentInterleavedWithText_RendersText() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span>Hello <!-- this go bye bye --> World <!--> This is fun<span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#65B042;"><span style="color:#65B042;">"</span>Hello World This is fun<span style="color:#65B042;">"</span></span>, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithHtmlTags_DoesNotStripHtml() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><strong>Hello</strong><!this go bye bye><span style="color:#65B042;">"</span></span>; Assert.AreEqual(<span style="color:#65B042;"><span style="color:#65B042;">"</span><strong>Hello</strong><span style="color:#65B042;">"</span></span>, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithCommentInAttribute_DoesNotStripAttributeValue() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><img alt=<span style="color:#DDF2A4;">\"</span><!-- This should remain --><span style="color:#DDF2A4;">\"</span> /><span style="color:#65B042;">"</span></span>; Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithCommentInSingleQuotedAttribute_DoesNotStripAttributeValue() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><img alt=<span style="color:#DDF2A4;">\'</span><!-- This should remain --><span style="color:#DDF2A4;">\'</span> /><span style="color:#65B042;">"</span></span>; Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithCommentInNonQuotedAttribute_DoesNotStripAttributeValue() { <span style="color:#99CF50;">string</span> s = <span style="color:#65B042;"><span style="color:#65B042;">"</span><p title=<!--Thisshouldremain-->Test</p><span style="color:#65B042;">"</span></span>; Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s)); } [TestMethod] <span style="color:#E28964;">public</span> <span style="color:#E28964;">void</span> Html_WithCommentBetweenNonTagButLooksLikeTag_DoesStripComment() { <span style="color:#99CF50;">string</span> s = @<span style="color:#65B042;"><span style="color:#65B042;">"</span><ç123 title=<span style="color:#65B042;">"</span></span><span style="color:#65B042;"><span style="color:#65B042;">"</span><!bc def><span style="color:#65B042;">"</span></span><span style="color:#65B042;"><span style="color:#65B042;">"</span>><span style="color:#65B042;">"</span></span>; Assert.AreEqual(@<span style="color:#65B042;"><span style="color:#65B042;">"</span><ç123 title=<span style="color:#65B042;">"</span></span><span style="color:#65B042;"><span style="color:#65B042;">"</span><span style="color:#65B042;">"</span></span><span style="color:#65B042;"><span style="color:#65B042;">"</span>><span style="color:#65B042;">"</span></span>, HtmlHelper.StripHtmlComments(s)); } </pre> </div> </div> <a href="http://refactormycode.com/codes/597-strip-html-comments" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>