function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
horaciohoracio 

HTMLENCODE from Apex code

Hello, I am trying to escape a rich textArea field to be shown in a visualforce page.

Right now, it's escaped using {!HTMLENCODE()}.

What I am trying to do is to allow a set of HTML tags, but only in some cases.

 

What I am looking for, ideally, is a way to call the HTMLENCODE function from apex code most of the time, and then implement my own escaping method for the cases that allow some HTML tags. What I am trying to avoid is re-implementing HtmlEncode in my classes.

 

So, the question is : is there a way to access the HTMLENCODE visualforce function from Apex code? If not, is there a functionally equivalent alternative I can reuse?

 

Any help will be appreciated, thanks!

 

Horacio

*werewolf**werewolf*

I believe what you're looking for is EncodingUtil.urlEncode().

horaciohoracio

 


*werewolf* wrote:

I believe what you're looking for is EncodingUtil.urlEncode().


 

I'm afraid that won't work, because I need to safely HTML encode the text, in order to show it in a page.

Anyhow, I ended up implementing my own HTMLEncode algorithm, dumb but safe, with the behaviour I needed.

Thanks for your help anyway!

Pradeep_NavatarPradeep_Navatar

In apex code you can do through string.replace(). In replace method the character is replaced by ASCII code.

 

Hope this helps.

Tom MaherTom Maher

http://code.google.com/p/force-dot-com-esapi/wiki/GettingStarted

 

Our port of the ESAPI provides Apex equivalents of JSENCODE, HTMLENCODE, JSINHTMLENCODE and URLENCODE functions.

 

usertext = ESAPI.encoder().SFDC_HTMLENCODE(unsafe_text);

 

Kaushik_SupportKaushik_Support

Hello,

 

I am trying to do the same and want to perform HTML decode.

 

Any solution finalised for the same..?

 

Thanks in Advance,

Ray