Skip to content

Commit 50281cf

Browse files
author
graeme
committed
fix for GRAILS-2471
git-svn-id: https://svn.codehaus.org/grails/trunk@6708 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent a36575f commit 50281cf

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

src/web/org/codehaus/groovy/grails/web/taglib/GroovyIfTag.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,24 @@ public class GroovyIfTag extends GroovySyntaxTag {
2727
private static final String ATTRIBUTE_ENV = "env";
2828

2929
public void doStartTag() {
30-
String env = (String) attributes.get(ATTRIBUTE_ENV);
31-
String test = (String) attributes.get(ATTRIBUTE_TEST);
32-
env = StringUtils.isBlank(env) ? null : env;
33-
test = StringUtils.isBlank(test) ? null : test;
34-
if((env == null) && (test == null))
30+
String env = attributeValueOrNull(ATTRIBUTE_ENV);
31+
String test = attributeValueOrNull(ATTRIBUTE_TEST);
32+
33+
if((env == null) && (test == null)) {
3534
throw new GrailsTagException(
3635
"Tag ["+TAG_NAME+"] must have one or both of the attributes ["+ATTRIBUTE_TEST+"] or ["+ATTRIBUTE_ENV+"]");
37-
if (env != null) {
38-
env = calculateExpression(env);
39-
}
40-
if ((env != null) && (test != null)) {
41-
out.print("if((GrailsUtil.environment == '"+env+"') && (");
42-
out.print(test);
43-
out.println(")) {");
44-
} else if (env != null) {
45-
// double (( is deliberate... to avoid thorny logic
46-
out.print("if(GrailsUtil.environment == '"+env+"') {");
47-
} else {
48-
out.print("if(");
49-
out.print(test);
50-
out.println(") {");
5136
}
52-
}
5337

38+
String envExpression = environmentExpressionOrTrue(env);
39+
String testExpression = testExpressionOrTrue(test);
40+
41+
out.print("if(");
42+
out.print(envExpression);
43+
out.print(" && ");
44+
out.print(testExpression);
45+
out.println(") {");
46+
}
47+
5448
public void doEndTag() {
5549
out.println("}");
5650
}
@@ -66,4 +60,25 @@ public boolean isBufferWhiteSpace() {
6660
public boolean hasPrecedingContent() {
6761
return true;
6862
}
63+
64+
private String attributeValueOrNull(String attributeName) {
65+
String attributeValue = (String) attributes.get(attributeName);
66+
return StringUtils.isBlank(attributeValue) ? null : attributeValue;
67+
}
68+
69+
private String environmentExpressionOrTrue(String envAttributeValue) {
70+
String expression = "true";
71+
if (envAttributeValue != null) {
72+
expression = "(GrailsUtil.environment == '" + calculateExpression(envAttributeValue) + "')";
73+
}
74+
return expression;
75+
}
76+
77+
private String testExpressionOrTrue(String testAttributeValue) {
78+
String expression = "true";
79+
if (testAttributeValue != null) {
80+
expression = "(" + testAttributeValue + ")";
81+
}
82+
return expression;
83+
}
6984
}

test/groovy/org/codehaus/groovy/grails/web/taglib/CoreTagsTests.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ package org.codehaus.groovy.grails.web.taglib
1313

1414
import grails.util.GrailsUtil
1515

16+
import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
17+
1618
class CoreTagsTests extends AbstractGrailsTagTests {
1719

1820
void testIfElse() {
@@ -57,6 +59,17 @@ bar
5759
assertOutputEquals("foo", template2, [foo:true])
5860
}
5961

62+
void testIfWithEnvAndWithoutTestAttribute() {
63+
def template = '''<g:if env="development">foo</g:if>'''
64+
assertOutputEquals("foo", template)
65+
}
66+
67+
void testIfWithoutEnvAndTestAttributes() {
68+
shouldFail(GrailsTagException) {
69+
applyTemplate("<g:if>foo</g:if>")
70+
}
71+
}
72+
6073
void testElseIf() {
6174

6275
def template = '''

0 commit comments

Comments
 (0)