Move unescape_html_entities from LabelsHelper to Label model

This commit is contained in:
Douglas Barbosa Alexandre 2016-06-27 17:08:39 -03:00
parent ab811b6ab9
commit d6b60e83ed
4 changed files with 13 additions and 19 deletions

View file

@ -1,12 +1,6 @@
module LabelsHelper
include ActionView::Helpers::TagHelper
TABLE_FOR_ESCAPE_HTML_ENTITIES = {
'&' => '&',
'<' => '&lt;',
'>' => '&gt;'
}
# Link to a Label
#
# label - Label object to link to
@ -136,11 +130,7 @@ module LabelsHelper
label.subscribed?(current_user) ? 'Unsubscribe' : 'Subscribe'
end
def unescape_html_entities(value)
value.to_s.gsub(/(&gt;)|(&lt;)|(&amp;)/, TABLE_FOR_ESCAPE_HTML_ENTITIES.invert)
end
# Required for Banzai::Filter::LabelReferenceFilter
module_function :render_colored_label, :render_colored_cross_project_label,
:text_color_for_bg, :escape_once, :unescape_html_entities
:text_color_for_bg, :escape_once
end

View file

@ -10,6 +10,12 @@ class Label < ActiveRecord::Base
DEFAULT_COLOR = '#428BCA'
TABLE_FOR_ESCAPE_HTML_ENTITIES = {
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;'
}
default_value_for :color, DEFAULT_COLOR
belongs_to :project
@ -134,6 +140,10 @@ class Label < ActiveRecord::Base
end
def sanitize_title(value)
LabelsHelper.unescape_html_entities(Sanitize.clean(value.to_s))
unescape_html_entities(Sanitize.clean(value.to_s))
end
def unescape_html_entities(value)
value.to_s.gsub(/(&gt;)|(&lt;)|(&amp;)/, TABLE_FOR_ESCAPE_HTML_ENTITIES.invert)
end
end

View file

@ -68,7 +68,7 @@ module Banzai
end
def unescape_html_entities(text)
LabelsHelper.unescape_html_entities(text)
text.to_s.gsub(/(&gt;)|(&lt;)|(&amp;)/, Label::TABLE_FOR_ESCAPE_HTML_ENTITIES.invert)
end
end
end

View file

@ -77,10 +77,4 @@ describe LabelsHelper do
expect(text_color_for_bg('#000')).to eq '#FFFFFF'
end
end
describe 'unescape_html_entities' do
it 'decodes &, <, and > named entities' do
expect(unescape_html_entities('foo &amp; bar &lt; zoo &gt; boo &eacute;')).to eq 'foo & bar < zoo > boo &eacute;'
end
end
end