[12.x] Add Str::sanitize() for HTML sanitization#59194
Open
SanderMuller wants to merge 3 commits intolaravel:12.xfrom
Open
[12.x] Add Str::sanitize() for HTML sanitization#59194SanderMuller wants to merge 3 commits intolaravel:12.xfrom
SanderMuller wants to merge 3 commits intolaravel:12.xfrom
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
Str::sanitize()andStringable::sanitize()methods that wrap Symfony'shtml-sanitizercomponent, providing a clean API for HTML sanitization, following the same pattern asStr::markdown().Usage
Motivation
Laravel has
e()for escaping andStr::markdown()for rendering Markdown, but no built-in way to sanitize HTML, allowing safe elements through while stripping dangerous ones. This is a common need when accepting rich text input (WYSIWYG editors, user-submitted content, etc.). The current options arestrip_tags()(too aggressive, removes everything) or pulling in a package.This follows the same approach as
Str::markdown(): a static method onStrthat wraps a well-maintained library (symfony/html-sanitizer, same ecosystem as the 10+ Symfony packages Laravel already depends on) with a simple, expressive API.