Class: Html::PermitScrubber
- Inherits:
-
Loofah::Scrubber
- Object
- Loofah::Scrubber
- Html::PermitScrubber
- Defined in:
- lib/html/sanitizer/scrubbers.rb
Overview
Html::PermitScrubber
Html::PermitScrubber allows you to permit only your own tags and/or attributes.
Html::PermitScrubber can be subclassed to determine:
-
When a node should be skipped via
skip_node?
. -
When a node is allowed via
allowed_node?
. -
When an attribute should be scrubbed via
scrub_attribute?
.
Subclasses don’t need to worry if tags or attributes are set or not. If tags or attributes are not set, Loofah’s behavior will be used. If you override allowed_node?
and no tags are set, it will not be called. Instead Loofahs behavior will be used. Likewise for scrub_attribute?
and attributes respectively.
Text and CDATA nodes are skipped by default. Unallowed elements will be stripped, i.e. element is removed but its subtree kept. Supplied tags and attributes should be Enumerables.
tags=
If set, elements excluded will be stripped. If not, elements are stripped based on Loofahs HTML5::Scrub.allowed_element?
.
attributes=
If set, attributes excluded will be removed. If not, attributes are removed based on Loofahs HTML5::Scrub.scrub_attributes
.
class CommentScrubber < Html::PermitScrubber
def allowed_node?(node)
!%w(form script comment blockquote).include?(node.name)
end
def skip_node?(node)
node.text?
end
def scrub_attribute?(name)
name == "style"
end
end
See the documentation for Nokogiri::XML::Node to understand what’s possible with nodes: nokogiri.org/Nokogiri/XML/Node.html
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#initialize ⇒ PermitScrubber
constructor
A new instance of PermitScrubber.
- #scrub(node) ⇒ Object
Constructor Details
#initialize ⇒ PermitScrubber
Returns a new instance of PermitScrubber.
48 49 50 51 |
# File 'lib/html/sanitizer/scrubbers.rb', line 48 def initialize @direction = :bottom_up @tags, @attributes = nil, nil end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
46 47 48 |
# File 'lib/html/sanitizer/scrubbers.rb', line 46 def attributes @attributes end |
#tags ⇒ Object
Returns the value of attribute tags.
46 47 48 |
# File 'lib/html/sanitizer/scrubbers.rb', line 46 def @tags end |
Instance Method Details
#scrub(node) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/html/sanitizer/scrubbers.rb', line 61 def scrub(node) return CONTINUE if skip_node?(node) unless keep_node?(node) return STOP if scrub_node(node) == STOP end scrub_attributes(node) end |