blob: 56479d0272f2624e4028533f27f3f6e01d5602e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
namespace ipl\Web;
use ipl\Html\Attribute;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\ValidHtml;
use Throwable;
class Style extends LessRuleset implements ValidHtml
{
/** @var ?string */
protected $module;
/** @var ?string */
protected $nonce;
/**
* Get the used CSP nonce
*
* @return ?string
*/
public function getNonce(): ?string
{
return $this->nonce;
}
/**
* Set the CSP nonce to use
*
* @param ?string $nonce
*
* @return $this
*/
public function setNonce(?string $nonce): self
{
$this->nonce = $nonce;
return $this;
}
/**
* Get the Icinga module name the ruleset is scoped to
*
* @return ?string
*/
public function getModule(): ?string
{
return $this->module;
}
/**
* Set the Icinga module name to use as scope for the ruleset
*
* @param ?string $name
*
* @return $this
*/
public function setModule(?string $name): self
{
$this->module = $name;
return $this;
}
/**
* Add CSS properties for the given element
*
* The created ruleset will be applied by an `#ID` selector. If the given
* element does not have an ID set yet, one is automatically set.
*
* @param BaseHtmlElement $element Element to apply the properties to
* @param array<string, string> $properties CSS properties
*
* @return $this
*/
public function addFor(BaseHtmlElement $element, array $properties): self
{
/** @var ?string $id */
$id = $element->getAttribute('id')->getValue();
if ($id === null) {
$id = uniqid('csp-style', false);
$element->setAttribute('id', $id);
}
return $this->add('#' . $id, $properties);
}
public function render(): string
{
if ($this->module !== null) {
$ruleset = (new static())
->setSelector(".icinga-module.module-$this->module")
->addRuleset($this);
} else {
$ruleset = $this;
}
return (new HtmlElement(
'style',
(new Attributes())->addAttribute(new Attribute('nonce', $this->getNonce())),
HtmlString::create($ruleset->renderCss())
))->render();
}
/**
* Render to HTML
*
* @return string
*/
public function __toString(): string
{
try {
return $this->render();
} catch (Throwable $e) {
return sprintf('<!-- Failed to render style: %s -->', $e->getMessage());
}
}
}
|