Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Inline escape_javascript from Rails - much easier than trying to pull it

in via a gem
  • Loading branch information...
commit 19ba1d5b1d8621e9fdff301c82fec726dcbb14eb 1 parent fb538b7
@bradleybuda bradleybuda authored
Showing with 29 additions and 7 deletions.
  1. +29 −6 lib/mixpanel/tracker.rb
  2. +0 −1  mixpanel.gemspec
View
35 lib/mixpanel/tracker.rb
@@ -4,10 +4,6 @@
require 'thread'
require 'base64'
-require 'active_support/concern'
-require 'action_view/helpers/capture_helper'
-require 'action_view/helpers/javascript_helper'
-
module Mixpanel
class Tracker
require 'mixpanel/async'
@@ -18,8 +14,6 @@ class Tracker
include Mixpanel::Event
include Mixpanel::Person
- include ActionView::Helpers::JavaScriptHelper
-
def initialize(token, options={})
@token = token
@async = !!options.fetch(:async, false)
@@ -134,5 +128,34 @@ def escape_object_for_js(object, i = 0)
end
end
+
+ # All this code borrowed from rails/action_pack - ActionView::Helpers::JavascriptHelper
+
+ JS_ESCAPE_MAP = {
+ '\\' => '\\\\',
+ '</' => '<\/',
+ "\r\n" => '\n',
+ "\n" => '\n',
+ "\r" => '\n',
+ '"' => '\\"',
+ "'" => "\\'"
+ }
+
+ JS_ESCAPE_MAP["\342\200\250".force_encoding(Encoding::UTF_8).encode!] = '&#x2028;'
+ JS_ESCAPE_MAP["\342\200\251".force_encoding(Encoding::UTF_8).encode!] = '&#x2029;'
+
+ # Escapes carriage returns and single and double quotes for JavaScript segments.
+ #
+ # Also available through the alias j(). This is particularly helpful in JavaScript
+ # responses, like:
+ #
+ # $('some_element').replaceWith('<%=j render 'some/element_template' %>');
+ def escape_javascript(javascript)
+ if javascript
+ javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) {|match| JS_ESCAPE_MAP[match] }
+ else
+ ''
+ end
+ end
end
end
View
1  mixpanel.gemspec
@@ -17,7 +17,6 @@ spec = Gem::Specification.new do |s|
s.add_dependency 'json'
s.add_dependency 'rack'
s.add_dependency 'escape'
- s.add_dependency 'actionpack'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'fakeweb'
Please sign in to comment.
Something went wrong with that request. Please try again.