Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def paragraphs
def bookmarks
bkmrks_hsh = {}
bkmrks_ary = @doc.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node }
# also scan headers and footers so their bookmarks can be read and edited
bkmrks_ary += headers.values.flat_map { |h| h.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node } }
bkmrks_ary += footers.values.flat_map { |f| f.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node } }
# auto-generated by office 2010
bkmrks_ary.reject! { |b| b.name == '_GoBack' }
bkmrks_ary.each { |b| bkmrks_hsh[b.name] = b }
Expand Down
36 changes: 36 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,42 @@
end
end

describe 'bookmarks in headers and footers' do
before do
@doc = Docx::Document.open(@fixtures_path + '/multi_doc_bookmarks.docx')
@new_doc_path = @fixtures_path + '/multi_doc_bookmarks_saved.docx'
end

after do
File.delete(@new_doc_path) if File.exist?(@new_doc_path)
end

it 'includes bookmarks found in headers and footers' do
expect(@doc.bookmarks['header_bookmark']).to_not be_nil
expect(@doc.bookmarks['footer_bookmark']).to_not be_nil
end

it 'allows inserting text at a header bookmark' do
@doc.bookmarks['header_bookmark'].insert_text_after('Inserted ')
expect(@doc.headers['header1'].text).to eq 'Inserted Hello from the header.'
end

it 'allows inserting text at a footer bookmark' do
@doc.bookmarks['footer_bookmark'].insert_text_after('Inserted ')
expect(@doc.footers['footer1'].text).to eq 'Inserted Hello from the footer.'
end

it 'persists header and footer bookmark edits after save' do
@doc.bookmarks['header_bookmark'].insert_text_after('Inserted ')
@doc.bookmarks['footer_bookmark'].insert_text_after('Inserted ')
@doc.save(@new_doc_path)

reopened = Docx::Document.open(@new_doc_path)
expect(reopened.headers['header1'].text).to eq 'Inserted Hello from the header.'
expect(reopened.footers['footer1'].text).to eq 'Inserted Hello from the footer.'
end
end

describe 'read tables' do
before do
@doc = Docx::Document.open(@fixtures_path + '/tables.docx')
Expand Down
Binary file added spec/fixtures/multi_doc_bookmarks.docx
Binary file not shown.
Loading