how encoding & decoding works in grails with decodeHTML & encodeAsHTML in grails?

10,318

escapeAsHtml end up calling StringEscapeUtils.escapeHtml in apache commons lang

As it says in the docs for that method;

Escapes the characters in a String using HTML entities.

For example:

"bread" & "butter"

becomes: "bread" & "butter".

Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used apostrophe escape character (') is not a legal entity and so is not supported).

It does not convert all characters to their entity value, so things like !, * and / are left as-is. Here's an example in Groovy:

@Grab( 'commons-lang:commons-lang:2.6' )
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml

'!@£$%^&*()_+€-={}[]:"|;\'\\<>?,./~'.each {
    println "$it -> ${escapeHtml( it )}"
}

That prints:

! -> !
@ -> @
£ -> &pound;
$ -> $
% -> %
^ -> ^
& -> &amp;
* -> *
( -> (
) -> )
_ -> _
+ -> +
€ -> &euro;
- -> -
= -> =
{ -> {
} -> }
[ -> [
] -> ]
: -> :
" -> &quot;
| -> |
; -> ;
' -> '
\ -> \
< -> &lt;
> -> &gt;
? -> ?
, -> ,
. -> .
/ -> /
~ -> ~
Share:
10,318
user1298426
Author by

user1298426

Updated on June 04, 2022

Comments

  • user1298426
    user1298426 almost 2 years

    I am trying to understand how encoding & decoding works in grails with decodeHTML & encodeAsHTML

    // decode Example is

    List symbols = ['!', '*', '/']
    symbols.each { String symbol ->
        println symbol.decodeHTML()
    }
    

    it should print

    &#33;    // but it prints !
    &#42;   // but it prints *
    &#47;   // but it prints /
    

    // encode Example is

    List symbols = ['&#33;', '&#42;', '&#47;']
    symbols.each { String symbol ->
        println symbol.encodeAsHTML()
    }
    

    it should print

    '!'  // but it prints &amp;#33;
    '*'  // but it prints &amp;#42;
    '/'  // but it prints &amp;#47;
    
  • Ivar
    Ivar about 7 years
    "escapeAsHtml" Don't you mean encodeAsHTML? And if so you sure that it ends up calling StringEscapeUtils.escapeHtml? Because when I print this: "\"café\"".encodeAsHTML() it ends up as &quot;café&quot;, while if I use StringEscapeUtils.escapeHtml("\"café\"") it ends up as &quot;caf&eacute;&quot;. (It escapes the é as well.)