All Projects → Exercise → Htmlpurifierbundle

Exercise / Htmlpurifierbundle

Licence: other
HTML Purifier is a standards-compliant HTML filter library written in PHP.

Projects that are alternatives of or similar to Htmlpurifierbundle

Web Profiler Bundle
The WebProfilerBundle provides detailed technical information about each request execution and displays it in both the web debug toolbar and the profiler.
Stars: ✭ 1,905 (+714.1%)
Mutual labels:  twig, symfony
Twig Bridge
Provides integration for Twig with various Symfony components.
Stars: ✭ 2,170 (+827.35%)
Mutual labels:  twig, symfony
Sflive Paris2016 Workflow
Demo app for the new symfony/workflow component
Stars: ✭ 160 (-31.62%)
Mutual labels:  twig, symfony
Nodefony Starter
Nodefony Starter Node.js Framework
Stars: ✭ 95 (-59.4%)
Mutual labels:  twig, symfony
Rich Model Forms Bundle
Provides additional data mappers that ease the use of the Symfony Form component with rich models.
Stars: ✭ 198 (-15.38%)
Mutual labels:  symfony, form
Knppaginatorbundle
SEO friendly Symfony paginator to sort and paginate
Stars: ✭ 1,534 (+555.56%)
Mutual labels:  twig, symfony
Twig Bundle
The Twig Bundle provides configuration for using Twig in your applications.
Stars: ✭ 2,150 (+818.8%)
Mutual labels:  twig, symfony
Twig Lambda
Lambda expressions for Twig and filters that make use of them
Stars: ✭ 40 (-82.91%)
Mutual labels:  twig, filter
Form
The Form component allows you to easily create, process and reuse HTML forms.
Stars: ✭ 2,406 (+928.21%)
Mutual labels:  symfony, form
Grav
Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
Stars: ✭ 13,067 (+5484.19%)
Mutual labels:  twig, symfony
Platform
Shopware 6 is an open source eCommerce platform realised by the ideas and the spirit of its community.
Stars: ✭ 1,267 (+441.45%)
Mutual labels:  twig, symfony
Auditor Bundle
Doctrine audits logs made easy.
Stars: ✭ 221 (-5.56%)
Mutual labels:  twig, symfony
Kimai2
Kimai v2 is a web-based multiuser time-tracking application. Free for everyone: freelancers, agencies, companies, organizations - all can track their times, generate invoices and more. SaaS version available at https://www.kimai.cloud
Stars: ✭ 1,216 (+419.66%)
Mutual labels:  twig, symfony
Symfony Demo App
A Symfony demo application with basic user management
Stars: ✭ 122 (-47.86%)
Mutual labels:  twig, symfony
Fmbbcodebundle
🔠 BBCode bundle for Symfony projects
Stars: ✭ 56 (-76.07%)
Mutual labels:  twig, symfony
Qr Code Bundle
Bundle for generating QR codes in Symfony
Stars: ✭ 169 (-27.78%)
Mutual labels:  twig, symfony
Migrify
Futuristic Grinder for Legacy Code with Effortles Confidence
Stars: ✭ 25 (-89.32%)
Mutual labels:  twig, symfony
Idea Php Drupal Symfony2 Bridge
PhpStorm plugin to support Symfony components inside Drupal 8
Stars: ✭ 34 (-85.47%)
Mutual labels:  twig, symfony
Msgphp
Reusable domain layers. Shipped with industry standard infrastructure.
Stars: ✭ 182 (-22.22%)
Mutual labels:  twig, symfony
Core
Zikula Core Framework
Stars: ✭ 213 (-8.97%)
Mutual labels:  twig, symfony

Total Downloads Latest Stable Version License Build Status

ExerciseHTMLPurifierBundle

This bundle integrates HTMLPurifier into Symfony.

Installation

Symfony 3.4 and above (using Composer)

Require the bundle in your composer.json file:

{
    "require": {
        "exercise/htmlpurifier-bundle": "*"
    }
}

Install the bundle:

$ composer require exercise/htmlpurifier-bundle

Register the bundle in Symfony 3:

// app/AppKernel.php

public function registerBundles()
{
    return [
        // ...
        new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
    ];
}

Configuration in Symfony 3

The configuration is the same as the following section, but the path should be app/config.yml instead.

Configuration in Symfony 4 and up

If you do not explicitly configure this bundle, an HTMLPurifier service will be defined as exercise_html_purifier.default. This behavior is the same as if you had specified the following configuration:

# config/packages/exercise_html_purifier.yaml

exercise_html_purifier:
    default_cache_serializer_path: '%kernel.cache_dir%/htmlpurifier'

The default profile is special, it is always defined and its configuration is inherited by all custom profiles. exercise_html_purifier.default is the default service using the base configuration.

# config/packages/exercise_html_purifier.yaml

exercise_html_purifier:
    default_cache_serializer_path: '%kernel.cache_dir%/htmlpurifier'
    html_profiles:
        custom:
            config:
                Core.Encoding: 'ISO-8859-1'
                HTML.Allowed: 'a[href|target],p,br'
                Attr.AllowedFrameTargets: '_blank'

In this example, a exercise_html_purifier.custom service will also be defined, which includes cache, encoding, HTML tags and attributes options. Available configuration options may be found in HTMLPurifier's configuration documentation.

Note: If you define a default profile but omit Cache.SerializerPath, it will still default to the path above. You can specify a value of null for the option to suppress the default path.

Autowiring

By default type hinting \HtmlPurifier in your services will autowire the exercise_html_purifier.default service. To override it and use your own config as default autowired services just add this configuration:

# config/services.yaml
services:
    #...
    
    exercise_html_purifier.default: '@exercise_html_purifier.custom'

Using a custom purifier class as default

If you want to use your own class as default purifier, define the new alias as below:

# config/services.yaml
services:
    # ...

    exercise_html_purifier.default: '@App\Html\CustomHtmlPurifier'

Argument binding (Symfony >= 4.4)

The bundle also leverages the alias argument binding for each profile. So the following config:

    html_profiles:
        blog:
            # ...
        gallery:
            # ...

will register the following binding:

 // default config is bound whichever argument name is used
public function __construct(\HTMLPurifier $purifier) {}
public function __construct(\HTMLPurifier $htmlPurifier) {}
public function __construct(\HTMLPurifier $blogPurifier) {} // blog config
public function __construct(\HTMLPurifier $galleryPurifier) {} // gallery config

Form Type Extension

This bundles provides a form type extension for filtering form fields with HTMLPurifier. Purification is done early during the PRE_SUBMIT event, which means that client data will be filtered before being bound to the form.

Two options are automatically available in all TextType based types:

<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('content', TextareaType::class, ['purify_html' => true]) // will use default profile 
            ->add('sneek_peak', TextType::class, ['purify_html' => true, 'purify_html_profile' => 'sneak_peak'])
            // ...
        ;
    }
    
    // ...
}

Every type extending TextType (i.e: TextareaType) inherit these options. It also means that if you use a type such as CKEditorType, you will benefit from these options without configuring anything.

Twig Filter

This bundles registers a purify filter with Twig. Output from this filter is marked safe for HTML, much like Twig's built-in escapers. The filter may be used as follows:

{# Filters text's value through the "default" HTMLPurifier service #}
{{ text|purify }}

{# Filters text's value through the "custom" HTMLPurifier service #}
{{ text|purify('custom') }}

Purifiers Registry

A Exercise\HtmlPurifierBundle\HtmlPurifiersRegistry class is registered by default as a service. To add your custom instance of purifier, and make it available to the form type and Twig extensions through its profile name, you can use the tag exercise.html_purifier as follow:

# config/services.yaml

services:
    # ...
    
    App\HtmlPurifier\CustomPurifier:
        tags:
            - name: exercise.html_purifier
              profile: custom

Now your purifier can be used when:

// In a form type
$builder
    ->add('content', TextareaType::class, [
        'purify_html' => true,
        'purify_html_profile' => 'custom',
    ])
    // ...
{# in a template #}
{{ html_string|purify('custom') }}

How to Customize a Config Definition

Whitelist Attributes

In some case, you might want to set some rules for a specific tag. This is what the following config is about:

# config/packages/exercise_html_purifier.yaml
exercise_html_purifier:
    html_profiles:
        default:
            config:
                HTML.Allowed: <
                    *[id|class|name],
                    a[href|title|rel|target],
                    img[src|alt|height|width],
                    br,div,embed,object,u,em,ul,ol,li,strong,span
            attributes:
                img:
                    # attribute name, type (Integer, Color, ...)
                    data-id: ID
                    data-image-size: Text
                span:
                    data-link: URI

See HTMLPurifier_AttrTypes for more options.

Whitelist Elements

In some case, you might want to set some rules for a specific tag. This is what the following config is about:

# config/packages/exercise_html_purifier.yaml
exercise_html_purifier:
    html_profiles:
        default:
            # ...
            elements:
                video:
                    - Block
                    - 'Optional: (source, Flow) | (Flow, source) | Flow'
                    - Common # allows a set of common attributes
                    # The 4th and 5th arguments are optional
                    - src: URI # list of type rules by attributes
                      type: Text
                      width: Length
                      height: Length
                      poster: URI
                      preload: 'Enum#auto,metadata,none'
                      controls: Bool
                source:
                    - Block
                    - Flow
                    - Common
                    - { src: URI, type: Text }
                    - [style] # list of forbidden attributes

Would be equivalent to:

$def = $config->getHTMLDefintion(true);
$def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
    'src' => 'URI',
    'type' => 'Text',
    'width' => 'Length',
    'height' => 'Length',
    'poster' => 'URI',
    'preload' => 'Enum#auto,metadata,none',
    'controls' => 'Bool',
]);
$source = $def->addElement('source', 'Block', 'Flow', 'Common', [
    'src' => 'URI',
    'type' => 'Text',
]);
$source->excludes = ['style' => true];

See HTMLPurifier documentation for more details.

Blank Elements

It might happen that you need a tag clean from any attributes. Then just add it to the list:

# config/packages/exercise_html_purifier.yaml
exercise_html_purifier:
    html_profiles:
        default:
            # ...
            blank_elements: [legend, figcaption]

How to Reuse Profiles

What can really convenient is to reuse some profile definition to build other custom definitions.

# config/packages/exercise_html_purifier.yaml
exercise_html_purifier:
    html_profiles:
        base:
            # ...
        video:
            # ...
        all:
            parents: [base, video]

In this example the profile named "all" will inherit the "default" profile, then the two custom ones. The order is important as each profile overrides the previous, and "all" could define its own rules too.

Contributing

PRs are welcomed :). Please target the 2.0 branch for bug fixes and master for new features.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].