From d6b2c89f6f1d942378feaba8229bbb9c2a3b0e3f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 13 Jan 2015 12:29:49 +0100 Subject: [PATCH 1/2] fixed CS --- lib/Twig/Environment.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 3a00db3b0..9bb828c86 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -223,12 +223,12 @@ class Twig_Environment return $this->cache; } - /** - * Sets the cache directory or false if cache is disabled. - * - * @param string|false $cache The absolute path to the compiled templates, - * or false to disable cache - */ + /** + * Sets the cache directory or false if cache is disabled. + * + * @param string|false $cache The absolute path to the compiled templates, + * or false to disable cache + */ public function setCache($cache) { $this->cache = $cache ? $cache : false; From 4481bf5f38640bb143e44ab86159f2ed100389d4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 13 Jan 2015 11:38:20 +0100 Subject: [PATCH 2/2] added an escaping strategy based on the template filename extension --- CHANGELOG | 5 +- composer.json | 2 +- doc/api.rst | 10 ++-- ext/twig/php_twig.h | 2 +- lib/Twig/Environment.php | 3 +- lib/Twig/Extension/Escaper.php | 4 ++ lib/Twig/FileExtensionEscapingStrategy.php | 49 +++++++++++++++++++ test/Twig/Tests/FileCachingTest.php | 9 ++++ .../FileExtensionEscapingStrategyTest.php | 49 +++++++++++++++++++ 9 files changed, 124 insertions(+), 9 deletions(-) create mode 100644 lib/Twig/FileExtensionEscapingStrategy.php create mode 100644 test/Twig/Tests/FileExtensionEscapingStrategyTest.php diff --git a/CHANGELOG b/CHANGELOG index 877240b42..5cbfa0582 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ -* 1.16.4 (2015-XX-XX) +* 1.17.0 (2015-XX-XX) - * n/a + * added a 'filename' autoescaping strategy, which dynamically chooses the + autoescaping strategy for a template based on template file extension. * 1.16.3 (2014-12-25) diff --git a/composer.json b/composer.json index 27b7b99a7..f35873b61 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.16-dev" + "dev-master": "1.17-dev" } } } diff --git a/doc/api.rst b/doc/api.rst index 775612714..f6619473c 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -96,11 +96,13 @@ The following options are available: * ``autoescape``: If set to ``true``, auto-escaping will be enabled by default for all templates (default to ``true``). As of Twig 1.8, you can set the - escaping strategy to use (``html``, ``js``, ``false`` to disable). - As of Twig 1.9, you can set the escaping strategy to use (``css``, ``url``, - ``html_attr``, or a PHP callback that takes the template "filename" and must + escaping strategy to use (``html``, ``js``, ``false`` to disable). As of Twig + 1.9, you can set the escaping strategy to use (``css``, ``url``, + ``html_attr``, or a PHP callback that takes the template "filename" and must return the escaping strategy to use -- the callback cannot be a function name - to avoid collision with built-in escaping strategies). + to avoid collision with built-in escaping strategies). As of Twig 1.17, the + ``filename`` escaping strategy determines the escaping strategy to use for a + template based on the template filename extension. * ``optimizations``: A flag that indicates which optimizations to apply (default to ``-1`` -- all optimizations are enabled; set it to ``0`` to diff --git a/ext/twig/php_twig.h b/ext/twig/php_twig.h index b84c07edd..76ab3542a 100644 --- a/ext/twig/php_twig.h +++ b/ext/twig/php_twig.h @@ -15,7 +15,7 @@ #ifndef PHP_TWIG_H #define PHP_TWIG_H -#define PHP_TWIG_VERSION "1.16.4-DEV" +#define PHP_TWIG_VERSION "1.17.0-DEV" #include "php.h" diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 9bb828c86..9bc5443f8 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -16,7 +16,7 @@ */ class Twig_Environment { - const VERSION = '1.16.4-DEV'; + const VERSION = '1.17.0-DEV'; protected $charset; protected $loader; @@ -72,6 +72,7 @@ class Twig_Environment * * false: disable auto-escaping * * true: equivalent to html * * html, js: set the autoescaping to one of the supported strategies + * * filename: set the autoescaping strategy based on the template filename extension * * PHP callback: a PHP callback that returns an escaping strategy based on the template "filename" * * * optimizations: A flag that indicates which optimizations to apply diff --git a/lib/Twig/Extension/Escaper.php b/lib/Twig/Extension/Escaper.php index d3e5ad0ec..0edf563ab 100644 --- a/lib/Twig/Extension/Escaper.php +++ b/lib/Twig/Extension/Escaper.php @@ -64,6 +64,10 @@ class Twig_Extension_Escaper extends Twig_Extension $defaultStrategy = 'html'; } + if ('filename' === $defaultStrategy) { + $defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess'); + } + $this->defaultStrategy = $defaultStrategy; } diff --git a/lib/Twig/FileExtensionEscapingStrategy.php b/lib/Twig/FileExtensionEscapingStrategy.php new file mode 100644 index 000000000..b1ace7dcf --- /dev/null +++ b/lib/Twig/FileExtensionEscapingStrategy.php @@ -0,0 +1,49 @@ + + */ +class Twig_FileExtensionEscapingStrategy +{ + /** + * Guesses the best autoescaping strategy based on the file name. + * + * @param string $filename The template file name + * + * @return string The escaping strategy name to use + */ + public static function guess($filename) + { + if (!preg_match('{\.(js|css|txt)(?:\.[^/\\\\]+)?$}', $filename, $match)) { + return 'html'; + } + + switch ($match[1]) { + case 'js': + return 'js'; + + case 'css': + return 'css'; + + case 'txt': + return false; + } + } +} diff --git a/test/Twig/Tests/FileCachingTest.php b/test/Twig/Tests/FileCachingTest.php index 36cdd337a..3eaee593b 100644 --- a/test/Twig/Tests/FileCachingTest.php +++ b/test/Twig/Tests/FileCachingTest.php @@ -1,5 +1,14 @@ assertEquals($strategy, Twig_FileExtensionEscapingStrategy::guess($filename)); + } + + public function getGuessData() + { + return array( + // default + array('html', 'foo.html'), + array('html', 'foo.html.twig'), + array('html', 'foo'), + array('html', 'foo.bar.twig'), + array('html', 'foo.txt/foo'), + array('html', 'foo.txt/foo.js/'), + + // css + array('css', 'foo.css'), + array('css', 'foo.css.twig'), + array('css', 'foo.twig.css'), + + // js + array('js', 'foo.js'), + array('js', 'foo.js.twig'), + array('js', 'foo.txt/foo.js'), + array('js', 'foo.txt.twig/foo.js'), + + // txt + array(false, 'foo.txt'), + array(false, 'foo.txt.twig'), + ); + } +}