Skip to content

Commit de42ce8

Browse files
rymohrSimeon F. Willbanks
authored andcommitted
Generalize https filter
1 parent 7f98992 commit de42ce8

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

lib/html/pipeline/https_filter.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
module HTML
22
class Pipeline
3-
# HTML Filter for replacing http github urls with https versions.
3+
# HTML Filter for replacing http references to :base_url with https versions.
4+
# Subdomain references are not rewritten.
5+
#
6+
# Context options:
7+
# :base_url - The url to force https
48
class HttpsFilter < Filter
59
def call
6-
doc.css('a[href^="http://github.com"]').each do |element|
10+
doc.css(%Q(a[href^="#{context[:base_url]}"])).each do |element|
711
element['href'] = element['href'].sub(/^http:/,'https:')
812
end
913
doc
1014
end
15+
16+
# Raise error if :base_url undefined
17+
def validate
18+
needs :base_url
19+
end
1120
end
1221
end
13-
end
22+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "test_helper"
2+
3+
HttpsFilter = HTML::Pipeline::HttpsFilter
4+
5+
class HTML::Pipeline::AutolinkFilterTest < Test::Unit::TestCase
6+
def filter(html, base_url="http://github.com")
7+
HttpsFilter.to_html(html, :base_url => base_url)
8+
end
9+
10+
def test_http
11+
assert_equal %(<a href="https://github.com">github.com</a>),
12+
filter(%(<a href="http://github.com">github.com</a>))
13+
end
14+
15+
def test_https
16+
assert_equal %(<a href="https://github.com">github.com</a>),
17+
filter(%(<a href="https://github.com">github.com</a>))
18+
end
19+
20+
def test_subdomain
21+
assert_equal %(<a href="http://help.github.com">github.com</a>),
22+
filter(%(<a href="http://help.github.com">github.com</a>))
23+
end
24+
25+
def test_other
26+
assert_equal %(<a href="http://github.io">github.io</a>),
27+
filter(%(<a href="http://github.io">github.io</a>))
28+
end
29+
30+
def test_validation
31+
exception = assert_raise(ArgumentError) { HttpsFilter.call(nil, {}) }
32+
assert_match "HTML::Pipeline::HttpsFilter: :base_url", exception.message
33+
end
34+
end

0 commit comments

Comments
 (0)