Skip to content

Commit

Permalink
GRAILS-11222 - add params support for createLink with uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Scott Brown committed Mar 18, 2014
1 parent ab827a4 commit 71fb435
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Expand Up @@ -796,4 +796,22 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
appCtx.getBean(FormTagLib.name).requestDataValueProcessor = null
}
}

void testCreateLinkWithUriAndParams() {
unRegisterRequestDataValueProcessor()
def template = '''<g:createLink uri="/some/path" params="[width:21, height:12]"/>'''
assertOutputEquals '/some/path?width=21&height=12', template
}

void testCreateLinkWithParamsAndUriContainsRequestParams() {
unRegisterRequestDataValueProcessor()
def template = '''<g:createLink uri="/some/path?name=JSB" params="[width:21, height:12]"/>'''
assertOutputEquals '/some/path?name=JSB&width=21&height=12', template
}

void testUrlEncodingParamsCreateLinkWithUri() {
unRegisterRequestDataValueProcessor()
def template = '''<g:createLink uri="/some/path" params="['some height':12, name: 'Jeff Brown']"/>'''
assertOutputEquals '/some/path?some+height=12&name=Jeff+Brown', template
}
}
Expand Up @@ -17,6 +17,7 @@ package org.codehaus.groovy.grails.web.mapping

import grails.util.Environment
import grails.util.GrailsNameUtils
import grails.util.GrailsWebUtil
import grails.web.UrlConverter
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
Expand Down Expand Up @@ -105,7 +106,21 @@ class DefaultLinkGenerator implements LinkGenerator, PluginManagerAware {
writer << cp
}
}
writer << attrs.get(ATTRIBUTE_URI).toString()
def uri = attrs.get(ATTRIBUTE_URI).toString()
writer << uri

def paramsAttribute = attrs.get(ATTRIBUTE_PARAMS)
Map params = paramsAttribute && (paramsAttribute instanceof Map) ? (Map)paramsAttribute : null
if(params) {
def charset = GrailsWebUtil.DEFAULT_ENCODING
def paramString = params.collect { k, v ->
def encodedKey = URLEncoder.encode(k as String, charset)
def encodedValue = URLEncoder.encode(v as String, charset)
"$encodedKey=$encodedValue"
}.join('&')
writer << (uri.indexOf('?') >= 0 ? '&' : '?')
writer << paramString
}
}
else if (attrs.get(ATTRIBUTE_RELATIVE_URI) != null) {
String relativeUri = attrs.get(ATTRIBUTE_RELATIVE_URI)
Expand Down

0 comments on commit 71fb435

Please sign in to comment.