Modify `Styleable` to avoid using static inline styles

To prevent CSP violation the following changes has been made in `Styleable::class`:
1) `Styleable::getStyle()` method is modified to return `ipl\Web\Style::class`
2) `Styleable::additionalStyle` property type is changed to array
3) `Styleable::setAdditionalStyle()` parameter type is changed to array
This commit is contained in:
raviks789 2023-08-17 12:28:42 +02:00
parent 4e8107c231
commit 97a14d7b18
3 changed files with 20 additions and 13 deletions

View File

@ -112,7 +112,7 @@ class BarGraph extends Styleable implements Drawable
$rect->setAttribute('data-icinga-graph-index', $index);
}
$rect->setAttribute('data-icinga-graph-type', 'bar');
$rect->setAdditionalStyle('clip-path: url(#clip);');
$rect->setAdditionalStyle(['clip-path' => 'url(#clip)']);
return $rect;
}

View File

@ -165,8 +165,8 @@ class LineGraph extends Styleable implements Drawable
$path->setFill($this->fill);
}
$path->setAdditionalStyle('clip-path: url(#clip);');
$path->setId($this->id);
$path->setAdditionalStyle(['clip-path' => 'url(#clip)']);
$path->setId($this->id ?? uniqid('line-graph-'));
$group = $path->toSvg($ctx);
foreach ($this->dataset as $x => $point) {
@ -180,7 +180,7 @@ class LineGraph extends Styleable implements Drawable
if (isset($this->tooltips[$x])) {
$invisible = new Circle($point[0], $point[1], 20);
$invisible->setFill($this->strokeColor);
$invisible->setAdditionalStyle('opacity: 0.0;');
$invisible->setAdditionalStyle(['opacity' => '0.0']);
$data = array(
'label' => isset($this->graphs[$this->order]['label']) ?
strtolower($this->graphs[$this->order]['label']) : '',

View File

@ -5,6 +5,8 @@
namespace Icinga\Chart\Primitive;
use DOMElement;
use Icinga\Util\Csp;
use ipl\Web\Style;
/**
* Base class for stylable drawables
@ -36,14 +38,14 @@ class Styleable
/**
* Additional styles to be appended to the style attribute
*
* @var string
* @var array<string, string>
*/
public $additionalStyle = '';
public $additionalStyle = [];
/**
* The id of this element
*
* @var string
* @var ?string
*/
public $id = null;
@ -83,7 +85,7 @@ class Styleable
/**
* Set additional styles for this drawable
*
* @param string $styles The styles to set additionally
* @param array<string, string> $styles The styles to set additionally
*
* @return $this Fluid interface
*/
@ -121,15 +123,20 @@ class Styleable
}
/**
* Return the content of the style attribute as a string
* Return the ruleset used for styling the DOMNode
*
* @return string A string containing styles
* @return Style A ruleset containing styles
*/
public function getStyle()
{
$base = sprintf("fill: %s; stroke: %s;stroke-width: %s;", $this->fill, $this->strokeColor, $this->strokeWidth);
$base .= ';' . $this->additionalStyle . ';';
return $base;
$styles = $this->additionalStyle;
$styles['fill'] = $this->fill;
$styles['stroke'] = $this->strokeColor;
$styles['stroke-width'] = (string) $this->strokeWidth;
return (new Style())
->setNonce(Csp::getStyleNonce())
->add("#$this->id", $styles);
}
/**