Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate title before escaping HTML characters #180

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/meta_tags/text_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ def helpers
def strip_tags(string)
if defined?(Loofah)
# Instead of strip_tags we will use Loofah to strip tags from now on
stripped_unescaped = Loofah.fragment(string).text(encode_special_chars: false)
ERB::Util.html_escape stripped_unescaped
Loofah.fragment(string).text(encode_special_chars: false)
else
ERB::Util.html_escape helpers.strip_tags(string)
helpers.strip_tags(string)
end
end

Expand Down Expand Up @@ -133,7 +132,7 @@ def truncate(string, limit = nil, natural_separator = ' ')
length: limit,
separator: natural_separator,
omission: '',
escape: false,
escape: true,
)
end

Expand Down
6 changes: 6 additions & 0 deletions spec/text_normalizer/normalize_title_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
MetaTags.config.title_limit = nil
expect(subject.normalize_title(site_title, title, '-')).to eq("#{site_title}-#{title}")
end

it 'should truncate title when limit is reached on unescaped value' do
site_title = 'a' * 20
title = '"' * (MetaTags.config.title_limit + 10)
expect(subject.normalize_title(site_title, title, '-')).to eq("#{site_title}-#{'"' * (MetaTags.config.title_limit - 21)}")
end
end

context 'when truncate_site_title_first is true' do
Expand Down
4 changes: 2 additions & 2 deletions spec/view_helper/title_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
it 'should escape a very long title when "set_meta_tags" is used' do
subject.set_meta_tags(title: 'Kombucha kale chips forage try-hard & green juice. IPhone marfa PBR&B venmo listicle, irony kitsch thundercats.')
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to eq('<title>someSite | Kombucha kale chips forage try-hard &amp; green juice.</title>')
expect(meta).to eq('<title>someSite | Kombucha kale chips forage try-hard &amp; green juice. IPhone</title>')
end
end

Expand All @@ -61,7 +61,7 @@
it 'should strip tags from very long titles' do
subject.set_meta_tags(title: 'Kombucha <b>kale</b> chips forage try-hard & green juice. IPhone marfa PBR&B venmo listicle, irony kitsch thundercats.')
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to eq('<title>someSite | Kombucha kale chips forage try-hard &amp; green juice.</title>')
expect(meta).to eq('<title>someSite | Kombucha kale chips forage try-hard &amp; green juice. IPhone</title>')
end
end

Expand Down