Class: Gollum::Sanitization
- Inherits:
-
Object
- Object
- Gollum::Sanitization
- Defined in:
- lib/gollum/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' ].freeze
- 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'] }.freeze
- PROTOCOLS =
Default whitelisted protocols for URLs.
{ 'a' => {'href' => ['http', 'https', 'mailto', 'ftp', 'irc', :relative]}, 'img' => {'src' => ['http', 'https', :relative]} }.freeze
- ADD_ATTRIBUTES =
lambda do |env, node| if add = env[:config][:add_attributes][node.name] add.each do |key, value| node[key] = value end end end
- REMOVE_CONTENTS =
Default elements whose contents will be removed in addition to the elements themselve
[ 'script', 'style' ].freeze
- TRANSFORMERS =
Default transformers to force @id attributes with ‘wiki-’ prefix
lambda do |env| node = env[:node] return if env[:is_whitelisted] || !node.element? prefix = env[:config][:id_prefix] found_attrs = %w(id name).select do |key| if value = node[key] node[key] = value.gsub(/\A(#{prefix})?/, prefix) end end if found_attrs.size > 0 ADD_ATTRIBUTES.call(env, node) {} end end, lambda do |env| node = env[:node] return unless value = node['href'] prefix = env[:config][:id_prefix] node['href'] = value.gsub(/\A\#(#{prefix})?/, '#'+prefix) ADD_ATTRIBUTES.call(env, node) {} end ].freeze
Instance Attribute Summary collapse
-
#add_attributes ⇒ Object
readonly
Gets a Hash describing HTML attributes that Sanitize should add.
-
#allow_comments ⇒ Object
writeonly
Sets a boolean determining whether Sanitize allows HTML comments in the output.
-
#attributes ⇒ Object
readonly
Gets a Hash describing which attributes are allowed in which HTML elements.
-
#elements ⇒ Object
readonly
Gets an Array of whitelisted HTML elements.
-
#id_prefix ⇒ Object
Gets or sets a String prefix which is added to ID attributes.
-
#protocols ⇒ Object
readonly
Gets a Hash describing which URI protocols are allowed in HTML attributes.
-
#remove_contents ⇒ Object
readonly
Gets an Array of element names whose contents will be removed in addition to the elements themselves.
-
#transformers ⇒ Object
readonly
Gets a Hash describing which URI protocols are allowed in HTML attributes.
Instance Method Summary collapse
-
#allow_comments? ⇒ Boolean
Determines if Sanitize should allow HTML comments.
-
#history_sanitization ⇒ Object
Modifies the current Sanitization instance to sanitize older revisions of pages.
-
#initialize {|_self| ... } ⇒ Sanitization
constructor
A new instance of Sanitization.
-
#to_hash ⇒ Object
Builds a Hash of options suitable for Sanitize.clean.
-
#to_sanitize ⇒ Object
Builds a Sanitize instance from the current options.
Constructor Details
#initialize {|_self| ... } ⇒ Sanitization
Returns a new instance of Sanitization.
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gollum/sanitization.rb', line 122 def initialize @elements = ELEMENTS @attributes = ATTRIBUTES @protocols = PROTOCOLS @transformers = TRANSFORMERS @add_attributes = {} @remove_contents = REMOVE_CONTENTS @allow_comments = false @id_prefix = 'wiki-' yield self if block_given? end |
Instance Attribute Details
#add_attributes ⇒ Object (readonly)
Gets a Hash describing HTML attributes that Sanitize should add. Default: {}
112 113 114 |
# File 'lib/gollum/sanitization.rb', line 112 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.
120 121 122 |
# File 'lib/gollum/sanitization.rb', line 120 def allow_comments=(value) @allow_comments = value end |
#attributes ⇒ Object (readonly)
Gets a Hash describing which attributes are allowed in which HTML elements. Default: ATTRIBUTES.
96 97 98 |
# File 'lib/gollum/sanitization.rb', line 96 def attributes @attributes end |
#elements ⇒ Object (readonly)
Gets an Array of whitelisted HTML elements. Default: ELEMENTS.
92 93 94 |
# File 'lib/gollum/sanitization.rb', line 92 def elements @elements end |
#id_prefix ⇒ Object
Gets or sets a String prefix which is added to ID attributes. Default: ‘wiki-’
108 109 110 |
# File 'lib/gollum/sanitization.rb', line 108 def id_prefix @id_prefix end |
#protocols ⇒ Object (readonly)
Gets a Hash describing which URI protocols are allowed in HTML attributes. Default: PROTOCOLS
100 101 102 |
# File 'lib/gollum/sanitization.rb', line 100 def protocols @protocols end |
#remove_contents ⇒ Object (readonly)
Gets an Array of element names whose contents will be removed in addition to the elements themselves. Default: REMOVE_CONTENTS
116 117 118 |
# File 'lib/gollum/sanitization.rb', line 116 def remove_contents @remove_contents end |
#transformers ⇒ Object (readonly)
Gets a Hash describing which URI protocols are allowed in HTML attributes. Default: TRANSFORMERS
104 105 106 |
# File 'lib/gollum/sanitization.rb', line 104 def transformers @transformers end |
Instance Method Details
#allow_comments? ⇒ Boolean
Determines if Sanitize should allow HTML comments.
Returns True if comments are allowed, or False.
137 138 139 |
# File 'lib/gollum/sanitization.rb', line 137 def allow_comments? !!@allow_comments end |
#history_sanitization ⇒ Object
Modifies the current Sanitization instance to sanitize older revisions of pages.
Returns a Sanitization instance.
145 146 147 148 149 |
# File 'lib/gollum/sanitization.rb', line 145 def history_sanitization self.class.new do |sanitize| sanitize.add_attributes['a'] = {'rel' => 'nofollow'} end end |
#to_hash ⇒ Object
Builds a Hash of options suitable for Sanitize.clean.
Returns a Hash.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/gollum/sanitization.rb', line 154 def to_hash { :elements => elements, :attributes => attributes, :protocols => protocols, :add_attributes => add_attributes, :remove_contents => remove_contents, :allow_comments => allow_comments?, :transformers => transformers, :id_prefix => id_prefix } end |
#to_sanitize ⇒ Object
Builds a Sanitize instance from the current options.
Returns a Sanitize instance.
169 170 171 |
# File 'lib/gollum/sanitization.rb', line 169 def to_sanitize Sanitize.new(to_hash) end |