Class: DragonflySvg::Processors::ExtendIds

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_svg/processors/extend_ids.rb

Instance Method Summary collapse

Instance Method Details

#call(content, append_str = SecureRandom.urlsafe_base64(8), options = {}) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dragonfly_svg/processors/extend_ids.rb', line 7

def call(content, append_str = SecureRandom.urlsafe_base64(8), options = {})
  raise UnsupportedFormat unless content.ext
  raise UnsupportedFormat unless SUPPORTED_FORMATS.include?(content.ext.downcase)

  doc = Nokogiri::XML(content.data)

  # nodes with id attributes
  doc.xpath('//*[@id]').each do |node|
    node_id = node.get_attribute 'id'
    node.set_attribute 'id', [node_id, append_str].join('-')
  end

  # nodes with id references
  doc.xpath('//*[@href]').each do |node|
    node_href = node.get_attribute 'href'
    node.set_attribute 'href', [node_href, append_str].join('-')
  end

  content.update(doc.to_xml, 'name' => 'temp.svg')
end