Search code examples
grailstaglibhtml-encode

TagLib outputs wrong encoding


I'm using a TagLib, but it is presenting a weird behaviour, instead of output character like <, ", and > it outputs &lt;, &quot;, and &gt;.

the code:

class LoginTagLib {
static defaultEncodeAs = [taglib:'html']
//static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
def loginControl = 
{
    if(session.user)
    {
        out << "Hello ${session.user.name}"
        out << """[${link(action:"logout", controller:"user"){"Logout"}}]"""
    }
    else
    {
        out << """[${link(action:"login", controller:"user"){"Login"}}]"""
    }
}

}

in the .gsp I've got

<g:loginControl />

When I open the page I have:

Hello Jane Smith[<a href="/Blogito/user/logout">Logout</a>] 

but the page's source code is:

<div id="loginHeader">
Hello Jane Smith[&lt;a href=&quot;/Blogito/user/logout&quot;&gt;Logout&lt;/a&gt;]
</div>

I've tried using .encodeAsHTML(), decodeHTML, .replace('&gt;', '<') and nothing has worked so far


Solution

  • Remove this line:

    static defaultEncodeAs = [taglib:'html']

    and try again.