diff --git a/WebLoader/Compiler.php b/WebLoader/Compiler.php index 370995e..51df234 100644 --- a/WebLoader/Compiler.php +++ b/WebLoader/Compiler.php @@ -34,6 +34,9 @@ class Compiler /** @var bool */ private $debugging = FALSE; + /** @var string */ + private $nonce = NULL; + public function __construct(IFileCollection $files, IOutputNamingConvention $convention, $outputDir) { $this->collection = $files; @@ -71,6 +74,22 @@ public function enableDebugging($allow = TRUE) $this->debugging = (bool) $allow; } + /** + * @return string + */ + public function getNonce() + { + return $this->nonce; + } + + /** + * @param string $nonce + */ + public function setNonce($nonce) + { + $this->nonce = $nonce; + } + /** * Get temp path * @return string diff --git a/WebLoader/Nette/Extension.php b/WebLoader/Nette/Extension.php index 4e5013e..6825284 100644 --- a/WebLoader/Nette/Extension.php +++ b/WebLoader/Nette/Extension.php @@ -37,6 +37,7 @@ public function getDefaultConfig() 'filters' => array(), 'fileFilters' => array(), 'joinFiles' => TRUE, + 'nonce' => NULL, 'namingConvention' => '@' . $this->prefix('jsNamingConvention'), ), 'cssDefaults' => array( @@ -51,6 +52,7 @@ public function getDefaultConfig() 'filters' => array(), 'fileFilters' => array(), 'joinFiles' => TRUE, + 'nonce' => NULL, 'namingConvention' => '@' . $this->prefix('cssNamingConvention'), ), 'js' => array( @@ -126,7 +128,8 @@ private function addWebLoader(ContainerBuilder $builder, $name, $config) $config['tempDir'], )); - $compiler->addSetup('setJoinFiles', array($config['joinFiles'])); + $compiler->addSetup('setJoinFiles', array($config['joinFiles'])) + ->addSetup('setNonce', array($config['nonce'])); if ($builder->parameters['webloader']['debugger']) { $compiler->addSetup('@' . $this->prefix('tracyPanel') . '::addLoader', array( diff --git a/WebLoader/Nette/JavaScriptLoader.php b/WebLoader/Nette/JavaScriptLoader.php index 1b138b2..84835fe 100644 --- a/WebLoader/Nette/JavaScriptLoader.php +++ b/WebLoader/Nette/JavaScriptLoader.php @@ -20,7 +20,15 @@ class JavaScriptLoader extends WebLoader */ public function getElement($source) { - return Html::el("script")->type("text/javascript")->src($source); + $el = Html::el("script")->type("text/javascript")->src($source); + + $nonce = $this->getCompiler()->getNonce(); + if ($nonce) + { + $el->nonce($nonce); + } + + return $el; } -} \ No newline at end of file +} diff --git a/tests/Nette/ExtensionTest.php b/tests/Nette/ExtensionTest.php index 2260335..dcc9326 100644 --- a/tests/Nette/ExtensionTest.php +++ b/tests/Nette/ExtensionTest.php @@ -77,6 +77,23 @@ public function testJoinFilesOffInOneService() $this->assertFalse($this->container->getService('webloader.cssJoinOffCompiler')->getJoinFiles()); } + public function testNonceSet() + { + $this->prepareContainer(array( + __DIR__ . '/../fixtures/extension.neon', + __DIR__ . '/../fixtures/extensionNonce.neon', + )); + $this->assertEquals('rAnd0m123', $this->container->getService('webloader.jsDefaultCompiler')->getNonce()); + } + + public function testNonceNotSet() + { + $this->prepareContainer(array( + __DIR__ . '/../fixtures/extension.neon', + )); + $this->assertNull($this->container->getService('webloader.jsDefaultCompiler')->getNonce()); + } + public function testExtensionName() { $tempDir = __DIR__ . '/../temp'; diff --git a/tests/fixtures/extensionNonce.neon b/tests/fixtures/extensionNonce.neon new file mode 100644 index 0000000..ceb396f --- /dev/null +++ b/tests/fixtures/extensionNonce.neon @@ -0,0 +1,3 @@ +webloader: + jsDefaults: + nonce: rAnd0m123