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:
! -> !
@ -> @
£ -> £
$ -> $
% -> %
^ -> ^
& -> &
* -> *
( -> (
) -> )
_ -> _
+ -> +
€ -> €
- -> -
= -> =
{ -> {
} -> }
[ -> [
] -> ]
: -> :
" -> "
| -> |
; -> ;
' -> '
\ -> \
< -> <
> -> >
? -> ?
, -> ,
. -> .
/ -> /
~ -> ~
Author by
user1298426
Updated on June 04, 2022Comments
-
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
! // but it prints ! * // but it prints * / // but it prints /
// encode Example is
List symbols = ['!', '*', '/'] symbols.each { String symbol -> println symbol.encodeAsHTML() }
it should print
'!' // but it prints &#33; '*' // but it prints &#42; '/' // but it prints &#47;
-
Ivar about 7 years"escapeAsHtml" Don't you mean
encodeAsHTML
? And if so you sure that it ends up callingStringEscapeUtils.escapeHtml
? Because when I print this:"\"café\"".encodeAsHTML()
it ends up as"café"
, while if I useStringEscapeUtils.escapeHtml("\"café\"")
it ends up as"café"
. (It escapes theé
as well.)