Class: Gollum::SiteSanitization

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum-site/sanitization.rb

Overview

Encapsulate sanitization options.

This class does not yet support all options of Sanitize library. See github.com/rgrove/sanitize/.

Constant Summary collapse

ELEMENTS =

Default whitelisted elements.

[
  'a', 'abbr', 'acronym', 'address', 'area', 'b', 'big',
  'blockquote', 'br', 'button', 'caption', 'center', 'cite',
  'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir',
  'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'form', 'h1',
  'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'img', 'input',
  'ins', 'kbd', 'label', 'legend', 'li', 'map', 'menu',
  'ol', 'optgroup', 'option', 'p', 'pre', 'q', 's', 'samp',
  'select', 'small', 'span', 'strike', 'strong', 'sub',
  'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th',
  'thead', 'tr', 'tt', 'u', 'ul', 'var', 'iframe'
]
ATTRIBUTES =

Default whitelisted attributes.

{
  'a'   => ['href'],
  'img' => ['src'],
  :all  => ['abbr', 'accept', 'accept-charset',
            'accesskey', 'action', 'align', 'alt', 'axis',
            'border', 'cellpadding', 'cellspacing', 'char',
            'charoff', 'class', 'charset', 'checked', 'cite',
            'clear', 'cols', 'colspan', 'color',
            'compact', 'coords', 'datetime', 'dir',
            'disabled', 'enctype', 'for', 'frame',
            'headers', 'height', 'hreflang',
            'hspace', 'ismap', 'label', 'lang',
            'longdesc', 'maxlength', 'media', 'method',
            'multiple', 'name', 'nohref', 'noshade',
            'nowrap', 'prompt', 'readonly', 'rel', 'rev',
            'rows', 'rowspan', 'rules', 'scope',
            'selected', 'shape', 'size', 'span',
            'start', 'summary', 'tabindex', 'target',
            'title', 'type', 'usemap', 'valign', 'value',
            'vspace', 'width', 'frameborder', 'id']
}.freeze
PROTOCOLS =

Default whitelisted protocols for URLs.

{
  'a'   => {'href' => ['http', 'https', 'mailto', :relative]},
  'img' => {'src'  => ['http', 'https', :relative]}
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ SiteSanitization

Returns a new instance of SiteSanitization.

Yields:

  • (_self)

Yield Parameters:



69
70
71
72
73
74
75
76
# File 'lib/gollum-site/sanitization.rb', line 69

def initialize
  @elements       = ELEMENTS
  @attributes     = ATTRIBUTES
  @protocols      = PROTOCOLS
  @add_attributes = {}
  @allow_comments = false
  yield self if block_given?
end

Instance Attribute Details

#add_attributesObject (readonly)

Gets a Hash describing HTML attributes that Sanitize should add. Default: {}



63
64
65
# File 'lib/gollum-site/sanitization.rb', line 63

def add_attributes
  @add_attributes
end

#allow_comments=(value) ⇒ Object (writeonly)

Sets a boolean determining whether Sanitize allows HTML comments in the output. Default: false.



67
68
69
# File 'lib/gollum-site/sanitization.rb', line 67

def allow_comments=(value)
  @allow_comments = value
end

#attributesObject (readonly)

Gets a Hash describing which attributes are allowed in which HTML elements. Default: ATTRIBUTES.



55
56
57
# File 'lib/gollum-site/sanitization.rb', line 55

def attributes
  @attributes
end

#elementsObject (readonly)

Gets an Array of whitelisted HTML elements. Default: ELEMENTS.



51
52
53
# File 'lib/gollum-site/sanitization.rb', line 51

def elements
  @elements
end

#protocolsObject (readonly)

Gets a Hash describing which URI protocols are allowed in HTML attributes. Default: PROTOCOLS



59
60
61
# File 'lib/gollum-site/sanitization.rb', line 59

def protocols
  @protocols
end

Instance Method Details

#allow_comments?Boolean

Determines if Sanitize should allow HTML comments.

Returns True if comments are allowed, or False.

Returns:

  • (Boolean)


81
82
83
# File 'lib/gollum-site/sanitization.rb', line 81

def allow_comments?
  !!@allow_comments
end

#history_sanitizationObject

Modifies the current Sanitization instance to sanitize older revisions of pages.

Returns a Sanitization instance.



89
90
91
92
93
# File 'lib/gollum-site/sanitization.rb', line 89

def history_sanitization
  self.class.new do |sanitize|
    sanitize.add_attributes['a'] = {'rel' => 'nofollow'}
  end
end

#to_hashObject

Builds a Hash of options suitable for Sanitize.clean.

Returns a Hash.



98
99
100
101
102
103
104
105
# File 'lib/gollum-site/sanitization.rb', line 98

def to_hash
  { :elements       => elements,
    :attributes     => attributes,
    :protocols      => protocols,
    :add_attributes => add_attributes,
    :allow_comments => allow_comments?
  }
end

#to_sanitizeObject

Builds a Sanitize instance from the current options.

Returns a Sanitize instance.



110
111
112
# File 'lib/gollum-site/sanitization.rb', line 110

def to_sanitize
  Sanitize.new(to_hash)
end