Skip to content

Commit

Permalink
resolves asciidoctor#443 escape double quotes in alt text
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Jan 7, 2024
1 parent f415911 commit 5d9141f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>]
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
Expand Down Expand Up @@ -1145,6 +1150,8 @@ def convert_video(node)
</figure>)
end

# @param node [Asciidoctor::Block]
# @return [String]
def convert_image(node)
target = node.image_uri node.attr 'target'
register_media_file node, target, 'image'
Expand Down Expand Up @@ -1333,6 +1340,10 @@ def output_content(node)
node.content_model == :simple ? %(<p>#{node.content}</p>) : node.content
end

def encode_attribute_value(val)
val.gsub '"', '&quot;'
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
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/image-dimensions/chapter.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion spec/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
expect(chapter.content).to include '<img src="square.png" alt="50x50" width="50" />'
expect(chapter.content).to include '<img src="square.png" alt="50x?" width="50" />'
expect(chapter.content).to include '<img src="square.png" alt="25%x?" style="width: 25%" />'
expect(chapter.content).to include '<img src="square.png" alt="invalid" width="25em" />'
expect(chapter.content).to include '<img src="square.png" alt="invalid&quot;" width="25em" />'
end

# If this test fails for you, make sure you're using gepub >= 1.0.11
Expand Down

0 comments on commit 5d9141f

Please sign in to comment.