[limb-svn] r6397 - in 3.x/trunk/limb/macro: src tests/cases

svn at limb-project.com svn at limb-project.com
Mon Oct 8 22:51:45 MSD 2007


Author: pachanga
Date: 2007-10-08 22:51:45 +0400 (Mon, 08 Oct 2007)
New Revision: 6397
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6397

Modified:
   3.x/trunk/limb/macro/src/lmbMacroParser.class.php
   3.x/trunk/limb/macro/src/lmbMacroTagParsingState.class.php
   3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php
   3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php
   3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php
Log:
-- new tokenizer php event added

Modified: 3.x/trunk/limb/macro/src/lmbMacroParser.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroParser.class.php	2007-10-08 13:13:58 UTC (rev 6396)
+++ 3.x/trunk/limb/macro/src/lmbMacroParser.class.php	2007-10-08 18:51:45 UTC (rev 6397)
@@ -128,6 +128,11 @@
     $this->active_parsing_state->characters($text);
   }
 
+  function php($text)
+  {
+    $this->active_parsing_state->php($text);
+  }
+
   function unexpectedEOF($text)
   {
     $this->active_parsing_state->unexpectedEOF($text);

Modified: 3.x/trunk/limb/macro/src/lmbMacroTagParsingState.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTagParsingState.class.php	2007-10-08 13:13:58 UTC (rev 6396)
+++ 3.x/trunk/limb/macro/src/lmbMacroTagParsingState.class.php	2007-10-08 18:51:45 UTC (rev 6397)
@@ -142,6 +142,11 @@
     $this->tree_builder->addContent($text, $this->parser->getCurrentLocation());
   }
 
+  function php($text)
+  {
+    $this->tree_builder->addTextNode($text);
+  }
+
   function unexpectedEOF($text)
   {
     $this->tree_builder->addTextNode($text);

Modified: 3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php	2007-10-08 13:13:58 UTC (rev 6396)
+++ 3.x/trunk/limb/macro/src/lmbMacroTokenizer.class.php	2007-10-08 18:51:45 UTC (rev 6397)
@@ -51,6 +51,67 @@
       $this->position++;
   }
 
+  protected function _findTag($start)
+  {
+    do
+    {
+      $php_start = strpos($this->rawtext, '<?php', $start);
+      $tag_start = strpos($this->rawtext, '{{', $start);
+
+      //no php found
+      if($php_start === false)
+      {
+        //tag candidate found
+        if($tag_start !== false)
+        {
+          if($tag_start > $start)
+            $this->observer->characters(substr($this->rawtext, $start, $tag_start - $start));
+          return $tag_start;
+        }
+        //no tags at all
+        else
+        {
+          if($start != $this->length)
+            $this->observer->characters(substr($this->rawtext, $start, $this->length - $start));
+          return null;
+        }
+      }
+      //php found
+      else
+      {
+        $php_end = strpos($this->rawtext, '?>', $start);
+        //php end found
+        if($php_end !== false)
+        {
+          //at the same time tag found and it's not inside php
+          if($tag_start !== false && $tag_start < $php_start)
+          {
+            if($start < $tag_start)
+              $this->observer->characters(substr($this->rawtext, $start, $tag_start - $start));
+            return $tag_start;
+          }
+          //extract php block
+          else
+          {
+            //add preceding characters
+            if($start < $php_start)
+              $this->observer->characters(substr($this->rawtext, $start, $php_start - $start));
+            $this->observer->php(substr($this->rawtext, $php_start, $php_end - $php_start + 2));
+            $start = $php_end + 2;
+          }
+        }
+        //no php end found, everything is php then
+        else
+        {
+          $this->observer->php(substr($this->rawtext, $php_start));
+          return null;
+        }
+                      
+      }
+    }while($start < $this->length);
+    return null;
+  }
+
   /**
   * Begins the parsing operation, setting up any decorators, depending on
   * parse options invoking _parse() to execute parsing
@@ -65,19 +126,11 @@
     do
     {
       $start = $this->position;
-      $this->position = strpos($this->rawtext, '{{', $start);
-      if($this->position === false)
-      {
-        if($start < $this->length)
-          $this->observer->characters(substr($this->rawtext, $start));
+
+      $this->position = $this->_findTag($start);
+      if($this->position === null)
         return;
-      }
 
-      if($this->position > $start)
-      {
-        $this->observer->characters(substr($this->rawtext, $start, $this->position - $start));
-      }
-
       $this->position += 2;   // ignore '{{' string
       if($this->position >= $this->length)
       {
@@ -247,11 +300,10 @@
           }
 
           $this->position += 1;
-
           break;
         }
     }
-    while ($this->position < $this->length);
+    while($this->position < $this->length);
   }
 }
 

Modified: 3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php	2007-10-08 13:13:58 UTC (rev 6396)
+++ 3.x/trunk/limb/macro/src/lmbMacroTokenizerListener.interface.php	2007-10-08 18:51:45 UTC (rev 6397)
@@ -19,6 +19,7 @@
   function endElement($tag_name);
   function emptyElement($tag_name, $attrs);
   function characters($data);
+  function php($data);
   function unexpectedEOF($data);
   function invalidEntitySyntax($data);
   function invalidAttributeSyntax($data);

Modified: 3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php	2007-10-08 13:13:58 UTC (rev 6396)
+++ 3.x/trunk/limb/macro/tests/cases/lmbMacroTokenizerTest.class.php	2007-10-08 18:51:45 UTC (rev 6397)
@@ -93,6 +93,90 @@
     $this->parser->parse('{{/}}');
   }
 
+  function testSelfClosingPHPBlock()
+  {
+    $this->listener->expectNever('startElement');
+    $this->listener->expectNever('characters');
+    $this->listener->expectNever('endElement');
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->listener->expectOnce('php', array('<?php $var = "{{tag}}{{/tag}}";?>'));
+    $this->parser->parse('<?php $var = "{{tag}}{{/tag}}";?>');
+  }
+
+  function testSeveralPHPBlocks()
+  {
+    $this->listener->expectCallCount('characters', 2);
+    $this->listener->expectArgumentsAt(0, 'characters', array('hey'));
+    $this->listener->expectArgumentsAt(1, 'characters', array('foo'));
+    $this->listener->expectCallCount('php', 2);
+    $this->listener->expectArgumentsAt(0, 'php', array('<?php $yo = "{{foo/}}";?>'));
+    $this->listener->expectArgumentsAt(1, 'php', array('<?php $var = "{{tag}}{{/tag}}";?>'));
+    $this->listener->expectNever('startElement');
+    $this->listener->expectNever('endElement');
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->parser->parse('<?php $yo = "{{foo/}}";?>hey<?php $var = "{{tag}}{{/tag}}";?>foo');
+  }
+
+  function testNonClosingPHPBlock()
+  {
+    $this->listener->expectNever('startElement');
+    $this->listener->expectNever('characters');
+    $this->listener->expectNever('endElement');
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->listener->expectOnce('php', array('<?php $var = "{{tag}}{{/tag}}";'));
+    $this->parser->parse('<?php $var = "{{tag}}{{/tag}}";');
+  }
+
+  function testTagAfterPHPBlock()
+  {
+    $this->listener->expectOnce('startElement', array('foo', array()));
+    $this->listener->expectOnce('characters', array('hey'));
+    $this->listener->expectOnce('endElement', array('foo'));
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->listener->expectOnce('php', array('<?php $var = "{{tag}}{{/tag}}";?>'));
+    $this->parser->parse('<?php $var = "{{tag}}{{/tag}}";?>{{foo}}hey{{/foo}}');
+  }
+
+  function testTagBeforePHPBlock()
+  {
+    $this->listener->expectOnce('startElement', array('foo', array()));
+    $this->listener->expectOnce('characters', array('hey'));
+    $this->listener->expectOnce('endElement', array('foo'));
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->listener->expectOnce('php', array('<?php $var = "{{tag}}{{/tag}}";?>'));
+    $this->parser->parse('{{foo}}hey{{/foo}}<?php $var = "{{tag}}{{/tag}}";?>');
+  }
+
+  function testCharactersBeforePHPBlock()
+  {
+    $this->listener->expectNever('startElement');
+    $this->listener->expectOnce('characters', array('hey'));
+    $this->listener->expectNever('endElement');
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->listener->expectOnce('php', array('<?php $var = "{{tag}}{{/tag}}";?>'));
+    $this->parser->parse('hey<?php $var = "{{tag}}{{/tag}}";?>');
+  }
+
+  function testMixedTagsAndPHPBlocks()
+  {
+    $this->listener->expectCallCount('startElement', 2);
+    $this->listener->expectArgumentsAt(0, 'startElement', array('foo', array()));
+    $this->listener->expectArgumentsAt(1, 'startElement', array('zoo', array()));
+    $this->listener->expectCallCount('characters', 4);
+    $this->listener->expectArgumentsAt(0, 'characters', array('hey'));
+    $this->listener->expectArgumentsAt(1, 'characters', array('baz'));
+    $this->listener->expectArgumentsAt(2, 'characters', array('wow'));
+    $this->listener->expectArgumentsAt(3, 'characters', array('hm..'));
+    $this->listener->expectCallCount('endElement', 2);
+    $this->listener->expectArgumentsAt(0, 'endElement', array('foo'));
+    $this->listener->expectArgumentsAt(1, 'endElement', array('zoo'));
+    $this->listener->expectNever('invalidAttributeSyntax');
+    $this->listener->expectCallCount('php', 2);
+    $this->listener->expectArgumentsAt(0, 'php', array('<?php $var = "{{tag}}{{/tag}}";?>'));
+    $this->listener->expectArgumentsAt(1, 'php', array('<?php echo 1;?>'));
+    $this->parser->parse('{{foo}}hey{{/foo}}baz<?php $var = "{{tag}}{{/tag}}";?>{{zoo}}wow{{/zoo}}hm..<?php echo 1;?>');
+  }
+
   function testOutputTag()
   {
     $this->listener->expectOnce('startElement', array('$value', array()));



More information about the limb-svn mailing list