lib/esvg/symbol.rb in esvg-4.6.0 vs lib/esvg/symbol.rb in esvg-4.6.1
- old
+ new
@@ -81,11 +81,10 @@
end
def data
{
path: @path,
- id: @id,
name: @name,
group: @group,
mtime: @mtime,
size: @size,
content: @content,
@@ -100,14 +99,16 @@
{ id: @id, 'data-name' => @name }.merge @size
end
def use(options={})
+ # If preset key is set, merge presets from configuration
if options[:preset] && preset = @config[:presets][ options.delete(:preset).to_sym ]
options = options.merge( preset )
end
+ # If size key is set, merge size class from configuration
if options[:size] && size_class = @config[:sizes][ options.delete(:size).to_sym ]
options = options.merge( size_class )
end
options.delete(:fallback)
@@ -163,24 +164,27 @@
read if changed?
# Only optimize again if the file has changed
return @optimized if @optimized && @optimized_at && @optimized_at > @mtime
- @optimized = @content
-
if svgo?
- response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@optimized}' -o -})
+ response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@content}' -o -})
if !response[0].empty? && response[2].success?
@optimized = response[0]
@svgo_optimized = true
end
+
+ post_optimize
+ @optimized_at = Time.now.to_i
+
+ @optimized
end
- post_optimize
- @optimized_at = Time.now.to_i
+ end
- @optimized
+ def symbol
+ symbolize( optimize || @content )
end
def changed?
last_modified != mtime
end
@@ -277,20 +281,25 @@
.gsub(/>\s+</, '><') # Remove whitespace between tags
.gsub(/\s?fill="(#0{3,6}|black|none|rgba?\(0,0,0\))"/,'') # Strip black fill
end
def post_optimize
- @optimized = set_attributes
+ @optimized.gsub!(/\w+=""/,'') # Remove empty attributes
+ end
+
+ def symbolize( str )
+ strip_attributes( str )
.gsub(/<\/svg/,'</symbol') # Replace svgs with symbols
.gsub(/\w+=""/,'') # Remove empty attributes
+ .sub(/<svg/, "<symbol #{attributes(attr)}")
end
- def set_attributes
+ def strip_attributes( str )
attr.keys.each do |key|
- @optimized.sub!(/ #{key}=".+?"/,'')
+ str.sub!(/ #{key}=".+?"/,'')
end
- @optimized.sub(/<svg/, "<symbol #{attributes(attr)}")
+ str
end
# Scans <def> blocks for IDs
# If urls(#id) are used, ensure these IDs are unique to this file
# Only replace IDs if urls exist to avoid replacing defs