Skip to content

Commit 8a6e545

Browse files
committed
Merge branch 'master' into moktins_master
2 parents ed37cb6 + d569dc3 commit 8a6e545

File tree

9 files changed

+92
-22
lines changed

9 files changed

+92
-22
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: ruby
2+
rvm:
3+
- 1.8.7
4+
- 1.9.2
5+
- 1.9.3
6+
- jruby-18mode # JRuby in 1.8 mode
7+
- jruby-19mode # JRuby in 1.9 mode
8+
- rbx-18mode
9+
- rbx-19mode
10+
# uncomment this line if your project needs to run something other than `rake`:
11+
# script: bundle exec rspec spec

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
![Gon. You should try this. If you look closer - you will see an elephant.](https://github.com/gazay/gon/raw/master/doc/logo_small.png)
44

55

6+
### Build Status ![http://travis-ci.org/gazay/gon](https://secure.travis-ci.org/gazay/gon.png)
7+
68
If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!
79

810
Now with [Jbuilder](https://github.com/rails/jbuilder) and [Rabl](https://github.com/nesquena/rabl) support!
@@ -24,7 +26,7 @@ you might be doing something like this:
2426
3. Then there can be two ways in js:
2527
+ if you previously wrote data in data
2628
attributes - you should parse this attributes and write data to some
27-
js variable.
29+
js variable.
2830
+ if you wrote js right in view (many frontenders would shame you for
2931
that) - you just use data from this js - OK.
3032
4. You can use your data in your js
@@ -285,13 +287,13 @@ wouldn't work. You can read about this in common usage above.
285287
Puts this line into `Gemfile` then run `$ bundle`:
286288

287289
``` ruby
288-
gem 'gon', '2.1.0'
290+
gem 'gon', '2.1.2'
289291
```
290292

291293
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
292294

293295
``` ruby
294-
config.gem 'gon', :version => '2.1.0'
296+
config.gem 'gon', :version => '2.1.2'
295297
```
296298

297299
Or manually install gon gem: `$ gem install gon`

Rakefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
require 'rubygems'
22
require 'rake'
33
require 'bundler'
4-
Bundler::GemHelper.install_tasks
4+
Bundler::GemHelper.install_tasks
5+
6+
desc 'Run all tests by default'
7+
task :default do
8+
system("rspec spec")
9+
end

gon.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ Gem::Specification.new do |s|
2323
s.add_development_dependency "rabl"
2424
s.add_development_dependency "rspec"
2525
s.add_development_dependency "jbuilder"
26+
s.add_development_dependency "rake"
2627
end

lib/gon.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'action_view'
66
require 'action_controller'
77
require 'gon/helpers'
8+
require 'gon/escaper'
89
if defined?(Rabl)
910
require 'gon/rabl'
1011
end
@@ -16,16 +17,16 @@ module Gon
1617
class << self
1718

1819
def all_variables
19-
@request_env[:gon]
20+
@request_env['gon']
2021
end
2122

2223
def clear
23-
@request_env[:gon] = {}
24+
@request_env['gon'] = {}
2425
end
2526

2627
def request_env=(environment)
2728
@request_env = environment
28-
@request_env[:gon] ||= {}
29+
@request_env['gon'] ||= {}
2930
end
3031

3132
def request_env
@@ -54,11 +55,11 @@ def method_missing(m, *args, &block)
5455
end
5556

5657
def get_variable(name)
57-
@request_env[:gon][name]
58+
@request_env['gon'][name]
5859
end
5960

6061
def set_variable(name, value)
61-
@request_env[:gon][name] = value
62+
@request_env['gon'][name] = value
6263
end
6364

6465
%w(rabl jbuilder).each do |builder_name|

lib/gon/escaper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Gon
2+
module Escaper
3+
class << self
4+
5+
GON_JS_ESCAPE_MAP = {
6+
'</' => '<\/'
7+
}
8+
9+
def escape(javascript)
10+
if javascript
11+
result = javascript.gsub(/(<\/)/u) {|match| GON_JS_ESCAPE_MAP[match] }
12+
javascript.html_safe? ? result.html_safe : result
13+
else
14+
''
15+
end
16+
end
17+
18+
end
19+
end
20+
end

lib/gon/helpers.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ def self.included base
66
end
77

88
module InstanceMethods
9+
910
def include_gon(options = {})
10-
if Gon.request_env && Gon.all_variables.present?
11+
if Gon.request_env && Gon.all_variables.present? && Gon.request == request.object_id
1112
data = Gon.all_variables
1213
namespace = options[:namespace] || 'gon'
13-
script = '<script>window.' + namespace + ' = {};'
14+
start = '<script>window.' + namespace + ' = {};'
15+
script = ''
1416
if options[:camel_case]
1517
data.each do |key, val|
1618
script << namespace + '.' + key.to_s.camelize(:lower) + '=' + val.to_json + ';'
@@ -20,31 +22,35 @@ def include_gon(options = {})
2022
script << namespace + '.' + key.to_s + '=' + val.to_json + ';'
2123
end
2224
end
23-
script << '</script>'
25+
script = start + Gon::Escaper.escape(script) + '</script>'
2426
script.html_safe
2527
else
2628
""
2729
end
2830
end
31+
2932
end
3033
end
3134

3235
module GonHelpers
36+
3337
def self.included base
3438
base.send(:include, InstanceMethods)
3539
end
3640

3741
module InstanceMethods
42+
3843
def gon
3944
if !Gon.request_env || Gon.request != request.object_id
4045
Gon.request = request.object_id
4146
Gon.request_env = request.env
4247
end
4348
Gon
4449
end
50+
4551
end
46-
end
4752

53+
end
4854
end
4955

5056
ActionView::Base.send :include, Gon::Helpers

lib/gon/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Gon
2-
VERSION = '2.1.0'
2+
VERSION = '2.1.2'
33
end

spec/gon/gon_spec.rb

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,43 @@
2626
Gon.klass = Hash
2727
end
2828

29-
it 'output as js correct' do
30-
Gon.clear
31-
Gon.int = 1
32-
ActionView::Base.instance_methods.map(&:to_s).include?('include_gon').should == true
33-
base = ActionView::Base.new
34-
base.include_gon.should == "<script>window.gon = {};" +
35-
"gon.int=1;" +
36-
"</script>"
29+
describe '#include_gon' do
30+
31+
before(:each) do
32+
Gon.clear
33+
Gon.instance_variable_set(:@request_id, request.object_id)
34+
ActionView::Base.instance_methods.map(&:to_s).include?('include_gon').should == true
35+
@base = ActionView::Base.new
36+
@base.request = request
37+
end
38+
39+
it 'outputs correct js with an integer' do
40+
Gon.int = 1
41+
@base.include_gon.should == '<script>window.gon = {};' +
42+
'gon.int=1;' +
43+
'</script>'
44+
end
45+
46+
it 'outputs correct js with a string' do
47+
Gon.str = %q(a'b"c)
48+
@base.include_gon.should == '<script>window.gon = {};' +
49+
%q(gon.str="a'b\"c";) +
50+
'</script>'
51+
end
52+
53+
it 'outputs correct js with a script string' do
54+
Gon.str = %q(</script><script>alert('!')</script>)
55+
@base.include_gon.should == '<script>window.gon = {};' +
56+
%q(gon.str="<\\/script><script>alert('!')<\\/script>";) +
57+
'</script>'
58+
end
59+
3760
end
3861

3962
it 'returns exception if try to set public method as variable' do
4063
Gon.clear
4164
lambda { Gon.all_variables = 123 }.should raise_error
65+
lambda { Gon.rabl = 123 }.should raise_error
4266
end
4367

4468
describe '.rabl' do

0 commit comments

Comments
 (0)