-
Notifications
You must be signed in to change notification settings - Fork 23
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
1 parent
95b50e9
commit 729ade3
Showing
3 changed files
with
118 additions
and
57 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,56 @@ | ||
<?php | ||
|
||
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Icingadb\Common; | ||
|
||
use Icinga\Application\Icinga; | ||
use Icinga\Util\Csp; | ||
use ipl\Html\BaseHtmlElement; | ||
use ipl\Web\Style; | ||
|
||
trait InlineStyleWithCsp | ||
{ | ||
/** @var ?Style Used classes are prefixed with `style` */ | ||
private $style; | ||
|
||
protected function getStyle(): Style | ||
{ | ||
if ($this->style === null) { | ||
$parentSelector = $this->getCssClasses($this); | ||
|
||
Csp::createNonces(); // todo: must work without | ||
$this->style = (new Style()) | ||
->setSelector($parentSelector) | ||
->setModule(Icinga::app()->getRequest()->getModuleName()) | ||
->setNonce(Csp::getStyleNonce()); | ||
} | ||
|
||
return $this->style; | ||
} | ||
|
||
|
||
/** | ||
* Get css classes of the given element (with `.` prefix) | ||
* | ||
* @param BaseHtmlElement $element | ||
* | ||
* @return string | ||
*/ | ||
protected function getCssClasses(BaseHtmlElement $element): string | ||
{ | ||
$classes = array_map( | ||
function($value) {return preg_replace('/^|\s/', '.', $value);}, | ||
(array) $element->getAttributes()->get('class')->getValue() | ||
); | ||
|
||
return implode('', $classes); | ||
} | ||
|
||
protected function addStyle(BaseHtmlElement $element, array $properties): void | ||
{ | ||
$randomClass = uniqid('style-', false); | ||
$this->getStyle()->add($this->getCssClasses($element) . '.' . $randomClass, $properties); | ||
$element->addAttributes(['class' => $randomClass]); | ||
} | ||
} |
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