Sha256: f7fdc1779e128b889de2bc4766c92b259b668700efeb1e2ecea1b8b1b47ea62c

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

module Slacken::Filters
  # Public: Sanitize not allowed tags in links.
  class SanitizeLink < Slacken::Filter
    def call(component)
      if component.type.member_of?(%i(img a iframe))
        component.derive(
          component.children.map(&method(:sanitize))
        )
      else
        component.derive(
          component.children.map(&method(:call))
        )
      end
    end

    def valid?(component)
      if component.type.member_of?(%i(img a iframe))
        component.children.all?(&method(:link_containers_sanitized?))
      else
        component.children.all?(&method(:valid?))
      end
    end

    private

    def sanitize(component)
      if component.type.allowed_in_link?
        component.derive(
          component.children.map(&method(:sanitize))
        )
      else
        # No block tags are allowed.
        component.derive(
          component.children.map(&method(:sanitize)),
          type: :span
        )
      end
    end

    def link_containers_sanitized?(component)
      if component.type.allowed_in_link?
        component.children.all?(&method(:link_containers_sanitized?))
      else
        false
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slacken-0.1.6 lib/slacken/filters/sanitize_link.rb
slacken-0.1.5 lib/slacken/filters/sanitize_link.rb
slacken-0.1.4 lib/slacken/filters/sanitize_link.rb
slacken-0.1.3 lib/slacken/filters/sanitize_link.rb
slacken-0.1.2 lib/slacken/filters/sanitize_link.rb
slacken-0.1.1 lib/slacken/filters/sanitize_link.rb