Skip to content

Commit

Permalink
Merge pull request #1098 from spiral/hotfix/stempler-dircetive-zeros
Browse files Browse the repository at this point in the history
Fixed bug in directive parser
  • Loading branch information
butschster committed Apr 3, 2024
2 parents 819a092 + 0e980cd commit a8d0c93
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Stempler/src/Parser/Syntax/DynamicSyntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ private function fetchValues(string $body): array
$src = new StringStream($body);

while ($n = $src->peak()) {
if (\in_array($n, ['"', '"'])) {
if (\in_array($n, ['"', "'"], true)) {
$values[\count($values) - 1] .= $n;
while ($nn = $src->peak()) {
while (($nn = $src->peak()) !== null) {
$values[\count($values) - 1] .= $nn;
if ($nn === $n) {
break;
Expand Down
45 changes: 45 additions & 0 deletions src/Stempler/tests/Directive/DirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Directive;

use Spiral\Tests\Stempler\fixtures\ImageDirective;
use Spiral\Tests\Stempler\Directive\BaseTestCase;

final class DirectiveTest extends BaseTestCase
{
protected const DIRECTIVES = [
ImageDirective::class,
];

public function testStringWithZeroChars(): void
{
$doc = $this->parse('@image("blog", "test.png", "150|250", "webp")');

$this->assertSame(
'<img title="blog" src="test.png" size="150|250" type="webp">',
$this->compile($doc),
);
}

public function testStringWithSingleQuotes(): void
{
$doc = $this->parse("@image('blog', 'test.png', '150|250', 'webp')");

$this->assertSame(
"<img title='blog' src='test.png' size='150|250' type='webp'>",
$this->compile($doc),
);
}

public function testVariableInjection(): void
{
$doc = $this->parse('@image("blog", $src, "150|250", "webp")');

$this->assertSame(
'<img title="blog" src="<?php echo $src; ?>" size="150|250" type="webp">',
$this->compile($doc),
);
}
}
22 changes: 22 additions & 0 deletions src/Stempler/tests/fixtures/ImageDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Stempler\fixtures;

use Spiral\Stempler\Directive\AbstractDirective;
use Spiral\Stempler\Node\Dynamic\Directive;

final class ImageDirective extends AbstractDirective
{
public function renderImage(Directive $directive): string
{
return \sprintf(
'<img title=%s src=%s size=%s type=%s>',
$directive->values[0],
\str_starts_with($directive->values[1], '$') ? \sprintf('"<?php echo %s; ?>"', $directive->values[1]) : $directive->values[1],
$directive->values[2],
$directive->values[3],
);
}
}

0 comments on commit a8d0c93

Please sign in to comment.