Skip to content

Commit 7ca3b95

Browse files
committed
[HtmlSanitizer] Add functions to handle operations on multiple attributes or elements at the same time
- Wrapped some logic in private functions to stay DRY - Fixed PHPDoc comments - Fixed typo
1 parent 6160dcf commit 7ca3b95

File tree

1 file changed

+90
-44
lines changed

1 file changed

+90
-44
lines changed

src/Symfony/Component/HtmlSanitizer/HtmlSanitizerConfig.php

Lines changed: 90 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,7 @@ public function forceHttpsUrls(bool $forceHttpsUrls = true): static
260260
public function allowElement(string $element, array|string $allowedAttributes = []): static
261261
{
262262
$clone = clone $this;
263-
264-
// Unblock the element is necessary
265-
unset($clone->blockedElements[$element]);
266-
267-
$clone->allowedElements[$element] = [];
268-
269-
$attrs = ('*' === $allowedAttributes) ? array_keys(W3CReference::ATTRIBUTES) : (array) $allowedAttributes;
270-
foreach ($attrs as $allowedAttr) {
271-
$clone->allowedElements[$element][$allowedAttr] = true;
272-
}
263+
$this->handleAllowElement($clone, $element, $allowedAttributes);
273264

274265
return $clone;
275266
}
@@ -279,19 +270,19 @@ public function allowElement(string $element, array|string $allowedAttributes =
279270
*
280271
* Allowed elements are elements the sanitizer should retain from the input.
281272
*
282-
* A list of allowed attributes for this element can be passed as a second argument.
273+
* A list of allowed attributes for these elements can be passed as a second argument.
283274
* Passing "*" will allow all standard attributes on this element. By default, no
284275
* attributes are allowed on the element.
285276
*
286-
* @param list<string> $elements
277+
* @param string[] $elements
287278
* @param list<string>|string $allowedAttributes
288279
*/
289280
public function allowElements(array $elements, array|string $allowedAttributes = []): static
290281
{
291282
$clone = clone $this;
292283

293284
foreach ($elements as $element) {
294-
$clone = $clone->allowElement($element, $allowedAttributes);
285+
$this->handleAllowElement($clone, $element, $allowedAttributes);
295286
}
296287

297288
return $clone;
@@ -306,11 +297,7 @@ public function allowElements(array $elements, array|string $allowedAttributes =
306297
public function blockElement(string $element): static
307298
{
308299
$clone = clone $this;
309-
310-
// Disallow the element is necessary
311-
unset($clone->allowedElements[$element]);
312-
313-
$clone->blockedElements[$element] = true;
300+
$this->handleBlockElement($clone, $element);
314301

315302
return $clone;
316303
}
@@ -326,7 +313,7 @@ public function blockElements(array $elements): static
326313
$clone = clone $this;
327314

328315
foreach ($elements as $element) {
329-
$clone = $clone->blockElement($element);
316+
$this->handleBlockElement($clone, $element);
330317
}
331318

332319
return $clone;
@@ -345,7 +332,7 @@ public function blockElements(array $elements): static
345332
public function dropElement(string $element): static
346333
{
347334
$clone = clone $this;
348-
unset($clone->allowedElements[$element], $clone->blockedElements[$element]);
335+
$this->handleDropElement($clone, $element);
349336

350337
return $clone;
351338
}
@@ -367,7 +354,7 @@ public function dropElements(array $elements): static
367354
$clone = clone $this;
368355

369356
foreach ($elements as $element) {
370-
$clone = $clone->dropElement($element);
357+
$this->handleDropElement($clone, $element);
371358
}
372359

373360
return $clone;
@@ -386,18 +373,7 @@ public function dropElements(array $elements): static
386373
public function allowAttribute(string $attribute, array|string $allowedElements): static
387374
{
388375
$clone = clone $this;
389-
$allowedElements = ('*' === $allowedElements) ? array_keys($clone->allowedElements) : (array) $allowedElements;
390-
391-
// For each configured element ...
392-
foreach ($clone->allowedElements as $element => $attrs) {
393-
if (\in_array($element, $allowedElements, true)) {
394-
// ... if the attribute should be allowed, add it
395-
$clone->allowedElements[$element][$attribute] = true;
396-
} else {
397-
// ... if the attribute should not be allowed, remove it
398-
unset($clone->allowedElements[$element][$attribute]);
399-
}
400-
}
376+
$this->handleAllowAttribute($clone, $allowedElements, $attribute);
401377

402378
return $clone;
403379
}
@@ -412,15 +388,15 @@ public function allowAttribute(string $attribute, array|string $allowedElements)
412388
*
413389
* To configure each attribute for a specific element, please use the allowAttribute method instead.
414390
*
415-
* @param list<string> $attributes
391+
* @param string[] $attributes
416392
* @param list<string>|string $allowedElements
417393
*/
418394
public function allowAttributes(array $attributes, array|string $allowedElements): static
419395
{
420396
$clone = clone $this;
421397

422398
foreach ($attributes as $attribute) {
423-
$clone = $clone->allowAttribute($attribute, $allowedElements);
399+
$this->handleAllowAttribute($clone, $allowedElements, $attribute);
424400
}
425401

426402
return $clone;
@@ -443,13 +419,7 @@ public function allowAttributes(array $attributes, array|string $allowedElements
443419
public function dropAttribute(string $attribute, array|string $droppedElements): static
444420
{
445421
$clone = clone $this;
446-
$droppedElements = ('*' === $droppedElements) ? array_keys($clone->allowedElements) : (array) $droppedElements;
447-
448-
foreach ($droppedElements as $element) {
449-
if (isset($clone->allowedElements[$element][$attribute])) {
450-
unset($clone->allowedElements[$element][$attribute]);
451-
}
452-
}
422+
$this->handleDropAttribute($clone, $droppedElements, $attribute);
453423

454424
return $clone;
455425
}
@@ -466,15 +436,15 @@ public function dropAttribute(string $attribute, array|string $droppedElements):
466436
* automatically. This method let you drop attributes that were allowed earlier
467437
* in the configuration.
468438
*
469-
* @param list<string> $attributes
439+
* @param string[] $attributes
470440
* @param list<string>|string $droppedElements
471441
*/
472442
public function dropAttributes(array $attributes, array|string $droppedElements): static
473443
{
474444
$clone = clone $this;
475445

476446
foreach ($attributes as $attribute) {
477-
$clone = $clone->dropAttribute($attribute, $droppedElements);
447+
$this->handleDropAttribute($clone, $droppedElements, $attribute);
478448
}
479449

480450
return $clone;
@@ -617,4 +587,80 @@ public function getAttributeSanitizers(): array
617587
{
618588
return $this->attributeSanitizers;
619589
}
590+
591+
/**
592+
* @param HtmlSanitizerConfig $clone
593+
* @param string[]|string $allowedElements
594+
* @param string $attribute
595+
*/
596+
public function handleAllowAttribute(HtmlSanitizerConfig $clone, array|string $allowedElements, string $attribute): void
597+
{
598+
$allowedElements = ('*' === $allowedElements) ? array_keys($clone->allowedElements) : (array)$allowedElements;
599+
600+
// For each configured element ...
601+
foreach ($clone->allowedElements as $element => $attrs) {
602+
if (\in_array($element, $allowedElements, true)) {
603+
// ... if the attribute should be allowed, add it
604+
$clone->allowedElements[$element][$attribute] = true;
605+
} else {
606+
// ... if the attribute should not be allowed, remove it
607+
unset($clone->allowedElements[$element][$attribute]);
608+
}
609+
}
610+
}
611+
612+
/**
613+
* @param HtmlSanitizerConfig $clone
614+
* @param string $element
615+
* @param string[]|string $allowedAttributes
616+
*/
617+
private function handleAllowElement(HtmlSanitizerConfig $clone, string $element, array|string $allowedAttributes): void
618+
{
619+
// Unblock the element is necessary
620+
unset($clone->blockedElements[$element]);
621+
622+
$clone->allowedElements[$element] = [];
623+
624+
$attrs = ('*' === $allowedAttributes) ? array_keys(W3CReference::ATTRIBUTES) : (array) $allowedAttributes;
625+
foreach ($attrs as $allowedAttr) {
626+
$clone->allowedElements[$element][$allowedAttr] = true;
627+
}
628+
}
629+
630+
/**
631+
* @param HtmlSanitizerConfig $clone
632+
* @param string $element
633+
*/
634+
private function handleBlockElement(HtmlSanitizerConfig $clone, string $element): void
635+
{
636+
// Disallow the element is necessary
637+
unset($clone->allowedElements[$element]);
638+
639+
$clone->blockedElements[$element] = true;
640+
}
641+
642+
/**
643+
* @param HtmlSanitizerConfig $clone
644+
* @param string[]|string $droppedElements
645+
* @param string $attribute
646+
*/
647+
private function handleDropAttribute(HtmlSanitizerConfig $clone, array|string $droppedElements, string $attribute): void
648+
{
649+
$droppedElements = ('*' === $droppedElements) ? array_keys($clone->allowedElements) : (array) $droppedElements;
650+
651+
foreach ($droppedElements as $element) {
652+
if (isset($clone->allowedElements[$element][$attribute])) {
653+
unset($clone->allowedElements[$element][$attribute]);
654+
}
655+
}
656+
}
657+
658+
/**
659+
* @param HtmlSanitizerConfig $clone
660+
* @param string $element
661+
*/
662+
private function handleDropElement(HtmlSanitizerConfig $clone, string $element): void
663+
{
664+
unset($clone->allowedElements[$element], $clone->blockedElements[$element]);
665+
}
620666
}

0 commit comments

Comments
 (0)