Skip to content
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
14 changes: 8 additions & 6 deletions lib/flutie/page_title_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module PageTitleHelper
def page_title(options = {})
page_title = Flutie::PageTitle.new(options)

Flutie::PageTitlePresenter.new(
page_title.app_name,
content_for(page_title.page_title_symbol),
page_title.separator,
options[:reverse]
).to_s
escape_once(
Flutie::PageTitlePresenter.new(
page_title.app_name,
content_for(page_title.page_title_symbol),
page_title.separator,
options[:reverse],
).to_s,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neilvcarvalho

there shouldn't be any backwards compatibility issue in making flutie return HTML-safe strings

My first solution was calling .html_safe here and it worked, but only for the page title content. To make sure custom separator and custom app_name are also html-escaped, and once, I think we need to go with escape_once

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, that's a great point. I hadn't thought of separators and app names needing to be escaped, as in "Johnson & Johnson > São Paulo Headquarters". TIL escape_once.

).html_safe
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails/OutputSafety: Tagging a string as html safe may be a security risk.

end
end
27 changes: 27 additions & 0 deletions spec/helpers/page_title_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,57 @@
describe PageTitleHelper, type: :helper do
describe 'with no options' do
subject { helper.page_title }

it { should == 'Flutie' }
end

describe 'with a site name' do
subject { helper.page_title(app_name: 'Test') }

it { should == 'Test' }
end

describe 'with content for page title' do
before { helper.content_for(:page_title, 'page title') }
subject { helper.page_title }

it { should == 'Flutie : page title' }
end

describe 'with content for page title, and an alternate separator' do
before { helper.content_for(:page_title, 'page title') }
subject { helper.page_title(separator: ' | ') }

it { should == 'Flutie | page title' }
end

describe 'with an alternate symbol' do
before { helper.content_for(:alt_page_title, 'page title') }
subject { helper.page_title(page_title_symbol: :alt_page_title) }

it { should == 'Flutie : page title' }
end

describe 'in a reversed position' do
before { helper.content_for(:page_title, 'page title') }
subject { helper.page_title(reverse: true) }

it { should == 'page title : Flutie' }
end

it "html-escapes" do
helper.content_for(:page_title, 'Page & "Title"')

expect(
helper.page_title(app_name: "Flutie's", separator: " > "),
).to eq("Flutie's > Page & "Title"")
end

it "html-escapes only once when rendering" do
helper.content_for(:page_title, 'Page & "Title"')

render(inline: '<%= page_title(app_name: "Flutie\'s", separator: " > ") %>')

expect(rendered).to eq("Flutie&#39;s &gt; Page &amp; &quot;Title&quot;")
end
end