$ show mewebstudio/Purifier
This is a Laravel package that integrates the HTMLPurifier library to sanitize and purify HTML input, primarily for XSS protection. It provides a service provider, a facade, a global helper function (`clean()`), and custom Eloquent casts (`CleanHtml`, `CleanHtmlInput`, `CleanHtmlOutput`) for automatic sanitization on model attributes. The core `Purifier` class wraps HTMLPurifier with Laravel's config and filesystem, supporting dynamic configuration, custom HTML definitions, and a non-string bypass option. The package is well-tested and supports Laravel 5.8 through 13.0, with active maintenance and low technical drift.
The LLM identified 2 files worth highlighting for reuse:
The main class that orchestrates HTMLPurifier. It handles configuration merging, custom HTML definitions (elements, attributes), and provides a `clean()` method that recursively sanitizes arrays and supports a closure for post-config hooks. The custom definition logic (addCustomDefinition, addCustomAttributes, addCustomElements) is a non-trivial pattern for extending HTMLPurifier's whitelist, which could be extracted for standalone use.
An Eloquent cast that automatically cleans HTML on both get and set. It uses the `WithConfig` trait to accept optional configuration, demonstrating a reusable pattern for creating configurable custom casts. The trait itself is minimal but enables dependency injection of purifier settings into casts.