html_terminator 2.0.2 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bd4d0609098d033eece8510d9bee3dbd0cb09966
4
- data.tar.gz: 96b86b54353b1b72afe70eac1fbed82257cc2896
2
+ SHA256:
3
+ metadata.gz: 1827dcef5defdc9254e5e2bea701325b2bd7de9629a6aabf86b7e1d518145405
4
+ data.tar.gz: 5a07da734d0fc6443e157d29cd9a297ad4f75abe6704589c6b884580e6af4b88
5
5
  SHA512:
6
- metadata.gz: 650230f42f73afa03d5bcf6c37dc76e3cd253a5704a6bef83196a49b49d9847b39141b6f47be404681df422272cbd024f379f0b42595e17bee79eedcf4d55115
7
- data.tar.gz: 4e0fa09f6dbb339404b9fd2c95d167ede09980151ff8f82c0bd3eb78c1eccc2067a05c256b993efdff4a1bfd513516ea7e323254b5ca31409b9a31931b695ebc
6
+ metadata.gz: 710b061a16096c74d8b31a3f1d9542c12b12ba8621ffbffa519f17f09f58e5ac5d5a7ec7fb62ff094be5042f17bc2a59b48e61deeb8debbd02b7641ca5ff9fce
7
+ data.tar.gz: 9318ee326d6dd7246d4ec4105607f02abaec016c75612256a214ac717e9e265510dd1de9304ee403f57d8cb968c0d9c4b966447194ff235f7ef5c5ba679cb6d9
@@ -0,0 +1,22 @@
1
+ name: CI
2
+ on: [push]
3
+ jobs:
4
+ lint-test:
5
+ name: Test
6
+ runs-on: ubuntu-latest
7
+ timeout-minutes: 10
8
+ strategy:
9
+ matrix:
10
+ ruby:
11
+ - 2.5
12
+ - 2.6
13
+ - 2.7
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{ matrix.ruby }}
19
+
20
+
21
+ - run: bundle install
22
+ - run: bundle exec rspec
data/Gemfile CHANGED
@@ -4,8 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :test do
7
- gem "activerecord", "~> 4.2"
8
- gem 'sqlite3'
9
- gem 'guard-rspec'
10
- gem 'rb-fsevent'
7
+ gem "activerecord", "~> 6.0"
8
+ gem 'sqlite3', "~> 1.4.2" # tied to activerecord
9
+ gem 'rspec'
11
10
  end
data/README.md CHANGED
@@ -52,15 +52,13 @@ In your Rails models:
52
52
 
53
53
  terminate_html :field1, :field2, :field3
54
54
 
55
- or
56
-
57
- terminate_html :except => [:field8, :field9]
58
-
59
55
  ## Options
60
56
 
61
57
  Out of the box, HTML Terminator will strip out ALL html. You can pass in specific elements you want to preserve like this:
62
58
 
63
59
  terminate_html :field1, :elements => ["b", "i", "em"]
60
+ terminate_html :field2, :elements => ["br"]
61
+ terminate_html :field3, :elements => ["em"]
64
62
 
65
63
  Learn more about configuration options [Here](https://github.com/rgrove/sanitize#custom-configuration)
66
64
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["steel@polleverywhere.com", "matt@polleverywhere.com"]
11
11
  spec.description = %q{Terminate Active Records fields of html}
12
12
  spec.summary = %q{Terminate Active Records fields of html}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/polleverywhere/html_terminator/"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "bundler", "~> 2.2.0"
22
22
  spec.add_development_dependency "rake"
23
23
 
24
- spec.add_runtime_dependency "sanitize", "~> 4.0"
24
+ spec.add_runtime_dependency "sanitize", "~> 6.0.0"
25
25
  end
@@ -1,3 +1,3 @@
1
1
  module HtmlTerminator
2
- VERSION = "2.0.2"
2
+ VERSION = '6.0.0'
3
3
  end
@@ -11,44 +11,49 @@ module HtmlTerminator
11
11
  if val.is_a?(String)
12
12
  # Sanitize produces escaped content.
13
13
  # Unescape it to get the raw html
14
- CGI.unescapeHTML(Sanitize.fragment(val, config).strip).html_safe
14
+ CGI.unescapeHTML(Sanitize.fragment(val, config).strip)
15
15
  else
16
16
  val
17
17
  end
18
18
  end
19
19
 
20
20
  module ClassMethods
21
- def terminate_html(*args)
22
- class_attribute :html_terminator_fields
23
- class_attribute :html_terminator_options
21
+ def fields
22
+ self.columns.inject([]) do |list, col|
23
+ if col.type == :string or col.type == :text
24
+ list << col.name.to_sym
25
+ end
26
+
27
+ list
28
+ end
29
+ end
24
30
 
31
+ def terminate_html(*args)
25
32
  # Table may not exist yet when schema is initially getting loaded
26
33
  if self.table_exists?
27
- # By default all fields are to be seen by the terminator
28
- self.html_terminator_fields = self.columns.inject([]) do |list, col|
29
- if col.type == :string or col.type == :text
30
- list << col.name.to_sym
31
- end
32
-
33
- list
34
+ # object key/value of field => options
35
+ unless method_defined?(:html_terminator_fields)
36
+ class_attribute :html_terminator_fields
37
+ self.html_terminator_fields = {}
34
38
  end
35
39
 
36
- self.html_terminator_options = SANITIZE_OPTIONS.merge(args.extract_options!)
37
- self.html_terminator_fields = args if args.length > 0
40
+ options = args.extract_options!
41
+ options = SANITIZE_OPTIONS.clone.merge(options)
38
42
 
39
- # Handle exceptions
40
- exceptions = self.html_terminator_options.delete(:except) || []
41
- self.html_terminator_fields -= (exceptions)
43
+ valid_fields = self.fields & args
44
+
45
+ valid_fields.each do |field|
46
+ self.html_terminator_fields[field] = options.deep_dup
47
+ end
42
48
 
43
49
  unless self.html_terminator_fields.empty?
44
- # sanitize writes
45
50
  before_validation :terminate_html
46
51
 
47
52
  # sanitize reads
48
- self.html_terminator_fields.each do |attr|
53
+ valid_fields.each do |attr|
49
54
  define_method(attr) do |*rargs|
50
55
  # sanitize it
51
- HtmlTerminator.sanitize super(*rargs), self.html_terminator_options
56
+ HtmlTerminator.sanitize super(*rargs), options
52
57
  end
53
58
  end
54
59
  end
@@ -58,11 +63,11 @@ module HtmlTerminator
58
63
 
59
64
  module InstanceMethods
60
65
  def terminate_html
61
- self.html_terminator_fields.each do |field|
66
+ self.html_terminator_fields.each do |field, options|
62
67
  value = self[field]
63
68
 
64
69
  unless value.nil?
65
- self[field] = HtmlTerminator.sanitize(value, self.html_terminator_options)
70
+ self[field] = HtmlTerminator.sanitize(value, options)
66
71
  end
67
72
  end
68
73
  end
@@ -1,80 +1,88 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe HtmlTerminator do
4
4
  it "sanitizes only fields specified" do
5
- @user = OnlyFirstName.new
5
+ user = OnlyFirstName.new
6
6
 
7
- @user.first_name = "Hello <img>"
8
- @user.first_name.should == "Hello"
7
+ user.first_name = "Hello <img>"
8
+ expect(user.first_name).to eql("Hello")
9
9
 
10
- @user.last_name = "Hello <img>"
11
- @user.last_name.should == "Hello <img>"
10
+ user.last_name = "Hello <img>"
11
+ expect(user.last_name).to eql("Hello <img>")
12
12
 
13
- @user.age = 3
14
- @user.age.should == 3
13
+ user.age = 3
14
+ expect(user.age).to eql(3)
15
15
  end
16
16
 
17
17
  it "doesn't escape ampersands" do
18
- @user = OnlyFirstName.new
18
+ user = OnlyFirstName.new
19
19
 
20
- @user.first_name = "A & B & C"
21
- @user.first_name.should == "A & B & C"
20
+ user.first_name = "A & B & C"
21
+ expect(user.first_name).to eql("A & B & C")
22
22
  end
23
23
 
24
24
  it "skips sanitize when only one bracket" do
25
- @user = OnlyFirstName.new
25
+ user = OnlyFirstName.new
26
26
 
27
- @user.first_name = "1 < 2"
28
- @user.first_name.should == "1 < 2"
27
+ user.first_name = "1 < 2"
28
+ expect(user.first_name).to eql("1 < 2")
29
29
 
30
- @user.first_name = "2 > 1"
31
- @user.first_name.should == "2 > 1"
30
+ user.first_name = "2 > 1"
31
+ expect(user.first_name).to eql("2 > 1")
32
32
  end
33
33
 
34
34
  it "handles ampersands" do
35
- @user = OnlyFirstName.new
35
+ user = OnlyFirstName.new
36
36
 
37
- @user.first_name = "Mr. & Mrs. Smith"
38
- @user.first_name.should == "Mr. & Mrs. Smith"
39
- end
40
-
41
- it "sanitizes all except what is specified" do
42
- @user = ExceptFirstName.new
43
-
44
- @user.first_name = "Hello <img>"
45
- @user.first_name.should == "Hello <img>"
46
-
47
- @user.last_name = "Hello <img>"
48
- @user.last_name.should == "Hello"
49
- end
50
-
51
- it "doesn't blow up if value is nil" do
52
- @user = ExceptFirstName.new
53
- @user.first_name = nil
54
- @user.first_name.should == nil
37
+ user.first_name = "Mr. & Mrs. Smith"
38
+ expect(user.first_name).to eql("Mr. & Mrs. Smith")
55
39
  end
56
40
 
57
41
  it "doesn't blow up if value is not a string" do
58
- @user = OnlyFirstName.new
59
- @user.first_name = 1
60
- @user.first_name.should == "1"
42
+ user = OnlyFirstName.new
43
+ user.first_name = 1
44
+ expect(user.first_name).to eql("1")
61
45
  end
62
46
 
63
47
  it "honors options that are passed in" do
64
- @user = FirstNameWithOptions.new
65
- @user.first_name = "Hello <flexbox></flexbox><hr><br><img>"
66
- @user.first_name.should == "Hello <flexbox></flexbox>"
48
+ user = FirstNameWithOptions.new
49
+ user.first_name = "Hello <flexbox></flexbox><hr><br><img>"
50
+ expect(user.first_name).to eql("Hello <flexbox></flexbox>")
67
51
  end
68
52
 
69
53
  describe "#sanitize" do
70
54
  it "strips out all html by default" do
71
55
  val = HtmlTerminator.sanitize "<flexbox></flexbox><hr><br><img>"
72
- val.should == ""
56
+ expect(val).to eql("")
73
57
  end
74
58
 
75
- it "marks the output as html_safe" do
59
+ it "does not mark the output as html_safe" do
76
60
  val = HtmlTerminator.sanitize "<flexbox></flexbox><hr><br><img>"
77
- val.html_safe?.should == true
61
+ expect(val.html_safe?).to eql(false)
62
+ end
63
+
64
+ it "does not escape output that isn't stripped" do
65
+ val = HtmlTerminator.sanitize "<div>I said, \"Hello, John O'hare.\"</div>"
66
+ expect(val).to eql("I said, \"Hello, John O'hare.\"")
78
67
  end
79
68
  end
69
+
70
+ it "sanitizes different fields with different options" do
71
+ user = TwoFieldsWithOptions.new
72
+ user.first_name = "Hello <br><strong>strong</strong><em>em</em>"
73
+ user.last_name = "Hello <br><strong>strong</strong><em>em</em>"
74
+
75
+ expect(user.first_name).to eql("Hello <strong>strong</strong>em")
76
+ expect(user.last_name).to eql("Hello strong<em>em</em>")
77
+ end
78
+
79
+ it "sanitizes on validation" do
80
+ user = TwoFieldsWithOptions.new
81
+ user.first_name = "Hello <br><strong>strong</strong><em>em</em>"
82
+ user.last_name = "Hello <br><strong>strong</strong><em>em</em>"
83
+ user.valid?
84
+
85
+ expect(user.read_attribute(:first_name)).to eql("Hello <strong>strong</strong>em")
86
+ expect(user.read_attribute(:last_name)).to eql("Hello strong<em>em</em>")
87
+ end
80
88
  end
@@ -13,7 +13,7 @@ ActiveRecord::Schema.define do
13
13
  t.column "age", :integer
14
14
  end
15
15
 
16
- create_table "except_first_names", :force => true do |t|
16
+ create_table "two_fields_with_options", :force => true do |t|
17
17
  t.column "first_name", :text
18
18
  t.column "last_name", :text
19
19
  t.column "age", :integer
@@ -32,10 +32,11 @@ class OnlyFirstName < ActiveRecord::Base
32
32
  terminate_html :first_name
33
33
  end
34
34
 
35
- class ExceptFirstName < ActiveRecord::Base
35
+ class TwoFieldsWithOptions < ActiveRecord::Base
36
36
  include HtmlTerminator
37
37
 
38
- terminate_html :except => [:first_name]
38
+ terminate_html :first_name, elements: ["strong"]
39
+ terminate_html :last_name, elements: ["em"]
39
40
  end
40
41
 
41
42
  class FirstNameWithOptions < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_terminator
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steel Fu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-03 00:00:00.000000000 Z
12
+ date: 2021-09-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '1.3'
20
+ version: 2.2.0
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '1.3'
27
+ version: 2.2.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '4.0'
48
+ version: 6.0.0
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '4.0'
55
+ version: 6.0.0
56
56
  description: Terminate Active Records fields of html
57
57
  email:
58
58
  - steel@polleverywhere.com
@@ -61,10 +61,9 @@ executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
+ - ".github/workflows/ci.yml"
64
65
  - ".gitignore"
65
- - ".travis.yml"
66
66
  - Gemfile
67
- - Guardfile
68
67
  - LICENSE.txt
69
68
  - README.md
70
69
  - Rakefile
@@ -75,7 +74,7 @@ files:
75
74
  - spec/html_terminator_spec.rb
76
75
  - spec/spec_helper.rb
77
76
  - spec/support/active_record.rb
78
- homepage: ''
77
+ homepage: https://github.com/polleverywhere/html_terminator/
79
78
  licenses:
80
79
  - MIT
81
80
  metadata: {}
@@ -94,8 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
93
  - !ruby/object:Gem::Version
95
94
  version: '0'
96
95
  requirements: []
97
- rubyforge_project:
98
- rubygems_version: 2.2.3
96
+ rubygems_version: 3.0.3
99
97
  signing_key:
100
98
  specification_version: 4
101
99
  summary: Terminate Active Records fields of html
data/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- script: bundle exec rspec
2
- language: ruby
3
- rvm:
4
- - 2.0.0
data/Guardfile DELETED
@@ -1,5 +0,0 @@
1
- guard 'rspec', :version => 2, :cli => '--colour --format nested' do
2
- watch(%r{^spec/.+_spec\.rb$})
3
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
- watch('spec/spec_helper.rb') { "spec" }
5
- end