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

resolves #3694 add support for style attribute to icon macro #3741

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/asciidoctor/converter/html5.rb
Expand Up @@ -1174,9 +1174,15 @@ def convert_inline_footnote node
end
end

# Convert {AbstractNode}s with the {AbstractNode.node_name} +"inline_image"+ to their associated _HTML_ markup
#
# node - the {AbstractNode} to convert
#
# Returns (String) stringified _HTML_ markup representing the inline image node
def convert_inline_image node
if (type = node.type || 'image') == 'icon' && (node.document.attr? 'icons', 'font')
class_attr_val = %(fa fa-#{node.target})
icon_style = node.attr('style', node.document.attr('icon_style',''))[0]
class_attr_val = %(fa#{icon_style} fa-#{node.target})
{ 'size' => 'fa-', 'rotate' => 'fa-rotate-', 'flip' => 'fa-flip-' }.each do |key, prefix|
class_attr_val = %(#{class_attr_val} #{prefix}#{node.attr key}) if node.attr? key
end
Expand Down
75 changes: 52 additions & 23 deletions test/substitutions_test.rb
Expand Up @@ -863,34 +863,63 @@
end
end

test 'an icon macro should be interpreted as an icon if icons are enabled' do
para = block_from_string 'icon:github[]', attributes: { 'icons' => '' }
assert_equal %{<span class="icon"><img src="./images/icons/github.png" alt="github"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
context 'icon macro' do
test 'an icon macro should be interpreted as an icon if icons are enabled' do
para = block_from_string 'icon:github[]', attributes: { 'icons' => '' }
assert_equal %{<span class="icon"><img src="./images/icons/github.png" alt="github"></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro should be interpreted as alt text if icons are disabled' do
para = block_from_string 'icon:github[]'
assert_equal %{<span class="icon">[github]</span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro should be interpreted as alt text if icons are disabled' do
para = block_from_string 'icon:github[]'
assert_equal %{<span class="icon">[github]</span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro should output alt text if icons are disabled and alt is given' do
para = block_from_string 'icon:github[alt="GitHub"]'
assert_equal %{<span class="icon">[GitHub]</span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro should output alt text if icons are disabled and alt is given' do
para = block_from_string 'icon:github[alt="GitHub"]'
assert_equal %{<span class="icon">[GitHub]</span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro should be interpreted as a font-based icon when icons=font' do
para = block_from_string 'icon:github[]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fa fa-github"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
context 'font-based icon' do
test 'an icon macro should be interpreted as a font-based icon when icons=font' do
para = block_from_string 'icon:github[]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fa fa-github"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro with a size should be interpreted as a font-based icon with a size when icons=font' do
para = block_from_string 'icon:github[4x]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fa fa-github fa-4x"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
test 'an icon macro with a size should be interpreted as a font-based icon with a size when icons=font' do
para = block_from_string 'icon:github[4x]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fa fa-github fa-4x"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro with a role and title should be interpreted as a font-based icon with a class and title when icons=font' do
para = block_from_string 'icon:heart[role="red", title="Heart me"]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon red"><i class="fa fa-heart" title="Heart me"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro interpreted as a font-based icon should have an icon style based on the abreviated icon_style document attribute value' do
para = block_from_string 'icon:sass[]', attributes: { 'icons' => 'font', 'icon_style' => 'b' }
assert_equal %{<span class="icon"><i class="fab fa-sass"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro with a role and title should be interpreted as a font-based icon with a class and title when icons=font' do
para = block_from_string 'icon:heart[role="red", title="Heart me"]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon red"><i class="fa fa-heart" title="Heart me"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
test 'an icon macro interpreted as a font-based icon should have an icon style based on the icon_style document attribute value' do
para = block_from_string 'icon:sass[]', attributes: { 'icons' => 'font', 'icon_style' => 'brand' }
assert_equal %{<span class="icon"><i class="fab fa-sass"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro interpreted as a font-based icon should have an icon style based on the abreviated style macro attribute value' do
para = block_from_string 'icon:sass[style="b"]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fab fa-sass"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro interpreted as a font-based icon should have an icon style based on the style macro attribute value' do
para = block_from_string 'icon:sass[style="brand"]', attributes: { 'icons' => 'font' }
assert_equal %{<span class="icon"><i class="fab fa-sass"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end

test 'an icon macro interpreted as a font-based icon should prioratiset the icon style macro attribute value over the icon_style document attribute value' do
para = block_from_string 'icon:sass[style="brand"]', attributes: { 'icons' => 'font', 'icon_style' => 'light' }
assert_equal %{<span class="icon"><i class="fab fa-sass"></i></span>}, para.sub_macros(para.source).gsub(/>\s+</, '><')
end
end
end

test 'a single-line footnote macro should be registered and output as a footnote' do
Expand Down