Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix image alt text when it has double quotes #443

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ def register_media_file node, target, media_type

def resolve_image_attrs node
img_attrs = []
img_attrs << %(alt="#{node.attr 'alt'}") if node.attr? 'alt'
img_attrs << %(alt="#{encode_alt_text node, (node.attr 'alt'), true}") if node.attr? 'alt'

# 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 @@ -1765,6 +1765,11 @@ def class_string node
def role_valid_class? role
role.is_a? String
end

def encode_alt_text node, val, to_attr = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest naming this in a more generic way, like encode_attribute_value from HTML5 converter. We also do not need to escape anything besides " because others are already escaped.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! Will try to implement them and add unit tests. Mine was a quick WIP hack copying code from asciidoctor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, we even already have a function for that: xml_sanitize does exactly what is needed.

val = node.sub_specialchars val if (val.include? '<') || (val.include? '&') || (val.include? '>')
to_attr && (val.include? '"') ? (val.gsub '"', '&quot;') : val
end
end

class DocumentIdGenerator
Expand Down
Loading