Skip to content

Commit 68a5fb3

Browse files
committed
Document SafeListSanitizer and Scrubber prune option in README
1 parent 0d0bf32 commit 68a5fb3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ safe_list_sanitizer.sanitize(@article.body, scrubber: ArticleScrubber.new)
8686

8787
# safe list sanitizer can also sanitize css
8888
safe_list_sanitizer.sanitize_css('background-color: #000;')
89+
90+
# fully prune nodes from the tree instead of stripping tags and leaving inner content
91+
safe_list_sanitizer = Rails::Html::SafeListSanitizer.new(prune: true)
8992
```
9093

9194
### Scrubbers
@@ -107,6 +110,24 @@ html_fragment.scrub!(scrubber)
107110
html_fragment.to_s # => "<a></a>"
108111
```
109112

113+
By default, inner content is left, but it can be removed as well.
114+
115+
```ruby
116+
scrubber = Rails::Html::PermitScrubber.new
117+
scrubber.tags = ['a']
118+
119+
html_fragment = Loofah.fragment('<a><span>text</span></a>')
120+
html_fragment.scrub!(scrubber)
121+
html_fragment.to_s # => "<a>text</a>"
122+
123+
scrubber = Rails::Html::PermitScrubber.new(prune: true)
124+
scrubber.tags = ['a']
125+
126+
html_fragment = Loofah.fragment('<a><span>text</span></a>')
127+
html_fragment.scrub!(scrubber)
128+
html_fragment.to_s # => "<a></a>"
129+
```
130+
110131
#### `Rails::Html::TargetScrubber`
111132

112133
Where `PermitScrubber` picks out tags and attributes to permit in sanitization,
@@ -124,6 +145,23 @@ html_fragment.scrub!(scrubber)
124145
html_fragment.to_s # => "<a></a>"
125146
```
126147

148+
Similarly to `PermitScrubber`, nodes can be fully pruned.
149+
150+
```ruby
151+
scrubber = Rails::Html::TargetScrubber.new
152+
scrubber.tags = ['span']
153+
154+
html_fragment = Loofah.fragment('<a><span>text</span></a>')
155+
html_fragment.scrub!(scrubber)
156+
html_fragment.to_s # => "<a>text</a>"
157+
158+
scrubber = Rails::Html::TargetScrubber.new(prune: true)
159+
scrubber.tags = ['span']
160+
161+
html_fragment = Loofah.fragment('<a><span>text</span></a>')
162+
html_fragment.scrub!(scrubber)
163+
html_fragment.to_s # => "<a></a>"
164+
```
127165
#### Custom Scrubbers
128166

129167
You can also create custom scrubbers in your application if you want to.

0 commit comments

Comments
 (0)