This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-expressive-platesrenderer for the canonical source repository | ||
* @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-platesrenderer/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Plates\Extension; | ||
|
||
use League\Plates\Engine; | ||
use League\Plates\Extension\ExtensionInterface; | ||
use Zend\Escaper\Escaper; | ||
use Zend\Escaper\Exception\InvalidArgumentException; | ||
|
||
class EscaperExtension implements ExtensionInterface | ||
{ | ||
/** | ||
* Register functions with the Plates engine. | ||
* | ||
* Registers: | ||
* | ||
* - escapeHtml($string) : string | ||
* - escapeHtmlAttr($string) : string | ||
* - escapeJs($string) : string | ||
* - escapeCss($string) : string | ||
* - escapeUrl($string) : string | ||
* | ||
* @param Engine $engine | ||
* @return void | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function register(Engine $engine) | ||
{ | ||
$escaper = new Escaper(); | ||
|
||
$engine->registerFunction('escapeHtml', [$escaper, 'escapeHtml']); | ||
$engine->registerFunction('escapeHtmlAttr', [$escaper, 'escapeHtmlAttr']); | ||
$engine->registerFunction('escapeJs', [$escaper, 'escapeJs']); | ||
$engine->registerFunction('escapeCss', [$escaper, 'escapeCss']); | ||
$engine->registerFunction('escapeUrl', [$escaper, 'escapeUrl']); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace ZendTest\Expressive\Plates\Extension; | ||
|
||
use League\Plates\Engine; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Zend\Escaper\Escaper; | ||
use Zend\Expressive\Plates\Extension\EscaperExtension; | ||
|
||
class EscaperExtensionTest extends TestCase | ||
{ | ||
public function testRegistersEscaperFunctionsWithEngine() | ||
{ | ||
$extension = new EscaperExtension(); | ||
|
||
$engine = $this->prophesize(Engine::class); | ||
$engine | ||
->registerFunction('escapeHtml', Argument::that(function ($argument) { | ||
return is_array($argument) && $argument[0] instanceof Escaper && $argument[1] === 'escapeHtml'; | ||
}))->shouldBeCalled(); | ||
$engine | ||
->registerFunction('escapeHtmlAttr', Argument::that(function ($argument) { | ||
return is_array($argument) && $argument[0] instanceof Escaper && $argument[1] === 'escapeHtmlAttr'; | ||
}))->shouldBeCalled(); | ||
$engine | ||
->registerFunction('escapeJs', Argument::that(function ($argument) { | ||
return is_array($argument) && $argument[0] instanceof Escaper && $argument[1] === 'escapeJs'; | ||
}))->shouldBeCalled(); | ||
$engine | ||
->registerFunction('escapeCss', Argument::that(function ($argument) { | ||
return is_array($argument) && $argument[0] instanceof Escaper && $argument[1] === 'escapeCss'; | ||
}))->shouldBeCalled(); | ||
$engine | ||
->registerFunction('escapeUrl', Argument::that(function ($argument) { | ||
return is_array($argument) && $argument[0] instanceof Escaper && $argument[1] === 'escapeUrl'; | ||
}))->shouldBeCalled(); | ||
|
||
$extension->register($engine->reveal()); | ||
} | ||
} |