@@ -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 ($ attrsas $ 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 forthis element can be passed as a second argument.
273
+ * A list of allowed attributes forthese 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
- * @paramlist< 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 ($ elementsas $ 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 ($ elementsas $ 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
}
@@ -360,14 +347,14 @@ public function dropElement(string $element): static
360
347
* automatically. This method let you drop elements that were allowed earlier
361
348
* in the configuration.
362
349
*
363
- * @paramlist< string> $elements
350
+ * @param string[] $elements
364
351
*/
365
352
public function dropElements (array $ elements ):static
366
353
{
367
354
$ clone =clone $ this ;
368
355
369
356
foreach ($ elementsas $ 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
- * @paramlist< 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 ($ attributesas $ 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 ($ droppedElementsas $ 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
- * @paramlist< 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 ($ attributesas $ attribute ) {
477
- $ clone = $ clone -> dropAttribute ( $ attribute ,$ droppedElements );
447
+ $ this -> handleDropAttribute ( $ clone ,$ droppedElements, $ attribute );
478
448
}
479
449
480
450
return $ clone ;
@@ -617,4 +587,66 @@ public function getAttributeSanitizers(): array
617
587
{
618
588
return $ this ->attributeSanitizers ;
619
589
}
590
+
591
+ /**
592
+ * @param string[]|string $allowedElements
593
+ */
594
+ public function handleAllowAttribute (self $ clone ,array |string $ allowedElements ,string $ attribute ):void
595
+ {
596
+ $ allowedElements = ('* ' ===$ allowedElements ) ?array_keys ($ clone ->allowedElements ) : (array )$ allowedElements ;
597
+
598
+ // For each configured element ...
599
+ foreach ($ clone ->allowedElements as $ element =>$ attrs ) {
600
+ if (\in_array ($ element ,$ allowedElements ,true )) {
601
+ // ... if the attribute should be allowed, add it
602
+ $ clone ->allowedElements [$ element ][$ attribute ] =true ;
603
+ }else {
604
+ // ... if the attribute should not be allowed, remove it
605
+ unset($ clone ->allowedElements [$ element ][$ attribute ]);
606
+ }
607
+ }
608
+ }
609
+
610
+ /**
611
+ * @param string[]|string $allowedAttributes
612
+ */
613
+ private function handleAllowElement (self $ clone ,string $ element ,array |string $ allowedAttributes ):void
614
+ {
615
+ // Unblock the element is necessary
616
+ unset($ clone ->blockedElements [$ element ]);
617
+
618
+ $ clone ->allowedElements [$ element ] = [];
619
+
620
+ $ attrs = ('* ' ===$ allowedAttributes ) ?array_keys (W3CReference::ATTRIBUTES ) : (array )$ allowedAttributes ;
621
+ foreach ($ attrsas $ allowedAttr ) {
622
+ $ clone ->allowedElements [$ element ][$ allowedAttr ] =true ;
623
+ }
624
+ }
625
+
626
+ private function handleBlockElement (self $ clone ,string $ element ):void
627
+ {
628
+ // Disallow the element is necessary
629
+ unset($ clone ->allowedElements [$ element ]);
630
+
631
+ $ clone ->blockedElements [$ element ] =true ;
632
+ }
633
+
634
+ /**
635
+ * @param string[]|string $droppedElements
636
+ */
637
+ private function handleDropAttribute (self $ clone ,array |string $ droppedElements ,string $ attribute ):void
638
+ {
639
+ $ droppedElements = ('* ' ===$ droppedElements ) ?array_keys ($ clone ->allowedElements ) : (array )$ droppedElements ;
640
+
641
+ foreach ($ droppedElementsas $ element ) {
642
+ if (isset ($ clone ->allowedElements [$ element ][$ attribute ])) {
643
+ unset($ clone ->allowedElements [$ element ][$ attribute ]);
644
+ }
645
+ }
646
+ }
647
+
648
+ private function handleDropElement (self $ clone ,string $ element ):void
649
+ {
650
+ unset($ clone ->allowedElements [$ element ],$ clone ->blockedElements [$ element ]);
651
+ }
620
652
}