Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Compiler
'LoopStack',
'ExtendsStack',
'HelpersStack',
'CustomDirective',
'Json',
'Class',
'Import'
Expand Down Expand Up @@ -115,11 +116,19 @@ class Compiler
protected $result = '';

/**
* The expression pattern
* The expression pattern.
*
* Group 2 uses a recursive subpattern so the inner capture stops at the
* matching `)` instead of greedily eating up to the last `)` on the line.
* Without this, a single-line directive whose body also contains parens
* (e.g. `%if ($x) <span>{{ $x }}</span> %endif`, where the echo pass has
* already rewritten `{{ $x }}` into `<?php echo e($x); ?>`) would have its
* head extended past the real close-paren, producing broken PHP like
* `<?php if ($x) <span>...e($x): ?>; ?></span>`.
*
* @var string
*/
protected $condition_pattern = '/(%s\s*\((.*)\))\s*/sm';
protected $condition_pattern = '/(%s\s*\(((?:[^()]|\((?2)\))*)\))\s*/s';

/**
* The option expression pattern
Expand Down
2 changes: 1 addition & 1 deletion src/Lexique/CompileCustomDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
trait CompileCustomDirective
{
/**
* Compile the custom directive
* Compile the custom statement
*
* @param string $expression
* @return string
Expand Down
71 changes: 71 additions & 0 deletions tests/CompileIfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,77 @@ public function testcompileEndifStatement()
$this->assertEquals($render, '<?php endif; ?>');
}

public function testInlineIfWithEchoBody()
{
$source = '%if ($service->runtime_version) <span>{{ $service->runtime_version }}</span> %endif';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php if ($service->runtime_version): ?>', $render);
$this->assertStringContainsString('<?php echo e($service->runtime_version); ?>', $render);
$this->assertStringContainsString('<?php endif; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

public function testInlineIfWithNestedParens()
{
$source = '%if (count($items) > 0) yes %endif';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php if (count($items) > 0): ?>', $render);
}

/**
* Regression: %unless shares condition_pattern with %if.
*/
public function testInlineUnlessWithEchoBody()
{
$source = '%unless ($name) <span>{{ $name }}</span> %endunless';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php if (! ($name)): ?>', $render);
$this->assertStringContainsString('<?php echo e($name); ?>', $render);
$this->assertStringContainsString('<?php endif; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

/**
* Regression: %isset shares condition_pattern with %if.
*/
public function testInlineIssetWithEchoBody()
{
$source = '%isset ($name) <span>{{ $name }}</span> %endisset';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php if (isset($name)): ?>', $render);
$this->assertStringContainsString('<?php echo e($name); ?>', $render);
$this->assertStringContainsString('<?php endif; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

/**
* Regression: %elseif (and the %elif alias) share condition_pattern.
* Exercise an if/elseif/else chain with echoes in every branch.
*/
public function testInlineIfElseIfElseWithEchoBodies()
{
$source = '%if ($a) <span>{{ $a }}</span> %elseif ($b) <span>{{ $b }}</span> %else <span>{{ $c }}</span> %endif';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php if ($a): ?>', $render);
$this->assertStringContainsString('<?php elseif ($b): ?>', $render);
$this->assertStringContainsString('<?php else: ?>', $render);
$this->assertStringContainsString('<?php echo e($a); ?>', $render);
$this->assertStringContainsString('<?php echo e($b); ?>', $render);
$this->assertStringContainsString('<?php echo e($c); ?>', $render);
$this->assertStringContainsString('<?php endif; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

public function testBlockStatement()
{
$html = file_get_contents(__DIR__ . '/view/sample.tintin.php');
Expand Down
48 changes: 48 additions & 0 deletions tests/CompileLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,54 @@ public function testCompileBreaker()
$this->assertEquals($render, '<?php if ($name == "Tintin"): continue; endif; ?>');
}

/**
* Regression: a single-line %loop whose body contains an echo must not
* have its head extended past the real `)` by the greedy condition
* pattern (shared with %if).
*/
public function testInlineLoopWithEchoBody()
{
$source = '%loop ($items as $item) <span>{{ $item }}</span> %endloop';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php foreach ($items as $item): ?>', $render);
$this->assertStringContainsString('<?php echo e($item); ?>', $render);
$this->assertStringContainsString('<?php endforeach; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

/**
* Regression: same greedy-match bug, %while variant.
*/
public function testInlineWhileWithEchoBody()
{
$source = '%while ($i < count($items)) <span>{{ $i }}</span> %endwhile';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php while ($i < count($items)): ?>', $render);
$this->assertStringContainsString('<?php echo e($i); ?>', $render);
$this->assertStringContainsString('<?php endwhile; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

/**
* Regression: %for has the extra wrinkle of semicolons inside the head.
* The balanced-paren matcher must still stop at the head's real `)`.
*/
public function testInlineForWithEchoBody()
{
$source = '%for ($i = 0; $i < count($items); $i++) <span>{{ $i }}</span> %endfor';

$render = $this->compiler->compile($source);

$this->assertStringContainsString('<?php for ($i = 0; $i < count($items); $i++): ?>', $render);
$this->assertStringContainsString('<?php echo e($i); ?>', $render);
$this->assertStringContainsString('<?php endfor; ?>', $render);
$this->assertStringNotContainsString('): ?>; ?>', $render);
}

/**
* A multi-line %loop expression must compile the same as the single-line form.
*/
Expand Down
Loading