Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions WebLoader/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion WebLoader/Nette/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getDefaultConfig()
'filters' => array(),
'fileFilters' => array(),
'joinFiles' => TRUE,
'nonce' => NULL,
'namingConvention' => '@' . $this->prefix('jsNamingConvention'),
),
'cssDefaults' => array(
Expand All @@ -51,6 +52,7 @@ public function getDefaultConfig()
'filters' => array(),
'fileFilters' => array(),
'joinFiles' => TRUE,
'nonce' => NULL,
'namingConvention' => '@' . $this->prefix('cssNamingConvention'),
),
'js' => array(
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 10 additions & 2 deletions WebLoader/Nette/JavaScriptLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}
17 changes: 17 additions & 0 deletions tests/Nette/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/extensionNonce.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
webloader:
jsDefaults:
nonce: rAnd0m123