slim-embedded-minify 0.2.4 → 0.2.5

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
  SHA256:
3
- metadata.gz: f18ed0ba3778ee7349be7c14d9d3974077bff29611ecd20eddab983394b38eb3
4
- data.tar.gz: 9956d7da18d3074e0b9209491db3a057c85fb015823b231d7db11a7c1bbb1c22
3
+ metadata.gz: 14375e81992194bca2bc3c42733011ccb783db89cf89a3dffbb62ec3245f2184
4
+ data.tar.gz: 824370e31b97a05335c8d4c4a64c1bed55ea02ea9a678faa8609a4308c1657b6
5
5
  SHA512:
6
- metadata.gz: e9da11747c1bbe17719d9e7844d5f846a0eb28182c64f285dddf6f61fee5330cac386a33c490397ff770c205b38e7895e59961070f9b3110617c9321c539a193
7
- data.tar.gz: c0b7c531155113b1d118212976dbd60bdd6ed887bca10939ab18c5f881ee98a599cfe329f3bdd6adda8c3a1161403b7cce7cd69a5972de92a44e859b89885cf8
6
+ metadata.gz: 947549a68e10777ab21e2779ac67491a7be49cdb54e800f08e8b0fa891138ee72dc249f3216bbe9ac8f2b9f1e3574f1592a156c2ef90fa3c751771b1237e317b
7
+ data.tar.gz: df9d4962e7ba6e74492306c5367ff7c44bb6c5a9527b77dd19514bd2104da0a0de0020cd0bac68e58ccdbe33de7cfe48bb3819eef2cb4c4fb2391a38540e69e4
data/CHANGELOG.md CHANGED
@@ -1,8 +1,12 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.2.5 - 2023-07-27
4
+
5
+ Fix to not delete anything other than comments.
6
+
3
7
  ## 0.2.4 - 2023-07-27
4
8
 
5
- - Fix deletion of strings when `/*` is present in the string
9
+ - Fix deletion of strings when `/*` is present in the string.
6
10
 
7
11
  ## 0.2.3 - 2023-07-27
8
12
 
@@ -11,11 +11,41 @@ module Slim
11
11
  super(engine, minified_body, attrs)
12
12
  end
13
13
 
14
- private
15
-
16
14
  def remove_comments!(line)
17
- line.last.gsub!(/((?<!['"])\/\*.*?\*\/(?<!['"]))/, '')
18
- line.last.gsub!(/((?<!['"])\/\/.*)/, '')
15
+ need_deletion = false
16
+ need_deletion_all = false
17
+ inside_char = nil
18
+ line[-1] = line.last.chars.each_with_index.map do |char, index|
19
+ next if need_deletion_all
20
+
21
+ if char == "/" && next_char(line, index) == "*" && inside_char.nil?
22
+ if remaining_string_range(line, index).include?("*/")
23
+ need_deletion = true
24
+ next
25
+ end
26
+ elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
27
+ need_deletion = false
28
+ next
29
+ elsif char == "/" && next_char(line, index) == "/" && inside_char.nil? && !need_deletion
30
+ need_deletion_all = true
31
+ next
32
+ elsif char == '"' && !need_deletion
33
+ if inside_char == '"'
34
+ inside_char = nil
35
+ next char
36
+ end
37
+
38
+ inside_char = '"' if inside_char.nil?
39
+ elsif char == "'" && !need_deletion
40
+ if inside_char == "'"
41
+ inside_char = nil
42
+ next char
43
+ end
44
+
45
+ inside_char = "'" if inside_char.nil?
46
+ end
47
+ char unless need_deletion
48
+ end&.compact&.join
19
49
  end
20
50
  end
21
51
  end
@@ -13,43 +13,87 @@ module Slim
13
13
 
14
14
  def minify(body)
15
15
  multiline_comment = false
16
- body.map do |line|
16
+ body.filter_map do |line|
17
17
  if line.instance_of?(Array) && line.first == :slim
18
18
  remove_comments!(line)
19
- remove_whitespace!(line)
19
+ next if line.last.nil?
20
20
 
21
+ remove_whitespace!(line)
21
22
  stripped_quotes = stripped_quotes(line)
22
- if stripped_quotes.match?(%r{/\*})
23
+ if stripped_quotes.include?("/*") && !multiline_comment
23
24
  multiline_comment = true
24
- line.last.sub!(/(?<!['"])\/\*.*$/, '')
25
+ line[-1] = line.last.reverse.sub(%r{.*?\*/}, "").reverse
25
26
  elsif multiline_comment
26
- next unless stripped_quotes.match?(%r{\*/})
27
+ next unless stripped_quotes.include?("*/")
27
28
 
28
29
  multiline_comment = false
29
- line.last.sub!(/.*\*\/(?<!['"])/, '')
30
+ line.last.sub!(%r{.*\*/(?<!['"])}, "")
31
+ end
32
+ if stripped_quotes.include?("/*") && !multiline_comment
33
+ multiline_comment = true
34
+ line[-1] = line.last.reverse.sub(%r{.*?\*/}, "").reverse
30
35
  end
31
36
  next if empty_line?(line)
32
37
  end
33
38
  line
34
- end.compact
39
+ end
35
40
  end
36
41
 
37
42
  def remove_comments!(line)
38
- line.last.gsub!(/((?<!['"])\/\*.*?\*\/(?<!['"]))/, '')
43
+ need_deletion = false
44
+ inside_char = nil
45
+ line[-1] = line.last.chars.each_with_index.map do |char, index|
46
+ if char == "/" && next_char(line, index) == "*" && inside_char.nil?
47
+ if remaining_string_range(line, index).include?("*/")
48
+ need_deletion = true
49
+ next
50
+ end
51
+ elsif char == "/" && prev_char(line, index) == "*" && inside_char.nil? && need_deletion
52
+ need_deletion = false
53
+ next
54
+ elsif char == '"' && !need_deletion
55
+ if inside_char == '"'
56
+ inside_char = nil
57
+ next char
58
+ end
59
+
60
+ inside_char = '"' if inside_char.nil?
61
+ elsif char == "'" && !need_deletion
62
+ if inside_char == "'"
63
+ inside_char = nil
64
+ next char
65
+ end
66
+
67
+ inside_char = "'" if inside_char.nil?
68
+ end
69
+ char unless need_deletion
70
+ end&.compact&.join
71
+ end
72
+
73
+ def remaining_string_range(line, index)
74
+ line.last[index..]
75
+ end
76
+
77
+ def prev_char(line, index)
78
+ line.last[index - 1]
79
+ end
80
+
81
+ def next_char(line, index)
82
+ line.last[index + 1]
39
83
  end
40
84
 
41
85
  def remove_whitespace!(line)
42
- if line.last.gsub(/\n/, '').match?(/^\s*$/)
43
- line.last.gsub!(/^\s*$/, '')
44
- end
86
+ return unless line.last.delete("\n").match?(/^\s*$/)
87
+
88
+ line.last.gsub!(/^\s*$/, "")
45
89
  end
46
90
 
47
91
  def stripped_quotes(line)
48
- line.last.gsub(/(['"]).*?\1/, '')
92
+ line.last.gsub(/(['"]).*?\1/, "")
49
93
  end
50
94
 
51
95
  def empty_line?(line)
52
- line.last.gsub(/[\n\s]/, '').empty?
96
+ line.last.gsub(/[\n\s]/, "").empty?
53
97
  end
54
98
  end
55
99
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SlimEmbeddedMinify
4
- VERSION = "0.2.4"
4
+ VERSION = "0.2.5"
5
5
  end
@@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.bindir = "exe"
28
28
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
- spec.add_runtime_dependency 'slim', '~> 5.1'
30
+ spec.add_runtime_dependency "slim", "~> 5.1"
31
+ spec.metadata["rubygems_mfa_required"] = "true"
31
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim-embedded-minify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yudai Takada
@@ -49,6 +49,7 @@ metadata:
49
49
  homepage_uri: https://github.com/ydah/slim-embedded-minify
50
50
  source_code_uri: https://github.com/ydah/slim-embedded-minify
51
51
  changelog_uri: https://github.com/ydah/slim-embedded-minify/releases
52
+ rubygems_mfa_required: 'true'
52
53
  post_install_message:
53
54
  rdoc_options: []
54
55
  require_paths: