-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement of auto-escaping (#1030)
* Evolution of auto-escaping: no double-escaping when using the 'escape' modifier; add the 'force' mode to the 'escape' modifier; add the 'raw' modifier. * Add 'raw' modifier's documentation --------- Co-authored-by: Simon Wisselink <[email protected]>
- Loading branch information
Showing
12 changed files
with
165 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Improvement of auto-escaping [#1030](https://github.com/smarty-php/smarty/pull/1030) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# raw | ||
|
||
Prevents variable escaping when [auto-escaping](../../api/configuring.md#enabling-auto-escaping) is activated. | ||
|
||
## Basic usage | ||
```smarty | ||
{$myVar|raw} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
namespace Smarty\Compile\Modifier; | ||
|
||
use Smarty\Exception; | ||
|
||
/** | ||
* Smarty raw modifier plugin | ||
* Type: modifier | ||
* Name: raw | ||
* Purpose: when escaping is enabled by default, generates a raw output of a variable | ||
* | ||
* @author Amaury Bouchard | ||
*/ | ||
|
||
class RawModifierCompiler extends Base { | ||
|
||
public function compile($params, \Smarty\Compiler\Template $compiler) { | ||
$compiler->setRawOutput(true); | ||
return ($params[0]); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,68 @@ function ($params, $content) { return $content == null ? null : "<p>".$content. | |
$this->assertEquals("<p>hi</p>", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
/** | ||
* test autoescape + raw modifier | ||
*/ | ||
public function testAutoEscapeRaw() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|raw}'); | ||
$tpl->assign('foo', '<[email protected]>'); | ||
$this->assertEquals("<[email protected]>", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
/** | ||
* test autoescape + escape modifier = no double-escaping | ||
*/ | ||
public function testAutoEscapeNoDoubleEscape() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|escape}'); | ||
$tpl->assign('foo', '<[email protected]>'); | ||
$this->assertEquals("<[email protected]>", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
/** | ||
* test autoescape + escape modifier = force double-escaping | ||
*/ | ||
public function testAutoEscapeForceDoubleEscape() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|escape:\'force\'}'); | ||
$tpl->assign('foo', '<[email protected]>'); | ||
$this->assertEquals("&lt;[email protected]&gt;", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
/** | ||
* test autoescape + escape modifier = special escape | ||
*/ | ||
public function testAutoEscapeSpecialEscape() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|escape:\'url\'}'); | ||
$tpl->assign('foo', 'aa bb'); | ||
$this->assertEquals("aa%20bb", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
/** | ||
* test autoescape + escape modifier = special escape | ||
*/ | ||
public function testAutoEscapeSpecialEscape2() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|escape:\'url\'}'); | ||
$tpl->assign('foo', '<BR>'); | ||
$this->assertEquals("%3CBR%3E", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
/** | ||
* test autoescape + escape modifier = special escape | ||
*/ | ||
public function testAutoEscapeSpecialEscape3() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|escape:\'htmlall\'}'); | ||
$tpl->assign('foo', '<BR>'); | ||
$this->assertEquals("<BR>", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
|
||
/** | ||
* test autoescape + escape modifier = special escape | ||
*/ | ||
public function testAutoEscapeSpecialEscape4() { | ||
$tpl = $this->smarty->createTemplate('eval:{$foo|escape:\'javascript\'}'); | ||
$tpl->assign('foo', '<\''); | ||
$this->assertEquals("<\\'", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
} |