FEATURE: Enable Typeform support (#394)

FEATURE: Enable Typeform support (#394)

This reverts commit 5a293d67626ab2c099b8a3acfd75ce42d885cc20.

  • Append necessary param to typeform URL so it can be aware that it’s being embedded and avoid focus stealing on re-render

  • Revert placeholder_html to display the iframe preview instead of an image

  • If there are other params, append the embed type at the end.

diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb
index f2cef77..4eb4675 100644
--- a/lib/onebox/engine.rb
+++ b/lib/onebox/engine.rb
@@ -161,6 +161,7 @@ require_relative "engine/slides_onebox"
 require_relative "engine/xkcd_onebox"
 require_relative "engine/giphy_onebox"
 require_relative "engine/gfycat_onebox"
+require_relative "engine/typeform_onebox"
 require_relative "engine/vimeo_onebox"
 require_relative "engine/steam_store_onebox"
 require_relative "engine/sketchfab_onebox"
diff --git a/lib/onebox/engine/typeform_onebox.rb b/lib/onebox/engine/typeform_onebox.rb
new file mode 100644
index 0000000..016d38e
--- /dev/null
+++ b/lib/onebox/engine/typeform_onebox.rb
@@ -0,0 +1,41 @@
+module Onebox
+  module Engine
+    class TypeformOnebox
+      include Engine
+
+      matches_regexp(/^https?:\/\/[a-z0-9]+\.typeform\.com\/to\/[a-zA-Z0-9]+/)
+      always_https
+
+      def to_html
+        typeform_src = build_typeform_src
+
+        <<~HTML
+          <iframe src="#{typeform_src}"
+                  width="100%"
+                  height="600px"
+                  scrolling="no"
+                  frameborder="0">
+          </iframe>
+        HTML
+      end
+      alias placeholder_html to_html
+
+      private
+
+      def build_typeform_src
+        escaped_src = ::Onebox::Helpers.normalize_url_for_output(@url)
+        query_params = CGI::parse(URI::parse(escaped_src).query || '')
+
+        return escaped_src if query_params.has_key?('typeform-embed')
+
+        if query_params.empty?
+          escaped_src << '?' unless escaped_src.end_with?('?')
+        else
+          escaped_src << '&'
+        end
+
+        escaped_src << 'typeform-embed=embed-widget'
+      end
+    end
+  end
+end
diff --git a/spec/lib/onebox/engine/typeform_onebox_spec.rb b/spec/lib/onebox/engine/typeform_onebox_spec.rb
new file mode 100644
index 0000000..500bdc7
--- /dev/null
+++ b/spec/lib/onebox/engine/typeform_onebox_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+describe Onebox::Engine::TypeformOnebox do
+  it 'Appends the embed widget param when is missing' do
+    raw_preview = Onebox.preview('https://basvanleeuwen1.typeform.com/to/NzdRpx').to_s
+
+    query_params = get_query_params(raw_preview)
+
+    expect_to_have_embed_widget(query_params)
+  end
+
+  it 'Uses the URL as it is when the embed widget param is present' do
+    raw_preview = Onebox.preview('https://basvanleeuwen1.typeform.com/to/NzdRpx?typeform-embed=embed-widget').to_s
+
+    query_params = get_query_params(raw_preview)
+
+    expect_to_have_embed_widget(query_params)
+  end
+
+  it 'Does not adds an ? when it is already present' do
+    raw_preview = Onebox.preview('https://basvanleeuwen1.typeform.com/to/NzdRpx?').to_s
+
+    query_params = get_query_params(raw_preview)
+
+    expect_to_have_embed_widget(query_params)
+  end
+
+  it 'Appends it to the end when there are other params present' do
+    raw_preview = Onebox.preview('https://basvanleeuwen1.typeform.com/to/NzdRpx?param1=value1').to_s
+
+    query_params = get_query_params(raw_preview)
+
+    expect_to_have_embed_widget(query_params)
+  end
+
+  def expect_to_have_embed_widget(query_params)
+    expected_widget_type = ['embed-widget']
+    current_widget_type = query_params.fetch('typeform-embed', [])
+
+    expect(current_widget_type).to eq expected_widget_type
+  end
+
+  def get_query_params(raw_preview)
+    preview = Nokogiri::HTML::DocumentFragment.parse(raw_preview)
+
+    form_url = preview.css('iframe').first['src']
+    CGI::parse(URI::parse(form_url).query || '')
+  end
+end

GitHub sha: f65e6cbc