generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from tonysm/tm/sanitization
New Sanitization Example
- Loading branch information
Showing
9 changed files
with
100 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Html; | ||
|
||
use Illuminate\Support\HtmlString; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizer; | ||
|
||
class Sanitizer | ||
{ | ||
public function __construct(private HtmlSanitizer $sanitizer) | ||
{ | ||
// | ||
} | ||
|
||
public function sanitize(string $html, string $element = 'body'): HtmlString | ||
{ | ||
return new HtmlString($this->sanitizer->sanitizeFor($element, $html)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Html; | ||
|
||
use Symfony\Component\HtmlSanitizer\HtmlSanitizer; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; | ||
|
||
class SanitizerFactory | ||
{ | ||
private static $cache = []; | ||
|
||
public static function make($config = null): Sanitizer | ||
{ | ||
return new Sanitizer(new HtmlSanitizer(static::configFor($config))); | ||
} | ||
|
||
private static function configFor($config = null): HtmlSanitizerConfig | ||
{ | ||
return static::$cache[$config ?? 'default'] ??= match ($config) { | ||
'minimal' => static::minimalConfig(), | ||
default => static::defaultConfig(), | ||
}; | ||
} | ||
|
||
private static function defaultConfig(): HtmlSanitizerConfig | ||
{ | ||
return (new HtmlSanitizerConfig()) | ||
->allowSafeElements() | ||
->allowAttribute('class', '*'); | ||
} | ||
|
||
private static function minimalConfig(): HtmlSanitizerConfig | ||
{ | ||
return (new HtmlSanitizerConfig()) | ||
->allowElement('br') | ||
->allowElement('div') | ||
->allowElement('p') | ||
->allowElement('span') | ||
->allowElement('img', ['class', 'src', 'alt']) | ||
->allowElement('a', ['href']) | ||
->allowElement('strong') | ||
->allowElement('b') | ||
->allowElement('em') | ||
->allowElement('i') | ||
->allowAttribute('class', '*'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use Workbench\App\Html\SanitizerFactory; | ||
|
||
if (! function_exists('clean')) { | ||
function clean(string $html, string $element = 'body', $config = null) | ||
{ | ||
return SanitizerFactory::make($config)->sanitize($html, $element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters