1
1
[ ![ Total Downloads] ( https://poser.pugx.org/exercise/htmlpurifier-bundle/downloads )] ( https://packagist.org/packages/exercise/htmlpurifier-bundle )
2
2
[ ![ Latest Stable Version] ( https://poser.pugx.org/exercise/htmlpurifier-bundle/v/stable )] ( https://packagist.org/packages/exercise/htmlpurifier-bundle )
3
3
[ ![ License] ( https://poser.pugx.org/exercise/htmlpurifier-bundle/license )] ( https://packagist.org/packages/exercise/htmlpurifier-bundle )
4
- [ ![ Build Status] ( https://travis-ci.org/Exercise/HTMLPurifierBundle.svg?branch=2.0 )] ( https://travis-ci.org/Exercise/HTMLPurifierBundle )
4
+ [ ![ Build Status] ( https://travis-ci.org/Exercise/HTMLPurifierBundle.svg?branch=master )] ( https://travis-ci.org/Exercise/HTMLPurifierBundle )
5
5
6
6
# ExerciseHTMLPurifierBundle
7
7
@@ -36,14 +36,14 @@ Register the bundle in Symfony 3:
36
36
37
37
public function registerBundles()
38
38
{
39
- return array(
40
- new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
39
+ return [
41
40
// ...
42
- );
41
+ new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
42
+ ];
43
43
}
44
44
```
45
45
46
- ## Configuration in Symfony 3 without Symfony Flex
46
+ ## Configuration in Symfony 3
47
47
48
48
If you do not explicitly configure this bundle, an HTMLPurifier service will be
49
49
defined as ` exercise_html_purifier.default ` . This behavior is the same as if you
@@ -53,8 +53,7 @@ had specified the following configuration:
53
53
# app/config.yml
54
54
55
55
exercise_html_purifier :
56
- default :
57
- Cache.SerializerPath : ' %kernel.cache_dir%/htmlpurifier'
56
+ default_cache_serializer_path : ' %kernel.cache_dir%/htmlpurifier'
58
57
` ` `
59
58
60
59
The ` default` profile is special in that it is used as the configuration for the
@@ -65,10 +64,11 @@ other profiles you might define.
65
64
# app/config.yml
66
65
67
66
exercise_html_purifier:
68
- default:
69
- Cache.SerializerPath: '%kernel.cache_dir%/htmlpurifier'
70
- custom:
71
- Core.Encoding: 'ISO-8859-1'
67
+ default_cache_serializer_path: '%kernel.cache_dir%/htmlpurifier'
68
+ html_profiles:
69
+ custom:
70
+ config:
71
+ Core.Encoding: 'ISO-8859-1'
72
72
` ` `
73
73
74
74
In this example, a `exercise_html_purifier.custom` service will also be defined,
@@ -81,7 +81,7 @@ option to suppress the default path.
81
81
82
82
[configuration documentation] : http://htmlpurifier.org/live/configdoc/plain.html
83
83
84
- # # Configuration using Symfony Flex
84
+ # # Configuration in Symfony 4 and up
85
85
86
86
If you do not explicitly configure this bundle, an HTMLPurifier service will be
87
87
defined as `exercise_html_purifier.default`. This behavior is the same as if you
@@ -91,8 +91,7 @@ had specified the following configuration:
91
91
# config/packages/exercise_html_purifier.yaml
92
92
93
93
exercise_html_purifier:
94
- default:
95
- Cache.SerializerPath: '%kernel.cache_dir%/htmlpurifier'
94
+ default_cache_serializer_path: '%kernel.cache_dir%/htmlpurifier'
96
95
` ` `
97
96
98
97
The `default` profile is special, it is *always* defined and its configuration
@@ -104,27 +103,33 @@ configuration.
104
103
# config/packages/exercise_html_purifier.yaml
105
104
106
105
exercise_html_purifier:
107
- default:
108
- Cache.SerializerPath: '%kernel.cache_dir%/htmlpurifier'
109
- custom:
110
- Core.Encoding: 'ISO-8859-1'
106
+ default_cache_serializer_path: 'tmp/htmlpurifier'
107
+ html_profiles:
108
+ default:
109
+ config:
110
+ Cache.SerializerPermissions: 777
111
+ custom:
112
+ config:
113
+ Core.Encoding: 'ISO-8859-1'
111
114
` ` `
112
-
115
+
113
116
# # Autowiring
114
117
115
118
By default type hinting `\HtmlPurifier` in your services will autowire
116
119
the `exercise_html_purifier.default` service.
117
120
To override it and use your own config as default autowired services just add
118
- this in you `app/config/services.yml` or `config/services.yaml` :
121
+ this in you `app/config/services.yml` in you use symfony 3 or `config/services.yaml`
122
+ if you use symfony 4 :
119
123
120
124
` ` ` yaml
125
+ # config/services.yaml
121
126
services:
122
- # ...
123
-
127
+ #...
128
+
124
129
exercise_html_purifier.default: '@exercise_html_purifier.custom'
125
130
` ` `
126
131
127
- # # Using a custom purifier class as default
132
+ # ## Using a custom purifier class as default
128
133
129
134
If you want to use your own class as default purifier, define a new alias :
130
135
@@ -139,6 +144,29 @@ services:
139
144
In such case, the custom purifier will use its own defined configuration,
140
145
ignoring the bundle configuration.
141
146
147
+ # ## Argument binding
148
+
149
+ The bundle also leverages the alias argument binding for each profile. So the
150
+ following config :
151
+
152
+ ` ` ` yaml
153
+ html_profiles:
154
+ blog:
155
+ # ...
156
+ gallery:
157
+ # ...
158
+ ` ` `
159
+
160
+ will register the following binding :
161
+
162
+ ` ` ` php
163
+ // default config is bound whichever argument name is used
164
+ public function __construct(\H TMLPurifier $purifier) {}
165
+ public function __construct(\H TMLPurifier $htmlPurifier) {}
166
+ public function __construct(\H TMLPurifier $blogPurifier) {} // blog config
167
+ public function __construct(\H TMLPurifier $galleryPurifier) {} // gallery config
168
+ ` ` `
169
+
142
170
# # Form Type Extension
143
171
144
172
This bundles provides a form type extension for filtering form fields with
@@ -229,6 +257,128 @@ $builder
229
257
{{ html_string|purify('custom') }}
230
258
` ` `
231
259
260
+ # # How to Customize a Config Definition
261
+
262
+ # Custom Attributes
263
+
264
+ In some case, you might want to set some rules for a specific tag.
265
+ This is what the following config is about :
266
+
267
+ ` ` ` yaml
268
+ # config/packages/exercise_html_purifier.yaml
269
+ exercise_html_purifier:
270
+ html_profiles:
271
+ default:
272
+ config:
273
+ HTML.Allowed: <
274
+ *[id|class|name],
275
+ a[href|title|rel|target],
276
+ img[src|alt|height|width],
277
+ br,div,embed,object,u,em,ul,ol,li,strong,span
278
+ attributes:
279
+ img:
280
+ # attribute name, type (Integer, Color, ...)
281
+ data-id: ID
282
+ data-image-size: Text
283
+ span:
284
+ data-link: URI
285
+ ` ` `
286
+
287
+ See [HTMLPurifier_AttrTypes][] for more options.
288
+
289
+ [HTMLPurifier_AttrTypes] : https://github.com/ezyang/htmlpurifier/blob/master/library/HTMLPurifier/AttrTypes.php
290
+
291
+ # Custom Elements
292
+
293
+ In some case, you might want to set some rules for a specific tag.
294
+ This is what the following config is about :
295
+
296
+ ` ` ` yaml
297
+ # config/packages/exercise_html_purifier.yaml
298
+ exercise_html_purifier:
299
+ html_profiles:
300
+ default:
301
+ # ...
302
+ elements:
303
+ video:
304
+ - Block
305
+ - 'Optional: (source, Flow) | (Flow, source) | Flow'
306
+ - Common # allows a set of common attributes
307
+ # The 4th and 5th arguments are optional
308
+ - src: URI # list of type rules by attributes
309
+ type: Text
310
+ width: Length
311
+ height: Length
312
+ poster: URI
313
+ preload: 'Enum#auto,metadata,none'
314
+ controls: Bool
315
+ source:
316
+ - Block
317
+ - Flow
318
+ - Common
319
+ - { src: URI, type: Text }
320
+ - [style] # list of forbidden attributes
321
+ ` ` `
322
+
323
+ Would be equivalent to :
324
+
325
+ ` ` ` php
326
+ $def = $config->getHTMLDefintion(true);
327
+ $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
328
+ 'src' => 'URI',
329
+ 'type' => 'Text',
330
+ 'width' => 'Length',
331
+ 'height' => 'Length',
332
+ 'poster' => 'URI',
333
+ 'preload' => 'Enum#auto,metadata,none',
334
+ 'controls' => 'Bool',
335
+ ]);
336
+ $source = $def->addElement('source', 'Block', 'Flow', 'Common', [
337
+ 'src' => 'URI',
338
+ 'type' => 'Text',
339
+ ]);
340
+ $source->excludes = ['style' => true];
341
+ ` ` `
342
+
343
+ See [HTMLPurifier documentation][] for more details.
344
+
345
+ [HTMLPurifier documentation] : http://htmlpurifier.org/docs/enduser-customize.html
346
+
347
+ # Blank Elements
348
+
349
+ It might happen that you need a tag clean from any attributes.
350
+ Then just add it to the list :
351
+
352
+ ` ` ` yaml
353
+ # config/packages/exercise_html_purifier.yaml
354
+ exercise_html_purifier:
355
+ html_profiles:
356
+ default:
357
+ # ...
358
+ blank_elements: [legend, figcaption]
359
+ ` ` `
360
+
361
+ # # How to Reuse Profiles
362
+
363
+ What can really convenient is to reuse some profile definition
364
+ to build other custom definitions.
365
+
366
+ ` ` ` yaml
367
+ # config/packages/exercise_html_purifier.yaml
368
+ exercise_html_purifier:
369
+ html_profiles:
370
+ base:
371
+ # ...
372
+ video:
373
+ # ...
374
+ all:
375
+ parents: [base, video]
376
+ ` ` `
377
+
378
+ In this example the profile named "all" will inherit the "default" profile,
379
+ then the two custom ones. The order is important as each profile overrides the
380
+ previous, and "all" could define its own rules too.
381
+
232
382
# # Contributing
233
383
234
384
PRs are welcomed :). Please target the `2.0` branch for bug fixes and `master`
0 commit comments