Sha256: 18899021d1d1a8528762960b3bfd813de3e13a736031e3455b36aa71505fcb5b

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

module Slacken::Filters
  # Public: Sanitize not allowed tags in headline.
  class SanitizeHeadline < Slacken::Filter
    def call(component)
      # NOTE: each special tag (list, headline, and table) is not allowed to occur
      #       in another type special tags.
      if component.type.name =~ /h\d/
        component.derive(component.children.map(&method(:sanitize_headline)))
      else
        component.derive(component.children.map(&method(:call)))
      end
    end

    def valid?(component)
      if component.type.name =~ /h\d/
        component.children.all?(&method(:headline_sanitized?))
      else
        component.children.all?(&method(:valid?))
      end
    end

    private

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

    def headline_sanitized?(component)
      if component.type.allowed_in_headline?
        component.children.all?(&method(:headline_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_headline.rb
slacken-0.1.5 lib/slacken/filters/sanitize_headline.rb
slacken-0.1.4 lib/slacken/filters/sanitize_headline.rb
slacken-0.1.3 lib/slacken/filters/sanitize_headline.rb
slacken-0.1.2 lib/slacken/filters/sanitize_headline.rb
slacken-0.1.1 lib/slacken/filters/sanitize_headline.rb