Commit 47b719c2 authored by Sean McGivern's avatar Sean McGivern

Merge branch '29284-video-preview-not-working' into 'master'

Fix inline videos in Markdown for uppercase extensions

See merge request gitlab-org/gitlab!17581
parents 3a23d865 7c43c310
---
title: Fix inline rendering of videos for uploads with uppercase file extensions
merge_request: 17581
author:
type: fixed
...@@ -8,8 +8,8 @@ module Banzai ...@@ -8,8 +8,8 @@ module Banzai
# a "Download" link in the case the video cannot be played. # a "Download" link in the case the video cannot be played.
class VideoLinkFilter < HTML::Pipeline::Filter class VideoLinkFilter < HTML::Pipeline::Filter
def call def call
doc.xpath(query).each do |el| doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |el|
el.replace(video_node(doc, el)) el.replace(video_node(doc, el)) if has_video_extension?(el)
end end
doc doc
...@@ -17,22 +17,10 @@ module Banzai ...@@ -17,22 +17,10 @@ module Banzai
private private
def query def has_video_extension?(element)
@query ||= begin src_attr = context[:asset_proxy_enabled] ? 'data-canonical-src' : 'src'
src_query = UploaderHelper::SAFE_VIDEO_EXT.map do |ext|
"'.#{ext}' = substring(@src, string-length(@src) - #{ext.size})"
end
if context[:asset_proxy_enabled].present? element.attr(src_attr).downcase.end_with?(*UploaderHelper::SAFE_VIDEO_EXT)
src_query.concat(
UploaderHelper::SAFE_VIDEO_EXT.map do |ext|
"'.#{ext}' = substring(@data-canonical-src, string-length(@data-canonical-src) - #{ext.size})"
end
)
end
"descendant-or-self::img[not(ancestor::a) and (#{src_query.join(' or ')})]"
end
end end
def video_node(doc, element) def video_node(doc, element)
......
...@@ -17,27 +17,32 @@ describe Banzai::Filter::VideoLinkFilter do ...@@ -17,27 +17,32 @@ describe Banzai::Filter::VideoLinkFilter do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
context 'when the element src has a video extension' do shared_examples 'replaces the image tag with a video tag' do |ext|
UploaderHelper::SAFE_VIDEO_EXT.each do |ext| it "replaces the image tag 'path/video.#{ext}' with a video tag" do
it "replaces the image tag 'path/video.#{ext}' with a video tag" do container = filter(link_to_image("/path/video.#{ext}")).children.first
container = filter(link_to_image("/path/video.#{ext}")).children.first
expect(container.name).to eq 'div' expect(container.name).to eq 'div'
expect(container['class']).to eq 'video-container' expect(container['class']).to eq 'video-container'
video, paragraph = container.children
video, paragraph = container.children expect(video.name).to eq 'video'
expect(video['src']).to eq "/path/video.#{ext}"
expect(video.name).to eq 'video' expect(paragraph.name).to eq 'p'
expect(video['src']).to eq "/path/video.#{ext}"
expect(paragraph.name).to eq 'p' link = paragraph.children.first
link = paragraph.children.first expect(link.name).to eq 'a'
expect(link['href']).to eq "/path/video.#{ext}"
expect(link['target']).to eq '_blank'
end
end
expect(link.name).to eq 'a' context 'when the element src has a video extension' do
expect(link['href']).to eq "/path/video.#{ext}" UploaderHelper::SAFE_VIDEO_EXT.each do |ext|
expect(link['target']).to eq '_blank' it_behaves_like 'replaces the image tag with a video tag', ext
end it_behaves_like 'replaces the image tag with a video tag', ext.upcase
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment