Skip to content

Commit ed5a5ca

Browse files
committed
Fix handling of invalid iframe URLs
Fixes freeCodeCamp#590.
1 parent c42ef5d commit ed5a5ca

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

lib/docs/filters/core/normalize_urls.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def update_attribute(tag, attribute)
1515
css(tag.to_s).each do |node|
1616
next unless value = node[attribute]
1717
next if fragment_url_string?(value) || data_url_string?(value)
18-
node[attribute] = normalize_url(value)
18+
node[attribute] = normalize_url(value) || (tag == :iframe ? value : '#')
1919
end
2020
end
2121

@@ -31,7 +31,7 @@ def normalize_url(str)
3131

3232
url.to_s
3333
rescue URI::InvalidURIError
34-
'#'
34+
nil
3535
end
3636

3737
def to_absolute_url(str)

test/lib/docs/filters/core/normalize_urls_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ class NormalizeUrlsFilterTest < MiniTest::Spec
3939
assert_equal link_to(context[:url]), filter_output_string
4040
end
4141

42-
it "rewrites invalid relative urls" do
42+
it "rewrites invalid link urls" do
4343
@body = link_to '%'
4444
assert_equal link_to('#'), filter_output_string
4545
end
4646

47+
it "rewrites invalid image urls" do
48+
@body = '<img src="%">'
49+
assert_equal '<img src="#">', filter_output_string
50+
end
51+
52+
it "doesn't rewrite invalid iframe urls" do
53+
@body = '<iframe src="%"></iframe>'
54+
assert_equal @body, filter_output_string
55+
end
56+
4757
it "repairs un-encoded spaces" do
4858
@body = link_to 'http://example.com/#foo bar '
4959
assert_equal link_to('http://example.com/#foo%20bar'), filter_output_string

0 commit comments

Comments
 (0)