Skip to content

Commit

Permalink
Fixed bug with double HTML escaping in title separator
Browse files Browse the repository at this point in the history
  • Loading branch information
kpumuk committed Apr 26, 2011
1 parent 98c50e4 commit 31ab934
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/meta_tags/controller_helper.rb
Expand Up @@ -35,7 +35,6 @@ def set_meta_tags(meta_tags)
@meta_tags ||= {}
@meta_tags.merge!(meta_tags || {})
end

protected :set_meta_tags
end
end
Expand Down
17 changes: 12 additions & 5 deletions lib/meta_tags/view_helper.rb
Expand Up @@ -158,7 +158,7 @@ def display_meta_tags(default = {})
# Title
title = meta_tags[:title]
if meta_tags[:lowercase] === true and !title.blank?
title = [*title].map { |t| t.downcase }
title = Array(title).map { |t| t.downcase }
end

result = []
Expand All @@ -167,10 +167,13 @@ def display_meta_tags(default = {})
if title.blank?
result << content_tag(:title, meta_tags[:site])
else
title = normalize_title(title).unshift(meta_tags[:site])
title = normalize_title(title).unshift(h(meta_tags[:site]))
title.reverse! if meta_tags[:reverse] === true
sep = prefix + separator + suffix
result << content_tag(:title, title.join(sep))
sep = h(prefix) + h(separator) + h(suffix)
title = title.join(sep)
# We escaped every chunk of the title, so the whole title should be HTML safe
title = title.html_safe if title.respond_to?(:html_safe)
result << content_tag(:title, title)
end

# description
Expand Down Expand Up @@ -206,10 +209,14 @@ def display_meta_tags(default = {})
result.respond_to?(:html_safe) ? result.html_safe : result
end

if respond_to? :safe_helper
safe_helper :display_meta_tags
end

private

def normalize_title(title)
[*title].map { |t| h(strip_tags(t)) }
Array(title).map { |t| h(strip_tags(t)) }
end

def normalize_description(description)
Expand Down
3 changes: 2 additions & 1 deletion spec/meta_tags_spec.rb
Expand Up @@ -106,7 +106,8 @@
@view.title('someTitle')
@view.display_meta_tags(:site => 'someSite', :separator => '-').should == '<title>someSite - someTitle</title>'
@view.display_meta_tags(:site => 'someSite', :separator => ':').should == '<title>someSite : someTitle</title>'
@view.display_meta_tags(:site => 'someSite', :separator => '&mdash;').should == '<title>someSite &mdash; someTitle</title>'
@view.display_meta_tags(:site => 'someSite', :separator => '&mdash;').should == '<title>someSite &amp;mdash; someTitle</title>'
@view.display_meta_tags(:site => 'someSite', :separator => '&mdash;'.html_safe).should == '<title>someSite &mdash; someTitle</title>'
end

it 'should use custom prefix and suffix if available' do
Expand Down

0 comments on commit 31ab934

Please sign in to comment.