]> git.openstreetmap.org Git - rails.git/commitdiff
URI encode the X-Page-Title header
authorTom Hughes <tom@compton.nu>
Tue, 17 Mar 2015 18:49:39 +0000 (18:49 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 17 Mar 2015 18:49:39 +0000 (18:49 +0000)
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.

app/assets/javascripts/index.js
app/helpers/title_helper.rb
test/helpers/title_helper_test.rb

index 17476bb515633cc14eed42d0cd8129bca0ed21de..085b615c7244f66214f2cddee69f89400c28d3d0 100644 (file)
@@ -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')
index 3103a339f91c0a3c555586dd9b6b55931717d927..eb1724d93aac343ae23a559111b7317a3b4785fd 100644 (file)
@@ -8,10 +8,10 @@ module TitleHelper
   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
index 2a3cbc46e3803cbde109a4321e820fb702b85a56..d07a5bd0707dc9cdb6698f3e99753638a1a1d183 100644 (file)
@@ -1,3 +1,4 @@
+# coding: utf-8
 require "test_helper"
 
 class TitleHelperTest < ActionView::TestCase
@@ -7,11 +8,15 @@ class TitleHelperTest < ActionView::TestCase
     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