youtube tag timestamp format fix (#16873)

* youtube tag timestamp format fix

* spec update
This commit is contained in:
yheuhtozr 2022-03-22 23:55:45 +09:00 committed by GitHub
parent 26e10fc71e
commit 8b8d14479b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View file

@ -1,8 +1,8 @@
class YoutubeTag < LiquidTagBase
PARTIAL = "liquids/youtube".freeze
# rubocop:disable Layout/LineLength
REGISTRY_REGEXP = %r{https?://(?:www\.)?(?:youtube\.com|youtu\.be)/(?:embed/|watch\?v=)?(?<video_id>[a-zA-Z0-9_-]{11})(?:(?:&|\?)(?:t=|start=)(?<time_parameter>(?:\d{1,}h)?(?:\d{1,2}m)?(?:\d{1,2}s)?))?}
VALID_ID_REGEXP = /\A(?<video_id>[a-zA-Z0-9_-]{11})(?:(?:&|\?)(?:t=|start=)(?<time_parameter>(?:\d{1,}h)?(?:\d{1,2}m)?(?:\d{1,2}s)?))?\Z/
REGISTRY_REGEXP = %r{https?://(?:www\.)?(?:youtube\.com|youtu\.be)/(?:embed/|watch\?v=)?(?<video_id>[a-zA-Z0-9_-]{11})(?:(?:&|\?)(?:t=|start=)(?<time_parameter>\d+s?|(?:\d{1,}h)?(?:\d{1,2}m)?(?:\d{1,2}s)?))?}
VALID_ID_REGEXP = /\A(?<video_id>[a-zA-Z0-9_-]{11})(?:(?:&|\?)(?:t=|start=)(?<time_parameter>\d+s?|(?:\d{1,}h)?(?:\d{1,2}m)?(?:\d{1,2}s)?))?\Z/
# rubocop:enable Layout/LineLength
REGEXP_OPTIONS = [REGISTRY_REGEXP, VALID_ID_REGEXP].freeze
@ -47,7 +47,7 @@ class YoutubeTag < LiquidTagBase
end
def translate_start_time(video_id, time_parameter)
return "#{video_id}?start=#{time_parameter}" if time_parameter.match?(/\A\d+\Z/)
return "#{video_id}?start=#{time_parameter.delete_suffix('s')}" if time_parameter.match?(/\A\d+s?\Z/)
time_elements = time_parameter.split(/[a-z]/)
time_markers = time_parameter.split(/\d+/)[1..]

View file

@ -4,6 +4,8 @@ RSpec.describe YoutubeTag, type: :liquid_tag do
describe "#id" do
let(:valid_id_no_time) { "fhH5xX_yW6U" }
let(:valid_id_with_time) { "fhH5xX_yW6U?t=0h5m0s" }
let(:valid_id_with_time_num) { "fhH5xX_yW6U?t=300" }
let(:valid_id_with_time_sec) { "fhH5xX_yW6U?t=300s" }
let(:invalid_id) { Faker::Lorem.characters(number: rand(12..100)) }
def generate_new_liquid(id)
@ -26,6 +28,20 @@ RSpec.describe YoutubeTag, type: :liquid_tag do
expect(liquid).to include('src="https://www.youtube.com/embed/fhH5xX_yW6U?start=300"')
end
it "accepts valid YouTube ID with starting time as integer" do
liquid = generate_new_liquid(valid_id_with_time_num).render
expect(liquid).to include('<iframe')
expect(liquid).to include('src="https://www.youtube.com/embed/fhH5xX_yW6U?start=300"')
end
it "accepts valid YouTube ID with starting time in seconds" do
liquid = generate_new_liquid(valid_id_with_time_sec).render
expect(liquid).to include('<iframe')
expect(liquid).to include('src="https://www.youtube.com/embed/fhH5xX_yW6U?start=300"')
end
it "accepts YouTube ID with no start time and an empty space" do
liquid = generate_new_liquid("#{valid_id_no_time} ").render
@ -39,6 +55,20 @@ RSpec.describe YoutubeTag, type: :liquid_tag do
expect(liquid).to include('<iframe')
expect(liquid).to include('src="https://www.youtube.com/embed/fhH5xX_yW6U?start=300"')
end
it "accepts YouTube ID with start time as integer and one empty space" do
liquid = generate_new_liquid("#{valid_id_with_time_num} ").render
expect(liquid).to include('<iframe')
expect(liquid).to include('src="https://www.youtube.com/embed/fhH5xX_yW6U?start=300"')
end
it "accepts YouTube ID with start time in seconds and one empty space" do
liquid = generate_new_liquid("#{valid_id_with_time_sec} ").render
expect(liquid).to include('<iframe')
expect(liquid).to include('src="https://www.youtube.com/embed/fhH5xX_yW6U?start=300"')
end
# rubocop:enable Style/StringLiterals
it "raises an error for invalid IDs or URLs" do