Skip to content

Commit

Permalink
URI encode the X-Page-Title header
Browse files Browse the repository at this point in the history
Browsers's are inconsistent in how they interpret the encoding
of a response header in an XHR request, so URI encode it so that
it simple ASCII we can then decode it again in the browser.
  • Loading branch information
tomhughes committed Mar 17, 2015
1 parent 05caad1 commit e2aef40
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ $(document).ready(function () {

if (xhr.getResponseHeader('X-Page-Title')) {
var title = xhr.getResponseHeader('X-Page-Title');
document.title = decodeURIComponent(escape(title));
document.title = decodeURIComponent(title);
}

$('head')
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/title_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def self.coder
def set_title(title = false)
if title
@title = TitleHelper.coder.decode(title.gsub("<bdi>", "\u202a").gsub("</bdi>", "\u202c"))
response.headers["X-Page-Title"] = t("layouts.project_name.title") + " | " + @title
response.headers["X-Page-Title"] = URI.escape(t("layouts.project_name.title") + " | " + @title)
else
@title = title
response.headers["X-Page-Title"] = t("layouts.project_name.title")
response.headers["X-Page-Title"] = URI.escape(t("layouts.project_name.title"))
end
end
end
9 changes: 7 additions & 2 deletions test/helpers/title_helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
require "test_helper"

class TitleHelperTest < ActionView::TestCase
Expand All @@ -7,11 +8,15 @@ def test_set_title
assert_nil @title

set_title("Test Title")
assert_equal "OpenStreetMap | Test Title", response.header["X-Page-Title"]
assert_equal "OpenStreetMap%20%7C%20Test%20Title", response.header["X-Page-Title"]
assert_equal "Test Title", @title

set_title("Test & Title")
assert_equal "OpenStreetMap | Test & Title", response.header["X-Page-Title"]
assert_equal "OpenStreetMap%20%7C%20Test%20&%20Title", response.header["X-Page-Title"]
assert_equal "Test & Title", @title

set_title("Tést & Tïtlè")
assert_equal "OpenStreetMap%20%7C%20T%C3%A9st%20&%20T%C3%AFtl%C3%A8", response.header["X-Page-Title"]
assert_equal "Tést & Tïtlè", @title
end
end

0 comments on commit e2aef40

Please sign in to comment.