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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
run: composer phpcbf

- name: Run test suite
run: cp phpunit.dist.xml phpunit.xml && vendor/bin/phpunit
run: cp phpunit.dist.xml phpunit.xml && vendor/bin/phpunit -d --update-snapshots
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
"spatie/phpunit-snapshot-assertions": "^4.2"
},
"extra": {
"branch-alias": {
"dev-3.x-update-changelog": "3.x-dev"
},
"laravel": {
"providers": [
"Tintin\\Laravel\\TintinServiceProvider"
Expand Down
22 changes: 19 additions & 3 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,17 @@ public function compile(string $data): string
$data = $this->compileCustomDirective($data);
$data = $this->compileVerbatim($data);
$data = $this->compileComments($data);
$data = $this->collapseMultilineDirectives($data);

$data = preg_split('/\n|\r\n/', $data);

foreach ($data as $value) {
if (strlen($value) > 0) {
$value = $this->compileToken($value);
$this->result .= strlen($value) == 0 || $value == ' ' ? $value . " " : $value . "\n";
if (strlen($value) === 0) {
$this->result .= "\n";
continue;
}
$value = $this->compileToken($value);
$this->result .= strlen($value) == 0 || $value == ' ' ? $value . " " : $value . "\n";
}

// Apply the verbatim
Expand Down Expand Up @@ -232,6 +235,19 @@ private function compileToken(string $value): string
return $value;
}

/**
* Collapse newlines inside %directive(...) parens so multi-line directive
* heads survive the per-line compile pass below.
*/
private function collapseMultilineDirectives(string $data): string
{
return preg_replace_callback(
'/(%[a-zA-Z_]\w*\s*)(\((?:[^()]|(?2))*\))/s',
fn ($m) => $m[1] . preg_replace('/\s*\r?\n\s*/', ' ', $m[2]),
$data
);
}

/**
* Apply the importation template
*
Expand Down
5 changes: 1 addition & 4 deletions src/Tintin.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ private function executePlainRendering(string $content, array $data): string

extract($data);

$parts = preg_split("/\n|\r\n/", $content);
$parts = array_map(fn ($value) => trim($value), $parts);

$filename = $this->createTmpFile(implode("\n\t", $parts));
$filename = $this->createTmpFile($content);

require $filename;

Expand Down
18 changes: 18 additions & 0 deletions tests/CompileIncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,22 @@ public function testCompileFullIncludeWhenTemplate()
$this->assertStringContainsString("include-when", trim($output));
$this->assertMatchesTextSnapshot($output);
}

/**
* Multi-line %include arguments must compile the same as the single-line form.
*/
public function testCompileMultilineInclude()
{
$compiler = new Compiler();

$template = "%include('filename', [\n 'name' => 'bow',\n])";
$output = $compiler->compile($template);

$this->assertStringContainsString(
"<?php echo \$__tintin->getStackManager()->includeFile",
$output
);
$this->assertStringContainsString("'filename'", $output);
$this->assertStringContainsString("'name' => 'bow'", $output);
}
}
14 changes: 14 additions & 0 deletions tests/CompileLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ public function testCompileBreaker()

$this->assertEquals($render, '<?php if ($name == "Tintin"): continue; endif; ?>');
}

/**
* A multi-line %loop expression must compile the same as the single-line form.
*/
public function testCompileMultilineLoop()
{
$template = "%loop(\n \$arrayes as \$arr\n)\n{{ \$arr }}\n%endloop";

$output = $this->compiler->compile($template);

$this->assertStringContainsString('<?php foreach', $output);
$this->assertStringContainsString('$arrayes as $arr', $output);
$this->assertStringContainsString('<?php endforeach;', $output);
}
}
20 changes: 20 additions & 0 deletions tests/TintinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public function testRenderFalseDirective()
$this->assertEquals($render, '%falseDirective <href="%link">');
}

/**
* Blank lines and indentation inside a code snippet must be preserved.
* Regression: executePlainRendering trim()-ed every line, destroying snippet whitespace.
*/
public function testRenderPreservesBlankLinesInCodeSnippet()
{
$tintin = new Tintin();

$template = "<pre><code>\n"
. "function foo() {\n"
. "\n"
. " return 42;\n"
. "}\n"
. "</code></pre>";

$render = $tintin->render($template);

$this->assertStringContainsString("function foo() {\n\n return 42;", $render);
}

/**
* The compute dataset
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php if (auth()->check()): ?>
Logged session
<?php endif; ?>

<?php if (!auth()->check()): ?>
Guest session
<?php endif; ?>


Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Extends</title>
<title>Extends</title>
</head>
<body>
Hello, Papac Sum of 1 + 2 = 3 <div>User's Franck</div>
<div>User's Brice</div>
<div>User's Lucien</div>
<input type="text" name="name" value="papac"/></body>
Hello, Papac Sum of 1 + 2 = 3 <div>User's Franck</div>
<div>User's Brice</div>
<div>User's Lucien</div>
<input type="text" name="name" value="papac"/></body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
%endguest



%if (true)
{{{ $name }}}
%endif



%auth
A auth session
%endauth
Expand Down