Sha256: 33b8933abde3e290a69b94ffd8041a8d0b2e4f3f036bf117a9c00adb9e214377

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

module Slacken::Filters
  # Public: Sanitize not allowed tags in list.
  class SanitizeList < Slacken::Filter
    def call(component)
      case component.type.name
      when :ul, :ol, :dl
        component.derive(component.children.map(&method(:sanitize_list)))
      else
        component.derive(component.children.map(&method(:call)))
      end
    end

    def valid?(component)
      case component.type.name
      when :ul, :ol, :dl
        component.children.all?(&method(:list_sanitized?))
      else
        component.children.all?(&method(:valid?))
      end
    end

    private

    def sanitize_list(component)
      if component.type.member_of?(:li, :dd)
        head, *tails = component.children
        component.derive(
          [sanitize_list_item(head), *tails.map(&method(:sanitize_list))]
        )
      elsif component.type.allowed_in_list?
        component.derive(component.children.map(&method(:sanitize_list)))
      else
        component.derive(
          component.children.map(&method(:sanitize_list)),
          type: component.block? ? :div : :span
        )
      end
    end

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

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