Skip to content

Commit

Permalink
Merge pull request #5414 from avalonmediasystem/canvas_reference
Browse files Browse the repository at this point in the history
Add canvas reference to root range when childless divs are present
  • Loading branch information
masaball authored Oct 11, 2023
2 parents 69f159b + 6a4c3b2 commit 3215aee
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
16 changes: 15 additions & 1 deletion app/models/iiif_canvas_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ def simple_iiif_range(label = stream_info[:embed_title])
end

def structure_to_iiif_range
div_to_iiif_range(structure_ng_xml.root)
root_to_iiif_range(structure_ng_xml.root)
end

def root_to_iiif_range(root_node)
range = div_to_iiif_range(root_node)

if only_empty_descendants?(root_node)
range.items.prepend(IiifCanvasPresenter.new(master_file: master_file, stream_info: stream_info, media_fragment: "t=0,#{stream_info[:duration]}"))
end

return range
end

def div_to_iiif_range(div_node)
Expand Down Expand Up @@ -152,6 +162,10 @@ def span_to_iiif_range(span_node)
)
end

def only_empty_descendants?(node)
node.xpath('.//Span').empty?
end

FLOAT_PATTERN = Regexp.new(/^\d+([.]\d*)?$/).freeze

def parse_hour_min_sec(s)
Expand Down
40 changes: 39 additions & 1 deletion spec/models/iiif_canvas_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,45 @@
expect(subject.label.to_s).to eq "{\"none\"=>[\"#{master_file.embed_title}\"]}"
expect(subject.items.size).to eq 1
expect(subject.items.first).to be_a IiifCanvasPresenter
expect(subject.items.first.media_fragment).to eq "t=0,#{(master_file.duration.to_f)/1000}"
expect(subject.items.first.media_fragment).to eq "t=0,#{master_file.duration.to_f/1000}"
end
end

context 'with childless divs' do
context 'without spans' do
let(:structure_xml) { '<?xml version="1.0"?><Item label="Test"><Div label="Div 1"><Div label="Div 2"><Div label="Div 3"/></Div></Div></Item>' }

it 'generates a canvas reference in the root range' do
expect(subject.label.to_s).to eq '{"none"=>["Test"]}'
expect(subject.items.size).to eq 2
expect(subject.items.first).to be_a IiifCanvasPresenter
expect(subject.items.first.media_fragment).to eq "t=0,#{master_file.duration.to_f/1000}"
expect(subject.items.second.label.to_s).to eq '{"none"=>["Div 1"]}'
expect(subject.items.second.items.size).to eq 1
expect(subject.items.second.items.first.label.to_s).to eq '{"none"=>["Div 2"]}'
expect(subject.items.second.items.first.items.size).to eq 1
expect(subject.items.second.items.first.items.first.label.to_s).to eq '{"none"=>["Div 3"]}'
expect(subject.items.second.items.first.items.first.items.size).to eq 0
end
end

context 'with spans' do
let(:structure_xml) { '<?xml version="1.0"?><Item label="Test"><Div label="Div 1"><Div label="Div 2"><Div label="Div 3"/></Div><Span label="Span 1" begin="00:00:00.000" end="00:00:01.235"/></Div></Item>' }

it 'does not generate a canvas reference in the root range' do
expect(subject.label.to_s).to eq '{"none"=>["Test"]}'
expect(subject.items.size).to eq 1
expect(subject.items.first.label.to_s).to eq '{"none"=>["Div 1"]}'
expect(subject.items.first.items.size).to eq 2
expect(subject.items.first.items.first.label.to_s).to eq '{"none"=>["Div 2"]}'
expect(subject.items.first.items.first.items.size).to eq 1
expect(subject.items.first.items.first.items.first.label.to_s).to eq '{"none"=>["Div 3"]}'
expect(subject.items.first.items.first.items.first.items.size).to eq 0
expect(subject.items.first.items.second.label.to_s).to eq '{"none"=>["Span 1"]}'
expect(subject.items.first.items.second.items.size).to eq 1
expect(subject.items.first.items.second.items.first).to be_a IiifCanvasPresenter
expect(subject.items.first.items.second.items.first.media_fragment).to eq 't=0.0,1.235'
end
end
end
end
Expand Down

0 comments on commit 3215aee

Please sign in to comment.