Commit f377fd89 by Seldaek

* {literal} and {strip} now follow the LooseOpeningsHandling setting

git-svn-id: svn://dwoo.org/dwoo/trunk@64 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 9749a55f
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* Optimized scope-handling functions, {loop} and {with} are now slightly faster * Optimized scope-handling functions, {loop} and {with} are now slightly faster
* Fixed a bug in {date_format} that prevented anything but unix timestamps to * Fixed a bug in {date_format} that prevented anything but unix timestamps to
work work
* {literal} and {strip} now follow the LooseOpeningsHandling setting
[2008-05-10] 0.9.0 [2008-05-10] 0.9.0
! BC Break: changed all class names to be PEAR compliant (aka use underscores ! BC Break: changed all class names to be PEAR compliant (aka use underscores
......
...@@ -514,8 +514,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -514,8 +514,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
// handles the built-in strip function // handles the built-in strip function
if(($pos = strpos($tpl, $this->ld.'strip'.$this->rd)) !== false && substr($tpl, $pos-1, 1) !== '\\') if(preg_match('/'.$this->ldr . ($this->allowLooseOpenings ? '\s*' : '') . 'strip' . ($this->allowLooseOpenings ? '\s*' : '') . $this->rdr.'/s', $tpl, $pos, PREG_OFFSET_CAPTURE) && substr($tpl, $pos[0][1]-1, 1) !== '\\')
$tpl = preg_replace_callback('{'.$this->ldr.'strip'.$this->rdr.'(.+?)'.$this->ldr.'/strip'.$this->rdr.'}s', array($this, 'stripPreprocessorHelper'), $tpl); {
if(!preg_match('/'.$this->ldr . ($this->allowLooseOpenings ? '\s*' : '') . '\/strip' . ($this->allowLooseOpenings ? '\s*' : '') . $this->rdr.'/s', $tpl))
throw new Dwoo_Compilation_Exception('The {strip} blocks must be closed explicitly');
$tpl = preg_replace_callback('/'.$this->ldr.($this->allowLooseOpenings ? '\s*' : '').'strip'.($this->allowLooseOpenings ? '\s*' : '').$this->rdr.'(.+?)'.$this->ldr.($this->allowLooseOpenings ? '\s*' : '').'\/strip'.($this->allowLooseOpenings ? '\s*' : '').$this->rdr.'/s', array($this, 'stripPreprocessorHelper'), $tpl);
}
while(true) while(true)
{ {
...@@ -544,12 +548,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -544,12 +548,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$compiled .= substr($tpl, $ptr, $pos-$ptr-1).$this->ld; $compiled .= substr($tpl, $ptr, $pos-$ptr-1).$this->ld;
$ptr = $pos+strlen($this->ld); $ptr = $pos+strlen($this->ld);
} }
elseif(strpos($tpl, $this->ld.'literal'.$this->rd, $pos) === $pos) elseif(preg_match('/^'.$this->ldr . ($this->allowLooseOpenings ? '\s*' : '') . 'literal' . ($this->allowLooseOpenings ? '\s*' : '') . $this->rdr.'/s', substr($tpl, $pos), $litOpen))
{ {
$endpos = strpos($tpl, $this->ld.'/literal'.$this->rd, $pos); if(!preg_match('/'.$this->ldr . ($this->allowLooseOpenings ? '\s*' : '') . '\/literal' . ($this->allowLooseOpenings ? '\s*' : '') . $this->rdr.'/s', $tpl, $litClose, PREG_OFFSET_CAPTURE, $pos))
$compiled .= substr($tpl, $ptr, $pos-$ptr); throw new Dwoo_Compilation_Exception('The {literal} blocks must be closed explicitly');
$compiled .= substr($tpl, $pos + strlen($this->ld.'literal'.$this->rd), $endpos-$pos-strlen($this->ld.'literal'.$this->rd)); $endpos = $litClose[0][1];
$ptr = $endpos+strlen($this->ld.'/literal'.$this->rd); $compiled .= substr($tpl, $ptr, $pos-$ptr) . substr($tpl, $pos + strlen($litOpen[0]), $endpos-$pos-strlen($litOpen[0]));
$ptr = $endpos+strlen($litClose[0][0]);
} }
else else
{ {
......
...@@ -140,6 +140,16 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -140,6 +140,16 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals("abca\nb\nc", $this->dwoo->get($tpl, array(), $this->compiler)); $this->assertEquals("abca\nb\nc", $this->dwoo->get($tpl, array(), $this->compiler));
} }
/**
* @expectedException Dwoo_Compilation_Exception
*/
public function testUnclosedStrip()
{
$tpl = new Dwoo_Template_String("{strip}a\nb\nca\nb\nc");
$tpl->forceCompilation();
$this->dwoo->get($tpl, array(), $this->compiler);
}
public function testWhitespace() public function testWhitespace()
{ {
$tpl = new Dwoo_Template_String("{\$foo}{\$foo}\n{\$foo}\n\n{\$foo}\n\n\n{\$foo}"); $tpl = new Dwoo_Template_String("{\$foo}{\$foo}\n{\$foo}\n\n{\$foo}\n\n\n{\$foo}");
...@@ -154,6 +164,16 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -154,6 +164,16 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals('{$foo}{hurray}', $this->dwoo->get($tpl, array(), $this->compiler)); $this->assertEquals('{$foo}{hurray}', $this->dwoo->get($tpl, array(), $this->compiler));
} }
/**
* @expectedException Dwoo_Compilation_Exception
*/
public function testUnclosedLiteral()
{
$tpl = new Dwoo_Template_String('{literal}{$foo}{hurray}');
$tpl->forceCompilation();
$this->dwoo->get($tpl, array(), $this->compiler);
}
public function testEscaping() public function testEscaping()
{ {
$tpl = new Dwoo_Template_String('\{foo\{bar\\\\{$var}}{"tes\}t"}{"foo\"lol\"bar"}'); $tpl = new Dwoo_Template_String('\{foo\{bar\\\\{$var}}{"tes\}t"}{"foo\"lol\"bar"}');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment