r9672 - in helma-ng/trunk: modules/helma src/org/helma/template

[email protected] Fri, 24 Apr 2009 19:38:34 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090424173834.4BE1B3D0D6@mia>
Author: hannes
Date: 2009-04-24 19:38:33 +0200 (Fri, 24 Apr 2009)
New Revision: 9672

Modified:
   helma-ng/trunk/modules/helma/skin.js
   helma-ng/trunk/src/org/helma/template/MacroTag.java
Log:
Return existing submacro in MacroTag.getSubMacro() if one exists at the given position. Add builtin echo macro, and implement boolean and/or in if macro.

Details at http://dev.helma.org/trac/helma/changeset/9672

Modified: helma-ng/trunk/modules/helma/skin.js
===================================================================
--- helma-ng/trunk/modules/helma/skin.js	2009-04-23 21:33:09 UTC (rev 9671)
+++ helma-ng/trunk/modules/helma/skin.js	2009-04-24 17:38:33 UTC (rev 9672)
@@ -206,51 +206,81 @@
             return self.renderSubskin(skin, context);
         },
 
+        "echo": function(macro, context, separator) {
+            var result = macro.parameters.map(function(elem) {
+                return getEvaluatedParameter(elem, context, 'echo');
+            });
+            var wrapper = macro.getParameter("wrap") || macro.getParameter("echo-wrap");
+            if (wrapper != null) {
+                wrapper = getEvaluatedParameter(wrapper, context);
+                result = result.map(function(part) {return wrapper[0] + part + wrapper[1]});
+            }
+            var separator = macro.getParameter("separator") || separator;
+            if (separator != null) {
+                return result.join(getEvaluatedParameter(separator), context, 'for:separator');
+            }
+            return result.join(' ');
+        },
+
         "for": function(macro, context) {
             if (macro.parameters.length < 4)
-                throw Error("not enough parameters in for-in macro");
+                return "[Error in for-in macro: not enough parameters]";
             if (macro.parameters[1] != "in")
-                throw Error("syntax error in for-in macro: expected in")
+                return "[Error in for-in macro: expected in]";
             var name = getEvaluatedParameter(macro.parameters[0], context, 'for:name');
             var list = getEvaluatedParameter(macro.parameters[2], context, 'for:list');
             var subContext = context.clone();
             var subMacro = macro.getSubMacro(3);
-            result = [];
+            if (subMacro.name == "and") {
+                subMacro.name = "for";
+            }
+            var result = [];
             for (var [index, value] in list) {
                 subContext['index'] = index
                 subContext[name] = getEvaluatedParameter(value, context, 'for:value');
                 result.push(evaluateMacro(subMacro, subContext));
             }
-            var separator = getEvaluatedParameter(macro.getParameter("separator"),
-                    context, 'for:separator');
+            var wrapper = macro.getParameter("wrap") || macro.getParameter(name + "-wrap");
+            if (wrapper != null) {
+                wrapper = getEvaluatedParameter(wrapper, context);
+                result = result.map(function(part) {return wrapper[0] + part + wrapper[1]});
+            }
+            var separator = macro.getParameter("separator");
             if (separator != null) {
-                return result.join(separator);
+                return result.join(getEvaluatedParameter(separator), context, 'for:separator');
             }
-            return result;
+            return result.join('');
         },
 
-        "if": function(macro, context) {
+        "if": function(macro, context, bypass) {
             if (macro.parameters.length < 2)
-                throw Error("not enough parameters in if macro");
+                return "[Error in if macro: not enough parameters]";
             var negated = (macro.parameters[0] == "not");
-            var condition;
-            if (negated) {
-                if (macro.parameters.length < 3)
-                    throw Error("not enough parameters in if macro");
-                condition = getEvaluatedParameter(macro.parameters[1], context, 'if:condition');
+            if (negated && macro.parameters.length < 3)
+                return "[Error in if macro: not enough parameters]";
+            var index = negated ? 1 : 0;
+            var result = true;
+            if (bypass) {
+                index++;
             } else {
-                condition = getEvaluatedParameter(macro.parameters[0], context, 'if:condition');
+                var condition = getEvaluatedParameter(macro.parameters[index++], context, 'if:condition');
+                result = negated ? !condition : !!condition;
             }
-            if (negated ? !!condition : !condition) {
+            var subName = macro.parameters[index];
+            if (!result && macro.parameters[index] != "or") {
                 return "";
             }
-            var subMacro = macro.getSubMacro(negated ? 2 : 1);
+            var subMacro = macro.getSubMacro(index);
+            if (subName == "or" && result)
+                return builtin["if"](subMacro, context, true);
+            else if (subName == "and" || subName == "or")
+                return builtin["if"](subMacro, context);
             return evaluateMacro(subMacro, context);
         },
 
         "set": function(macro, context) {
             if (macro.parameters.length < 2)
-                throw Error("not enough parameters in with macro");
+                return "[Error in set macro: not enough parameters]";
             var map = getEvaluatedParameter(macro.parameters[0], context, 'with:map');
             var subContext = context.clone();
             var subMacro = macro.getSubMacro(1);

Modified: helma-ng/trunk/src/org/helma/template/MacroTag.java
===================================================================
--- helma-ng/trunk/src/org/helma/template/MacroTag.java	2009-04-23 21:33:09 UTC (rev 9671)
+++ helma-ng/trunk/src/org/helma/template/MacroTag.java	2009-04-24 17:38:33 UTC (rev 9672)
@@ -20,6 +20,7 @@
 import org.helma.util.CaseInsensitiveMap;
 import org.helma.util.ScriptableList;
 import org.helma.util.ScriptableMap;
+import org.helma.util.ScriptUtils;
 import org.mozilla.javascript.*;
 
 import java.util.List;
@@ -79,12 +80,18 @@
     }
 
     /**
-     * The name of the macro tag.
+     * Getter for the macro tag name.
      */
     public Object jsGet_name() {
         return name;
     }
 
+    /**
+     * Setter for the macro tag name.
+     */
+    public void jsSet_name(String name) {
+        this.name = name;
+    }
 
     /**
      * A Javascript array containing all parameter names in this macro tag.
@@ -139,10 +146,15 @@
     }
 
     public Object jsFunction_getSubMacro(int start) {
+        Object obj = args.get(start);
+        if (obj instanceof MacroTag) {
+            return ScriptUtils.javaToJS(obj, getTopLevelScope(this));
+        }
         MacroTag submacro = new MacroTag(start);
         submacro.setParentScope(getParentScope());
         submacro.setPrototype(getPrototype());
-        submacro.filter = filter;
+        // only apply filter to the top-level macro
+        // submacro.filter = filter;
         submacro.namedArgs = new CaseInsensitiveMap<String,Object>(namedArgs);
         submacro.args = new LinkedList<Object>();
         if (start + 1 < args.size()) {
@@ -150,7 +162,7 @@
                 submacro.args.add(i.next());
             }
         }
-        submacro.name = args.get(start);
+        submacro.name = obj;
         return submacro;
     }