Sha256: fe099961db5823a076efe1db009a19505525702730e21d5541c75d6f3e874fd9

Contents?: true

Size: 1.42 KB

Versions: 28

Compression:

Stored size: 1.42 KB

Contents

require 'sanitize'

module Softcover
  module Sanitizer
    extend self

    # Sanitization suitable for displaying untrusted generated html, while
    # retaining useful tags and attributes.

    def clean(html)
      return unless html

      sanitize_options = {
        elements: %w{div span p a ul ol li h1 h2 h3 h4
          pre em sup table tbody thead tr td img code strong},
        remove_contents: %w{script},
        attributes: {
          'div' => %w{id class data-tralics-id data-number data-chapter},
          'a'    => %w{id class href},
          'span' => %w{id class style},
          'ol'   => %w{id class},
          'ul'   => %w{id class},
          'li'   => %w{id class},
          'sup'  => %w{id class},
          'h1'   => %w{id class},
          'h2'   => %w{id class},
          'h3'   => %w{id class},
          'h4'   => %w{id class},
          'img'  => %w{id class src alt},
          'em'   => %w{id class},
          'code' => %w{id class},
          'strong' => %w{id class},
          'table'   => %w{id class},
          'tbody'   => %w{id class},
          'tr'   => %w{id class},
          'td'   => %w{id class colspan}
        },
        protocols: {
          'a'   => {'href' => [:relative, 'http', 'https', 'mailto']},
          'img' => {'src'  => [:relative, 'http', 'https']}
        },
        output: :xhtml
      }

      Sanitize.clean(html.force_encoding("UTF-8"), sanitize_options)
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
softcover-1.0.beta7 lib/softcover/sanitizer.rb
softcover-1.0.beta6 lib/softcover/sanitizer.rb
softcover-1.0.beta5 lib/softcover/sanitizer.rb
softcover-1.0.beta4 lib/softcover/sanitizer.rb
softcover-1.0.beta3 lib/softcover/sanitizer.rb
softcover-nonstop-1.0.beta2 lib/softcover/sanitizer.rb
softcover-1.0.beta2 lib/softcover/sanitizer.rb
softcover-1.0.beta1 lib/softcover/sanitizer.rb
softcover-0.9.23 lib/softcover/sanitizer.rb
softcover-0.9.22 lib/softcover/sanitizer.rb
softcover-0.9.21 lib/softcover/sanitizer.rb
softcover-0.9.20 lib/softcover/sanitizer.rb
softcover-0.9.19 lib/softcover/sanitizer.rb
softcover-0.9.18 lib/softcover/sanitizer.rb
softcover-0.9.17 lib/softcover/sanitizer.rb
softcover-0.9.16 lib/softcover/sanitizer.rb
softcover-0.9.15 lib/softcover/sanitizer.rb
softcover-0.9.13 lib/softcover/sanitizer.rb
softcover-0.9.12 lib/softcover/sanitizer.rb
softcover-0.9.11 lib/softcover/sanitizer.rb