Skip to content

Commit

Permalink
[9.x] Fixes blade escaped tags issue (#45928)
Browse files Browse the repository at this point in the history
* Fixes blade escaped tags issue

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
imanghafoori1 and taylorotwell authored Feb 2, 2023
1 parent efcf3a6 commit ac18940
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ protected function compileStatements($template)
{
preg_match_all('/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( [\S\s]*? ) \))?/x', $template, $matches);

$offset = 0;

for ($i = 0; isset($matches[0][$i]); $i++) {
$match = [
$matches[0][$i],
Expand Down Expand Up @@ -538,12 +540,46 @@ protected function compileStatements($template)
$match[4] = $match[4].$rest;
}

$template = Str::replaceFirst($match[0], $this->compileStatement($match), $template);
[$template, $offset] = $this->replaceFirstStatement(
$match[0],
$this->compileStatement($match),
$template,
$offset
);
}

return $template;
}

/**
* Replace the first match for a statement compilation operation.
*
* @param string $search
* @param string $replace
* @param string $subject
* @param int $offset
* @return array
*/
protected function replaceFirstStatement($search, $replace, $subject, $offset)
{
$search = (string) $search;

if ($search === '') {
return $subject;
}

$position = strpos($subject, $search, $offset);

if ($position !== false) {
return [
substr_replace($subject, $replace, $position, strlen($search)),
$position + strlen($replace)
];
}

return [$subject, 0];
}

/**
* Determine if the given expression has the same number of opening and closing parentheses.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/View/Blade/BladeEscapedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@ public function testEscapedWithAtDirectivesAreCompiled()
$i as $x
)'));
}

public function testNestedEscapes()
{
$template = '
@foreach($cols as $col)
@@foreach($issues as $issue_45915)
@@endforeach
@endforeach';
$compiled = '
<?php $__currentLoopData = $cols; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $col): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
@foreach($issues as $issue_45915)
@endforeach
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
$this->assertSame($compiled, $this->compiler->compileString($template));
}
}

0 comments on commit ac18940

Please sign in to comment.