File tree Expand file tree Collapse file tree 2 files changed +46
-3
lines changed Expand file tree Collapse file tree 2 files changed +46
-3
lines changed Original file line number Diff line number Diff line change 1
1
module HTML
2
2
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
4
8
class HttpsFilter < Filter
5
9
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 |
7
11
element [ 'href' ] = element [ 'href' ] . sub ( /^http:/ , 'https:' )
8
12
end
9
13
doc
10
14
end
15
+
16
+ # Raise error if :base_url undefined
17
+ def validate
18
+ needs :base_url
19
+ end
11
20
end
12
21
end
13
- end
22
+ end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments