rails_javascript_helpers 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,10 @@
1
1
  module RailsJavaScriptHelpers
2
+
3
+ JS_ESCAPE_MAP = {"\""=>"\\\"", "\r"=>"\\n", "\\"=>"\\\\", "'"=>"\\'", "\r\n"=>"\\n", "</"=>"<\\/", "\n"=>"\\n"}
4
+
2
5
  # arg:: Object
3
6
  # Does a simple transition on types from Ruby to Javascript.
4
7
  def format_type_to_js(arg)
5
- puts '&' * 400
6
8
  case arg
7
9
  when Array
8
10
  "[#{arg.map{|value| format_type_to_js(value)}.join(',')}]"
@@ -20,7 +22,7 @@ module RailsJavaScriptHelpers
20
22
  when NilClass
21
23
  'null'
22
24
  else
23
- arg.to_s =~ %r{^\s*function\s*\(} ? "'#{escape_javascript(arg.to_s)}'" : "'#{escape_javascript(arg.to_s)}'"
25
+ arg.to_s =~ %r{^\s*function\s*\(} ? arg.to_s : "'#{custom_escape_javascript(arg.to_s)}'"
24
26
  end
25
27
  end
26
28
 
@@ -32,4 +34,15 @@ module RailsJavaScriptHelpers
32
34
  def format_id(id)
33
35
  id.to_s[0,1] =~ /[A-Za-z0-9]/ ? "##{id}" : id
34
36
  end
37
+
38
+ private
39
+
40
+ # Pulled from rails to keep consistency
41
+ def custom_escape_javascript(javascript)
42
+ if javascript
43
+ javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) {|part| JS_ESCAPE_MAP[part]}
44
+ else
45
+ ''
46
+ end
47
+ end
35
48
  end
@@ -1,3 +1,3 @@
1
1
  module RailsJavaScriptHelpers
2
- VERSION = '1.0'
2
+ VERSION = '1.1'
3
3
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_javascript_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
- version: "1.0"
8
+ - 1
9
+ version: "1.1"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Chris Roberts