<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>string.js</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -37,33 +37,56 @@
 //   -%&gt; as the closing tag removes the newline (if the tag is the last
 //       thing on the line)
 
-exports.EJS = function(string) {
-  var self = this;
-  
-  self._process = function(string) {
-    var body = &quot;var s = '';\n&quot;;
-    var tags = /&lt;%=|%&gt;/m;
-    var match;
-    var insideTag = false;
-    while (match = tags.exec(string)) {
-      if (match[0] == &quot;&lt;%=&quot;) {
-        if (insideTag) throw &quot;Compile error: No '&lt;%=' allowed inside of '&lt;%='&quot;;
-        insideTag = true;
-        body += &quot;s += '&quot; + RegExp.leftContext + &quot;';&quot;
-      } else {
-        insideTag = false;
-        var re = RegExp.leftContext.replace(/^\s*/,'').replace(/\s*$/, '');
-        body += &quot;s += r.&quot; + re + &quot;;&quot;
+include(&quot;string&quot;);
+
+function onLoad() {
+  exports.EJS = function(string) {
+    var self = this;
+
+    self._process = function(string) {
+      var body = &quot;var s = '';\n&quot;;
+      var tags = /&lt;%(?:=)?|%&gt;/m;
+      var match;
+      var insideCodeTag = false;
+      var insideExprTag = false;
+      var that = this;
+      function insideTag() {
+        return insideCodeTag || insideExprTag;
       }
-      string = RegExp.rightContext
-    }
-    body += &quot;s += '&quot; + string + &quot;';\nreturn s;&quot;
-    self.template = new Function(&quot;r&quot;, body);
-  };
-  
-  self.result = function(context) {
-    return self.template(context);
+      while (match = tags.exec(string)) {
+        var left  = string.substring(0, match.index)
+        var right = string.substring(match.index+match[0].length)
+        if (match[0] == &quot;&lt;%=&quot;) {
+          if (insideTag()) throw &quot;Compile error: No '&lt;%=' or '&lt;%' allowed inside of '&quot; + match[0] + &quot;'&quot;;
+          insideCodeTag = true;
+          if (left != &quot;&quot;) body += &quot;s += '&quot; +  left.escape() + &quot;';\n&quot;
+        } else if (match[0] == &quot;&lt;%&quot;) {
+          if (insideTag()) throw &quot;Compile error: No '&lt;%=' or '&lt;%' allowed inside of '&quot; + match[0] + &quot;'&quot;;
+          insideExprTag = true;
+          if (left != &quot;&quot;) body += &quot;s += '&quot; +  left.escape() + &quot;';\n&quot;
+        } else {
+          if (insideCodeTag) {
+            insideCodeTag = false;
+            var re = left.strip()
+            body += &quot;s += r.&quot; + re + &quot;;\n&quot;
+          } else if (insideExprTag) {
+            insideExprTag = false;
+            var re = left.strip()
+            body += re + &quot;\n&quot;
+            right = right.lstrip()
+          }
+        }
+        string = right;
+      }
+      if (string != &quot;&quot;) body += &quot;s += '&quot; + string.escape() + &quot;';\n&quot;
+      body += &quot;return s;\n&quot;
+      self.template = new Function(&quot;r&quot;, body);
+    };
+
+    self.result = function(context) {
+      return self.template(context);
+    };
+
+    self._process(string);
   };
-  
-  self._process(string);
-};
\ No newline at end of file
+}</diff>
      <filename>ejs.js</filename>
    </modified>
    <modified>
      <diff>@@ -41,14 +41,14 @@ function onLoad() {
     var context = {replacement: &quot;some text&quot;}
     assertEquals(context.replacement, e.result(context))
   }
- 
+  
   suite.testCompileErrorOnDuplicateOpeningTag = function() {
     var t = &quot;&lt;%= oops &lt;%= %&gt;&quot;
     try {
       new EJS(t);
       assertUnreachable();
     } catch(e) {
-      assertTrue(/Compile error: No '&lt;%=' allowed inside of '&lt;%='/.test(e))
+      assertTrue(/Compile error: No '&lt;%=' or '&lt;%' allowed inside of '&lt;%='/.test(e))
     }
   }
   
@@ -59,5 +59,42 @@ function onLoad() {
     assertEquals(context.replacement, e.result(context))
   }
   
+  suite.testInsertedJSCode = function() {
+    var t = &quot;&lt;% \&quot;string\&quot; %&gt;&quot;
+    var e = new EJS(t);
+    var context = {}
+    assertEquals(&quot;&quot;, e.result(context))
+  }
+  
+  suite.testLoopInTemplate = function() {
+    var t = &quot;&lt;% for (var i=0; i &lt; 3; i++) { %&gt;\n&quot;
+    t += &quot;content\n&quot;
+    t += &quot;&lt;% }; %&gt;&quot;
+    var e = new EJS(t);
+    var context = {}
+    var result = e.result(context)
+    assertEquals(&quot;content\ncontent\ncontent\n&quot;, result)
+  }
+ 
+  suite.testReplacementInLoop = function() {
+    var t = &quot;&lt;% for (var i=0; i &lt; 3; i++) { %&gt;\n&quot;
+    t += &quot;&lt;%= replacement %&gt;\n&quot;
+    t += &quot;&lt;% }; %&gt;&quot;
+    var e = new EJS(t);
+    var context = {replacement: &quot;content&quot;}
+    var result = e.result(context)
+    assertEquals(&quot;content\ncontent\ncontent\n&quot;, result)
+  }
+
+  suite.testReplacementInLoopWithNewLineAtEnd = function() {
+    var t = &quot;&lt;% for (var i=0; i &lt; 3; i++) { %&gt;\n&quot;
+    t += &quot;&lt;%= replacement %&gt;\n&quot;
+    t += &quot;&lt;% }; %&gt;\n&quot;
+    var e = new EJS(t);
+    var context = {replacement: &quot;content&quot;}
+    var result = e.result(context)
+    assertEquals(&quot;content\ncontent\ncontent\n&quot;, result)
+  }
+
   suite.run();
 }
\ No newline at end of file</diff>
      <filename>test/test-ejs.js</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ba3a346c2f2a0ea454047169155f10d5dc87ca4d</id>
    </parent>
  </parents>
  <author>
    <name>Urban Hafner</name>
    <email>urban@bettong.net</email>
  </author>
  <url>http://github.com/ujh/frisbee/commit/049828b8095bc3918d430c4bb836e8e5707a35eb</url>
  <id>049828b8095bc3918d430c4bb836e8e5707a35eb</id>
  <committed-date>2009-06-04T08:02:48-07:00</committed-date>
  <authored-date>2009-06-04T08:02:48-07:00</authored-date>
  <message>EJS with support for &lt;%= and &lt;%. Closes #7</message>
  <tree>25a37b4cf2ad27b8cc1da1a2a57f8f61e7af72ea</tree>
  <committer>
    <name>Urban Hafner</name>
    <email>urban@bettong.net</email>
  </committer>
</commit>
