Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteebd050

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 parent6160dcf commiteebd050

File tree

1 file changed

+77
-45
lines changed

1 file changed

+77
-45
lines changed

‎src/Symfony/Component/HtmlSanitizer/HtmlSanitizerConfig.php

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,7 @@ public function forceHttpsUrls(bool $forceHttpsUrls = true): static
260260
publicfunctionallowElement(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 ($attrsas$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 forthis element can be passed as a second argument.
273+
* A list of allowed attributes forthese 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-
* @paramlist<string> $elements
277+
* @param string[] $elements
287278
* @param list<string>|string $allowedAttributes
288279
*/
289280
publicfunctionallowElements(array$elements,array|string$allowedAttributes = []):static
290281
{
291282
$clone =clone$this;
292283

293284
foreach ($elementsas$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
publicfunctionblockElement(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 ($elementsas$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
publicfunctiondropElement(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
}
@@ -360,14 +347,14 @@ public function dropElement(string $element): static
360347
* automatically. This method let you drop elements that were allowed earlier
361348
* in the configuration.
362349
*
363-
* @paramlist<string> $elements
350+
* @param string[] $elements
364351
*/
365352
publicfunctiondropElements(array$elements):static
366353
{
367354
$clone =clone$this;
368355

369356
foreach ($elementsas$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
publicfunctionallowAttribute(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->allowedElementsas$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-
* @paramlist<string> $attributes
391+
* @param string[] $attributes
416392
* @param list<string>|string $allowedElements
417393
*/
418394
publicfunctionallowAttributes(array$attributes,array|string$allowedElements):static
419395
{
420396
$clone =clone$this;
421397

422398
foreach ($attributesas$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
publicfunctiondropAttribute(string$attribute,array|string$droppedElements):static
444420
{
445421
$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);
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-
* @paramlist<string> $attributes
439+
* @param string[] $attributes
470440
* @param list<string>|string $droppedElements
471441
*/
472442
publicfunctiondropAttributes(array$attributes,array|string$droppedElements):static
473443
{
474444
$clone =clone$this;
475445

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

480450
return$clone;
@@ -617,4 +587,66 @@ public function getAttributeSanitizers(): array
617587
{
618588
return$this->attributeSanitizers;
619589
}
590+
591+
/**
592+
* @param string[]|string $allowedElements
593+
*/
594+
publicfunctionhandleAllowAttribute(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->allowedElementsas$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+
privatefunctionhandleAllowElement(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+
privatefunctionhandleBlockElement(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+
privatefunctionhandleDropAttribute(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+
privatefunctionhandleDropElement(self$clone,string$element):void
649+
{
650+
unset($clone->allowedElements[$element],$clone->blockedElements[$element]);
651+
}
620652
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp