Utilize ERB template in Youtube/Vimeo liquid tag (#2728) [ci skip]

This commit is contained in:
Mario See 2019-05-09 12:44:00 -04:00 committed by Mac Siri
parent e626002260
commit 20a50fb0f8
8 changed files with 68 additions and 57 deletions

View file

@ -1,23 +1,22 @@
class VimeoTag < LiquidTagBase
PARTIAL = "liquids/vimeo".freeze
def initialize(tag_name, token, tokens)
super
@id = id_for token
@id = id_for(token)
@width = 710
@height = 399
end
def render(_context)
finalize_html <<~HTML
<iframe
src="https://player.vimeo.com/video/#{@id}"
width="#{@width}"
height="#{@height}"
frameborder="0"
webkitallowfullscreen
mozallowfullscreen
allowfullscreen>
</iframe>
HTML
ActionController::Base.new.render_to_string(
partial: PARTIAL,
locals: {
id: @id,
width: @width,
height: @height
},
)
end
private

View file

@ -1,4 +1,6 @@
class YoutubeTag < LiquidTagBase
PARTIAL = "liquids/youtube".freeze
def initialize(tag_name, id, tokens)
super
@id = parse_id(id)
@ -7,15 +9,14 @@ class YoutubeTag < LiquidTagBase
end
def render(_context)
html = <<-HTML
<iframe
width="#{@width}"
height="#{@height}"
src="https://www.youtube.com/embed/#{@id}"
allowfullscreen>
</iframe>
HTML
finalize_html(html)
ActionController::Base.new.render_to_string(
partial: PARTIAL,
locals: {
id: @id,
width: @width,
height: @height
},
)
end
private

View file

@ -0,0 +1,9 @@
<iframe
src="https://player.vimeo.com/video/<%= id %>"
width="<%= width %>"
height="<%= height %>"
frameborder="0"
webkitallowfullscreen
mozallowfullscreen
allowfullscreen>
</iframe>

View file

@ -0,0 +1,6 @@
<iframe
width="<%= width %>"
height="<%= height %>"
src="https://www.youtube.com/embed/<%= id %>"
allowfullscreen>
</iframe>

View file

@ -3,19 +3,7 @@ require "rails_helper"
RSpec.describe YoutubeTag, type: :liquid_template do
describe "#id" do
let(:valid_id_no_time) { "dQw4w9WgXcQ" }
let(:valid_ids_with_time) do
%w[
QASbw8_0meM?t=8h12m26s
QASbw8_0meM?t=6h34m
QASbw8_0meM?t=7h
QASbw8_0meM?t=1h57s
dQw4w9WgXcQ?t=4m45s
dQw4w9WgXcQ?t=5m
dQw4w9WgXcQ?t=8s
]
end
let(:valid_ids_with_time) { "QASbw8_0meM?t=8h12m26s" }
let(:invalid_id) { Faker::Lorem.characters(rand(12..100)) }
def parsed_id(id)
@ -36,37 +24,24 @@ RSpec.describe YoutubeTag, type: :liquid_template do
Liquid::Template.parse("{% youtube #{id} %}")
end
def generate_iframe(id)
"<iframe "\
"width=\"710\" "\
"height=\"399\" "\
"src=\"https://www.youtube.com/embed/#{parsed_id(id)}\" "\
"allowfullscreen> "\
"</iframe>"
end
it "accepts a valid YouTube ID with no starting time" do
liquid = generate_new_liquid(valid_id_no_time)
expect(liquid.render).to eq(generate_iframe(valid_id_no_time))
liquid = generate_new_liquid(valid_id_no_time).render
Approvals.verify(liquid, name: "youtube_liquid_tag_no_time", format: :html)
end
it "accepts valid YouTube IDs with starting times" do
valid_ids_with_time.each do |id|
generated_liquid = generate_new_liquid(id)
expect(generated_liquid.render).to eq generate_iframe(id)
end
it "accepts valid YouTube ID with starting times" do
liquid = generate_new_liquid(valid_ids_with_time).render
Approvals.verify(liquid, name: "youtube_liquid_tag_with_time", format: :html)
end
it "accepts YouTube ID with no start time and an empty space" do
liquid = generate_new_liquid(valid_id_no_time + " ")
expect(liquid.render).to eq(generate_iframe(valid_id_no_time))
liquid = generate_new_liquid(valid_id_no_time + " ").render
Approvals.verify(liquid, name: "youtube_liquid_tag_no_time_trailing_space", format: :html)
end
it "accepts YouTube IDs with start times and one empty space" do
valid_ids_with_time.each do |id|
generated_liquid = generate_new_liquid(id + " ")
expect(generated_liquid.render).to eq generate_iframe(id)
end
it "accepts YouTube ID with start times and one empty space" do
liquid = generate_new_liquid(valid_ids_with_time + " ").render
Approvals.verify(liquid, name: "youtube_liquid_tag_with_time", format: :html)
end
it "raises an error for invalid IDs" do

View file

@ -0,0 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<iframe width="710" height="399" src="https://www.youtube.com/embed/dQw4w9WgXcQ" allowfullscreen="">
</iframe>
</body>
</html>

View file

@ -0,0 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<iframe width="710" height="399" src="https://www.youtube.com/embed/dQw4w9WgXcQ" allowfullscreen="">
</iframe>
</body>
</html>

View file

@ -0,0 +1,7 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<iframe width="710" height="399" src="https://www.youtube.com/embed/QASbw8_0meM?start=29546" allowfullscreen="">
</iframe>
</body>
</html>