@@ -260,16 +260,7 @@ public function forceHttpsUrls(bool $forceHttpsUrls = true): static
260
260
public function allowElement (string $ element , array |string $ allowedAttributes = []): static
261
261
{
262
262
$ 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 );
273
264
274
265
return $ clone ;
275
266
}
@@ -279,19 +270,19 @@ public function allowElement(string $element, array|string $allowedAttributes =
279
270
*
280
271
* Allowed elements are elements the sanitizer should retain from the input.
281
272
*
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.
283
274
* Passing "*" will allow all standard attributes on this element. By default, no
284
275
* attributes are allowed on the element.
285
276
*
286
- * @param list< string> $elements
277
+ * @param string[] $elements
287
278
* @param list<string>|string $allowedAttributes
288
279
*/
289
280
public function allowElements (array $ elements , array |string $ allowedAttributes = []): static
290
281
{
291
282
$ clone = clone $ this ;
292
283
293
284
foreach ($ elements as $ element ) {
294
- $ clone = $ clone -> allowElement ( $ element , $ allowedAttributes );
285
+ $ this -> handleAllowElement ( $ clone , $ element , $ allowedAttributes );
295
286
}
296
287
297
288
return $ clone ;
@@ -306,11 +297,7 @@ public function allowElements(array $elements, array|string $allowedAttributes =
306
297
public function blockElement (string $ element ): static
307
298
{
308
299
$ 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 );
314
301
315
302
return $ clone ;
316
303
}
@@ -326,7 +313,7 @@ public function blockElements(array $elements): static
326
313
$ clone = clone $ this ;
327
314
328
315
foreach ($ elements as $ element ) {
329
- $ clone = $ clone -> blockElement ( $ element );
316
+ $ this -> handleBlockElement ( $ clone , $ element );
330
317
}
331
318
332
319
return $ clone ;
@@ -345,7 +332,7 @@ public function blockElements(array $elements): static
345
332
public function dropElement (string $ element ): static
346
333
{
347
334
$ clone = clone $ this ;
348
- unset( $ clone -> allowedElements [ $ element ] , $ clone -> blockedElements [ $ element] );
335
+ $ this -> handleDropElement ( $ clone , $ element );
349
336
350
337
return $ clone ;
351
338
}
@@ -367,7 +354,7 @@ public function dropElements(array $elements): static
367
354
$ clone = clone $ this ;
368
355
369
356
foreach ($ elements as $ element ) {
370
- $ clone = $ clone -> dropElement ( $ element );
357
+ $ this -> handleDropElement ( $ clone , $ element );
371
358
}
372
359
373
360
return $ clone ;
@@ -386,18 +373,7 @@ public function dropElements(array $elements): static
386
373
public function allowAttribute (string $ attribute , array |string $ allowedElements ): static
387
374
{
388
375
$ 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 );
401
377
402
378
return $ clone ;
403
379
}
@@ -412,15 +388,15 @@ public function allowAttribute(string $attribute, array|string $allowedElements)
412
388
*
413
389
* To configure each attribute for a specific element, please use the allowAttribute method instead.
414
390
*
415
- * @param list< string> $attributes
391
+ * @param string[] $attributes
416
392
* @param list<string>|string $allowedElements
417
393
*/
418
394
public function allowAttributes (array $ attributes , array |string $ allowedElements ): static
419
395
{
420
396
$ clone = clone $ this ;
421
397
422
398
foreach ($ attributes as $ attribute ) {
423
- $ clone = $ clone -> allowAttribute ( $ attribute , $ allowedElements );
399
+ $ this -> handleAllowAttribute ( $ clone , $ allowedElements, $ attribute );
424
400
}
425
401
426
402
return $ clone ;
@@ -443,13 +419,7 @@ public function allowAttributes(array $attributes, array|string $allowedElements
443
419
public function dropAttribute (string $ attribute , array |string $ droppedElements ): static
444
420
{
445
421
$ 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 );
453
423
454
424
return $ clone ;
455
425
}
@@ -466,15 +436,15 @@ public function dropAttribute(string $attribute, array|string $droppedElements):
466
436
* automatically. This method let you drop attributes that were allowed earlier
467
437
* in the configuration.
468
438
*
469
- * @param list< string> $attributes
439
+ * @param string[] $attributes
470
440
* @param list<string>|string $droppedElements
471
441
*/
472
442
public function dropAttributes (array $ attributes , array |string $ droppedElements ): static
473
443
{
474
444
$ clone = clone $ this ;
475
445
476
446
foreach ($ attributes as $ attribute ) {
477
- $ clone = $ clone -> dropAttribute ( $ attribute , $ droppedElements );
447
+ $ this -> handleDropAttribute ( $ clone , $ droppedElements, $ attribute );
478
448
}
479
449
480
450
return $ clone ;
@@ -617,4 +587,80 @@ public function getAttributeSanitizers(): array
617
587
{
618
588
return $ this ->attributeSanitizers ;
619
589
}
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
+ }
620
666
}
0 commit comments