Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ferrum_pdf/html_preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module HTMLPreprocessor
# process("Some HTML", "https://example.org")
#
def self.process(html, base_url)
return html if base_url.blank?

base_url += "/" unless base_url.end_with? "/"
protocol = base_url.split("://").first
html = translate_relative_paths(html, base_url) if base_url
Expand Down
5 changes: 5 additions & 0 deletions test/html_preprocessing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class HtmlPreProcessingTest< ActiveSupport::TestCase
assert_equal "<h1>Hello</h1>", FerrumPdf::HTMLPreprocessor.process("<h1>Hello</h1>", "https://example.org")
end

test "does not raise error when url absent" do
assert_equal "<a href=\"/example.pdf\">PDF</a>", FerrumPdf::HTMLPreprocessor.process("<a href=\"/example.pdf\">PDF</a>", nil)
assert_equal "<a href=\"/example.pdf\">PDF</a>", FerrumPdf::HTMLPreprocessor.process("<a href=\"/example.pdf\">PDF</a>", "")
end

test "replaces relative paths" do
assert_equal "<a href=\"http://example.org/example.pdf\">PDF</a>", FerrumPdf::HTMLPreprocessor.process("<a href=\"/example.pdf\">PDF</a>", "http://example.org")
assert_equal "<a href=\"https://example.com/example.pdf\">PDF</a>", FerrumPdf::HTMLPreprocessor.process("<a href=\"/example.pdf\">PDF</a>", "https://example.com")
Expand Down