Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Do not transform vimeo or youtube url inside anchor tag #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/auto_html/vimeo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,47 @@ module AutoHtml
class Vimeo
include TagHelper

def initialize(width: 420, height: 315, allow_fullscreen: true)
def initialize(width: 420, height: 315, allow_fullscreen: true, lazy: false)
@width = width
@height = height
@allow_fullscreen = allow_fullscreen
@lazy = lazy
end

def call(text)
text.gsub(vimeo_pattern) do
vimeo_id = Regexp.last_match(2)
src = src_url(vimeo_id)
tag(:div, class: 'video vimeo') do
tag(:iframe, { src: src }.merge(iframe_attributes)) { '' }
tag(:div, class: 'responsive-embed') do
tag(:iframe, iframe_attributes(vimeo_id)) { '' }
end
end
end
end

private

def vimeo_pattern
@vimeo_pattern ||= %r{https?://(www.)?vimeo\.com/([A-Za-z0-9._%-]*)((\?|#)\S+)?}
@vimeo_pattern ||= %r{(?<!href=["'])https?://(www.)?vimeo\.com/([A-Za-z0-9._%-]*)((\?|#)\S+)?}
end

def src_url(vimeo_id)
"//player.vimeo.com/video/#{vimeo_id}"
end

def iframe_attributes
def iframe_attributes(vimeo_id)
src = src_url(vimeo_id)
{}.tap do |attrs|
attrs[:width] = @width
attrs[:height] = @height
attrs[:frameborder] = 0
attrs.merge!(fullscreen_attributes) if @allow_fullscreen
attrs.merge!(fullscreen_attributes) if @allow_fullscreen
if @lazy
attrs['data-src'] = src
attrs[:class] = 'lazyload'
else
attrs[:src] = src
end
end
end

Expand Down
29 changes: 23 additions & 6 deletions lib/auto_html/youtube.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ module AutoHtml
class YouTube
include TagHelper

def initialize(width: 420, height: 315)
def initialize(width: 420, height: 315, lazy: false)
@width = width
@height = height
@lazy = lazy
end

def call(text)
text.gsub(youtube_pattern) do
youtube_id = Regexp.last_match(4)
tag(:div, class: 'video youtube') do
tag(:iframe, iframe_attributes(youtube_id)) { '' }
no_href_attr = Regexp.last_match(1).nil?
if no_href_attr
youtube_id = Regexp.last_match(5)
tag(:div, class: 'video youtube') do
tag(:div, class: 'responsive-embed') do
tag(:iframe, iframe_attributes(youtube_id)) { '' }
end
end
else
# No transformation if matches a href attr
Regexp.last_match(0)
end
end
end
Expand All @@ -24,6 +33,7 @@ def call(text)
def youtube_pattern
@youtube_pattern ||=
%r{
(href=['"])?
(https?://)?
(www.)?
(
Expand All @@ -37,13 +47,20 @@ def youtube_pattern

def iframe_attributes(youtube_id)
src = "//www.youtube.com/embed/#{youtube_id}"
{
attrs = {
width: @width,
height: @height,
src: src,
'data-src': src,
frameborder: 0,
allowfullscreen: 'yes'
}
if @lazy
attrs['data-src'] = src
attrs[:class] = 'lazyload'
else
attrs[:src] = src
end
attrs
end
end
end
10 changes: 10 additions & 0 deletions spec/auto_html/vimeo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@
result = filter.call('http://www.vimeo.com/3300155')
expect(result).to eq '<div class="video vimeo"><iframe src="//player.vimeo.com/video/3300155" width="300" height="250" frameborder="0" webkitallowfullscreen="yes" mozallowfullscreen="yes" allowfullscreen="yes"></iframe></div>'
end

it 'does not transform vimeo url inside an anchor tag with double quotes' do
result = subject.call('<a href="http://www.vimeo.com/3300155">vimeo</a>')
expect(result).to eq '<a href="http://www.vimeo.com/3300155">vimeo</a>'
end

it 'does not transform vimeo url inside an anchor tag with single quotes' do
result = subject.call("<a href='http://www.vimeo.com/3300155'>vimeo</a>")
expect(result).to eq "<a href='http://www.vimeo.com/3300155'>vimeo</a>"
end
end
5 changes: 5 additions & 0 deletions spec/auto_html/youtube_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@
result = subject.call('www.youtube.com/watch?v=t7NdBIA4zJg')
expect(result).to eq '<div class="video youtube"><iframe width="420" height="315" src="//www.youtube.com/embed/t7NdBIA4zJg" frameborder="0" allowfullscreen="yes"></iframe></div>'
end

it 'does not transform url inside an anchor tag' do
result = subject.call('<a href="http://www.youtube.com/watch?v=t7NdBIA4zJg">youtube</a>')
expect(result).to eq '<a href="http://www.youtube.com/watch?v=t7NdBIA4zJg">youtube</a>'
end
end