Skip to content

Commit

Permalink
Change XPath to check for <Span> elements
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Colvard <chris.colvard@gmail.com>
  • Loading branch information
masaball and cjcolvar committed Oct 10, 2023
1 parent 9b4a6d8 commit 6a4c3b2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
6 changes: 3 additions & 3 deletions app/models/iiif_canvas_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def structure_to_iiif_range
def root_to_iiif_range(root_node)
range = div_to_iiif_range(root_node)

if empty_descendants?(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

Expand Down Expand Up @@ -162,8 +162,8 @@ def span_to_iiif_range(span_node)
)
end

def empty_descendants?(node)
node.xpath('.//Div[not(node())]').present?
def only_empty_descendants?(node)
node.xpath('.//Span').empty?
end

FLOAT_PATTERN = Regexp.new(/^\d+([.]\d*)?$/).freeze
Expand Down
49 changes: 33 additions & 16 deletions spec/models/iiif_canvas_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,40 @@
end

context 'with childless divs' 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>' }
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

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 2
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
expect(subject.items.second.items.second.label.to_s).to eq '{"none"=>["Span 1"]}'
expect(subject.items.second.items.second.items.size).to eq 1
expect(subject.items.second.items.second.items.first).to be_a IiifCanvasPresenter
expect(subject.items.second.items.second.items.first.media_fragment).to eq 't=0.0,1.235'
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 6a4c3b2

Please sign in to comment.