在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Exercise/HTMLPurifierBundle开源软件地址:https://github.com/Exercise/HTMLPurifierBundle开源编程语言:PHP 100.0%开源软件介绍:ExerciseHTMLPurifierBundleThis bundle integrates HTMLPurifier into Symfony. InstallationInstall the bundle: $ composer require exercise/htmlpurifier-bundle ConfigurationIf you do not explicitly configure this bundle, an HTMLPurifier service will be
defined as # config/packages/exercise_html_purifier.yaml
exercise_html_purifier:
default_cache_serializer_path: '%kernel.cache_dir%/htmlpurifier' The # 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 Note: If you define a AutowiringBy default type hinting # config/services.yaml
services:
#...
exercise_html_purifier.default: '@exercise_html_purifier.custom' Using a custom purifier class as defaultIf 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 bindingThe 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 ExtensionThis 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 <?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 Twig FilterThis bundles registers a {# Filters text's value through the "default" HTMLPurifier service #}
{{ text|purify }}
{# Filters text's value through the "custom" HTMLPurifier service #}
{{ text|purify('custom') }} Purifiers RegistryA # 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 DefinitionWhitelist AttributesIn 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 ElementsIn 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 ElementsIt 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 ProfilesWhat 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. ContributingPRs are welcomed :). Please target the |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论