mirror of https://github.com/freeCodeCamp/devdocs
parent
6f0214eaf3
commit
4e41ed9f25
@ -0,0 +1,21 @@
|
||||
module Docs
|
||||
class ApplyBaseUrlFilter < Filter
|
||||
URL_ATTRIBUTES = { 'a': 'href', 'img': 'src', 'iframe': 'src' }
|
||||
SCHEME_RGX = /\A[^:\/?#]+:/
|
||||
|
||||
def call
|
||||
base_url = at_css('base').try(:[], 'href')
|
||||
return doc unless base_url
|
||||
|
||||
URL_ATTRIBUTES.each_pair do |tag, attribute|
|
||||
css(tag).each do |node|
|
||||
next unless value = node[attribute]
|
||||
next if !relative_url_string?(value) || value[0] == '/'.freeze
|
||||
node[attribute] = "#{base_url}#{node[attribute]}"
|
||||
end
|
||||
end
|
||||
|
||||
doc
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,69 @@
|
||||
require 'test_helper'
|
||||
require 'docs'
|
||||
|
||||
class ApplyBaseUrlFilterTest < MiniTest::Spec
|
||||
include FilterTestHelper
|
||||
self.filter_class = Docs::ApplyBaseUrlFilter
|
||||
self.filter_type = 'html'
|
||||
|
||||
context "when there is no <base>" do
|
||||
it "does nothing" do
|
||||
@body = make_body nil, link_to('test')
|
||||
assert_equal link_to('test'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
end
|
||||
|
||||
context "when <base> is '/base/'" do
|
||||
it "rewrites relative urls" do
|
||||
@body = make_body '/base/', link_to('path#frag')
|
||||
assert_equal link_to('/base/path#frag'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "rewrites relative image urls" do
|
||||
@body = make_body '/base/', '<img src="../img.png">'
|
||||
assert_equal '<img src="/base/../img.png">', filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "rewrites relative iframe urls" do
|
||||
@body = make_body '/base/', '<iframe src="./test"></iframe>'
|
||||
assert_equal '<iframe src="/base/./test"></iframe>', filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "doesn't rewrite absolute urls" do
|
||||
@body = make_body '/base/', link_to('http://example.com')
|
||||
assert_equal link_to('http://example.com'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "doesn't rewrite protocol-less urls" do
|
||||
@body = make_body '/base/', link_to('//example.com')
|
||||
assert_equal link_to('//example.com'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "doesn't rewrite root-relative urls" do
|
||||
@body = make_body '/base/', link_to('/path')
|
||||
assert_equal link_to('/path'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "doesn't rewrite fragment-only urls" do
|
||||
@body = make_body '/base/', link_to('#test')
|
||||
assert_equal link_to('#test'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "doesn't rewrite email urls" do
|
||||
@body = make_body '/base/', link_to('mailto:test@example.com')
|
||||
assert_equal link_to('mailto:test@example.com'), filter_output.at_css('body').inner_html
|
||||
end
|
||||
|
||||
it "doesn't rewrite data urls" do
|
||||
@body = make_body '/base/', '<img src="data:image/gif;base64,aaaa">'
|
||||
assert_equal '<img src="data:image/gif;base64,aaaa">', filter_output.at_css('body').inner_html
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def make_body(base, body)
|
||||
base = %(<base href="#{base}">) if base
|
||||
"<html><meta charset=utf-8><title></title>#{base}#{body}</html>"
|
||||
end
|
||||
end
|
Loading…
Reference in new issue