rails_javascript_helpers 1.1 → 1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,17 @@
1
- module RailsJavaScriptHelpers
1
+ require 'action_view/helpers/javascript_helper'
2
2
 
3
- JS_ESCAPE_MAP = {"\""=>"\\\"", "\r"=>"\\n", "\\"=>"\\\\", "'"=>"\\'", "\r\n"=>"\\n", "</"=>"<\\/", "\n"=>"\\n"}
3
+ module RailsJavaScriptHelpers
4
4
 
5
5
  # arg:: Object
6
6
  # Does a simple transition on types from Ruby to Javascript.
7
7
  def format_type_to_js(arg)
8
+ @klass ||= Class.new.extend(ActionView::Helpers::JavaScriptHelper)
8
9
  case arg
9
10
  when Array
10
11
  "[#{arg.map{|value| format_type_to_js(value)}.join(',')}]"
11
12
  when Hash
12
13
  "{#{arg.map{ |key, value|
13
- k = key.is_a?(Symbol) ? key.to_s.camelize.sub(/^./, key.to_s[0,1].downcase) : key.to_s
14
+ k = key.is_a?(Symbol) ? key.to_s.camelize.sub(/^./, key.to_s[0,1].downcase) : format_type_to_js(key)
14
15
  "#{k}:#{format_type_to_js(value)}"
15
16
  }.join(',')}}"
16
17
  when Fixnum
@@ -22,7 +23,7 @@ module RailsJavaScriptHelpers
22
23
  when NilClass
23
24
  'null'
24
25
  else
25
- arg.to_s =~ %r{^\s*function\s*\(} ? arg.to_s : "'#{custom_escape_javascript(arg.to_s)}'"
26
+ arg.to_s =~ %r{^\s*function\s*\(} ? arg.to_s : "'#{escape_javascript(arg.to_s)}'"
26
27
  end
27
28
  end
28
29
 
@@ -35,14 +36,4 @@ module RailsJavaScriptHelpers
35
36
  id.to_s[0,1] =~ /[A-Za-z0-9]/ ? "##{id}" : id
36
37
  end
37
38
 
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
48
39
  end
@@ -1,3 +1,3 @@
1
1
  module RailsJavaScriptHelpers
2
- VERSION = '1.1'
2
+ VERSION = '1.2'
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: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
9
- version: "1.1"
8
+ - 2
9
+ version: "1.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Chris Roberts
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-10 00:00:00 -07:00
17
+ date: 2011-06-13 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency