html_terminator 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -2
- data/html_terminator.gemspec +1 -1
- data/lib/html_terminator/version.rb +1 -1
- data/lib/html_terminator.rb +34 -25
- data/spec/html_terminator_spec.rb +26 -2
- metadata +18 -25
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 39b8f069bc265e35b278f46ef68fc414ef234ee5
|
4
|
+
data.tar.gz: 0a5fa89a7c15d585814ce420d14b51711b479ab7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f4125c44661d12232e5200bc5a505cecb1a372457c62c6d94c1b0d37b3854ba6bf5460680a605b6baab66066a7dda81134aa5c0993ca523e250cad0066e08bc
|
7
|
+
data.tar.gz: cc2bfdb08ce2b0c7fa78f60e127cd0cacb931c2335e5ef9bc189bab5f2f6a963e6838647c272f48664c5edd02dd9c7bb03c506a9faba67b2682363022e7a21f0
|
data/Gemfile
CHANGED
data/html_terminator.gemspec
CHANGED
data/lib/html_terminator.rb
CHANGED
@@ -7,45 +7,54 @@ module HtmlTerminator
|
|
7
7
|
}
|
8
8
|
|
9
9
|
def self.sanitize(val)
|
10
|
-
if val
|
11
|
-
Sanitize.
|
10
|
+
if val.is_a?(String) && !skip_sanitize?(val)
|
11
|
+
Sanitize.fragment(val, SANITIZE_OPTIONS).strip.gsub(/&/, "&")
|
12
12
|
else
|
13
13
|
val
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
# Don't sanitize if only one bracket is present.
|
18
|
+
# Without this, "1 < 2" gets incorrectly sanitized as "1".
|
19
|
+
def self.skip_sanitize?(val)
|
20
|
+
val.count("<") + val.count(">") == 1
|
21
|
+
end
|
22
|
+
|
17
23
|
module ClassMethods
|
18
24
|
def terminate_html(*args)
|
19
25
|
class_attribute :html_terminator_fields
|
20
26
|
|
21
|
-
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
# Table may not exist yet when schema is initially getting loaded
|
28
|
+
if self.table_exists?
|
29
|
+
# By default all fields are to be seen by the terminator
|
30
|
+
self.html_terminator_fields = self.columns.inject([]) do |list, col|
|
31
|
+
if col.type == :string or col.type == :text
|
32
|
+
list << col.name.to_sym
|
33
|
+
end
|
26
34
|
|
27
|
-
|
28
|
-
|
35
|
+
list
|
36
|
+
end
|
29
37
|
|
30
|
-
|
31
|
-
|
38
|
+
if args.length == 1
|
39
|
+
if args[0].is_a?(Symbol)
|
40
|
+
self.html_terminator_fields = args
|
41
|
+
elsif args[0].is_a?(Object)
|
42
|
+
self.html_terminator_fields -= (args[0][:except] || [])
|
43
|
+
end
|
44
|
+
elsif args.length > 1
|
32
45
|
self.html_terminator_fields = args
|
33
|
-
elsif args[0].is_a?(Object)
|
34
|
-
self.html_terminator_fields -= (args[0][:except] || [])
|
35
46
|
end
|
36
|
-
elsif args.length > 1
|
37
|
-
self.html_terminator_fields = args
|
38
|
-
end
|
39
47
|
|
40
|
-
|
41
|
-
|
42
|
-
|
48
|
+
unless self.html_terminator_fields.empty?
|
49
|
+
# sanitize writes
|
50
|
+
before_validation :terminate_html
|
43
51
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
52
|
+
# sanitize reads
|
53
|
+
self.html_terminator_fields.each do |attr|
|
54
|
+
define_method(attr) do |*rargs|
|
55
|
+
# sanitize it
|
56
|
+
HtmlTerminator.sanitize super(*rargs)
|
57
|
+
end
|
49
58
|
end
|
50
59
|
end
|
51
60
|
end
|
@@ -70,4 +79,4 @@ module HtmlTerminator
|
|
70
79
|
end
|
71
80
|
end
|
72
81
|
|
73
|
-
ActiveRecord::Base.send :include, HtmlTerminator
|
82
|
+
ActiveRecord::Base.send :include, HtmlTerminator
|
@@ -14,6 +14,30 @@ describe HtmlTerminator do
|
|
14
14
|
@user.age.should == 3
|
15
15
|
end
|
16
16
|
|
17
|
+
it "doesn't escape ampersands" do
|
18
|
+
@user = OnlyFirstName.new
|
19
|
+
|
20
|
+
@user.first_name = "A & B & C"
|
21
|
+
@user.first_name.should == "A & B & C"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "skips sanitize when only one bracket" do
|
25
|
+
@user = OnlyFirstName.new
|
26
|
+
|
27
|
+
@user.first_name = "1 < 2"
|
28
|
+
@user.first_name.should == "1 < 2"
|
29
|
+
|
30
|
+
@user.first_name = "2 > 1"
|
31
|
+
@user.first_name.should == "2 > 1"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "handles ampersands" do
|
35
|
+
@user = OnlyFirstName.new
|
36
|
+
|
37
|
+
@user.first_name = "Mr. & Mrs. Smith"
|
38
|
+
@user.first_name.should == "Mr. & Mrs. Smith"
|
39
|
+
end
|
40
|
+
|
17
41
|
it "sanitizes all except what is specified" do
|
18
42
|
@user = ExceptFirstName.new
|
19
43
|
|
@@ -33,6 +57,6 @@ describe HtmlTerminator do
|
|
33
57
|
it "doesn't blow up if value is not a string" do
|
34
58
|
@user = OnlyFirstName.new
|
35
59
|
@user.first_name = 1
|
36
|
-
@user.first_name.should == 1
|
60
|
+
@user.first_name.should == "1"
|
37
61
|
end
|
38
|
-
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_terminator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Steel Fu
|
@@ -10,56 +9,50 @@ authors:
|
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: bundler
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
17
|
requirements:
|
20
|
-
- - ~>
|
18
|
+
- - "~>"
|
21
19
|
- !ruby/object:Gem::Version
|
22
20
|
version: '1.3'
|
23
21
|
type: :development
|
24
22
|
prerelease: false
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
24
|
requirements:
|
28
|
-
- - ~>
|
25
|
+
- - "~>"
|
29
26
|
- !ruby/object:Gem::Version
|
30
27
|
version: '1.3'
|
31
28
|
- !ruby/object:Gem::Dependency
|
32
29
|
name: rake
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
31
|
requirements:
|
36
|
-
- -
|
32
|
+
- - ">="
|
37
33
|
- !ruby/object:Gem::Version
|
38
34
|
version: '0'
|
39
35
|
type: :development
|
40
36
|
prerelease: false
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
38
|
requirements:
|
44
|
-
- -
|
39
|
+
- - ">="
|
45
40
|
- !ruby/object:Gem::Version
|
46
41
|
version: '0'
|
47
42
|
- !ruby/object:Gem::Dependency
|
48
43
|
name: sanitize
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
45
|
requirements:
|
52
|
-
- -
|
46
|
+
- - "~>"
|
53
47
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
48
|
+
version: '4.0'
|
55
49
|
type: :runtime
|
56
50
|
prerelease: false
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
52
|
requirements:
|
60
|
-
- -
|
53
|
+
- - "~>"
|
61
54
|
- !ruby/object:Gem::Version
|
62
|
-
version: '0'
|
55
|
+
version: '4.0'
|
63
56
|
description: Terminate Active Records fields of html
|
64
57
|
email:
|
65
58
|
- steel@polleverywhere.com
|
@@ -68,8 +61,8 @@ executables: []
|
|
68
61
|
extensions: []
|
69
62
|
extra_rdoc_files: []
|
70
63
|
files:
|
71
|
-
- .gitignore
|
72
|
-
- .travis.yml
|
64
|
+
- ".gitignore"
|
65
|
+
- ".travis.yml"
|
73
66
|
- Gemfile
|
74
67
|
- Guardfile
|
75
68
|
- LICENSE.txt
|
@@ -84,29 +77,29 @@ files:
|
|
84
77
|
homepage: ''
|
85
78
|
licenses:
|
86
79
|
- MIT
|
80
|
+
metadata: {}
|
87
81
|
post_install_message:
|
88
82
|
rdoc_options: []
|
89
83
|
require_paths:
|
90
84
|
- lib
|
91
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
-
none: false
|
93
86
|
requirements:
|
94
|
-
- -
|
87
|
+
- - ">="
|
95
88
|
- !ruby/object:Gem::Version
|
96
89
|
version: '0'
|
97
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
91
|
requirements:
|
100
|
-
- -
|
92
|
+
- - ">="
|
101
93
|
- !ruby/object:Gem::Version
|
102
94
|
version: '0'
|
103
95
|
requirements: []
|
104
96
|
rubyforge_project:
|
105
|
-
rubygems_version:
|
97
|
+
rubygems_version: 2.2.3
|
106
98
|
signing_key:
|
107
|
-
specification_version:
|
99
|
+
specification_version: 4
|
108
100
|
summary: Terminate Active Records fields of html
|
109
101
|
test_files:
|
110
102
|
- spec/html_terminator_spec.rb
|
111
103
|
- spec/spec_helper.rb
|
112
104
|
- spec/support/active_record.rb
|
105
|
+
has_rdoc:
|