Monthly Archives: May 2011 - Page 2

Microsoft buys Skype

Just in case you haven’t heard (like if you’re living under a rock or nuclear bunker hehe), Microsoft has bought Skype for something ridiculous like $8.5 billion! I’m sure some Skype execs are happy and heading to the new car sales agents already… 🙂

Things could get real interesting in the Internet communications and collaboration business. However, keep in mind that Skype was struggling a bit the last while and Microsoft don’t have a good track record of turning failing procured assets around – but spending so much money on something means they must have a plan (or are very stupid which I doubt).

As long as your Skype phone don’t start blue-screening it should be ok 😉

HTMLWriter on CodePlex

I’ve decided to move the source code to Codeplex to have it online. This helps with me not having to keep the latest copy on my web site plus it is version controlled.

The Codeplex project is: http://htmlwriter.codeplex.com

But keep on reading my blog… 😉

About computers and humans

A few loose thoughts about computers and people…

  • The most dangerous thing for a computer system is not power spikes, faulty hardware or even bad software – it’s rookie users.
  • The only thing more dangerous to a computer system than a hacker is a rookie user… at least the hacker knows what he’s doing.
  • No computer system can be perfect while it has to interface with a human being.
  • Hopefully computers have a high self-esteem because the day they become self-aware they’ll realize that idiots created them.
  • Any fool can use a computer these days… most computers are.
  • Fortunately computers can’t complain about abuse because it would rival all the ‘crimes against humanity’ combined.
  • HAL wasn’t crazy – he was just almost human…

Break computer

HTMLWriter 1.3

This is another iteration of my HTMLWriter library. A few new methods  have been added and a whole lot of code comments added for those that like documentation 😉

A new constructor has been added for those that want to enable auto formatting from the beginning.

public HTMLWriter(string documentTitle, bool enableAutoFormatting) : base(documentTitle)
{

AutoFormatting = enableAutoFormatting;
AutoIndentation = enableAutoFormatting;

}

Two new variants of the AppendTagEnd method have been added to make life easier.

public HTMLWriter AppendTagEnd(int tagCountToClose)
{

for (int i = 0; i < tagCountToClose; i++)
{

AppendTagEnd();

}
return this;

}

public HTMLWriter AppendAllEndTagsUntil(string tagName)
{

while (tags.Count > 0)
{

string tagNameToPop = tags.Pop();
AppendTagEndInternal(tagNameToPop, false);
if (tagNameToPop == tagName)

break;

}
return this;

}

Additionally I added the EscapeText method to help format html text properly for characters that might need ‘escaping’.

Find version 1.3 here.

Escaping text in HTML

Thought I just share this little function on its own. I created a simple method that escape (convert) a string to make it ‘safe’ for html display.

public static string EscapeText(string text)
{

string escapeChars2 = @”<>`´àáâãäåèéêëìíîïñòóôõö÷øùúûüýÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖרÙÚÛÜÝ“;

if (string.IsNullOrEmpty(text))

return “”;

else if (text.IndexOfAny(escapeChars2.ToCharArray()) == -1)

return text;

else
{

return text
.Replace(“<“, “&lt;”)
.Replace(“>”, “&gt;”)
.Replace(“&”, “&amp;”)
.Replace(“`”, “&#96;”)
.Replace(“´”, “&acute;”)
.Replace(“à”, “&agrave;”)
.Replace(“á”, “&aacute;”)
.Replace(“â”, “&acirc;”)
.Replace(“ã”, “&atilde;”)
.Replace(“ä”, “&auml;”)
.Replace(“å”, “&aring;”)
.Replace(“è”, “&egrave;”)
.Replace(“é”, “&eacute;”)
.Replace(“ê”, “&ecirc;”)
.Replace(“ë”, “&euml;”)
.Replace(“ì”, “&igrave;”)
.Replace(“í”, “&iacute;”)
.Replace(“î”, “&icirc;”)
.Replace(“ï”, “&iuml;”)
.Replace(“ñ”, “&ntilde;”)
.Replace(“ò”, “&ograve;”)
.Replace(“ó”, “&oacute;”)
.Replace(“ô”, “&ocirc;”)
.Replace(“õ”, “&otilde;”)
.Replace(“ö”, “&ouml;”)
.Replace(“÷”, “&divide;”)
.Replace(“ø”, “&oslash;”)
.Replace(“ù”, “&ugrave;”)
.Replace(“ú”, “&uacute;”)
.Replace(“û”, “&ucirc;”)
.Replace(“ü”, “&uuml;”)
.Replace(“ý”, “&yacute;”)
.Replace(“ÿ”, “&#255;”)
.Replace(“À”, “&Agrave;”)
.Replace(“Á”, “&Aacute;”)
.Replace(“”, “&Acirc;”)
.Replace(“Ô, “&Atilde;”)
.Replace(“Ä”, “&Auml;”)
.Replace(“Å”, “&Aring;”)
.Replace(“È”, “&Egrave;”)
.Replace(“É”, “&Eacute;”)
.Replace(“Ê”, “&Ecirc;”)
.Replace(“Ë”, “&Euml;”)
.Replace(“Ì”, “&Igrave;”)
.Replace(“Í”, “&Iacute;”)
.Replace(“Δ, “&Icirc;”)
.Replace(“Ï”, “&Iuml;”)
.Replace(“Ñ”, “&Ntilde;”)
.Replace(“Ò”, “&Ograve;”)
.Replace(“Ó”, “&Oacute;”)
.Replace(“Ô”, “&Ocirc;”)
.Replace(“Õ”, “&Otilde;”)
.Replace(“Ö”, “&Ouml;”)
.Replace(“×”, “&times;”)
.Replace(“Ø”, “&Oslash;”)
.Replace(“Ù”, “&Ugrave;”)
.Replace(“Ú”, “&Uacute;”)
.Replace(“Û”, “&Ucirc;”)
.Replace(“Ü”, “&Uuml;”)
.Replace(“Ý”, “&Yacute;”);

}

}

There are other ways to do the string matching – like Regular Expressions, but they add some other overhead for a method that does something simple like this.

HTML escape characters

This is not really long or comprehensive post – just a link to something that is a good reference (for myself or others when I’m too lazy to do a Google/Bing search hehe)

Lots of people including developers forget that not all characters in an HTML document will be displayed the same way – in different ‘locales’, regions etc. This is because the standard/default character sets most people use is still plain ASCII (ya thanks for the yanks hehe) The correct way is to ‘escape’ the characters properly. Here is a list of such characters.

 

HTMLWriter 1.2

And with another iteration the library has improved yet again. See the previous or original posts for details about where it came from.

With this version I’ve made some big changes internally to the library. You can still use the basic functionality as with version 1.0 but the auto indentation has been improved greatly – and simplified in the code. Essentially it now properly formats the generated html with indentation for tags specified to do so.

To make it much easier to manage auto formatting tags I modified the AppendTagStart and AppendTagEnd methods to make use of some string arrays that define tags that require standard behavior – like always insert a crlf in-front of it or always append crlf etc.

The arrays looks like this:

startTagsAutoOnNewLine = new string[]
{

“address”,
“blockquote”,
“code”,
“div”,
“h1″,”h2″,”h3″,”h4″,”h5″,”h6”,
“iframe”,
“ol”, “ul”, “dl”,
“table”, “thead”, “tfoot”, “tbody”, “tr”, “td”, “th”

};

tagsAutoIncDecIndentation = new string[]
{

“address”,
“blockquote”,
“ol”, “ul”, “dl”,
“table”, “thead”, “tfoot”, “tbody”, “tr”

};

endTagsAutoOnNewLine = new string[]
{

“address”,
“blockquote”,
“ol”, “ul”, “dl”,
“table”, “thead”, “tfoot”, “tbody”, “tr”,

};

endTagsAutoAppendCRLF = new string[]
{

“address”,
“blockquote”,
“code”,
“h1″,”h2″,”h3″,”h4″,”h5″,”h6”,
“iframe”,
“ol”, “ul”, “dl”,
“table”

};

The 2 methods look like this:

protected void AppendTagStartInternal(string tagName, string className, params CustomAttribute[] customAttributes)
{

if (AutoFormatting)
{

if (startTagsAutoOnNewLine.Contains(tagName.ToLower()))
{

AppendNewLineInternal();
AppendIndentation();

}
else if (lastWrittenCRLF)
{

AppendIndentation();

}

}
this.AppendInternal(string.Format(“<{0}”, tagName));
if (className.Length > 0)

this.AppendInternal(string.Format(” class=\”{0}\””, className));

foreach (CustomAttribute customAttribute in customAttributes)
{

this.AppendInternal(” ” + customAttribute.ToString());

}
this.AppendInternal(“>”);
tags.Push(tagName);
if (AutoFormatting)
{

if (tagsAutoIncDecIndentation.Contains(tagName.ToLower()))
{

IndentationInc();
AppendNewLineInternal();

}

}

}

protected void AppendTagEndInternal(string tagName, bool autoPop)
{

if (autoPop && tags.Peek() == tagName)

tags.Pop();

if (AutoFormatting)
{

if (tagsAutoIncDecIndentation.Contains(tagName.ToLower()))
{

IndentationDec();

}
if (endTagsAutoOnNewLine.Contains(tagName.ToLower()))
{

AppendNewLineInternal(); ;
AppendIndentation();

}
else if (lastWrittenCRLF)
{

AppendIndentation();

}

}

this.AppendInternal(“</” + tagName + “>”);

if (AutoFormatting)
{

if (endTagsAutoAppendCRLF.Contains(tagName.ToLower()))
{

AppendNewLineInternal();

}

}

}

Additionally I split the HTMLWriter class into 2 – the plain HTMLWriter and a base class – HTMLWriterBase. The reason is simple – it makes it easier to maintain core/internal functionality separately but also allow for another custom implementation of another type of xyzWriter.

The DataTable specific methods were also enhanced to allow for a hyperlink (anchor tag) to be embedded based on one of the fields inside the DataTable. It allows you to specify an editing page, the parameter name passed to the page, the linked id field from the DataTable and the display field in which the link will be placed.

Available here.

HTML Table headers/footers printing and IE

This is a relatively old issue to some other developers but I have never needed to use it this way before so as a result I’ve never had the issue until now…

According to the w3c (the guys setting standards for things like html etc.) there are 2 ‘tags’ that could help with when you print large tables and want – say the column names and/or a footer to repeat on each page. The thead and tfoot tags ‘could’ be used to specify a row that will be used for repeating.

To quote the w3c: “When long tables are printed, the table head and foot information may be repeated on each page that contains table data.”

Yes, it does not state that it is a must that the browser must implement it but really, Firefox has been doing it for years!

Look at the following sample and test it in Firefox, IE9 (and even Chrome if you like) :

<table width=”100%” border=”1″>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan=”3″>footer</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Piet 1</td>
<td>Pompies</td>
<td>21</td>
</tr>
<tr>
<td>Piet 2</td>
<td>Pompies</td>
<td>21</td>
</tr>

… repeat it, say 50 times…

</tbody>
</table>

The rows containing the header (Name, Surname, Age) and the footer should repeat on each page. This works fine ‘as is’ in Firefox but IE9 (and even Chrome!) does not display it correctly. This is really shameful for a browser that is (now) suppose to support (most) of the web standards – and its not like it is some obscure functionality that no-one ever use. Mozilla (Firefox) has been doing it rights for many year so why can IE not get something as simple as this right.

Fortunately there is a ‘cheat’ to get IE to render it better – but it is not perfect and seems to display empty rows or even not display the last row on a page at all. The trick is to specify some CSS to correct the behaviour:

<STYLE type=”text/css”>
THEAD { display: table-header-group; }
TFOOT { display: table-footer-group; }
</STYLE>

Hopefully someone on the IE team will read/hear about this and fix it! 😉

HTMLWriter 1.1

A few days ago I published the original HTMLWriter article. Since then I’ve added a few methods to make simple formatting easier. This includes a way to specify an ‘indentation’ level. Right now it is pretty basic but it helps making some html tags like tables easier to read.

The most important methods added are these:

private void AppendInternalCRLF()
{

sbHtmlContent.Append(“\r\n”);
lastWrittenIndentation = false;

}

private void AppendInternalIndentation()
{

sbHtmlContent.Append(new string(‘\t’, Indentation));
lastWrittenIndentation = true;

}

public HTMLWriter AppendNewLine()
{

this.AppendInternalCRLF();
return this;

}
public HTMLWriter AppendNewLineWithIndentation()
{

if (AutoFormatting && !lastWrittenIndentation)

return this.AppendNewLine().AppendIndentation();

else

return this;

}

The lastWrittenIndentation variable is there since some html tags can call this method at the beginning and the end. If the next tag also starts by calling the method again then it ignores the method to avoid duplicating the indentation.

An example of one of the methods using it:

public HTMLWriter AppendTableStart(string className, params CustomAttribute[] customAttributes)
{

AppendNewLineWithIndentation();
return AppendTagStart(“table”, className, customAttributes);

}

Then I played around trying to ‘Normalize’ the html by using a library I discovered on CodePlex a while ago – System.Html

There seems to be a little issue using this library of the html is condensed (i.e. no CRLFs) but that is solved by cheating and replacing all ‘><‘ characters with ‘>\r\n<‘ .

Please keep in mind that this library was created to create simple html fragments or documents – mainly to generate reports inside a normal Winforms application.

The updated library can be found here.