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

[4.x] Fix runtime behavior regression #8873

Closed
wants to merge 3 commits into from
Closed
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
19 changes: 16 additions & 3 deletions src/View/Antlers/Language/Runtime/NodeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1755,8 +1755,12 @@ public function reduce($processNodes)

// Only revert to parseLoop behavior if the tag contains
// parameters that are likely to dramatically change
// the scope or overall output of the tag result
if ($node->hasScopeAdjustingParameters) {
// the scope or overall output of the tag result.
// We will also revert to parseLoop behavior if
// the tag returns an associative array and
// the tag has parameters with the same
// name as modifiers for compatibility.
if ($node->hasModifierParameters() && Arr::isAssoc($output) || $node->hasScopeAdjustingParameters) {
$tagAssocOutput = $output;
$output = Arr::assoc($output) ? (string) $tag->parse($output) : (string) $tag->parseLoop($this->addLoopIterationVariables($output));
$tagCallbackResult = null;
Expand Down Expand Up @@ -2260,6 +2264,8 @@ public function reduce($processNodes)
}
}

$beforeAssignments = $this->runtimeAssignments;

$evalData = $val + $tmpArrayData;
$this->pushScope($node, $evalData);

Expand All @@ -2280,10 +2286,17 @@ public function reduce($processNodes)

$buffer .= $this->measureBufferAppend($node, $this->modifyBufferAppend($assocOutput));

$runtimeAssignmentsToProcess = $processor->getRuntimeAssignments();
$runtimeAssignmentsToProcess = [];

$this->popScope($node->refId);

// Ensure we don't leak variables.
foreach ($processor->getRuntimeAssignments() as $assignVar => $assignValue) {
if (array_key_exists($assignVar, $beforeAssignments)) {
$runtimeAssignmentsToProcess[$assignVar] = $assignValue;
}
}

if (! empty($runtimeAssignmentsToProcess)) {
$this->data = $lockData;
$this->processAssignments($runtimeAssignmentsToProcess);
Expand Down
223 changes: 223 additions & 0 deletions tests/Antlers/Sandbox/VariableAssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Facades\Tests\Factories\EntryFactory;
use Statamic\Facades\Collection;
use Statamic\Facades\Taxonomy;
use Statamic\Tags\Tags;
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
use Tests\Antlers\ParserTestCase;
use Tests\FakesViews;
Expand Down Expand Up @@ -197,6 +198,228 @@ public function test_assignments_are_processed_when_tags_contain_parameters_that
$this->assertSame($expected, trim($this->renderString($template, [], true)));
}

public function test_tags_with_modifier_names_that_return_associative_arrays_use_old_behavior()
{
(new class extends Tags
{
public static $handle = 'the_tag';

public function index()
{
// What this is doesn't matter, just that it's an associative array.
return [
'title' => 'The Title',
'content' => 'The Content',
];
}
})::register();

$template = <<<'EOT'
{{ the_tag :url="title" }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ items }}{{ value }}{{ /items }}

{{ the_tag :url="title" }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ items }}{{ value }}{{ /items }}

{{ /the_tag }}
{{ /the_tag }}

{{ items }}
{{ value }}
{{ /items }}
EOT;

// The final 'a b c' at the end is arguably wrong, but it's the old behavior.
$expected = <<<'EXP'
The Title
The Content


abc


The Title
The Content


abc





a

b

c
EXP;

$this->assertSame($expected, $this->renderString(trim($this->renderString($template, ['items' => ['d', 'e', 'f']], true))));

// Removing the modifier name from the tag will make the assignment not leak, matching old behavior.
$template = <<<'EOT'
{{ the_tag }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ items }}{{ value }}{{ /items }}

{{ the_tag :url="title" }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ items }}{{ value }}{{ /items }}

{{ /the_tag }}
{{ /the_tag }}

{{ items }}
{{ value }}
{{ /items }}
EOT;

$expected = <<<'EXP'
The Title
The Content


abc


The Title
The Content


abc





d

e

f
EXP;

$this->assertSame($expected, $this->renderString(trim($this->renderString($template, ['items' => ['d', 'e', 'f']], true))));

// No collisions anywhere.
$template = <<<'EOT'
{{ the_tag }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ items }}{{ value }}{{ /items }}

{{ the_tag }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ items }}{{ value }}{{ /items }}

{{ /the_tag }}
{{ /the_tag }}

{{ items }}
{{ value }}
{{ /items }}
EOT;

$this->assertSame($expected, $this->renderString(trim($this->renderString($template, ['items' => ['d', 'e', 'f']], true))));
}

public function test_tags_returning_associative_arrays_do_not_cause_variable_leakage()
{
(new class extends Tags
{
public static $handle = 'the_tag';

public function index()
{
// What this is doesn't matter, just that it's an associative array.
return [
'title' => 'The Title',
'content' => 'The Content',
];
}
})::register();

$template = <<<'EOT'
{{ the_tag }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ /the_tag }}

{{ items }}
{{ value }}
{{ /items }}
EOT;

$expected = <<<'EXP'
The Title
The Content





d

e

f
EXP;

$this->assertSame($expected, trim($this->renderString($template, ['items' => ['d', 'e', 'f']], true)));

$template = <<<'EOT'
Before: {{ items }}{{ value }}{{ /items }}
{{# This should trigger a scope reassignment. #}}
{{ items = [] /}}
{{ the_tag }}
{{ title }}
{{ content }}

{{ items = ['a', 'b', 'c'] }}
{{ /the_tag }}

{{ items }}{{ value }}{{ /items }}
EOT;

$expected = <<<'EXP'
Before: def



The Title
The Content




abc
EXP;

$this->assertSame($expected, trim($this->renderString($template, ['items' => ['d', 'e', 'f']], true)));
}

public function test_assignments_are_traced_from_nested_arrays_and_tags()
{
$data = [
Expand Down
Loading