Skip to content

Commit

Permalink
fix: clear comments fail on has sepcial chars
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 29, 2021
1 parent 5615bee commit a5a0cb4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [8.0]
php: [8.0, 8.1]
os: [ubuntu-latest, macOS-latest] # windows-latest,
# include: # will not testing on php 7.2
# - os: 'ubuntu-latest'
Expand Down
13 changes: 9 additions & 4 deletions src/Compiler/PregCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use function implode;
use function is_numeric;
use function preg_match;
use function preg_replace;
use function preg_replace_callback;
use function str_contains;
use function str_ends_with;
use function strlen;
use function substr;
use function trim;
Expand Down Expand Up @@ -65,6 +65,11 @@ public function compile(string $tplCode): string
$openTagE = $this->openTagE;
$closeTagE = $this->closeTagE;

// comments block. `{{# comments #}}`
if (str_contains($tplCode, "$this->openTag#")) {
$tplCode = preg_replace("~$openTagE#.+?#$closeTagE~s", '', $tplCode);
}

$flags = 0;
// $flags = PREG_OFFSET_CAPTURE;
// $flags = PREG_PATTERN_ORDER | PREG_SET_ORDER;
Expand Down Expand Up @@ -109,9 +114,9 @@ public function parseCodeBlock(string $block): string
}

// comments block. `{{# comments #}}`
if ($block[0] === '#' && str_ends_with($block, '#')) {
return '';
}
// if ($block[0] === '#' && str_ends_with($block, '#')) {
// return '';
// }

$isInline = !str_contains($trimmed, "\n");
$kwPattern = Token::getBlockNamePattern();
Expand Down
39 changes: 39 additions & 0 deletions test/Compiler/PregCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,45 @@ public function testCompile_customDirective():void
}
}

public function testCompile_has_comments():void
{
$p = new PregCompiler();

$str = <<<'TXT'
{{# comments #}} hello
TXT;
$out = $p->compile($str);
$this->assertStringNotContainsString('{{#', $out);
$this->assertStringNotContainsString('comments', $out);
$this->assertEquals(' hello', $out);

$str = <<<'TXT'
{{# multi
line
comments
#}} hello
TXT;
$out = $p->compile($str);
$this->assertStringNotContainsString('{{#', $out);
$this->assertStringNotContainsString('comments', $out);
$this->assertEquals(' hello', $out);

$str = <<<'TXT'
{{ foreach ($vars as $var): }}
{{#
comments
newInfo.incr{{ $var | ucfirst }}({{ $var }});
#}}
public void {{ $var | ucfirst }}(Integer value) {
{{ $var }} += value;
}
{{ endforeach }}
TXT;
$out = $p->compile($str);
$this->assertStringNotContainsString('{{#', $out);
$this->assertStringNotContainsString('comments', $out);
}

public function testCompile_if_block():void
{
$p = new PregCompiler();
Expand Down

0 comments on commit a5a0cb4

Please sign in to comment.