forked from gjtorikian/html-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
module HTML | ||
class Pipeline | ||
# HTML Filter for replacing http github urls with https versions. | ||
# HTML Filter for replacing http references to :base_url with https versions. | ||
# Subdomain references are not rewritten. | ||
# | ||
# Context options: | ||
# :base_url - The url to force https | ||
class HttpsFilter < Filter | ||
def call | ||
doc.css('a[href^="http://github.com"]').each do |element| | ||
doc.css(%Q(a[href^="#{context[:base_url]}"])).each do |element| | ||
element['href'] = element['href'].sub(/^http:/,'https:') | ||
end | ||
doc | ||
end | ||
|
||
# Raise error if :base_url undefined | ||
def validate | ||
needs :base_url | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "test_helper" | ||
|
||
HttpsFilter = HTML::Pipeline::HttpsFilter | ||
|
||
class HTML::Pipeline::AutolinkFilterTest < Test::Unit::TestCase | ||
def filter(html, base_url="http://github.com") | ||
HttpsFilter.to_html(html, :base_url => base_url) | ||
end | ||
|
||
def test_http | ||
assert_equal %(<a href="https://github.com">github.com</a>), | ||
filter(%(<a href="http://github.com">github.com</a>)) | ||
end | ||
|
||
def test_https | ||
assert_equal %(<a href="https://github.com">github.com</a>), | ||
filter(%(<a href="https://github.com">github.com</a>)) | ||
end | ||
|
||
def test_subdomain | ||
assert_equal %(<a href="http://help.github.com">github.com</a>), | ||
filter(%(<a href="http://help.github.com">github.com</a>)) | ||
end | ||
|
||
def test_other | ||
assert_equal %(<a href="http://github.io">github.io</a>), | ||
filter(%(<a href="http://github.io">github.io</a>)) | ||
end | ||
|
||
def test_validation | ||
exception = assert_raise(ArgumentError) { HttpsFilter.call(nil, {}) } | ||
assert_match "HTML::Pipeline::HttpsFilter: :base_url", exception.message | ||
end | ||
end |