esvg 4.3.0 → 4.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3215d01408d6127b3827a4514024675e5e45f46c
4
- data.tar.gz: 8b1a528f475f4cf98e2c674d8b25f57643906f41
3
+ metadata.gz: 4677251f95c7f1ab30e85a77dbbaecd683703680
4
+ data.tar.gz: f6dcf98027aab09923f34ad3d5065a3a29ed7128
5
5
  SHA512:
6
- metadata.gz: 915d328c730f351de449eab23ac219f8b2ec9aef6df69f05656a5fc020de9c9c62c9a6bcab5a2c68fb5bcecd893d54888a808c42191669e33811a4d74d17461b
7
- data.tar.gz: 7a65f080943ddd4d367b02474ecc0087f8246faa452973e7f1a37fbc10eab47daec0dfc3de0a710a18df6ef8df3e207829b48284c978f20cb046912e512937b4
6
+ metadata.gz: 43e0f618e64866961bc89de0f376e5caa1ec8d68f3f990a286552cdc67b959431c1ddd401599a45c2496c56d831b34a77955753a4700bcbf00df0db6efe959cb
7
+ data.tar.gz: 7ab9ce67fd94776ed55a7004a607bf76ddbfc69ecf6c6580e6cdcfe380217e4a4d1cee48edac794f7d45f290584f663c49d914c5fe86632a5afe585a5bc1bb09
data/lib/esvg/svg.rb CHANGED
@@ -11,6 +11,7 @@ module Esvg
11
11
  @name = name
12
12
  @config = config
13
13
  @symbols = symbols
14
+ @symbol_defs = []
14
15
  @asset = File.basename(name).start_with?('_')
15
16
  @version = @config[:version] || Digest::MD5.hexdigest(symbols.map(&:mtime).join)
16
17
  end
@@ -65,10 +66,6 @@ module Esvg
65
66
  end
66
67
  end
67
68
 
68
- def optimize
69
- @symbols.map(&:optimize).join.gsub("\n",'')
70
- end
71
-
72
69
  def svg
73
70
  attr = {
74
71
  "data-symbol-class" => @config[:class],
@@ -76,7 +73,13 @@ module Esvg
76
73
  "version" => "1.1",
77
74
  "style" => "height:0;position:absolute"
78
75
  }
79
- %Q{<svg id="esvg-#{id}" #{attributes(attr)}>#{optimize}</svg>}
76
+
77
+ defs = @symbols.map(&:defs).compact.join
78
+ defs = "<defs>#{defs}</defs>" unless defs.empty?
79
+
80
+ optimized = @symbols.map(&:optimize).join.gsub("\n",'')
81
+
82
+ %Q{<svg id="esvg-#{id}" #{attributes(attr)}>#{defs}#{optimized}</svg>}
80
83
  end
81
84
 
82
85
  end
data/lib/esvg/symbol.rb CHANGED
@@ -2,7 +2,7 @@ require 'open3'
2
2
 
3
3
  module Esvg
4
4
  class Symbol
5
- attr_reader :name, :id, :path, :content, :optimized, :size, :group, :mtime
5
+ attr_reader :name, :id, :path, :content, :optimized, :size, :group, :mtime, :defs
6
6
 
7
7
  include Esvg::Utils
8
8
 
@@ -17,25 +17,24 @@ module Esvg
17
17
  return if !File.exist?(@path)
18
18
 
19
19
  time = last_modified
20
- if @mtime != time
21
- @mtime = time
22
- @content = pre_optimize File.read(@path)
23
- @size = dimensions
24
- @optimized = nil
25
- @optimized_at = nil
26
- end
27
20
 
28
21
  # Ensure that cache optimization matches current optimization settings
29
22
  # If config has changed name, reset optimized build (name gets baked in)
30
- if @svgo_optimized != svgo? || name != file_name
23
+ if @mtime != time || @svgo_optimized != svgo? || name != file_name
31
24
  @optimized = nil
32
25
  @optimized_at = nil
33
26
  end
34
-
27
+
35
28
  @group = dir_key
36
29
  @name = file_name
37
30
  @id = file_id file_key
38
31
 
32
+ if @mtime != time
33
+ @content = prep_defs pre_optimize File.read(@path)
34
+ @mtime = time
35
+ @size = dimensions
36
+ end
37
+
39
38
  self
40
39
  end
41
40
 
@@ -56,6 +55,7 @@ module Esvg
56
55
  mtime: @mtime,
57
56
  size: @size,
58
57
  content: @content,
58
+ defs: @defs,
59
59
  optimized: @optimized,
60
60
  optimized_at: @optimized_at,
61
61
  svgo_optimized: svgo? && @svgo_optimized
@@ -138,8 +138,6 @@ module Esvg
138
138
  return @optimized if @optimized && @optimized_at > @mtime
139
139
 
140
140
  @optimized = @content
141
- sub_def_ids
142
- move_defs
143
141
 
144
142
  if svgo?
145
143
  response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@optimized}' -o -})
@@ -227,6 +225,7 @@ module Esvg
227
225
  end
228
226
 
229
227
  def pre_optimize(svg)
228
+
230
229
  # Generate a regex of attributes to be removed
231
230
  att = Regexp.new %w(xmlns xmlns:xlink xml:space version).map { |m| "#{m}=\".+?\"" }.join('|')
232
231
 
@@ -244,7 +243,6 @@ module Esvg
244
243
  def post_optimize
245
244
  @optimized = set_attributes
246
245
  .gsub(/<\/svg/,'</symbol') # Replace svgs with symbols
247
- .gsub(/class="def-/,'id="def-') # Replace <def> classes with ids (classes are generated in sub_def_ids)
248
246
  .gsub(/\w+=""/,'') # Remove empty attributes
249
247
  end
250
248
 
@@ -261,36 +259,28 @@ module Esvg
261
259
  # Only replace IDs if urls exist to avoid replacing defs
262
260
  # used in other svg files
263
261
  #
264
- def sub_def_ids
265
- @optimized.scan(/<defs>.+<\/defs>/m).flatten.each do |defs|
266
- defs.scan(/id="(.+?)"/).flatten.uniq.each_with_index do |id, index|
267
-
268
- # If there are urls which refer to
269
- # ids be sure to update both
270
- #
271
- if @optimized.match(/url\(##{id}\)/)
272
- new_id = "def-#{@id}-#{index}"
273
-
274
- @optimized = @optimized.gsub(/id="#{id}"/, %Q{class="#{new_id}"})
275
- .gsub(/url\(##{id}\)/, "url(##{new_id})")
276
-
277
- # Otherwise just leave the IDs of the
278
- # defs and change them to classes to
279
- # avoid SVGO ID mangling
280
- #
281
- else
282
- @optimized = @optimized.gsub /id="#{id}"/, %Q{class="#{id}"}
262
+ def prep_defs(svg)
263
+
264
+ # <defs> should be moved to the beginning of the SVG file for braod browser support. Ahem, Firefox ಠ_ಠ
265
+ # When symbols are reassembled, @defs will be added back
266
+
267
+ if @defs = svg.scan(/<defs>(.+)<\/defs>/m).flatten[0]
268
+ svg.sub!("<defs>#{@defs}</defs>", '')
269
+ @defs.gsub!(/(\n|\s{2,})/,'')
270
+
271
+ @defs.scan(/id="(.+?)"/).flatten.uniq.each_with_index do |id, index|
272
+
273
+ # If there are urls matching def ids
274
+ if svg.match(/url\(##{id}\)/)
275
+
276
+ new_id = "def-#{@id}-#{index}" # Generate a unique id
277
+ @defs.gsub!(/id="#{id}"/, %Q{id="#{new_id}"}) # Replace the def ids
278
+ svg.gsub!(/url\(##{id}\)/, "url(##{new_id})") # Replace url references to these old def ids
283
279
  end
284
280
  end
285
281
  end
286
- end
287
282
 
288
- # <defs> should be moved to the beginning of the SVG file for braod browser support. Ahem, Firefox ಠ_ಠ
289
- def move_defs
290
- if defs = @optimized.scan(/(\s*<defs>.+<\/defs>\s*)/m).flatten[0]
291
- @optimized.sub!(defs, '')
292
- @optimized.sub!(/(<svg.+?>)/, "\1#{defs}")
293
- end
283
+ svg
294
284
  end
295
285
  end
296
286
  end
data/lib/esvg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Esvg
2
- VERSION = "4.3.0"
2
+ VERSION = "4.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esvg
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 4.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-01 00:00:00.000000000 Z
11
+ date: 2017-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler