Permalink
Browse files

Merge branch '2.0.x' of github.com:grails/grails-core into 2.0.x

  • Loading branch information...
2 parents c0c0b6a + 1f6f8f7 commit 905d8cfc7b6e8643808dfde9132da74e80a4164b @graemerocher graemerocher committed Mar 28, 2012
@@ -43,6 +43,12 @@ class GroovyPageRenderingTests extends AbstractGrailsTagTests {
def result = applyTemplate(template, [toplist: [[sublist:['a','b']],[sublist:['c','d']]]])
assertEquals 'abcd', result
}
+
+ void testForeachIteratingMap() {
+ def template='<g:each var="k,v" in="[a:1,b:2,c:3]">${k}=${v},</g:each>'
+ def result = applyTemplate(template, [:])
+ assertEquals 'a=1,b=2,c=3,', result
+ }
void testForeachRenaming() {
def template='<g:each in="${list}"><g:each in="${list}">.</g:each></g:each>'
@@ -183,6 +183,12 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
assertOutputEquals('', template, [c:[:]])
assertOutputEquals('foo', template, [c:[a:[b:'foo']]])
}
+
+ void testSetTagWithScope() {
+ def template = '<g:set var="var1" value="1"/>${var1}<g:set var="var1" value="2"/> ${var1}<g:set var="var2" value="3" scope="request"/> ${var2}<g:set var="var2" value="4" scope="request"/> ${var2}'
+ assertOutputEquals('1 2 3 4', template)
+ }
+
void testIteration() {
def template = '''<g:set var="counter" value="${1}" />
@@ -37,6 +37,8 @@
private Binding parent;
private GroovyPage owner;
private Set<String> cachedParentVariableNames=new HashSet<String>();
+ private GroovyPageRequestBinding pageRequestBinding;
+ private boolean pageRequestBindingInitialized=false;
private boolean root;
public GroovyPageBinding() {
@@ -64,6 +66,24 @@ public GroovyPageBinding(String[] args) {
public Object getProperty(String property) {
return getVariable(property);
}
+
+ private GroovyPageRequestBinding findPageRequestBinding() {
+ if(!pageRequestBindingInitialized && parent != null) {
+ Binding nextParent = parent;
+ while(nextParent != null && pageRequestBinding==null) {
+ if(nextParent instanceof GroovyPageRequestBinding) {
+ pageRequestBinding = (GroovyPageRequestBinding)nextParent;
+ }
+ if (nextParent instanceof GroovyPageBinding) {
+ nextParent = ((GroovyPageBinding)nextParent).parent;
+ } else {
+ nextParent = null;
+ }
+ }
+ pageRequestBindingInitialized=true;
+ }
+ return pageRequestBinding;
+ }
@SuppressWarnings("unchecked")
@Override
@@ -76,9 +96,11 @@ public Object getVariable(String name) {
if (parent != null) {
val = parent.getVariable(name);
if (val != null) {
- // cache variable in this context since parent context cannot change during usage of this context
- getVariablesMap().put(name, val);
- cachedParentVariableNames.add(name);
+ if(findPageRequestBinding()==null || !findPageRequestBinding().isRequestAttributeVariable(name)) {
+ // cache variable in this context since parent context cannot change during usage of this context
+ getVariablesMap().put(name, val);
+ cachedParentVariableNames.add(name);
+ }
}
}
}
@@ -38,6 +38,7 @@
private GrailsWebRequest webRequest;
private Map<String, Class<?>> cachedDomainsWithoutPackage;
private boolean developmentMode = Environment.isDevelopmentMode();
+ private Set<String> requestAttributeVariables=new HashSet<String>();
private static Map<String, LazyRequestBasedValue> lazyRequestBasedValuesMap = new HashMap<String, LazyRequestBasedValue>();
static {
@@ -113,28 +114,33 @@ public GroovyPageRequestBinding(GrailsWebRequest webRequest) {
}
}
}
+
+ public boolean isRequestAttributeVariable(String name) {
+ return requestAttributeVariables.contains(name);
+ }
@Override
public Object getVariable(String name) {
Object val = getVariablesMap().get(name);
if (val == null && !getVariablesMap().containsKey(name) && webRequest != null) {
val = webRequest.getCurrentRequest().getAttribute(name);
-
- if (val == null) {
+ if(val != null) {
+ requestAttributeVariables.add(name);
+ } else {
LazyRequestBasedValue lazyValue = lazyRequestBasedValuesMap.get(name);
if (lazyValue != null) {
val = lazyValue.evaluate(webRequest);
}
- }
-
- if (val == null && cachedDomainsWithoutPackage != null) {
- val = cachedDomainsWithoutPackage.get(name);
- }
-
- // warn about missing variables in development mode
- if (val == null && developmentMode) {
- if (log.isDebugEnabled()) {
- log.debug("Variable '" + name + "' not found in binding or the value is null.");
+
+ if (val == null && cachedDomainsWithoutPackage != null) {
+ val = cachedDomainsWithoutPackage.get(name);
+ }
+
+ // warn about missing variables in development mode
+ if (val == null && developmentMode) {
+ if (log.isDebugEnabled()) {
+ log.debug("Variable '" + name + "' not found in binding or the value is null.");
+ }
}
}
}
@@ -14,11 +14,15 @@
*/
package org.codehaus.groovy.grails.web.sitemesh;
+import groovy.lang.Writable;
+
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
+import org.codehaus.groovy.grails.web.util.GrailsPrintWriter;
import org.codehaus.groovy.grails.web.util.GrailsPrintWriterAdapter;
+import org.codehaus.groovy.grails.web.util.StreamCharBuffer;
public class GrailsRoutablePrintWriter extends GrailsPrintWriterAdapter {
@@ -65,6 +69,7 @@ private PrintWriter getDestination() {
setError();
}
super.out = destination;
+ super.setTarget(destination);
}
return destination;
}
@@ -279,4 +284,22 @@ public void blockFlushAndClose() {
this.blockClose = true;
this.blockFlush = true;
}
+
+ @Override
+ public GrailsPrintWriter leftShift(Object value) throws IOException {
+ getDestination();
+ return super.leftShift(value);
+ }
+
+ @Override
+ public GrailsPrintWriter leftShift(StreamCharBuffer otherBuffer) {
+ getDestination();
+ return super.leftShift(otherBuffer);
+ }
+
+ @Override
+ public GrailsPrintWriter leftShift(Writable writable) {
+ getDestination();
+ return super.leftShift(writable);
+ }
}
@@ -139,7 +139,13 @@ protected void doEachMethod(String in) {
var = "_it"+ Math.abs(System.identityHashCode(this));
foreachRenamedIt = var;
}
-
+
+ String[] entryVars=null;
+ if(hasVar && var.indexOf(',') > -1) {
+ entryVars = var.split("\\s*,\\s*");
+ var = "_entry" + Math.abs(System.identityHashCode(this));
+ }
+
out.print("for( " + var);
out.print(" in "); // dot de-reference
out.print(parser != null ? parser.getExpressionText(in, false) : extractAttributeValue(in)); // object
@@ -148,6 +154,9 @@ protected void doEachMethod(String in) {
out.println();
if (!hasVar) {
out.println("changeItVariable(" + foreachRenamedIt +")" );
+ } else if (entryVars != null) {
+ out.println("def " + entryVars[0].trim() + "=" + var + ".getKey()");
+ out.println("def " + entryVars[1].trim() + "=" + var + ".getValue()");
}
}
@@ -31,6 +31,10 @@
public GrailsPrintWriterAdapter(Writer wrapped) {
super(new NullWriter());
+ setTarget(wrapped);
+ }
+
+ public void setTarget(Writer wrapped) {
if (wrapped instanceof GrailsPrintWriter) {
this.target = ((GrailsPrintWriter)wrapped);
}

0 comments on commit 905d8cf

Please sign in to comment.