FIX: prefer data-original-href attribute to get iframe URL

FIX: prefer data-original-href attribute to get iframe URL

diff --git a/lib/email/styles.rb b/lib/email/styles.rb
index 701c9ec..04f2935 100644
--- a/lib/email/styles.rb
+++ b/lib/email/styles.rb
@@ -150,7 +150,7 @@ module Email
             next
           end
 
-          src_uri = URI(i['src'])
+          src_uri = i["data-original-href"].present? ? URI(i["data-original-href"]) : URI(i['src'])
           # If an iframe is protocol relative, use SSL when displaying it
           display_src = "#{src_uri.scheme || 'https'}://#{src_uri.host}#{src_uri.path}#{src_uri.query.nil? ? '' : '?' + src_uri.query}#{src_uri.fragment.nil? ? '' : '#' + src_uri.fragment}"
           i.replace "<p><a href='#{src_uri.to_s}'>#{CGI.escapeHTML(display_src)}</a><p>"
diff --git a/spec/components/email/styles_spec.rb b/spec/components/email/styles_spec.rb
index d99440d..8e85d7b 100644
--- a/spec/components/email/styles_spec.rb
+++ b/spec/components/email/styles_spec.rb
@@ -106,6 +106,15 @@ describe Email::Styles do
       expect(frag.at('iframe')).to be_blank
       expect(frag.at('a')).to be_blank
     end
+
+    it "prefers data-original-href attribute to get iframe link" do
+      original_url = "https://vimeo.com/329875646"
+      iframe_url = "https://player.vimeo.com/video/329875646"
+      frag = html_fragment("<iframe src=\"#{iframe_url}\" data-original-href=\"#{original_url}\"></iframe>")
+      expect(frag.at('iframe')).to be_blank
+      expect(frag.at('a')).to be_present
+      expect(frag.at('a')['href']).to eq(original_url)
+    end
   end
 
   context "rewriting protocol relative URLs to the forum" do

GitHub sha: bd1db186

1 Like