Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best HTML encoder for Delphi?

Seems like my data is getting corrupted when using HTTPapp.HTMLEncode( string ): String;

HTMLEncode( 'Jo&hn D<oe' ); // returns 'Jo&am'

This is not correct, and is corrupting my data. Does anyone have suggestions for VCL components that work better? Other than spending my time encoding all the cases

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

Update

After understanding more about HTML, I have found there is no need to encode the other characters referenced in my link. You would only need to know about the four HTML reserved characters being

&,<,>,"

The issue with the VCL HTTPApp.HTMLEncode( ) function is because of the buffer size and the new Delphi 2009/2010 specifications for default Unicode string types, this can be fixed the way that @mason says below, or it can be fixed with a call to WideFormatBuf( ) instead of the FormatBuf( ) that is currently in use.

like image 557
wfoster Avatar asked Jun 23 '10 21:06

wfoster


1 Answers

Replacing the <, >, &, and " characters in a string is trivial. You could thus easily write your own routine for this. (And if your HTML page is UTF-8, there is absolutely no reason to encode any other characters, such as U+222B (the integral sign).)

But if you wish to stick to the Delphi RTL, then you can have a look at HTTPUtil.HTMLEscape with the exactly same signature as HTTPApp.HTMLEncode.

Or, have a look at this SO question.

like image 147
Andreas Rejbrand Avatar answered Sep 28 '22 19:09

Andreas Rejbrand