bypass 0.0.7 → 0.0.8

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
2
  SHA1:
3
- metadata.gz: 714bf5827bed4b9dc48e2e8b02331d3ea037dbcb
4
- data.tar.gz: 8308c92ac4c09c9f43ec0829014b372b45912f22
3
+ metadata.gz: fc4ddc7af4c8b964645097d13bf732dd61916ae7
4
+ data.tar.gz: 4db28ffabcd537ac7ab691a507bd1c943ce9e771
5
5
  SHA512:
6
- metadata.gz: 961a97ba6833870c7dbee87389ec2b98795b402baa2c879726f8c0b5d26e585a1234252fb8b6b6cd3da4142cd5e5c494685cb28579ca948f2594d8fc08595532
7
- data.tar.gz: 9a915ca624bb6bb3913834f322d3ba2985eff78a74abaa92f70f622d155afed1ac804e30e06bdd842268640143efc01b99a62b7b282e50762fd339b216aef099
6
+ metadata.gz: f57bc91cec3392defc8640589dcdb99da330910a0a76f3a1646f9319c8227a050a4e110cbee5258cb6ae5b8abbd02577bc6eeafae67713afb2f9f54ac00acbb3
7
+ data.tar.gz: 542bbfd4e0088f29e0a6c130f3e3dd0b114d38dcfc75b178919d63d8e2fdfee116825053719a694fcb847f3c1af9bcdcf15ab658c2e724480da68f7c81c8c895
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Derrick Reimer
1
+ Copyright (c) 2013-2016 Derrick Reimer
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,11 +1,11 @@
1
1
  module Bypass
2
2
  class Filter
3
3
  attr_reader :content, :fragment
4
-
4
+
5
5
  URL_PATTERN = /\bhttps?:\/\/
6
6
  [a-zA-Z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;=%]+
7
7
  [a-zA-Z0-9\-_~:\/\?#\[\]@!$&\*\+;=%]/x
8
-
8
+
9
9
  def initialize(content, options = {})
10
10
  @content = content.to_s.encode("UTF-8")
11
11
  @fragment = options.fetch(:fragment, true)
@@ -14,13 +14,13 @@ module Bypass
14
14
  def replace
15
15
  raise NotImplementedError
16
16
  end
17
-
17
+
18
18
  def to_s
19
19
  content
20
20
  end
21
-
21
+
22
22
  private
23
-
23
+
24
24
  def gsub_urls(text, &block)
25
25
  text.gsub(URL_PATTERN) do |match|
26
26
  yield(match.to_s)
@@ -29,8 +29,8 @@ module Bypass
29
29
 
30
30
  def parse_uri(uri)
31
31
  Bypass::URI.parse(uri.to_s.strip)
32
- rescue => ex
32
+ rescue
33
33
  nil
34
34
  end
35
35
  end
36
- end
36
+ end
@@ -3,6 +3,17 @@ require "nokogiri"
3
3
  module Bypass
4
4
  class HTMLFilter < Filter
5
5
 
6
+ def initialize(content, options = {})
7
+ if content.respond_to?(:traverse) # then this is Nokogiri
8
+ @content = content
9
+ @parsed_content = content
10
+ else
11
+ @content = content.to_s.encode("UTF-8")
12
+ end
13
+
14
+ @fragment = options.fetch(:fragment, true)
15
+ end
16
+
6
17
  def replace(&block)
7
18
  parsed_content.traverse do |node|
8
19
  if node.name == "a" && href = node.get_attribute("href")
@@ -16,7 +27,7 @@ module Bypass
16
27
 
17
28
  def auto_link(options = {})
18
29
  each_text_node do |node|
19
- text = gsub_urls(node.text.to_s) do |url|
30
+ text = gsub_urls(node.text.to_s) do |url|
20
31
  build_anchor_tag(url, url, options)
21
32
  end
22
33
  node.replace(parse_html_fragment(text))
@@ -29,10 +40,22 @@ module Bypass
29
40
  "<a href=\"#{href}\"#{attributes.join(' ')}>#{text}</a>"
30
41
  end
31
42
 
43
+ def parsed_content
44
+ @parsed_content ||= parse_content
45
+ end
46
+
32
47
  def content
33
48
  parsed_content.to_s
34
49
  end
35
50
 
51
+ def parse_content
52
+ if fragment
53
+ parse_html_fragment(@content)
54
+ else
55
+ parse_html_document(@content)
56
+ end
57
+ end
58
+
36
59
  private
37
60
 
38
61
  def each_text_node(&block)
@@ -45,16 +68,6 @@ module Bypass
45
68
  end
46
69
  end
47
70
 
48
- def parsed_content
49
- @parsed_content ||= begin
50
- if fragment
51
- parse_html_fragment(@content)
52
- else
53
- parse_html_document(@content)
54
- end
55
- end
56
- end
57
-
58
71
  def parse_html_fragment(html)
59
72
  Nokogiri::HTML::DocumentFragment.parse(html)
60
73
  end
@@ -63,4 +76,4 @@ module Bypass
63
76
  Nokogiri::HTML::Document.parse(html)
64
77
  end
65
78
  end
66
- end
79
+ end
@@ -1,4 +1,5 @@
1
1
  require "addressable/uri"
2
+ require "cgi"
2
3
 
3
4
  module Bypass
4
5
  class URI < Addressable::URI
@@ -1,3 +1,3 @@
1
1
  module Bypass
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -8,4 +8,4 @@ class Bypass::FilterTest < MiniTest::Test
8
8
  assert_equal "foo", filter.to_s
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -2,6 +2,23 @@ require 'addressable/uri'
2
2
  require File.dirname(__FILE__) + '/../test_helper.rb'
3
3
 
4
4
  class Bypass::HTMLFilterTest < MiniTest::Test
5
+ context "content parsing" do
6
+ should "parse string content on the fly" do
7
+ filter = Bypass::HTMLFilter.new("foo")
8
+ filter.expects(:parse_content).returns("bar")
9
+ assert_equal "bar", filter.content
10
+ assert_equal "bar", filter.parsed_content
11
+ end
12
+
13
+ should "skip parsing if Nokogiri document is passed in" do
14
+ document = Nokogiri::HTML::DocumentFragment.parse("<html></html>")
15
+ filter = Bypass::HTMLFilter.new(document)
16
+ filter.expects(:parse_content).never
17
+ assert_equal document.to_s, filter.content
18
+ assert_equal document, filter.parsed_content
19
+ end
20
+ end
21
+
5
22
  context "#replace" do
6
23
  should "replace hrefs" do
7
24
  text = "<a href=\"http://yahoo.com\">Yahoo</a>"
@@ -44,7 +61,7 @@ class Bypass::HTMLFilterTest < MiniTest::Test
44
61
  assert_equal "Hello <a href=\"http://www.google.com\">http://www.google.com</a>.", filter.content
45
62
  end
46
63
 
47
- should "not replace urls inside a-tags" do
64
+ should "not replace urls inside a-tags" do
48
65
  text = "<a href=\"http://yahoo.com\">http://yahoo.com</a>"
49
66
  filter = Bypass::HTMLFilter.new(text)
50
67
  filter.auto_link
@@ -58,7 +75,7 @@ class Bypass::HTMLFilterTest < MiniTest::Test
58
75
  tag = "<a href=\"http://www.google.com\">Google</a>"
59
76
  assert_equal tag, filter.build_anchor_tag("Google", "http://www.google.com")
60
77
  end
61
-
78
+
62
79
  should "include additional attributes" do
63
80
  filter = Bypass::HTMLFilter.new("")
64
81
  tag = "<a href=\"http://www.google.com\" class=\"button\">Google</a>"
@@ -104,4 +121,4 @@ class Bypass::HTMLFilterTest < MiniTest::Test
104
121
  def normalize_output(html)
105
122
  html.lines.map(&:strip).join("")
106
123
  end
107
- end
124
+ end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bypass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derrick Reimer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-19 00:00:00.000000000 Z
11
+ date: 2017-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: addressable
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '5.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: shoulda-context
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: Mutate URLs and hyperlinks in HTML and plain text documents
@@ -87,8 +87,8 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
91
- - .travis.yml
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
92
  - Gemfile
93
93
  - LICENSE.txt
94
94
  - README.md
@@ -114,17 +114,17 @@ require_paths:
114
114
  - lib
115
115
  required_ruby_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - '>='
117
+ - - ">="
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>='
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.3.0
127
+ rubygems_version: 2.5.1
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Mutate URLs and hyperlinks in HTML and plain text documents with ease
@@ -134,4 +134,3 @@ test_files:
134
134
  - test/bypass/text_filter_test.rb
135
135
  - test/bypass/uri_test.rb
136
136
  - test/test_helper.rb
137
- has_rdoc: