# frozen_string_literal: true
module Html2rss
module AttributePostProcessors
module HtmlTransformers
##
# Transformer that wraps tags into tags linking to `img.src`.
class WrapImgInA
##
# Wraps tags into tags that link to `img.src`.
#
# @param node_name [String]
# @param node [Nokogiri::XML::Node]
# @return [nil]
def call(node_name:, node:, **_env)
return unless already_wrapped?(node_name, node)
wrap_image_in_anchor(node)
end
def already_wrapped?(node_name, node)
node_name == 'img' && node.parent.name != 'a'
end
private
##
# Wraps the node in an tag.
#
# @param node [Nokogiri::XML::Node]
# @return [nil]
def wrap_image_in_anchor(node)
anchor = Nokogiri::XML::Node.new('a', node.document)
anchor['href'] = node['src']
node.add_next_sibling(anchor)
anchor.add_child(node.remove)
end
end
end
end
end