Sha256: 60f7ef6045dc1ab9d39aa5ffdd8efe525ee98315a78726bc646f63e8cdd19e02

Contents?: true

Size: 1.39 KB

Versions: 122

Compression:

Stored size: 1.39 KB

Contents

# modified from https://gist.github.com/ivan-kolmychek/ee2fdc53f3e2c637271d

module Bolognese
  class WhitelistScrubber < Loofah::Scrubber
    def initialize(options={})
      @direction = :bottom_up
      @tags = options[:tags]
      @attributes = options[:attributes]
    end

    def scrub(node)
      scrub_node_attributes(node) and return CONTINUE if node_allowed?(node)
      node.before node.children
      node.remove
    end

    private

    def scrub_node_attributes(node)
      fallback_scrub_node_attributes(node) and return true unless @attributes.present? && @attributes.respond_to?(:include?)
      node.attribute_nodes.each do |attr_node|
        attr_node.remove unless @attributes.include?(attr_node.name)
      end
    end

    def allowed_not_element_node_types
      [ Nokogiri::XML::Node::TEXT_NODE, Nokogiri::XML::Node::CDATA_SECTION_NODE ]
    end

    def fallback_scrub_node_attributes(node)
      Loofah::HTML5::Scrub.scrub_attributes(node)
    end

    def fallback_allowed_element_detection(node)
      Loofah::HTML5::Scrub.allowed_element?(node.name)
    end

    def node_allowed?(node)
      return fallback_allowed_element_detection(node) unless @tags.present? && @tags.respond_to?(:include?)
      return true if allowed_not_element_node_types.include?(node.type)
      return false unless node.type == Nokogiri::XML::Node::ELEMENT_NODE
      @tags.include? node.name
    end
  end
end

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
bolognese-0.10.21 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.20 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.19 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.18 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.17 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.16 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.15 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.14 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.13 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.11 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.10 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.9 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.8 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.7 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.6 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.5 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.4 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.3 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10.2 lib/bolognese/whitelist_scrubber.rb
bolognese-0.10 lib/bolognese/whitelist_scrubber.rb