Sha256: c6362fd6fd8c775de53a19874e3e81f9d5887b83993d0e77ce69a5c07d282cc8

Contents?: true

Size: 1.65 KB

Versions: 27

Compression:

Stored size: 1.65 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

      # Make a whitelist of acceptable elements and attributes.
      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
                     blockquote small},
        remove_contents: %w{script},
        attributes: {
          'div' => %w{id class data-tralics-id data-number data-chapter},
          'a'    => %w{id class href target rel},
          '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},
          'blockquote' => %w{id class},
          'strong' => %w{id class},
          'table'   => %w{id class},
          'tbody'   => %w{id class},
          'tr'   => %w{id class},
          'td'   => %w{id class colspan}
        },
        css: {
          properties: %w{color height width}
        },
        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

27 entries across 27 versions & 1 rubygems

Version Path
softcover-1.5.9 lib/softcover/sanitizer.rb
softcover-1.5.8 lib/softcover/sanitizer.rb
softcover-1.5.7 lib/softcover/sanitizer.rb
softcover-1.5.6 lib/softcover/sanitizer.rb
softcover-1.5.5 lib/softcover/sanitizer.rb
softcover-1.5.4 lib/softcover/sanitizer.rb
softcover-1.5.3 lib/softcover/sanitizer.rb
softcover-1.5.2 lib/softcover/sanitizer.rb
softcover-1.5.1 lib/softcover/sanitizer.rb
softcover-1.5.0 lib/softcover/sanitizer.rb
softcover-1.4.9 lib/softcover/sanitizer.rb
softcover-1.4.8 lib/softcover/sanitizer.rb
softcover-1.4.7 lib/softcover/sanitizer.rb
softcover-1.4.6 lib/softcover/sanitizer.rb
softcover-1.4.5 lib/softcover/sanitizer.rb
softcover-1.4.3 lib/softcover/sanitizer.rb
softcover-1.4.2 lib/softcover/sanitizer.rb
softcover-1.4.1 lib/softcover/sanitizer.rb
softcover-1.4.0 lib/softcover/sanitizer.rb
softcover-1.3.4 lib/softcover/sanitizer.rb