diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 3313b217..948fffcc 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -10,6 +10,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ * add XML declarations to XHTML files (#424 by @abbrev) * bump the oldest supported Ruby to 2.6 * bump the oldest supported Asciidoctor to 2.0 +* escape double quotes in alt text == 1.5.1 (2021-04-29) - @slonopotamus diff --git a/lib/asciidoctor-epub3/converter.rb b/lib/asciidoctor-epub3/converter.rb index e5a228e8..93b3baee 100644 --- a/lib/asciidoctor-epub3/converter.rb +++ b/lib/asciidoctor-epub3/converter.rb @@ -1057,9 +1057,14 @@ def register_media_file(node, target, media_type) @media_files[target] ||= { path: fs_path, media_type: media_type } end + # @param node [Asciidoctor::Block] + # @return [Array] def resolve_image_attrs(node) img_attrs = [] - img_attrs << %(alt="#{node.attr 'alt'}") if node.attr? 'alt' + + unless (alt = encode_attribute_value(node.alt)).empty? + img_attrs << %(alt="#{alt}") + end # Unlike browsers, Calibre/Kindle *do* scale image if only height is specified # So, in order to match browser behavior, we just always omit height @@ -1145,6 +1150,8 @@ def convert_video(node) ) end + # @param node [Asciidoctor::Block] + # @return [String] def convert_image(node) target = node.image_uri node.attr 'target' register_media_file node, target, 'image' @@ -1333,6 +1340,10 @@ def output_content(node) node.content_model == :simple ? %(

#{node.content}

) : node.content end + def encode_attribute_value(val) + val.gsub '"', '"' + end + # FIXME: merge into with xml_sanitize helper def xml_sanitize(value, target = :attribute) sanitized = value.include?('<') ? value.gsub(XML_ELEMENT_RX, '').strip.tr_s(' ', ' ') : value diff --git a/spec/fixtures/image-dimensions/chapter.adoc b/spec/fixtures/image-dimensions/chapter.adoc index 4080080b..b645a4f2 100644 --- a/spec/fixtures/image-dimensions/chapter.adoc +++ b/spec/fixtures/image-dimensions/chapter.adoc @@ -4,4 +4,4 @@ image::square.png[100x100,100,100] image::square.png[50x50,50,50] image::square.png[50x?,50] image::square.png[25%x?,25%] -image::square.png[invalid,25em] +image::square.png[invalid",25em] diff --git a/spec/image_spec.rb b/spec/image_spec.rb index f9bd2ea7..e178af81 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -78,7 +78,7 @@ expect(chapter.content).to include '50x50' expect(chapter.content).to include '50x?' expect(chapter.content).to include '25%x?' - expect(chapter.content).to include 'invalid' + expect(chapter.content).to include 'invalid"' end # If this test fails for you, make sure you're using gepub >= 1.0.11