class Sanitize::Whitelist

Constants

VERSION

Attributes

elements[R]
transformers[R]

Public Class Methods

new(&block) click to toggle source
# File lib/sanitize/whitelist.rb, line 5
def initialize(&block)
  @elements = {}
  @transformers = []
  eval_block(&block)
  freeze
end

Public Instance Methods

allow(elements) click to toggle source
# File lib/sanitize/whitelist.rb, line 37
def allow(elements)
  Array(elements).map { |name| element(name) }
end
allowed_elements() click to toggle source
# File lib/sanitize/whitelist.rb, line 61
def allowed_elements
  @elements.values.select(&:allowed?)
end
dup(&block) click to toggle source
Calls superclass method
# File lib/sanitize/whitelist.rb, line 26
def dup(&block)
  super.tap do |object|
    object.eval_block(&block)
    freeze
  end
end
element(name) click to toggle source
# File lib/sanitize/whitelist.rb, line 53
def element(name)
  @elements[name] ||= Sanitize::Whitelist::Element.new(name)
end
escape_non_whitelisted!() click to toggle source
# File lib/sanitize/whitelist.rb, line 49
def escape_non_whitelisted!
  @remove_non_whitelisted = false
end
eval_block(&block) click to toggle source
# File lib/sanitize/whitelist.rb, line 33
def eval_block(&block)
  block.arity == 1 ? block.call(self) : instance_eval(&block) if block
end
freeze() click to toggle source
Calls superclass method
# File lib/sanitize/whitelist.rb, line 12
def freeze
  super
  @elements.freeze.values.each(&:freeze)
  @transformers.freeze
end
initialize_dup(original) click to toggle source
Calls superclass method
# File lib/sanitize/whitelist.rb, line 18
def initialize_dup(original)
  @elements = original.elements.each_with_object({}) do |(name,element), elements|
    elements[name] = element.dup
  end
  @transformers = original.transformers.dup
  super
end
remove(boolean_or_elements) click to toggle source
# File lib/sanitize/whitelist.rb, line 41
def remove(boolean_or_elements)
  Array(boolean_or_elements).map { |name| element(name).remove! }
end
remove_elements() click to toggle source
# File lib/sanitize/whitelist.rb, line 65
def remove_elements
  @elements.values.select(&:remove?)
end
remove_non_whitelisted!() click to toggle source
# File lib/sanitize/whitelist.rb, line 45
def remove_non_whitelisted!
  @remove_non_whitelisted = true
end
to_hash() click to toggle source
# File lib/sanitize/whitelist.rb, line 69
def to_hash
  {}.tap do |result|
    result[:elements] = allowed_elements.map(&:name)

    if @remove_non_whitelisted
      result[:remove_contents] = @remove_non_whitelisted
    else
      elements = remove_elements.map(&:name)
      result[:remove_contents] = elements unless elements.empty?
    end

    result[:transformers] = @transformers unless @transformers.empty?

    attributes = allowed_elements.each_with_object({}) do |element,attrs|
      attrs.merge! element.to_hash
    end
    result[:attributes] = attributes unless attributes.empty?

    protocols = allowed_elements.each_with_object({}) do |element,attrs|
      attrs.merge! element.to_protocols_hash
    end
    result[:protocols] = protocols unless protocols.empty?
  end
end
transform(&block) click to toggle source
# File lib/sanitize/whitelist.rb, line 57
def transform(&block)
  @transformers << block
end