You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Attributes to add to specific elements. If the attribute already exists, it will be replaced with the value specified here. Specify all element names and attributes in lowercase.
259
253
260
254
```ruby
261
-
:add_attributes => {
255
+
add_attributes: {
262
256
'a' => {'rel' => 'nofollow'}
263
257
}
264
258
```
@@ -276,28 +270,28 @@ Whether or not to allow well-formed HTML doctype declarations such as "<!DOCTYPE
276
270
Attributes to allow on specific elements. Specify all element names and attributes in lowercase.
277
271
278
272
```ruby
279
-
:attributes => {
280
-
'a'=> ['href', 'title'],
273
+
attributes: {
274
+
'a' => ['href', 'title'],
281
275
'blockquote' => ['cite'],
282
-
'img'=> ['alt', 'src', 'title']
276
+
'img' => ['alt', 'src', 'title']
283
277
}
284
278
```
285
279
286
280
If you'd like to allow certain attributes on all elements, use the symbol `:all` instead of an element name.
287
281
288
282
```ruby
289
283
# Allow the class attribute on all elements.
290
-
:attributes => {
284
+
attributes: {
291
285
:all => ['class'],
292
-
'a'=> ['href', 'title']
286
+
'a' => ['href', 'title']
293
287
}
294
288
```
295
289
296
290
To allow arbitrary HTML5 `data-*` attributes, use the symbol `:data` in place of an attribute name.
297
291
298
292
```ruby
299
293
# Allow arbitrary HTML5 data-* attributes on <div> elements.
300
-
:attributes => {
294
+
attributes: {
301
295
'div' => [:data]
302
296
}
303
297
```
@@ -353,7 +347,7 @@ If you'd like to allow the use of relative URLs which don't have a protocol, inc
353
347
Array of HTML element names to allow. Specify all names in lowercase. Any elements not in this array will be removed.
354
348
355
349
```ruby
356
-
:elements =>%w[
350
+
elements:%w[
357
351
a abbr b blockquote br cite code dd dfn dl dt em i kbd li mark ol p pre
358
352
q s samp small strike strong sub sup time u ul var
359
353
]
@@ -373,10 +367,10 @@ Array of HTML element names to allow. Specify all names in lowercase. Any elemen
373
367
374
368
#### :parser_options (Hash)
375
369
376
-
[Parsing options](https://github.com/rubys/nokogumbo/tree/master#parsing-options) to be supplied to `nokogumbo`.
370
+
[Parsing options](https://nokogiri.org/tutorials/parsing_an_html5_document.html?h=parsing+options#parsing-options) to be supplied to Nokogiri.
377
371
378
372
```ruby
379
-
:parser_options => {
373
+
parser_options: {
380
374
max_errors:-1,
381
375
max_tree_depth:-1
382
376
}
@@ -387,16 +381,16 @@ Array of HTML element names to allow. Specify all names in lowercase. Any elemen
387
381
URL protocols to allow in specific attributes. If an attribute is listed here and contains a protocol other than those specified (or if it contains no protocol at all), it will be removed.
If you'd like to allow the use of relative URLs which don't have a protocol, include the symbol `:relative` in the protocol array:
397
391
398
392
```ruby
399
-
:protocols => {
393
+
protocols: {
400
394
'a' => {'href' => ['http', 'https', :relative]}
401
395
}
402
396
```
@@ -407,7 +401,7 @@ If this is `true`, Sanitize will remove the contents of any non-allowlisted elem
407
401
408
402
If this is an Array or Set of element names, then only the contents of the specified elements (when filtered) will be removed, and the contents of all other filtered elements will be left behind.
409
403
410
-
The default value is `%w[iframe math noembed noframes noscript plaintext script style svg xmp]`.
404
+
The default value can be seen in the [default config](lib/sanitize/config/default.rb).
411
405
412
406
#### :transformers (Array or callable)
413
407
@@ -420,20 +414,14 @@ Hash of element names which, when removed, should have their contents surrounded
420
414
Each element name is a key pointing to another Hash, which provides the specific whitespace that should be inserted `:before` and `:after` the removed element's position. The `:after` value will only be inserted if the removed element has children, in which case it will be inserted after those children.
421
415
422
416
```ruby
423
-
:whitespace_elements => {
424
-
'br'=> { :before => "\n", :after =>"" },
425
-
'div' => { :before => "\n", :after =>"\n" },
426
-
'p'=> { :before => "\n", :after =>"\n" }
417
+
whitespace_elements: {
418
+
'br' => { before:"\n", after:"" },
419
+
'div' => { before:"\n", after:"\n" },
420
+
'p' => { before:"\n", after:"\n" }
427
421
}
428
422
```
429
423
430
-
The default elements with whitespace added before and after are:
431
-
432
-
```
433
-
address article aside blockquote br dd div dl dt
434
-
footer h1 h2 h3 h4 h5 h6 header hgroup hr li nav
435
-
ol p pre section ul
436
-
```
424
+
The default elements with whitespace added before and after can be seen in [the default config](lib/sanitize/config/default.rb).
437
425
438
426
## Transformers
439
427
@@ -442,7 +430,7 @@ Transformers allow you to filter and modify HTML nodes using your own custom log
442
430
To use one or more transformers, pass them to the `:transformers` config setting. You may pass a single transformer or an array of transformers.
443
431
444
432
```ruby
445
-
Sanitize.fragment(html, :transformers => [
433
+
Sanitize.fragment(html, transformers: [
446
434
transformer_one,
447
435
transformer_two
448
436
])
@@ -493,7 +481,7 @@ transformer = lambda do |env|
493
481
end
494
482
495
483
# Prints "header", "span", "strong", "p", "footer".
Transformers have a tremendous amount of power, including the power to completely bypass Sanitize's built-in filtering. Be careful! Your safety is in your own hands.
@@ -503,20 +491,22 @@ Transformers have a tremendous amount of power, including the power to completel
503
491
The following example demonstrates how to remove image elements unless they use a relative URL or are hosted on a specific domain. It assumes that the `<img>` element and its `src` attribute are already allowlisted.
504
492
505
493
```ruby
506
-
require'uri'
494
+
require"uri"
507
495
508
496
image_allowlist_transformer =lambdado |env|
509
497
# Ignore everything except <img> elements.
510
-
returnunless env[:node_name] =='img'
498
+
returnunless env[:node_name] =="img"
511
499
512
-
node = env[:node]
513
-
image_uri =URI.parse(node['src'])
500
+
node = env[:node]
501
+
image_uri =URI.parse(node["src"])
514
502
515
503
# Only allow relative URLs or URLs with the example.com domain. The
516
504
# image_uri.host.nil? check ensures that protocol-relative URLs like
0 commit comments