Sha256: 9a5e8ead97572baa0c745d22e998ac11b10221b135832733c06ef81e81688c29

Contents?: true

Size: 765 Bytes

Versions: 1

Compression:

Stored size: 765 Bytes

Contents

# frozen_string_literal: true

module XmlSanitizer
  class DefaultPermitScrubber < Loofah::Scrubber
    attr_accessor :direction, :tags

    def initialize(tags: [])
      super
      @tags = tags
    end

    def scrub(node)
      return CONTINUE if skip_node?(node)

      node.remove unless allowed_node?(node)
    end

    def node_name(node)
      namespace_prefix?(node) ? node_name_with_namespace_prefix(node) : node.name
    end

    def namespace_prefix?(node)
      !!node&.namespace&.prefix
    end

    def node_name_with_namespace_prefix(node)
      "#{node.namespace.prefix}:#{node.name}"
    end

    protected

    def allowed_node?(node)
      tags.include?(node_name(node))
    end

    def skip_node?(node)
      !node.element?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xml_sanitizer-0.1.0 lib/xml_sanitizer/default_permit_scrubber.rb