FIX: skip hidden <img> (no tracking for you)

This commit is contained in:
Régis Hanol 2017-05-03 19:40:34 +02:00
parent b705634ee1
commit e38014772b
2 changed files with 16 additions and 2 deletions

View File

@ -37,7 +37,7 @@ class HtmlToMarkdown
end
def visit(node)
return if node["style"] && node["style"][/display[[:space:]]*:[[:space:]]*none/]
return if node["style"] && node["style"][/display\s*:\s*none/]
if node.description&.block? && node.parent&.description&.block? && @stack[-1].markdown.size > 0
block = @stack[-1].dup
@ -136,7 +136,7 @@ class HtmlToMarkdown
end
def visit_img(node)
if is_valid_url?(node["src"])
if is_valid_url?(node["src"]) && is_visible_img?(node)
if @opts[:keep_img_tags]
@stack[-1].markdown << node.to_html
else
@ -210,4 +210,11 @@ class HtmlToMarkdown
url.present? && (url.start_with?("http") || url.start_with?("www."))
end
def is_visible_img?(img)
return false if img["width"].present? && img["width"].to_i == 0
return false if img["height"].present? && img["height"].to_i == 0
return false if img["style"].present? && img["style"][/(width|height)\s*:\s*0/]
true
end
end

View File

@ -71,6 +71,13 @@ describe HtmlToMarkdown do
expect(html_to_markdown(%Q{<img src="foo.bar">})).to eq("")
end
it "skips hidden <img>" do
expect(html_to_markdown(%Q{<img src="https://www.discourse.org/logo.svg" width=0>})).to eq("")
expect(html_to_markdown(%Q{<img src="https://www.discourse.org/logo.svg" height="0">})).to eq("")
expect(html_to_markdown(%Q{<img src="https://www.discourse.org/logo.svg" style="width: 0">})).to eq("")
expect(html_to_markdown(%Q{<img src="https://www.discourse.org/logo.svg" style="height:0px">})).to eq("")
end
(1..6).each do |n|
it "converts <h#{n}>" do
expect(html_to_markdown("<h#{n}>Header #{n}</h#{n}>")).to eq("#" * n + " Header #{n}")