Skip to content

Commit

Permalink
Merge pull request #117 from utopia-php/feat-template-escape-html-by-…
Browse files Browse the repository at this point in the history
…default

feat: escape html by default in view params
  • Loading branch information
TorstenDittmann authored Jan 8, 2024
2 parents ad6f7e6 + 5b62a82 commit e3ff6b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ public function __construct(string $path = '')
*
* @throws Exception
*/
public function setParam(string $key, mixed $value): static
public function setParam(string $key, mixed $value, bool $escapeHtml = true): static
{
if (\strpos($key, '.') !== false) {
throw new Exception('$key can\'t contain a dot "." character');
}

if (is_string($value) && $escapeHtml) {
$value = \htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}

$this->params[$key] = $value;

return $this;
Expand Down
6 changes: 6 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ public function testCanFilterNewLinesToParagraphs()
{
$this->assertEquals('<p>line1</p><p>line2</p>', $this->view->print("line1\n\nline2", View::FILTER_NL2P));
}

public function testCanSetParamWithEscapedHtml()
{
$this->view->setParam('key', '<html>value</html>');
$this->assertEquals('&lt;html&gt;value&lt;/html&gt;', $this->view->getParam('key', 'default'));
}
}

0 comments on commit e3ff6b9

Please sign in to comment.