public static class HtmlHelper {
public static string StripHtmlComments(string html) {
if (html == null) {
throw new ArgumentNullException("html");
}
if (html.IndexOf("<!", StringComparison.Ordinal) < 0) {
return html;
}
var cleanedHtml = new char[html.Length];
bool inHtmlComment = false;
bool inHtmlTag = false;
int cleanCount = 0;
for (int i = 0; i < html.Length; i++) {
char current = html[i];
if (!inHtmlComment && !inHtmlTag) {
if (current == '<') {
if (i + 1 < html.Length) {
char nextChar = html[i + 1];
if (nextChar == '!') {
inHtmlComment = true;
continue;
}
else {
if (IsEnglishLetter(nextChar)) {
inHtmlTag = true;
}
}
}
}
}
else if(inHtmlComment) {
if (current == '>') {
if (inHtmlComment) {
inHtmlComment = false;
continue;
}
}
continue;
}
else if (inHtmlTag) {
if (current == '>') {
inHtmlTag = false;
}
}
cleanedHtml[cleanCount++] = current;
}
return new String(cleanedHtml, 0, cleanCount);
}
private static bool IsEnglishLetter(char nextChar) {
return ('a' <= nextChar && nextChar <= 'z') || ('A' <= nextChar && nextChar <= '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 = "This has <strong>No Comments</strong>!";
Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void StringWithOnlyCommentReturnsEmptyString() {
string s = "<!-- this go bye bye>";
Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithNonDashDashComment_ReturnsEmptyString() {
string s = "<! this go bye bye>";
Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void StringWithTwoConsecutiveCommentsReturnsEmptyString() {
string s = "<!-- this go bye bye><!-- another comment>";
Assert.AreEqual(string.Empty, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void CommentWithStringBeforeReturnsString() {
string s = "Hello<!-- this go bye bye -->";
Assert.AreEqual("Hello", HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void CommentWithStringAfterReturnsString() {
string s = "<!-- this go bye bye -->World";
Assert.AreEqual("World", HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithAngleBracketsButNotHtml_NotSripped() {
string s = "<$)*(@&$(@*>";
Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithCommentInterleavedWithText_RendersText() {
string s = "Hello <!-- this go bye bye --> World <!--> This is fun";
Assert.AreEqual("Hello World This is fun", HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithHtmlTags_DoesNotStripHtml() {
string s = "<strong>Hello</strong><!this go bye bye>";
Assert.AreEqual("<strong>Hello</strong>", HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithCommentInAttribute_DoesNotStripAttributeValue() {
string s = "<img alt=\"<!-- This should remain -->\" />";
Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithCommentInSingleQuotedAttribute_DoesNotStripAttributeValue() {
string s = "<img alt=\'<!-- This should remain -->\' />";
Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithCommentInNonQuotedAttribute_DoesNotStripAttributeValue() {
string s = "<p title=<!--Thisshouldremain-->Test</p>";
Assert.AreEqual(s, HtmlHelper.StripHtmlComments(s));
}
[TestMethod]
public void Html_WithCommentBetweenNonTagButLooksLikeTag_DoesStripComment() {
string s = @"<ç123 title=""<!bc def>"">";
Assert.AreEqual(@"<ç123 title="""">", HtmlHelper.StripHtmlComments(s));
}
Refactorings
No refactoring yet !
Elij
November 11, 2008, November 11, 2008 04:52, permalink
haven't tested -- but if it doesn't work it'll just be a case of reviewing the regex
public static class HtmlHelper {
public static string StripHtmlComments(string html) {
if (html == null) {
throw new ArgumentNullException("html");
}
System.Text.RegularExpressions.Regex regex =
new System.Text.RegularExpressions.Regex("((<!-- )((?!<!-- ).)*( -->))");
return regex.Replace(html, string.Empty);
}
}
Moonshield
November 11, 2008, November 11, 2008 05:14, permalink
I ran the test unit on the regex : 7/15 passed. If you still want to do it with a loop I've cleaned your code a little. I can take a look tomorrow too. I'm too tired to think tonight :)
public static class HtmlHelper
{
public static string StripHtmlComments(string pHtml)
{
if (pHtml == null)
throw new ArgumentNullException("pHtml");
// Contains cleaned content
StringBuilder oSb = new StringBuilder();
// Not comment --> skip
if (pHtml.IndexOf("<!", StringComparison.Ordinal) < 0)
oSb.Append(pHtml);
// Not comment --> skip
if (oSb.Length == 0)
{
bool InHtmlComment = false;
bool InHtmlTag = false;
char CurrentChar;
char NextChar;
for (int CharIndex = 0; CharIndex < pHtml.Length; CharIndex++)
{
CurrentChar = pHtml[CharIndex];
if (!InHtmlComment && !InHtmlTag)
{
if (CurrentChar == '<' && (CharIndex + 1 < pHtml.Length))
{
NextChar = pHtml[CharIndex + 1];
if (NextChar == '!')
{
InHtmlComment = true;
continue;
}
else if (NextChar.IsEnglishLetter())
InHtmlTag = true;
}
}
else if (InHtmlComment)
{
if (CurrentChar == '>')
{
if (InHtmlComment)
{
InHtmlComment = false;
continue;
}
}
continue;
}
else if (InHtmlTag && CurrentChar == '>')
InHtmlTag = false;
oSb.Append(CurrentChar);
}
}
// One entry point, one return point
return oSb.ToString();
}
private static bool IsEnglishLetter(this char nextChar)
{
return ('a' <= nextChar && nextChar <= 'z') || ('A' <= nextChar && nextChar <= 'Z');
}
}
Jeff Atwood
November 16, 2009, November 16, 2009 04:25, permalink
That "invalid HTML tag" case is really tough, a real edge condition. If you discard that one, it's pretty easy. I'd recommend doing a first pass to remove all invalid HTML tags, first. Like.. er.. <ç123>.
public static string StripHtmlComments(string html)
{
if (html == null)
{
throw new ArgumentNullException("html");
}
if (html.IndexOf("<!", StringComparison.Ordinal) < 0)
{
return html;
}
return Regex.Replace(html, "(?<!='|=\"|=)<![^>]+>", "");
}
asgddhd
February 3, 2010, February 03, 2010 01:50, permalink
Desktop Security 2010 is the new antivirus software which is created according to the up-to-date requirements of computer protection. The main aim of this product is to make your computer usage efficient and secure. Multilevel protection of the Desktop Security 2010 antivirus will provide your data integrity and safety, and master components of the program will prevent computer from infection and hack attacks. Special attention is devoted to the network and internet connections security because it is the most probable way of virus intrusion.
1. For me it became a rescuer after a serious virus infection of my pc. Kaspersky antivirus didn't cure viruses, and NOD antivirus did not even detect them. Desktop Security 2010 detected all the viruses very quickly and suggested to cure them. On the whole for now it has been working at my pc for a couple of months as the main antivirus, I don't have any problems with viruses, just sometimes while surfing the web I get messages about attempting intrusions which my Desktop Security 2010 blocks. I am very satisfied with its work.
2. Earlier I did not consider anything except for NOD antivirus. But recently I've been recovering my data for hours after virus intrusion to my computer, and this case happened several times. I understood that I needed to look for something new because my NOD antivirus became too choosy. Kaspersky antivirus is not a variant for me - each computer with a core duo processor hardly works with it. Dr Web is rather old-fashioned. I heard good recommendations about Desktop Security 2010 - and installed it, and it even cured a few viruses after NOD antivirus. Now I just never remind of my problems with viruses.
3. I think that it is one of the best anti-viruses, at least for me - it provides user-friendly menu, easy settings, it doesn't slow down my pc and its visual environment looks nice. It's a pretty good antivirus.
download Desktop Security 2010
virus Desktop Security 2010
antivirus Desktop Security 2010
trojan Desktop Security 2010
remove Desktop Security 2010
buy Desktop Security 2010
cheap Desktop Security 2010
strider
August 25, 2011, August 25, 2011 01:39, permalink
Pienso que no sois derecho. Puedo demostrarlo. Escriban en PM.
http://rsfiles.servehttp.com/
iomar
Kemaatrollirm
November 2, 2011, November 02, 2011 10:20, permalink
Illinois pikavipit offers the buyer an opportune strategy that enables them be able to pay out their own credit24 by phone rather than always invest some time and cash travelling to their offices. Purchasing the insurance will make sure which in case there is a car accident then your Illinois lainat company is capable responsible for the loss of men and women as well as home involved, healthcare expenditures, lost wages and also the discomfort that people who are involved in the crash go through. The particular The state of illinois pikavipin does not think about factors like on the reason behind the particular automobile accident, the volume of the property dropped and also the choice results of your accident.
Sunswsses
January 4, 2012, January 04, 2012 14:45, permalink
http://www.sunglassglasses.com/ray-ban-sunglasses.html is well known for their two predominating sunglasses the Aviator as sedately as the Wayfarer. Regardless of a aged supine dialect gossoon or possibly a sunset voter, if you hanker the ostentatiously sunglasses liable whatsoever in rank of shift and lens importance, shaft ban mark-down may be the human being to pick when you are guaranteed to look orderly and protect your vision while reveling life looking for the broadest. Replica http://www.sunglassglasses.com/ray-ban-sunglasses.html RayBan Sunglass have on the agenda c trick been peculiar instead of a long fourth dimension, so you can assume trust to their sunglasses are whatsoever extended and hot.Ostentate on each observation from the followings of comportment dominion be establish with undivided or more couple of Modify http://www.sunglassglasses.com/ray-ban-sunglasses.html RayBan Sunglass.You can stumble on amount of online stores where it is practicable to win this beautifully carved out assortment of fake tawdry http://www.sunglassglasses.com/ray-ban-sunglasses.html RayBan Sunglass, that has a deal in value of all over $20 billion and is very readying financial sling books in requital for budding buyers. Together with those base prices, With myriad designs and label logos,by. If you fundamental unlimited everything. seeing as there are links contents it that is to be updated continuously pinguid disadvantage info equates,via web.If you are looking for a bent, the most qualified offering doesn't at most present to her that you entirely envisage her growing to a gal but additionally something which she could money forever,Fortunately there stay several ways that you may location a fake. The Price This may become visible a teeny-weeny too obvious but you are not likely to discover a trade mark rattling new twins of natural http://www.sunglassglasses.com/ray-ban-sunglasses.html RayBan Sunglass Wayfarer sunglasses in compensation $30 anywhere. Peradventure the Junior Wayfarer models costs more than this. In any event , because 2 http://www.sunglassglasses.com/ray-ban-sunglasses.html RayBan Sunglass purpose sell for more than $100, doesn't perforce allude to it in all honesty is authentic. It may be a totally dishonest seller maddening to ameliorate his margins during hiking within the price to shenanigans gullible purchasers.
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. ;)