Skip to content

Commit

Permalink
Runtime: fixes raw modifier on fields with antlers: true (#6484)
Browse files Browse the repository at this point in the history
Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Aug 12, 2022
1 parent f7c410e commit 294b5de
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/View/Antlers/Language/Runtime/Sandbox/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ private function adjustValue($value, $originalNode)
{
if ($originalNode instanceof AbstractNode && $originalNode->modifierChain != null) {
if (! empty($originalNode->modifierChain->modifierChain)) {
$value = $this->checkForFieldValue($value, true);
$value = $this->checkForFieldValue($value, true, $originalNode->modifierChain->modifierChain);

return $this->applyModifiers($value, $originalNode->modifierChain);
}
Expand All @@ -1292,17 +1292,19 @@ private function adjustValue($value, $originalNode)
return $this->checkForFieldValue($value);
}

private function checkForFieldValue($value, $hasModifiers = false)
private function checkForFieldValue($value, $hasModifiers = false, $modifierChain = null)
{
if ($value instanceof Value) {
GlobalRuntimeState::$isEvaluatingUserData = true;
if ($value->shouldParseAntlers()) {
GlobalRuntimeState::$userContentEvalState = [
$value,
$this->nodeProcessor->getActiveNode(),
];
$value = $value->antlersValue($this->nodeProcessor->getAntlersParser(), $this->data);
GlobalRuntimeState::$userContentEvalState = null;
if (! $hasModifiers || ($modifierChain != null && $modifierChain[0]->nameNode->name != 'raw')) {
GlobalRuntimeState::$userContentEvalState = [
$value,
$this->nodeProcessor->getActiveNode(),
];
$value = $value->antlersValue($this->nodeProcessor->getAntlersParser(), $this->data);
GlobalRuntimeState::$userContentEvalState = null;
}
} else {
if (! $hasModifiers) {
$value = $value->value();
Expand Down
1 change: 1 addition & 0 deletions tests/Antlers/ParserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ protected function renderString($text, $data = [], $withCoreTagsAndModifiers = f
$processor->setData($data);

$runtimeParser = new RuntimeParser($documentParser, $processor, new AntlersLexer(), new LanguageParser());
$processor->setAntlersParserInstance($runtimeParser);

if ($withCoreTagsAndModifiers) {
$runtimeParser->cascade(app(Cascade::class));
Expand Down
22 changes: 22 additions & 0 deletions tests/Antlers/Runtime/CoreModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Tests\Antlers\Runtime;

use Carbon\Carbon;
use Facades\Statamic\Fields\FieldtypeRepository;
use Illuminate\Contracts\Support\Arrayable;
use Statamic\Fields\Field;
use Statamic\Fields\Fieldtype;
use Statamic\Fields\Value;
use Tests\Antlers\ParserTestCase;
Expand Down Expand Up @@ -401,6 +403,26 @@ public function shallowAugment($data)
$this->assertSame('test', $this->renderString('{{ variable | raw }}', ['variable' => $value], true));
}

public function test_raw_modifier_returns_raw_value_on_antlers_enabled_field()
{
// using markdown in this test just because its augmentation is simple.
$fieldtype = FieldtypeRepository::find('markdown');
$fieldtype->setField(new Field('test', ['antlers' => true]));

$value = new Value("# heading\nparagraph {{ foo }}", 'test', $fieldtype);

$vars = [
'test' => $value,
'foo' => 'bar',
];

$this->assertSame("<h1>heading</h1>\n<p>paragraph bar</p>\n", $this->renderString('{{ test }}', $vars));
$this->assertSame("# heading\nparagraph {{ foo }}", $this->renderString('{{ test | raw }}', $vars));
// Ensure other modifiers still work
$this->assertSame("<H1>HEADING</H1>\n<P>PARAGRAPH BAR</P>\n", $this->renderString('{{ test | upper }}', $vars));
$this->assertSame("# HEADING\nPARAGRAPH {{ FOO }}", $this->renderString('{{ test | raw | upper }}', $vars));
}

public function test_explode_on_tag_pairs()
{
// Issue: https://github.com/statamic/cms/issues/4979
Expand Down

0 comments on commit 294b5de

Please sign in to comment.