Skip to content

Commit

Permalink
feat: add Config\View::$enalbeConditionals to disable Parser conditio…
Browse files Browse the repository at this point in the history
…nals
  • Loading branch information
kenjis committed Mar 30, 2022
1 parent 94cc0fe commit 48e681f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/Config/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class View extends BaseView
*/
public $plugins = [];

/**
* Use Parser conditionals (if, else, and elseif) or not.
*
* This setting is for compatibility with templates that works on CodeIgniter3.
* If you have JavaScript code in templates, Parser may raise error
* when there are strings that can be interpreted as conditionals.
* In that case, set this false.
*
* @var bool
*/
public $enalbeConditionals = true;

/**
* View Decorators are class methods that will be run in sequence to
* have a chance to alter the generated output just prior to caching
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,11 @@ parameters:
count: 1
path: system/View/Parser.php

-
message: "#^Property Config\\\\View\\:\\:\\$enalbeConditionals \\(bool\\) on left side of \\?\\? is not nullable\\.$#"
count: 1
path: system/View/Parser.php

-
message: "#^Result of \\|\\| is always false\\.$#"
paths:
Expand Down
20 changes: 18 additions & 2 deletions system/View/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ class Parser extends View
*/
protected $dataContexts = [];

/**
* Use Parser conditionals (if, else, and elseif) or not.
*
* This setting is for compatibility with templates that works on CodeIgniter3.
* If you have JavaScript code in templates, Parser may raise error
* when there are strings that can be interpreted as conditionals.
* In that case, set this false.
*
* @var bool
*/
protected $enalbeConditionals = true;

/**
* Constructor
*
Expand All @@ -72,6 +84,8 @@ public function __construct(ViewConfig $config, ?string $viewPath = null, $loade
// Ensure user plugins override core plugins.
$this->plugins = $config->plugins ?? [];

$this->enalbeConditionals = $config->enalbeConditionals ?? $this->enalbeConditionals;

parent::__construct($config, $viewPath, $loader, $debug, $logger);
}

Expand Down Expand Up @@ -223,8 +237,10 @@ protected function parse(string $template, array $data = [], ?array $options = n
$template = $this->parseComments($template);
$template = $this->extractNoparse($template);
// Replace any conditional code here so we don't have to parse as much
$template = $this->parseConditionals($template);
if ($this->enalbeConditionals) {
// Replace any conditional code here so we don't have to parse as much
$template = $this->parseConditionals($template);
}
// Handle any plugins before normal data, so that
// it can potentially modify any template between its tags.
Expand Down
34 changes: 34 additions & 0 deletions tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,38 @@ public function testRenderFindsOtherView()
$expected = '<h1>Hello World</h1>';
$this->assertSame($expected, $this->parser->render('Simpler.html'));
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5831
*/
public function testEnalbeConditionalsFalse()
{
$this->config->enalbeConditionals = false;
$this->parser = new Parser($this->config, $this->viewsDir, $this->loader);

$data = [
'message' => 'Warning!',
];
$this->parser->setData($data);

$template = <<<'EOL'
<script type="text/javascript">
var f = function() {
if (true) {
alert('{message}');
}
}
</script>
EOL;
$expected = <<<'EOL'
<script type="text/javascript">
var f = function() {
if (true) {
alert('Warning!');
}
}
</script>
EOL;
$this->assertSame($expected, $this->parser->renderString($template));
}
}

0 comments on commit 48e681f

Please sign in to comment.