gon 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gon might be problematic. Click here for more details.

data/README.md CHANGED
@@ -92,6 +92,16 @@ You can initialize window.gon = {}; on each request
92
92
  ...
93
93
  ```
94
94
 
95
+ You can initialize script tag with type="text/javascript"
96
+
97
+ ``` erb
98
+ <head>
99
+ <title>some title</title>
100
+ <%= include_gon(:need_type => true) %>
101
+ <!-- include your action js code with 'serverExports' namespace -->
102
+ ...
103
+ ```
104
+
95
105
  You can get json without script tag (kudos to @afa):
96
106
 
97
107
  ``` erb
@@ -349,13 +359,13 @@ Thats it!
349
359
  Puts this line into `Gemfile` then run `$ bundle`:
350
360
 
351
361
  ``` ruby
352
- gem 'gon', '3.0.4'
362
+ gem 'gon', '3.0.5'
353
363
  ```
354
364
 
355
365
  Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
356
366
 
357
367
  ``` ruby
358
- config.gem 'gon', :version => '3.0.4'
368
+ config.gem 'gon', :version => '3.0.5'
359
369
  ```
360
370
 
361
371
  Or manually install gon gem: `$ gem install gon`
@@ -7,25 +7,20 @@ module Gon
7
7
  if Gon.global.all_variables.present?
8
8
  data[:global] = Gon.global.all_variables
9
9
  end
10
- namespace = options[:namespace] || 'gon'
11
- need_tag = options[:need_tag].nil? || options[:need_tag]
12
- start = "#{need_tag ? '<script>' : ''}window.#{namespace} = {};"
13
- script = ''
14
-
15
- if data.present?
16
- if options[:camel_case]
17
- data.each do |key, val|
18
- script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
19
- end
10
+ namespace, tag, cameled = parse_options options
11
+ start = "#{tag if tag}window.#{namespace} = {};"
12
+ script = ''
13
+
14
+ data.each do |key, val|
15
+ if cameled
16
+ script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
20
17
  else
21
- data.each do |key, val|
22
- script << "#{namespace}.#{key.to_s}=#{val.to_json};"
23
- end
18
+ script << "#{namespace}.#{key.to_s}=#{val.to_json};"
24
19
  end
25
20
  end
26
21
 
27
22
  script = start + Gon::Escaper.escape(script)
28
- script << '</script>' if need_tag
23
+ script << '</script>' if tag
29
24
  script.html_safe
30
25
  end
31
26
 
@@ -53,6 +48,16 @@ module Gon
53
48
 
54
49
  private
55
50
 
51
+ def parse_options(options)
52
+ namespace = options[:namespace] || 'gon'
53
+ need_tag = options[:need_tag].nil? || options[:need_tag]
54
+ need_type = options[:need_type].present? && options[:need_type]
55
+ cameled = options[:camel_case]
56
+ tag = need_tag && (need_type ? '<script type="text/javascript">' : '<script>')
57
+
58
+ [namespace, tag, cameled]
59
+ end
60
+
56
61
  def right_extension?(extension, template_path)
57
62
  File.extname(template_path) == ".#{extension}"
58
63
  end
@@ -3,7 +3,7 @@ module Gon
3
3
  class << self
4
4
 
5
5
  GON_JS_ESCAPE_MAP = {
6
- '</' => '<\/'
6
+ '</' => '\u003C/'
7
7
  }
8
8
 
9
9
  def escape(javascript)
@@ -24,7 +24,7 @@ module Gon
24
24
  end
25
25
 
26
26
  def clear
27
- env && (env[:gon] = {})
27
+ env && (env['gon'] = {})
28
28
  end
29
29
 
30
30
  end
@@ -1,3 +1,3 @@
1
1
  module Gon
2
- VERSION = '3.0.4'
2
+ VERSION = '3.0.5'
3
3
  end
@@ -18,13 +18,13 @@ describe Gon do
18
18
 
19
19
  it 'supports all data types' do
20
20
  Gon.clear
21
- Gon.int = 1
22
- Gon.float = 1.1
23
- Gon.string = 'string'
24
- Gon.array = [ 1, 'string' ]
25
- Gon.hash_var = { :a => 1, :b => '2'}
26
- Gon.hash_w_array = { :a => [ 2, 3 ] }
27
- Gon.klass = Hash
21
+ Gon.int = 1
22
+ Gon.float = 1.1
23
+ Gon.string = 'string'
24
+ Gon.array = [ 1, 'string' ]
25
+ Gon.hash_var = { :a => 1, :b => '2'}
26
+ Gon.hash_w_array = { :a => [ 2, 3 ] }
27
+ Gon.klass = Hash
28
28
  end
29
29
 
30
30
  end
@@ -60,7 +60,7 @@ describe Gon do
60
60
  it 'outputs correct js with a script string' do
61
61
  Gon.str = %q(</script><script>alert('!')</script>)
62
62
  @base.include_gon.should == '<script>window.gon = {};' +
63
- %q(gon.str="<\\/script><script>alert('!')<\\/script>";) +
63
+ %q(gon.str="\\u003C/script><script>alert('!')\\u003C/script>";) +
64
64
  '</script>'
65
65
  end
66
66
 
@@ -79,19 +79,33 @@ describe Gon do
79
79
  'gon.int=1;'
80
80
  end
81
81
 
82
-
83
- it 'outputs correct js without variables, without tag and gon init' do
82
+ it 'outputs correct js without variables, without tag and gon init if before there was data' do
83
+ Gon::Request.
84
+ instance_variable_set(:@request_id, 123)
85
+ Gon::Request.instance_variable_set(:@request_env, { 'gon' => { :a => 1 } })
84
86
  @base.include_gon(need_tag: false, init: true).should == \
85
87
  'window.gon = {};'
86
88
  end
87
89
 
88
90
  it 'outputs correct js without variables, without tag and gon init' do
91
+ @base.include_gon(need_tag: false, init: true).should == \
92
+ 'window.gon = {};'
93
+ end
94
+
95
+ it 'outputs correct js without variables, without tag, gon init and an integer' do
89
96
  Gon.int = 1
90
97
  @base.include_gon(need_tag: false, init: true).should == \
91
98
  'window.gon = {};' +
92
99
  'gon.int=1;'
93
100
  end
94
101
 
102
+ it 'outputs correct js with type text/javascript' do
103
+ @base.include_gon(need_type: true, init: true).should == \
104
+ '<script type="text/javascript">' +
105
+ 'window.gon = {};'\
106
+ '</script>'
107
+ end
108
+
95
109
  end
96
110
 
97
111
  it 'returns exception if try to set public method as variable' do
@@ -72,7 +72,7 @@ describe Gon::Global do
72
72
  it 'outputs correct js with a script string' do
73
73
  Gon.global.str = %q(</script><script>alert('!')</script>)
74
74
  @base.include_gon.should == "<script>window.gon = {};" +
75
- "gon.global={\"str\":\"<\\/script><script>alert('!')<\\/script>\"};" +
75
+ "gon.global={\"str\":\"\\u003C/script><script>alert('!')\\u003C/script>\"};" +
76
76
  "</script>"
77
77
  end
78
78
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gon
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.4
4
+ version: 3.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-02 00:00:00.000000000 Z
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  version: '0'
165
165
  requirements: []
166
166
  rubyforge_project: gon
167
- rubygems_version: 1.8.23
167
+ rubygems_version: 1.8.24
168
168
  signing_key:
169
169
  specification_version: 3
170
170
  summary: Get your Rails variables in your JS