### Install HTML Purifier with Composer Source: https://github.com/ezyang/htmlpurifier/blob/master/README.md This snippet shows how to install the HTML Purifier library using Composer, a dependency manager for PHP. It assumes you have Composer installed and configured. ```Shell composer require ezyang/htmlpurifier ``` -------------------------------- ### PHP Configuration Example Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/proposal-plists.txt Demonstrates adding an AutoFormat plugin to the configuration. This involves using a specific method to enable filters or formatters, potentially with magic backward compatibility. ```PHP add('AutoFormat', 'AutoParagraph'); ``` -------------------------------- ### Setup HTML Purifier Configuration Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/enduser-customize.html Initializes HTML Purifier with custom definition ID and revision. It retrieves a raw HTML definition object, which might be null if cached. ```PHP $config = HTMLPurifier_Config::createDefault(); $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial'); $config->set('HTML.DefinitionRev', 1); if ($def = $config->maybeGetRawHTMLDefinition()) { // our code will go here } ``` -------------------------------- ### Define Configuration Directive Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/dev-includes.txt Example of defining a configuration directive with its type, default value, aliases, and description. This format is inspired by PHPT and aims to be compact and human-readable. ```PHP Core.HiddenElements TYPE: lookup DEFAULT: array('script', 'style') // auto-converted during processing --ALIASES-- Core.InvisibleElements, Core.StupidElements --DESCRIPTION--

Blah blah

``` -------------------------------- ### HTML Purifier: Regex Content Model Example Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/enduser-customize.html Illustrates a complex content model using DTD-style regular expressions, specifying the order and quantity of child elements. ```php $config->set('HTML', 'AllowedChildren', 'a, b?, (c | d), e+, f*'); ``` -------------------------------- ### PHP Configuration Structure Example Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/proposal-plists.txt Illustrates two ways to structure configuration data in PHP. The first uses nested arrays for namespaces, while the second uses dot-separated strings for a flatter structure. The latter is discussed for its memory implications and ease of retrieval. ```PHP array( 'Namespace' => array( 'Directive' => 'val1', 'Directive2' => 'val2', ) ) ``` ```PHP array( 'Namespace.Directive' => 'val1', 'Namespace.Directive2' => 'val2', ) ``` -------------------------------- ### HTML Purifier AutoFormat.RemoveEmpty.RemoveNbsp Example Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/dev-config-naming.txt Demonstrates how boolean directives in HTML Purifier's AutoFormat namespace can be nested. Specifically, it shows how RemoveEmpty.RemoveNbsp can trigger RemoveEmpty.RemoveNbsp.Exceptions, highlighting the dependency between these configuration options. ```text RemoveEmpty.RemoveNbsp triggers RemoveEmpty.RemoveNbsp.Exceptions ``` -------------------------------- ### HTMLPurifier HTML.BlockWrapper Example Source: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/ConfigSchema/schema/HTML.BlockWrapper.txt Demonstrates how the HTML.BlockWrapper setting affects the output of HTMLPurifier. Inline elements within a block context, such as a blockquote, are wrapped with the specified block-level element. ```html
Foo
``` ```html

Foo

``` -------------------------------- ### Example Custom URI Filter - TransformImageScheme Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/enduser-uri-filter.html Provides a concrete example of a custom URI filter that transforms URIs with an 'image' scheme to a standard HTTP path. It demonstrates modifying the URI object within the filter method. ```PHP class HTMLPurifier_URIFilter_TransformImageScheme extends HTMLPurifier_URIFilter { public $name = 'TransformImageScheme'; public function filter(&$uri, $config, $context) { if ($uri->scheme !== 'image') return true; $img_name = $uri->path; // Overwrite the previous URI object $uri = new HTMLPurifier_URI('http', null, null, null, '/img/' . $img_name . '.png', null, null); return true; } } ``` -------------------------------- ### Configure SafeIframeRegexp for YouTube and Vimeo Source: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/ConfigSchema/schema/URI.SafeIframeRegexp.txt This example illustrates setting URI.SafeIframeRegexp to accept iframes from both YouTube and Vimeo embed URLs. It uses a combined regular expression for broader compatibility. ```html
%URI.SafeIframeRegexp = '%^http://(www.youtube.com/embed/|player.vimeo.com/video/)%';
``` -------------------------------- ### Configure SafeIframeRegexp for Vimeo Source: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/ConfigSchema/schema/URI.SafeIframeRegexp.txt This example shows how to configure URI.SafeIframeRegexp to permit iframes from Vimeo's video player URL. This setting enhances security by controlling which external video sources are allowed. ```html
%URI.SafeIframeRegexp = '%^http://player.vimeo.com/video/%';
``` -------------------------------- ### Inbound Filtering with HTML Purifier (PHP) Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/enduser-slow.html This example demonstrates how to perform inbound filtering of user-submitted HTML using the HTML Purifier library. It shows the process of receiving HTML from a form submission, purifying it, and then inserting the clean HTML into a database. The code assumes the existence of helper functions for displaying errors, success messages, forms, and database insertion. ```PHP ``` -------------------------------- ### HTML Purifier: Custom Content Model Example Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/enduser-customize.html Demonstrates a custom content model for an element requiring at least one 'li' child, specified as 'Required: li'. ```php $config->set('HTML', 'AllowedChildren', 'Required: li'); ``` -------------------------------- ### URI Munging with Additional Substitutions Source: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/ConfigSchema/schema/URI.Munge.txt This example shows how to use HTML Purifier's URI.Munge directive with advanced substitutions like %r (resource indicator), %n (tag name), %m (attribute name), and %p (CSS property name) for more granular control over URI redirection. ```php $config->set('URI', 'Munge', 'http://example.com/redirect?resource=%r&tag=%n&attr=%m&prop=%p'); ``` -------------------------------- ### Initialize HTML Purifier configuration Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/enduser-customize.html This PHP code snippet shows the initial setup for configuring HTML Purifier. It creates a default configuration object and sets specific definition identifiers and revision numbers, which are crucial for custom HTML definitions. ```PHP $config = HTMLPurifier_Config::createDefault(); $config->set('HTML.DefinitionID', 'enduser-customize.html tutorial'); $config->set('HTML.DefinitionRev', 1); $def = $config->getHTMLDefinition(true); ``` -------------------------------- ### Extract Style Blocks with HTML Purifier and CSSTidy (PHP) Source: https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/ConfigSchema/schema/Filter.ExtractStyleBlocks.txt This snippet demonstrates how to configure HTML Purifier to extract style blocks from HTML content. It utilizes CSSTidy for cleaning the extracted styles and shows how to save them to files or include them in the document. The example includes setting up HTML Purifier, processing dirty HTML, and handling the extracted styles. ```php '; ?> Filter.ExtractStyleBlocks body {color:#F00;} Some text'; $config = HTMLPurifier_Config::createDefault(); $config->set('Filter', 'ExtractStyleBlocks', true); $purifier = new HTMLPurifier($config); $html = $purifier->purify($dirty); // This implementation writes the stylesheets to the styles/ directory. // You can also echo the styles inside the document, but it's a bit // more difficult to make sure they get interpreted properly by // browsers; try the usual CSS armoring techniques. $styles = $purifier->context->get('StyleBlocks'); $dir = 'styles/'; if (!is_dir($dir)) mkdir($dir); $hash = sha1($_GET['html']); foreach ($styles as $i => $style) { file_put_contents($name = $dir . $hash . "_$i"); echo ''; } ?>
``` -------------------------------- ### HTML Purifier Hierarchy Example Source: https://github.com/ezyang/htmlpurifier/blob/master/docs/proposal-errors.txt This illustrates the current and potential hierarchical relationships between different HTML elements and their properties as processed by HTML Purifier. It shows the progression from tokens to attributes to CSS properties, and suggests future extensions. ```N/A token -> attr -> css property * -> syntax -> token -> attr -> css property -> url -> css stylesheet