From 415d4df648a9777e728cfa515ec4bb814adfca22 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 5 Nov 2013 13:40:39 +1100 Subject: [PATCH] work around bust encodings passed in to method --- app/helpers/application_helper.rb | 7 +++++++ spec/helpers/application_helper_spec.rb | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d1a1f4cf6..3a65bda5e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -26,6 +26,13 @@ module ApplicationHelper def escape_unicode(javascript) if javascript javascript = javascript.dup.force_encoding("utf-8") + + unless javascript.valid_encoding? + # work around bust string with a double conversion + javascript.encode!("utf-16","utf-8",:invalid => :replace) + javascript.encode!("utf-8","utf-16") + end + javascript.gsub!(/\342\200\250/u, '
') javascript.gsub!(/(<\/)/u, '\u003C/') javascript.html_safe diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 697f8d009..dbc92f925 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -2,7 +2,16 @@ require 'spec_helper' describe ApplicationHelper do - describe 'mobile_view?' do + describe "escape_unicode" do + it "encodes tags" do + helper.escape_unicode("").should == "\u003ctag>" + end + it "survives junk text" do + helper.escape_unicode("hello \xc3\x28 world").should =~ /hello.*world/ + end + end + + describe "mobile_view?" do context "enable_mobile_theme is true" do before do SiteSetting.stubs(:enable_mobile_theme).returns(true)