js-code-wrapper 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/VERSION +1 -1
- data/js-code-wrapper.gemspec +5 -2
- data/lib/js-code-wrapper.rb +5 -35
- data/spec/lib/js_code_wrapper_spec.rb +21 -6
- metadata +18 -4
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/js-code-wrapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "js-code-wrapper"
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["dom1nga"]
|
12
|
-
s.date = "2011-11-
|
12
|
+
s.date = "2011-11-29"
|
13
13
|
s.description = "Usage: wrap_js_code('<script>alert('wrap me!');</script>') # => '<code><script>alert('wrap me!');</script></code>'"
|
14
14
|
s.email = "beloved.dom1nga@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -43,15 +43,18 @@ Gem::Specification.new do |s|
|
|
43
43
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
44
44
|
s.add_development_dependency(%q<rake>, [">= 0"])
|
45
45
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
46
|
+
s.add_development_dependency(%q<nokogiri>, [">= 0"])
|
46
47
|
else
|
47
48
|
s.add_dependency(%q<rspec>, [">= 0"])
|
48
49
|
s.add_dependency(%q<rake>, [">= 0"])
|
49
50
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
51
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
50
52
|
end
|
51
53
|
else
|
52
54
|
s.add_dependency(%q<rspec>, [">= 0"])
|
53
55
|
s.add_dependency(%q<rake>, [">= 0"])
|
54
56
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
57
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
55
58
|
end
|
56
59
|
end
|
57
60
|
|
data/lib/js-code-wrapper.rb
CHANGED
@@ -1,41 +1,11 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
1
3
|
module JsCodeWrapper
|
2
4
|
def wrap_js_code(text)
|
3
|
-
|
4
|
-
|
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]] }
|
5
|
+
doc = ::Nokogiri::HTML("<text>#{text}</text>")
|
6
|
+
doc.xpath("//script[not(ancestor::code)] | //*/@*[starts-with(name(.), 'on')]/..").wrap('<code>')
|
7
|
+
doc.xpath("//body/*").to_s.scan(/<text>(.*)<\/text>/im)[0][0]
|
21
8
|
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| [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[1]
|
33
|
-
text[b[0]..(b[0]+tmp.length-1)] = '<code>'+ tmp +'</code>'
|
34
|
-
return text
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
9
|
end
|
40
10
|
|
41
11
|
Object.send(:include, JsCodeWrapper)
|
@@ -4,28 +4,43 @@ describe JsCodeWrapper do
|
|
4
4
|
|
5
5
|
describe "#wrap_js_code" do
|
6
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 ==
|
7
|
+
wrap_js_code("text <p>text</p><h1>text</h1>").should ==
|
8
|
+
"text <p>text</p>\n<h1>text</h1>"
|
8
9
|
end
|
9
10
|
|
10
11
|
it "should return the text with script body (type1) wrapped into code tags" do
|
11
|
-
wrap_js_code("<script>alert('wrap me!')</script>").should ==
|
12
|
+
wrap_js_code("<script>alert('wrap me!')</script>").should ==
|
13
|
+
"<code><script>alert('wrap me!')</script></code>"
|
12
14
|
end
|
13
15
|
|
14
16
|
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 ==
|
17
|
+
wrap_js_code('<script type="text/javascript" src="example.com/example.js"></script>').should ==
|
18
|
+
'<code><script type="text/javascript" src="example.com/example.js"></script></code>'
|
16
19
|
end
|
17
20
|
|
18
21
|
it "should return the text with script body wrapped into code tags when script body duplicated in text" do
|
19
22
|
wrap_js_code('<code><script>alert("wrap me!")</script></code><script>alert("wrap me!")</script>').should ==
|
20
|
-
|
23
|
+
'<code><script>alert("wrap me!")</script></code><code><script>alert("wrap me!")</script></code>'
|
21
24
|
end
|
22
25
|
|
23
26
|
it "should return the text with script body wrapped into code tags" do
|
24
|
-
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 ==
|
27
|
+
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 ==
|
28
|
+
"<code><script>alert('wrap me1!')</script><script type=\"text/javascript\" src=\"example.com/example1.js\"></script></code><p>text1</p>\n<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>"
|
25
29
|
end
|
26
30
|
|
27
31
|
it "should return the text without changes, if script body into code tags" do
|
28
|
-
wrap_js_code("<code><script>alert('wrap me!')</script></code>").should ==
|
32
|
+
wrap_js_code("<code><script>alert('wrap me!')</script></code>").should ==
|
33
|
+
"<code><script>alert('wrap me!')</script></code>"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return the text with link wrapped into code tags if link contains event attribute" do
|
37
|
+
wrap_js_code('<a href="#" onclick="alert(\'wrap me!\'); return 0;">link</a>').should ==
|
38
|
+
"<code><a href=\"#\" onclick=\"alert('wrap me!'); return 0;\">link</a></code>"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return the text with tags without changes if tags not contains event attributes" do
|
42
|
+
wrap_js_code('<a href="#">link</a><div class="div1"></div><p>text1</p>').should ==
|
43
|
+
"<a href=\"#\">link</a><div class=\"div1\"></div>\n<p>text1</p>"
|
29
44
|
end
|
30
45
|
|
31
46
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-code-wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- dom1nga
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-29 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
requirement: &id001 !ruby/object:Gem::Requirement
|
@@ -59,6 +59,20 @@ dependencies:
|
|
59
59
|
name: jeweler
|
60
60
|
prerelease: false
|
61
61
|
type: :development
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
version_requirements: *id004
|
73
|
+
name: nokogiri
|
74
|
+
prerelease: false
|
75
|
+
type: :development
|
62
76
|
description: "Usage: wrap_js_code('<script>alert('wrap me!');</script>') # => '<code><script>alert('wrap me!');</script></code>'"
|
63
77
|
email: beloved.dom1nga@gmail.com
|
64
78
|
executables: []
|