Skip to content

Commit 7042164

Browse files
committed
chore: Refactor a bit: use better namings
1 parent 1ac986f commit 7042164

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/CleanerConfig.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CleanerConfig
1010
{
1111
protected ?array $allowedElements = null;
1212

13-
protected array $blockedElements = ['script', 'frame', 'iframe', 'object', 'embed'];
13+
protected array $deniedElements = ['script', 'frame', 'iframe', 'object', 'embed'];
1414

1515
protected array $mediaElements = ['img', 'audio', 'video', 'iframe'];
1616

@@ -26,7 +26,7 @@ class CleanerConfig
2626

2727
protected string $inlineListenersPattern = '/\bon\w+\s*=\s*([\'"])(.*?)\1|javascript:[^"\' >]*/is';
2828

29-
protected string $invalidHtmlInlineListenersPattern = '/\bon\w+\s*=\s*([\'"])?([^\'"\s>]+)\1?(?=\s|>)/i';
29+
protected string $malformedListenersPattern = '/\bon\w+\s*=\s*([\'"])?([^\'"\s>]+)\1?(?=\s|>)/i';
3030

3131
public static function make(): CleanerConfig
3232
{
@@ -60,6 +60,26 @@ public function allowElement(string $element): CleanerConfig
6060
return $this;
6161
}
6262

63+
/**
64+
* Configures the given element as not allowed.
65+
*
66+
* Denied elements are elements the cleaner should escape from the input.
67+
*/
68+
public function denyElement(string $element): CleanerConfig
69+
{
70+
$this->deniedElements[] = $element;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* Alias for ::denyElement()
77+
*/
78+
public function blockElement(string $element): CleanerConfig
79+
{
80+
return $this->denyElement($element);
81+
}
82+
6383
/**
6484
* Configures the given element as media.
6585
*
@@ -87,18 +107,6 @@ public function removeMediaElement(string $element): CleanerConfig
87107
return $this;
88108
}
89109

90-
/**
91-
* Configures the given element as blocked.
92-
*
93-
* Blocked elements are elements the cleaner should escape from the input.
94-
*/
95-
public function blockElement(string $element): CleanerConfig
96-
{
97-
$this->blockedElements[] = $element;
98-
99-
return $this;
100-
}
101-
102110
/**
103111
* Allows only a given list of hosts to be used in media source attributes (img, audio, video, iframe...).
104112
*
@@ -116,7 +124,7 @@ public function allowMediaHosts(?array $allowMediaHosts): CleanerConfig
116124

117125
public function elementsPattern(): string
118126
{
119-
$pattern = collect($this->blockedElements)
127+
$pattern = collect($this->deniedElements)
120128
->reject(fn(string $element) => $this->allowedElements && in_array($element, $this->allowedElements))
121129
->map(fn(string $element) => "<{$element}.*{$element}>")
122130
->implode('|');
@@ -138,7 +146,7 @@ public function mediaElementsPattern(): string
138146
*/
139147
public function inlineListenersPatterns(): array
140148
{
141-
return [$this->inlineListenersPattern, $this->invalidHtmlInlineListenersPattern];
149+
return [$this->inlineListenersPattern, $this->malformedListenersPattern];
142150
}
143151

144152
public function shouldEscapeInlineListeners(): bool
@@ -158,13 +166,18 @@ public function setAllowedElements(?array $allowedElements): CleanerConfig
158166
return $this;
159167
}
160168

161-
public function setBlockedElements(array $blockedElements): CleanerConfig
169+
public function setDeniedElements(array $deniedElements): CleanerConfig
162170
{
163-
$this->blockedElements = $blockedElements;
171+
$this->deniedElements = $deniedElements;
164172

165173
return $this;
166174
}
167175

176+
public function setBlockedElements(array $blockedElements): CleanerConfig
177+
{
178+
return $this->setDeniedElements($blockedElements);
179+
}
180+
168181
public function setMediaElements(array $mediaElements): CleanerConfig
169182
{
170183
$this->mediaElements = $mediaElements;

0 commit comments

Comments
 (0)