Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fixes blade escaped tags issue #45928

Merged
merged 2 commits into from Feb 2, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
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
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));
}
}