-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Fluid code in comments no longer interpreted (#956)
Previously, the behavior of the `<f:comment>` ViewHelper was confusing: Even if invalid Fluid code (e. g. syntax errors or a non-existent ViewHelper) was wrapped with `<f:comment>`, it would still be interpreted by the Fluid parser and an exception would be thrown. This patch resolves this similarly to CDATA handling. It introduces a new TemplateProcessor, which is executed before any other parsing and strips out the contents of Fluid comments. The `<f:comment>` ViewHelpers are kept intact and are filled with empty lines. This can be helpful when debugging Fluid templates because the line numbers in exceptions are consistent to the original template file. This new TemplateProcessor is then registered in the default RenderingContext implementation. For systems that set their own TemplateProcessors, this would probably need adjustments. However, this change is not breaking since the old functionality is still there. Fluid's own tests however now assume that this new TemplateProcessor is registered and are adjusted properly in this patch.
- Loading branch information
Showing
6 changed files
with
69 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Core/Parser/TemplateProcessor/RemoveCommentsTemplateProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file belongs to the package "TYPO3 Fluid". | ||
* See LICENSE.txt that was shipped with this package. | ||
*/ | ||
|
||
namespace TYPO3Fluid\Fluid\Core\Parser\TemplateProcessor; | ||
|
||
use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessorInterface; | ||
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | ||
|
||
class RemoveCommentsTemplateProcessor implements TemplateProcessorInterface | ||
{ | ||
protected RenderingContextInterface $renderingContext; | ||
|
||
public function setRenderingContext(RenderingContextInterface $renderingContext): void | ||
{ | ||
$this->renderingContext = $renderingContext; | ||
} | ||
|
||
/** | ||
* Replaces all comment ViewHelpers with empty lines to exclude it | ||
* from further processing in the templateParser while maintaining | ||
* the line-count of the template string for the exception handler | ||
* to reference to. These empty lines are again wrapped inside | ||
* <f:comment> to not introduce any unwanted whitespace changes in | ||
* existing templates. | ||
*/ | ||
public function preProcessSource(string $templateSource): string | ||
{ | ||
$parts = preg_split('#(</?f:comment>)#', $templateSource, -1, PREG_SPLIT_DELIM_CAPTURE); | ||
|
||
$balance = 0; | ||
foreach ($parts as $index => $part) { | ||
if ($part === '<f:comment>') { | ||
$balance++; | ||
} | ||
if ($balance > 0) { | ||
$parts[$index] = '<f:comment>' . str_repeat(PHP_EOL, substr_count($part, PHP_EOL)) . '</f:comment>'; | ||
} | ||
if ($part === '</f:comment>') { | ||
$balance--; | ||
} | ||
} | ||
|
||
return implode('', $parts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters