js-code-wrapper 0.1.0
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.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +20 -0
- data/README.md +17 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/lib/js-code-wrapper.rb +42 -0
- data/spec/lib/js_code_wrapper_spec.rb +27 -0
- data/spec/spec_helper.rb +8 -0
- metadata +96 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.7.0)
|
12
|
+
rspec-core (~> 2.7.0)
|
13
|
+
rspec-expectations (~> 2.7.0)
|
14
|
+
rspec-mocks (~> 2.7.0)
|
15
|
+
rspec-core (2.7.1)
|
16
|
+
rspec-expectations (2.7.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.7.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
jeweler
|
25
|
+
rake
|
26
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 dom1nga
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# js-code-wrapper #
|
2
|
+
|
3
|
+
Gem provides helper method 'wrap_js_code' that wraps javascript into '<code></code>' tags.
|
4
|
+
|
5
|
+
## Installation ##
|
6
|
+
|
7
|
+
Add
|
8
|
+
``` ruby
|
9
|
+
gem 'js-code-wrapper'
|
10
|
+
```
|
11
|
+
to your `Gemfile`.
|
12
|
+
|
13
|
+
## Usage ##
|
14
|
+
|
15
|
+
``` ruby
|
16
|
+
wrap_js_code('<script>alert('wrap me!');</script>') # => '<code><script>alert('wrap me!');</script></code>'"
|
17
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
require 'rake'
|
7
|
+
|
8
|
+
begin
|
9
|
+
Bundler.setup(:default, :development)
|
10
|
+
rescue Bundler::BundlerError => e
|
11
|
+
$stderr.puts e.message
|
12
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
13
|
+
exit e.status_code
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'jeweler'
|
17
|
+
Jeweler::Tasks.new do |gem|
|
18
|
+
gem.name = "js-code-wrapper"
|
19
|
+
gem.homepage = "http://github.com/dom1nga/js-code-wrapper"
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = "Provides helper method 'wrap_js_code' that wraps javascript code into '<code></code>' tags"
|
22
|
+
gem.description = "Usage: wrap_js_code('<script>alert('wrap me!');</script>') # => '<code><script>alert('wrap me!');</script></code>'"
|
23
|
+
gem.email = "beloved.dom1nga@gmail.com"
|
24
|
+
gem.authors = ["dom1nga"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
RSpec::Core::RakeTask.new('spec')
|
30
|
+
|
31
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module JsCodeWrapper
|
2
|
+
def wrap_js_code(text)
|
3
|
+
code_coords = code_tag_coords(text)
|
4
|
+
matches_count = script_to_code_tags_inclusion(text, code_coords).count {|t| !t}
|
5
|
+
|
6
|
+
while matches_count > 0 do
|
7
|
+
text = insert_code_tags(text, code_coords)
|
8
|
+
matches_count = matches_count - 1
|
9
|
+
code_coords = code_tag_coords(text)
|
10
|
+
end
|
11
|
+
|
12
|
+
text
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def code_tag_coords(text)
|
18
|
+
code_begin = text.to_enum(:scan,/(<code)/i).map { |m,| $`.size }
|
19
|
+
code_end = text.to_enum(:scan,/(<\/code>)/i).map { |m,| $`.size }
|
20
|
+
0.upto(code_begin.length - 1).map {|i| [code_begin[i], code_end[i]] }
|
21
|
+
end
|
22
|
+
|
23
|
+
def script_to_code_tags_inclusion(text, code_coords)
|
24
|
+
text.to_enum(:scan,/(<script{1}[^<]*<\/script>{1})/i).map { |m,| $`.size }.map { |v| code_coords.map{|c| c[0] < v && v < c[1] }.compact.any? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def insert_code_tags(text, code_coords)
|
28
|
+
matches = text.gsub /(<script{1}[^<]*<\/script>{1})/i
|
29
|
+
|
30
|
+
script_to_code_tags_inclusion(text, code_coords).each do |b|
|
31
|
+
tmp = matches.next
|
32
|
+
unless b
|
33
|
+
text[tmp] = '<code>'+ tmp +'</code>'
|
34
|
+
return text
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
Object.send(:include, JsCodeWrapper)
|
42
|
+
ActionView::Base.send(:include, JsCodeWrapper) if defined?(ActionView::Base)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JsCodeWrapper do
|
4
|
+
|
5
|
+
describe "#wrap_js_code" do
|
6
|
+
it "should return the text without changes, if it doesn't contains script tags" do
|
7
|
+
wrap_js_code("text <p>text</p><h1>text</h1>").should == "text <p>text</p><h1>text</h1>"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return the text with script body (type1) wrapped into code tags" do
|
11
|
+
wrap_js_code("<script>alert('wrap me!')</script>").should == "<code><script>alert('wrap me!')</script></code>"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return the text with script body (type2) wrapped into code tags" do
|
15
|
+
wrap_js_code('<script type="text/javascript" src="example.com/example.js"></script>').should == '<code><script type="text/javascript" src="example.com/example.js"></script></code>'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return the text with script body wrapped into code tags" do
|
19
|
+
wrap_js_code("<code><script>alert('wrap me1!')</script><script type='text/javascript' src='example.com/example1.js'></script></code><p>text1</p><script>alert('wrap me2!')</script><script type='text/javascript' src='example.com/example2.js'></script><code><script>alert('wrap me3!')</script></code>").should == "<code><script>alert('wrap me1!')</script><script type='text/javascript' src='example.com/example1.js'></script></code><p>text1</p><code><script>alert('wrap me2!')</script></code><code><script type='text/javascript' src='example.com/example2.js'></script></code><code><script>alert('wrap me3!')</script></code>"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return the text without changes, if script body into code tags" do
|
23
|
+
wrap_js_code("<code><script>alert('wrap me!')</script></code>").should == "<code><script>alert('wrap me!')</script></code>"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: js-code-wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- dom1nga
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-27 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &6788990 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *6788990
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &6788420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *6788420
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: jeweler
|
38
|
+
requirement: &6787720 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *6787720
|
47
|
+
description: ! 'Usage: wrap_js_code(''<script>alert(''wrap me!'');</script>'') # =>
|
48
|
+
''<code><script>alert(''wrap me!'');</script></code>'''
|
49
|
+
email: beloved.dom1nga@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files:
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
files:
|
56
|
+
- .document
|
57
|
+
- .rspec
|
58
|
+
- Gemfile
|
59
|
+
- Gemfile.lock
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- lib/js-code-wrapper.rb
|
65
|
+
- spec/lib/js_code_wrapper_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
homepage: http://github.com/dom1nga/js-code-wrapper
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
hash: 465868685
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Provides helper method 'wrap_js_code' that wraps javascript code into '<code></code>'
|
95
|
+
tags
|
96
|
+
test_files: []
|