From 9d2cfc2561034f4ce2ec4fabdaadbe2c4489e9c2 Mon Sep 17 00:00:00 2001 From: Marat Radchenko Date: Sun, 12 May 2024 16:16:47 +0300 Subject: resolves #470 do not crash on an SVG image inside table cell (#473) closes #471 --- CHANGELOG.adoc | 1 + lib/asciidoctor-epub3/converter.rb | 11 ++++++++++- spec/fixtures/image-in-table/book.adoc | 9 +++++++++ spec/fixtures/image-in-table/circle.svg | 3 +++ spec/image_spec.rb | 7 +++++++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/image-in-table/book.adoc create mode 100644 spec/fixtures/image-in-table/circle.svg diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 822a584..771f7ce 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -8,6 +8,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ == Unreleased * fix crash when section title contains inline anchor (#472) +* fix crash on an SVG image inside table cell (#470) == 2.1.0 (2024-02-04) - @slonopotamus diff --git a/lib/asciidoctor-epub3/converter.rb b/lib/asciidoctor-epub3/converter.rb index 652e7f0..257d3b0 100644 --- a/lib/asciidoctor-epub3/converter.rb +++ b/lib/asciidoctor-epub3/converter.rb @@ -76,6 +76,10 @@ module Asciidoctor EPUB_EXTENSION_RX = /\.epub$/i.freeze + # This is a workaround for https://github.com/asciidoctor/asciidoctor/issues/4380 + # Currently, there is no access to parent cell from inner document + PARENT_CELL_FIELD_NAME = :@epub3_parent_cell + QUOTE_TAGS = begin tags = { monospaced: ['', '', true], @@ -796,6 +800,7 @@ document.addEventListener('DOMContentLoaded', function(event, reader) { else case cell.style when :asciidoc + cell.inner_document.instance_variable_set(PARENT_CELL_FIELD_NAME, cell) cell_content = %(
#{cell.content}
) when :verse cell_content = %(
#{cell.text}
) @@ -1156,7 +1161,11 @@ document.addEventListener('DOMContentLoaded', function(event, reader) { return nil if node.nil? return node unless get_chapter_filename(node).nil? - node = node.parent + node = if node.instance_variable_defined?(PARENT_CELL_FIELD_NAME) + node.instance_variable_get(PARENT_CELL_FIELD_NAME) + else + node.parent + end end end diff --git a/spec/fixtures/image-in-table/book.adoc b/spec/fixtures/image-in-table/book.adoc new file mode 100644 index 0000000..400621f --- /dev/null +++ b/spec/fixtures/image-in-table/book.adoc @@ -0,0 +1,9 @@ += Image in table +:doctype: book + +== Chapter + +|=== +a| +image::circle.svg[] +|=== diff --git a/spec/fixtures/image-in-table/circle.svg b/spec/fixtures/image-in-table/circle.svg new file mode 100644 index 0000000..3e8733f --- /dev/null +++ b/spec/fixtures/image-in-table/circle.svg @@ -0,0 +1,3 @@ + + + diff --git a/spec/image_spec.rb b/spec/image_spec.rb index e178af8..4200e24 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -81,6 +81,13 @@ describe 'Asciidoctor::Epub3::Converter - Image' do expect(chapter.content).to include 'invalid"' end + # Test for https://github.com/asciidoctor/asciidoctor-epub3/issues/470 + it 'supports image inside table cell' do + book, = to_epub fixture_file('image-in-table/book.adoc') + chapter = book.item_by_href '_chapter.xhtml' + expect(chapter).not_to be_nil + end + # If this test fails for you, make sure you're using gepub >= 1.0.11 it 'adds SVG attribute to EPUB manifest if chapter contains SVG images' do book, = to_epub fixture_file('svg/book.adoc') -- cgit v1.2.3