Skip to content

Commit c82a522

Browse files
committed
Add escaping in Renderer
1 parent 09bdfe1 commit c82a522

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

src/Renderer.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use function func_get_arg;
1515
use function get_class;
1616
use function htmlspecialchars;
17-
use function htmlspecialchars_decode;
1817
use function is_dir;
1918
use function ltrim;
2019
use function ob_end_clean;
@@ -245,27 +244,16 @@ public function render(string $view, array $params = []): string
245244
}
246245

247246
/**
248-
* Encodes special characters into HTML entities.
247+
* Escapes special characters, converts them to corresponding HTML entities.
249248
*
250-
* @param string $content content to be encoded.
251-
* @return string encoded content.
249+
* @param string $content content to be escaped.
250+
* @return string escaped content.
252251
*/
253-
public function encode(string $content): string
252+
public function esc(string $content): string
254253
{
255254
return htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', true);
256255
}
257256

258-
/**
259-
* Decodes special HTML entities back to the corresponding characters.
260-
*
261-
* @param string $content content to be decoded.
262-
* @return string decoded content.
263-
*/
264-
public function decode(string $content): string
265-
{
266-
return htmlspecialchars_decode($content, ENT_QUOTES);
267-
}
268-
269257
/**
270258
* Magic method used to call extension functions.
271259
*

tests/RendererTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,10 @@ public function testAddGlobalOverwriteFromParams(): void
148148

149149
public function testEncodeAndDecode(): void
150150
{
151-
$decoded = '<script>alert(123);</script>';
152-
$encoded = '&lt;script&gt;alert(123);&lt;/script&gt;';
153-
$this->assertSame($encoded, $this->renderer->encode($decoded));
154-
$this->assertSame($decoded, $this->renderer->decode($encoded));
155-
$this->assertSame($decoded, $this->renderer->decode($decoded));
151+
$this->assertSame(
152+
'&lt;script&gt;alert(123);&lt;/script&gt;',
153+
$this->renderer->esc('<script>alert(123);</script>')
154+
);
156155
}
157156

158157
public function testAddExtensionAndCallMagic(): void

0 commit comments

Comments
 (0)