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

fix: Parser fails with ({variable}) in loop #5840

Merged
merged 2 commits into from
Apr 12, 2022
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
13 changes: 2 additions & 11 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ protected function parsePair(string $variable, array $data, string $template): a
*/
foreach ($matches as $match) {
// Loop over each piece of $data, replacing
// it's contents so that we know what to replace in parse()
// its contents so that we know what to replace in parse()
$str = ''; // holds the new contents for this tag pair.

foreach ($data as $row) {
Expand Down Expand Up @@ -338,8 +338,7 @@ protected function parsePair(string $variable, array $data, string $template): a
$str .= $out;
}

// Escape | character from filters as it's handled as OR in regex
$escapedMatch = preg_replace('/(?<!\\\\)\\|/', '\\|', $match[0]);
$escapedMatch = preg_quote($match[0], '#');

$replace['#' . $escapedMatch . '#s'] = $str;
}
Expand Down Expand Up @@ -471,16 +470,8 @@ public function setDelimiters($leftDelimiter = '{', $rightDelimiter = '}'): Rend
*/
protected function replaceSingle($pattern, $content, $template, bool $escape = false): string
{
// Any dollar signs in the pattern will be misinterpreted, so slash them
$pattern = addcslashes($pattern, '$');
$content = (string) $content;

// Flesh out the main pattern from the delimiters and escape the hash
// See https://regex101.com/r/IKdUlk/1
if (preg_match('/^(#)(.+)(#(m?s)?)$/s', $pattern, $parts)) {
$pattern = $parts[1] . addcslashes($parts[2], '#') . $parts[3];
}

// Replace the content in the template
return preg_replace_callback($pattern, function ($matches) use ($content, $escape) {
// Check for {! !} syntax to not escape this one.
Expand Down
20 changes: 20 additions & 0 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,26 @@ public function testParseLoop()
$this->assertSame("Super Heroes\nTom Dick Henry ", $this->parser->renderString($template));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5825
*/
public function testParseLoopVariableWithParentheses()
{
$data = [
'title' => 'Super Heroes',
'powers' => [
['name' => 'Tom'],
['name' => 'Dick'],
['name' => 'Henry'],
],
];

$template = "{title}\n{powers}({name}) {/powers}";

$this->parser->setData($data);
$this->assertSame("Super Heroes\n(Tom) (Dick) (Henry) ", $this->parser->renderString($template));
}

public function testParseLoopObjectProperties()
{
$obj1 = new stdClass();
Expand Down