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

svn at limb-project.com svn at limb-project.com
Wed Jul 9 13:42:05 MSD 2008


Author: serega
Date: 2008-07-09 13:42:05 +0400 (Wed, 09 Jul 2008)
New Revision: 7103
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7103

Modified:
   3.x/trunk/limb/i18n/src/macro/clip.filter.php
   3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php
Log:
-- improved version of i18n_clip filter. Now it should works with boundary parameter (forth parameter of the filter).

Modified: 3.x/trunk/limb/i18n/src/macro/clip.filter.php
===================================================================
--- 3.x/trunk/limb/i18n/src/macro/clip.filter.php	2008-07-08 21:26:09 UTC (rev 7102)
+++ 3.x/trunk/limb/i18n/src/macro/clip.filter.php	2008-07-09 09:42:05 UTC (rev 7103)
@@ -15,10 +15,29 @@
  */
 class lmbI18NMacroClipFilter extends lmbMacroFilter
 {
+  protected $suffix_var;
+  protected $chunk_var;
+  
   function preGenerate($code)
   {
+    parent :: preGenerate($code);
+
     $code->registerInclude('limb/i18n/src/charset/driver.inc.php');
-    parent :: preGenerate($code);
+    $value = $this->base->getValue();
+    
+    if(count($this->params) > 2)
+    {
+      $this->suffix_var = $code->generateVar();
+      $limit = $this->params[0];
+      $offset = $this->params[1];
+      $code->writePhp("{$this->suffix_var} = '';\n");
+      $code->writePhp("if(lmb_strlen($value) > ($limit + $offset)) {$this->suffix_var} = " . $this->params[2] . ";\n");
+    }
+    if(count($this->params) > 3)
+    {
+      $this->chunk_var = $code->generateVar();
+      $code->writePhp($this->chunk_var . ' = lmb_substr('. $value .','. $this->params[1] .','. $this->params[0]. ');');
+    }
   }
 
   function getValue()
@@ -34,24 +53,16 @@
       return 'lmb_substr('. $value .','. $this->params[1] .','. $this->params[0]. ')';
       break;
     case 3:
-      $suffix = $this->_getSuffix($value,
-                                 $this->params[0],
-                                 $this->params[1],
-                                 $this->params[2]);
-        return 'lmb_substr(' . $value .','. $this->params[1] .','. $this->params[0] .')' . $suffix;
+        return 'lmb_substr(' . $value .','. $this->params[1] .','. $this->params[0] .') . ' . $this->suffix_var;
       break;
     case 4:
       $limit = $this->params[0];
       $offset = $this->params[1];
       $word_wrap = $this->params[3];
-      $suffix = $this->_getSuffix($value, $limit, $offset, $this->params[2]);
 
       if (strtoupper(substr($word_wrap,0,1)) != 'N')
       {
-          if(lmb_preg_match('~^(.{0,'.$limit.'}(?U)\w*)\b~ism', lmb_substr($value, $offset), $match))
-            return $match[1].$suffix;
-          else
-            return '';
+        return "(lmb_preg_match('~^(' .{$this->chunk_var} . '[^\s]*)~ism', lmb_substr($value, $offset), \$match) ? \$match[1] . {$this->suffix_var} : '')"; 
       }
       else
         return 'lmb_substr('. $value .','. $offset. ','. $limit .')' . $suffix;
@@ -60,12 +71,4 @@
         throw new lmbMacroException('Wrong number of filter params(1..4)');
     }
   }
-
-  function _getSuffix($value, $limit, $offset, $suffix)
-  {
-    $result = '';
-    if(lmb_strlen($value) > ($limit + $offset))
-      $result = '.' . $suffix;
-    return $result;
-  }
 }
\ No newline at end of file

Modified: 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php
===================================================================
--- 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php	2008-07-08 21:26:09 UTC (rev 7102)
+++ 3.x/trunk/limb/i18n/tests/cases/macro/lmbI18NClipMacroFilterTest.class.php	2008-07-09 09:42:05 UTC (rev 7103)
@@ -65,15 +65,15 @@
     $this->assertEqual($out, 'фреймворк');
   }
 
-  // can't implement this since PHP has some bugs with /b modifier in multibyte mode
-  function _testLongStringWordBoundary()
+  // don't know if boundary condition works for all cases. Should work for the simple ones.
+  function testLongStringWordBoundary()
   {
-    $code = '{$#var|i18n_clip:11,1,"...", "y"}';
+    $code = '{$#var|i18n_clip:12,0,"...", "y"}';
     $tpl = $this->_createMacroTemplate($code, 'clip_with_word_bound.html');
     $var = "фреймворк для веб-приложений";
     $tpl->set('var', $var);
     $out = $tpl->render();
-    $this->assertEqual($out, 'реймворк для...');
+    $this->assertEqual($out, 'фреймворк для...');
   }
 
   function testPathBasedDBELengthLimit()



More information about the limb-svn mailing list